/**
 * mo_slider - jQuery plugin to navigate images/any content in a carousel style widget.
 * @requires jQuery v1.3 or above //test jquery versions
 *
 * http://
 *
 *
 * Version: 1.0
 * Note: Requires jquery 1.3 or above from version  1.0
  *
 *
 * @cat Plugins/Image Gallery
 * @author Moez KACEM/moez.kacem@gmail.com
 */
(function(jQuery) {                                          
jQuery.fn.mo_slider = function(o) {
    o = jQuery.extend({
        mouseWheel: false,// for version 1.1
        auto: null,
        speed: 200,
        easing: null,
        visible: 3,// for version 1.1
        start: 0, //  for version 1.1
        x_space: 20, 
        v_space: 10, 
        beforeScroll: null,
        afterScroll: null 
    }, o || {});
	
	return this.each(function() {    
		var scroll = 1;
		var div = jQuery(this);
		var ul = jQuery("ul", div);
		var	li = jQuery("li", ul);
		var w_li = li.size();
		var	v = o.visible;
		var indx = 100*w_li;
		var h_img =  jQuery(ul).find('li:eq('+o.start+')').height();
		var runing = false;
		
		cpt = w_li;
		jQuery(li).each(function(i){	
			cpt--;
			if(o.start ){				
				if(i < o.start){
					jQuery(this).css('z-index',indx-(w_li-cpt));		
					jQuery(this).css('right',(o.x_space*o.start)-((i+1)*o.x_space));
					jQuery(this).find('.thumb').css('height',(h_img +(o.v_space*2) -(o.start *(o.v_space*2))) - (i *(o.v_space*2)));
					jQuery(this).find('.thumb').css('top',(o.start *10)+(10*i)-10);
				}else if(i > o.start){
					jQuery(this).css('z-index',indx-cpt);
					jQuery(this).css('right',(o.x_space*o.start)+(o.x_space*cpt));
					jQuery(this).find('.thumb').css('height',h_img -(w_li-(i-o.start))*(o.v_space*2) );
					jQuery(this).find('.thumb').css('top',(10*cpt)+10);
				}else{
					jQuery(this).css('right',(o.x_space*(w_li-1)));
				}
			}else{
				jQuery(this).css('z-index',indx-i);		
				jQuery(this).css('right',(o.x_space*(w_li-1))-(o.x_space*i));
				jQuery(this).find('.thumb').css('height',h_img -(i *(o.v_space*2)));
				jQuery(this).find('.thumb').css('top',o.v_space*i);
			}
			if(jQuery(this).index() == o.start){
				jQuery(this).addClass('front');
				jQuery(this).css('z-index',indx+1);
				
			}
		});		
		
		jQuery(div).find('li:not(.front) img').live('hover',function(){
			jQuery(this).parents('li').addClass('hover');
		});
		jQuery(div).find('li:not(.front) img').live('mouseleave',function(){
			jQuery(this).parents('li').removeClass('hover');
		});
		
		jQuery(div).find('li:not(.front)').live('click',function(){
			jQuery('.mask').show();
			
			if(jQuery(this).index() - jQuery('li.front').index()>0){
				scroll = jQuery(this).index() - jQuery('li.front').index();
			}else {
				scroll =  w_li -( jQuery('li.front').index() - jQuery(this).index()) ;
			}
			interleaf(scroll);
		});	
		
		if(o.auto)
            setInterval(function() {
				if(runing == false){
					jQuery('.mask').show();
					interleaf(1);
				}
            }, o.auto+o.speed);
	
		function interleaf(scroll){	
			runing = true;
			var complete = 0;
			
			if(o.beforeScroll)
				o.beforeScroll.call(this, vis());
					
			if(scroll){
				if(jQuery(div).find('li:eq(0)').hasClass('front')){
					indx = 100*w_li;				
					jQuery(li).each(function(i){	
						jQuery(this).css('z-index',indx-i);
					});
				}
				indx--;
				indx= indx-10;
				first = jQuery('li.front').index();		
				next = first > w_li-2 ? 0:first + 1;				
				
				jQuery(div).find('li').removeClass('front');
				jQuery(div).find('li:eq('+next+')').addClass('front');			
				jQuery(div).find('li:eq('+first+')').css('z-index',indx);		
				jQuery(div).find('li:eq('+next+')').css('z-index',100*w_li);		
				jQuery(div).find('li:eq('+first+')').css('right',-o.x_space);		
				jQuery(div).find('li:eq('+first+') .thumb').css('height',h_img -((w_li-1) *(o.v_space*2)));		
				jQuery(div).find('li:eq('+first+') .thumb').css('top',(w_li-1)*o.v_space	);		
				
				jQuery(div).find('li:not(li:eq('+first+')) .thumb').animate({
					top: '-='+o.v_space,
					height: '+='+(o.v_space*2)
					},  o.speed, o.easing, function() {
					// Animation complete.
				});	
				scroll--;	
				jQuery(div).find('li').animate({
					right: '+='+o.x_space
					},  o.speed, o.easing, function() {
					// Animation complete.
					complete++;
					if(complete == w_li){					
						if(o.afterScroll)
							o.afterScroll.call(this, vis());							
						
						if(scroll > 0){													
							interleaf(scroll);
						}
						if(scroll==0){
							jQuery('.mask').hide();
							runing = false;	
						}
					}
				});			
			}
		};
	});
};
})(jQuery);
