

jQuery.fn.extend({
	opts : new Object ,
	o 	 : new Object ,
	pos : new Array,
	interval : new String,
	curPos : new String,
	left : new String,
	galInit : function(opts) {
		cont = jQuery(this);
		o = opts;
		cont.append('<div class="galLoading" style="float:left"><img src="/loading.gif"/></div>');
		cont.find('ul').css('visibility','hidden');
		var LoadInt = window.setInterval(function() {
			totalItems = new Array;
			loadedItems = new Array();
			cont.find('ul li img').each(function(index) {
				var item = jQuery(this);
				
				if(item.width()>5) {
					loadedItems.push(1);					
				} 
				totalItems.push(1);
			});
			if(loadedItems.length>=totalItems.length) {
				window.clearInterval(LoadInt);
				jQuery.fn.gal();
			}
		},200);
	},
	
	gal : function() {
		cont.find('ul').css('visibility','visible');
		jQuery('.galLoading').hide();
		
		interval = 0;
		curPos = 0;
		pos = new Array;

		jQuery('.prev_button').click(function() {
			jQuery.fn.galDirect(-1);
			return false;
		});
		jQuery('.next_button').click(function() {
			jQuery.fn.galDirect(1);
			return false;
		});
			cont.find('ul').hover(function(){
				jQuery.fn.galDirect(0);
			},function() {
				
			});

			var totalWidth = 0;
			 cont.find('ul li img').each(function(index) {
			       var item = jQuery(this);
				   pos.push(totalWidth);
			       totalWidth+=item.width()+10;
				   
	        });
	    	cont.find('ul').css('width',pos.length*950);  
			cont.css('position','relative');
    		cont.find('ul').css('position','relative');
			left = 0;

			jQuery.fn.galStart(1);
	},
	galStart : function(i) {
		if (interval) {
			window.clearInterval(interval);
		}
					 
		interval = window.setInterval(function() {
			jQuery.fn.galDirect(i);
		},o.waitDuration);
	},
	galDirect : function(i) {
			curPos+=i;
			if(curPos<0) {
				curPos = pos.length-1;
			} else if(!pos[curPos]) {
				curPos = 0;
			}
	    	cont.find('ul').animate({ 
		        left: -pos[curPos]
		      }, o.animationDuration);
		if (interval) {
			window.clearInterval(interval);
		}
		window.setTimeout(function() {
			jQuery.fn.galStart(i);
		},o.animationDuration)	
	}
});

jQuery.fn.gal.defaults = {
  animationDuration : 1000,
  waitDuration      : 4000
}; 