/**
* J.Cambier -- @kokmok pour Akimedia
 */
(function($) {
        $.fn.smoothSlide = function(params) {
                defaultParams = {
                    time:300,
                    easing :'easeOutBack',
                    width : 'auto',
                    height:'100px',
                    leftArrow:"<",
                    rightArrow:">",
                    step:20,
                    callBack : '',
                    navSpace : 0,
					multipage : false,
					step : false,
					stepSize : 0,
					verticalCenter : true
                }
				params = $.extend( defaultParams, params);
				
    var speed = 0;
    var moovable = null;
    var interval = null;
    
    obj=this;
    obj.each(function(i) {
        var ul=$(this);           
        var moovable = ul;
        //WRAP et Controleurs
        ul.wrap('<div class="smoothSlideWrap" />');
		var wrap = ul.parent('.smoothSlideWrap');
		
		wrap.css({'width':params.width,'height':params.height});
		
		if (params.multipage)
		{
				if (ul.find('li').length>1)
				{
						var links = '';
						ul.find('li')
								.each
								(
										function(i,l)
										{
												var currentClass ="";
												if (i==0)
												{
													currentClass = ' currentSmooth';	
												}
												links+='<a href="#'+i+'" rel="'+i+'" class="smoothSlideBallLink'+currentClass+'"> </a>';
										}
								)
						
						ul.parent('.smoothSlideWrap')
								.append('<div id="smoothSlideControlerBall">'+links+'</div>')
								.css({'position':"relative"});
						$('#smoothSlideControlerBall').css({'position':'absolute','text-align':'center','bottom':'0','left':'0','width':params.width,'height':'20px'});
				}
				
				ul.css({'position':'absolute'});
		}
		else
		{
				if (ul.find('li').length>1)
				{
						ul.parent('.smoothSlideWrap')
								.append('<div class="smoothSlideControlers"><div class="smoothSlideControler" id="smoothSlideGotoLeft">'+params.leftArrow+'</div><div class="smoothSlideControler" id="smoothSlideGotoRight">'+params.rightArrow+'</div></div>')
						
				}
				
				ul.parent('.smoothSlideWrap')
						.css('position',"relative");
				//$('#smoothSlideGotoLeft').css({'left':'0'});
				//$('#smoothSlideGotoRight').css({'right':'0'});
				ul.css({'position':'absolute','left':params.navSpace+"px"});
		}
		
        //$('.smoothSlideControler').css({'position':'absolute'});
        
        
        var widthWrap = ul.parent('.smoothSlideWrap').width();
        
        //CSS PART
        
        //Width du UL
        ul.css({'display':'block',opacity:0});
        
        var internalWidth = 0;
       
        ul.find('li').each(
            function(){
                $(this).css({'display':'block','float':'left','margin':'0'});
                internalWidth+= $(this).find('.newHomeBox').length*116;
                internalWidth+=0;
				//log('width'+($(this).outerWidth()));
            }
        );
        ul.width(internalWidth);
        
        
        
        
        //Centrage
        ul.find('li').each(
            function(){
                if ($(this).find('img').length){
                    var padding = (params.height - $(this).find('img').height())/2;
                    $(this).css({'padding-top':padding+'px'});  
                }
            }
        );
        
        ul.animate({opacity:1},250);  
        
        
        wrap.css({'overflow':'hidden'});
        
        
        //Interactivité
        
		
		if (params.multipage)
		{
				$('.smoothSlideBallLink')
						.click
						(
								function()
								{
										if (!$(this).hasClass('currentSmooth'))
										{
												var index = $(this).attr('rel');
												var oldIndex = $('.currentSmooth').attr('rel');
												
												var diffIndex = Math.abs(oldIndex-index); 
												
												var toLeft = -1*(parseInt(params.width)*parseInt(index));
												
												$('.currentSmooth').removeClass('currentSmooth');
												$(this).addClass('currentSmooth');
												ul.stop(true,true).animate({'left':toLeft+'px'},params.time*diffIndex);
										}
										
								}
						)
		}
		else
		{
			//Left
				$('#smoothSlideGotoLeft').mousedown(
					function(){
						if (!params.step)
						{
								clearInterval(interval);
								speed = -params.step;
								interval = setInterval(move,40);		
						}
						else
						{
								var left = parseInt(moovable.css('left'));
								
								if (left<0)
								{
										
										var toLeft = left+params.stepSize;
										if (toLeft>0)
										{
												toLeft =0;
										}
										moovable.stop(true,true).animate({'left':toLeft+'px'},params.time);
										$('.smoothVisible:last').removeClass('smoothVisible');
										if ($('.smoothVisible:first').prev('.newHomeBox').length)
										{
												$('.smoothVisible:first').prev('.newHomeBox').addClass('smoothVisible');		
										}
										else
										{
												$('.smoothVisible:last').parent().prev().find('.newHomeBox:last').addClass('smoothVisible');
										}
										
								}	
								
						}
					
					}
				);
				
				//Right
				$('#smoothSlideGotoRight').mousedown(
					function(){
						if (!params.step)
						{
								clearInterval(interval);
								speed = params.step;
								interval = setInterval(move,40);
						}
						else
						{
								var left = parseInt(moovable.css('left'));
								if (left+moovable.outerWidth()>wrap.width())
								{
										log(left+moovable.outerWidth()+'||||'+wrap.width());
										var toLeft = left-params.stepSize;
										moovable.stop(true,true).animate({'left':toLeft+'px'},params.time);
										$('.smoothVisible:first').removeClass('smoothVisible');
										if ($('.smoothVisible:last').next('.newHomeBox').length)
										{
												$('.smoothVisible:last').next('.newHomeBox').addClass('smoothVisible');
										}
										else
										{
												$('.smoothVisible:last').parent().next().find('.newHomeBox:first').addClass('smoothVisible');
										}
										
										
								}
								
								
								
						}
					}
				);
				
				 $('.smoothSlideControler').mouseup(
					function(){
						if (!params.step)
						{
								clearInterval(interval);
								interval = setInterval(stop,40);
						}
					}
				);	
		}
        
        
		//Fonctions de multipage
		
        
        //Fonction de multipage = false
        function move(){
            var left = parseInt(moovable.css('left'));
           
            if (canMove(left)){
                moovable.css({'left':left-speed+'px'});
            }else{
                clearInterval(interval);
            }
        }
        
        
        function stop(){
            var left = parseInt(moovable.css('left'));
            
           if (canMove(left)){
                moovable.css({'left':left-speed+'px'});
                speed = speed/1.2;
           }else{
                clearInterval(interval);
           }
            
            if (speed<1 && speed>-1){
                clearInterval(interval);
            }
        }
        
        function canMove(left){
            if (speed>0){
                if (widthWrap<(left-speed)+internalWidth+params.navSpace){
                    return true;
                }else{
                    return false;
                }
            }else{
                if (left-speed<params.navSpace){
                     return true;
                }else{
                     return false;
                }
            }
        }
        
        
        
        
    });
    // Permettre le chaînage par jQuery
    return this;
    };
})(jQuery);
