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;
		o.inAction = false;
		o.clicked = false;
		//cont.append('');
		
		cont.find('ul').css('visibility','hidden');
		cont.find('mycarousel-action').css('visibility','hidden');
		// set events
		$(this).find('.next').css('cursor','pointer').css('courser','hand').click(function() {
		   if(o.inAction) {
		    cont.find('ul:eq(0)').stop();
		   }
       jQuery.fn.galDirect('+1',1000); 
    });
		$(this).find('.prev').css('cursor','pointer').css('courser','hand').click(function() {
		   if(o.inAction) {
		    cont.find('ul:eq(0)').stop();
		   }
       jQuery.fn.galDirect('-1',1000); 
    });
		// set over
		var LoadInt = window.setInterval(function() {
			totalItems = new Array;
			loadedItems = new Array();
			  	// register mouse event
			cont.hover(function() {
					cont.mousemove(function(e){
						o.x  = e.clientX-cont.offset().left;
					});
			},function() {
				o.x = false;
				cont.unbind('mousemove');
			});
			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');
		cont.find('mycarousel-action').css('visibility','visible');
		jQuery('.galLoading').hide();
		interval = 0;
		curPos = 0;
		pos = new Array;
			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);
			
			cont.find('ul li img').each(function(index) {
			       var item = jQuery(this);
				     // build text hover
				     if(item.parent().attr('title')) {
               item.before('<span class="txt">'+item.parent().attr('title')+'</span>');
               var txt = item.parent().find('span');
               txt.css({
                  'background-color' : 'white',
                  'color'            : 'C34E44',
                  'padding'          : '3px 0px 3px 0px',
                  'display'          : 'none',
                  'height'           : '17px',
                  'overflow'         : 'hidden',
                  'line-height'      : '17px',
                  'opacity'          : '0.70',
                  'filter'           : 'alpha(opacity=70)',
                  'position'         : 'absolute',
                  //'left'             : parseInt(item.offset().left)+'px',
                  'top'              : parseInt(item.height())+'px',
                  'margin-left'      : '-'+parseInt(item.width()+10)+'px',
                  'width'            : item.width()
               });
               item.parent().attr('title','');
               item.parent().css({
                  'text-decoration' : 'none'
               });
               item.hover(function() {
                  jQuery(this).parent().find('span').fadeIn();
               },function() {
                   jQuery(this).parent().find('span').fadeOut();
               });
             }
      });
	},
	galStart : function(i) {
		if (interval) {
			window.clearInterval(interval);
		}
		if(o.clicked) {
      return false;
    }
    /*
		interval = window.setInterval(function() {
			jQuery.fn.galDirect(i);
		},o.waitDuration);
		*/
	},
	galDirect : function(i,aniduration) {
	     if(typeof(aniduration)=='undefined') {
        aniduration = o.animationDuration;
       }
			// MOUSE FUNCTION
			if (o.x > 0) {
				if (o.x > (cont.width() / 2)) {
					i=1;
				}
				else {
					i=-1;
				}
			}
			/////
			
			curPos+=i;
			if(curPos<0) {
				i=1;
				curPos = 1;
			} else if(curPos>(pos.length-1)) {
				i=-1;
				curPos = pos.length-2;
			}
			o.inAction = true;
	    	cont.find('ul').animate({ 
		        left: -pos[curPos]
		      }, aniduration,function() {
			  	o.inAction = false;
			  });
		if (interval) {
			window.clearInterval(interval);
		}
		window.setTimeout(function() {
			jQuery.fn.galStart(i);
		},aniduration);
	}
});

jQuery.fn.gal.defaults = {
  animationDuration : 1000,
  waitDuration      : 1000
}; 