$(document).ready(onSliderReady);

var timer=new Array();

var SLIDER_TRANSITION_TIME="500";
var SLIDER_TRANSITION_TYPE="easeOutCubic";

var SLIDER_CHILD_TAB=new Array();


function onSliderReady()
{
	// initialisation de la variable r�f�rences
		$(".SliderContent").attr('i', '0');
	
	// initialistaion de la largeur du conteneur
		initWidth();
		
	// initialisation de l'indicateur
		initIndicator();
	
	// initialisation des actions
		$(".SliderRightButton").click(gotoLeft);
		$(".SliderLeftButton").click(gotoRight);
		
		onEndAnimation();
	
}

function initWidth()
{
	$.each($(".SliderContent") ,  function(i , n)
			{
				var width=0;
				$.each($(n).children() , function(i , child)
						{
							$(child).css("width", $(child).parents(".Slider").width());
							$(child).css("float", "left");
							width+=$(child).width();
						});
			
				$(n).css("min-width" , $($(n).parent()).width() );
				$(n).css("width" , width);
				
				if($(n).children().length>1)
				{
				/*	// �v�nements swipe
						$(n).bind("swipeleft" , gotoLeft);
						$(n).bind("swiperight" , gotoRight);
					*/
					// initialisation des timers : 
						initTimers(i , n);
				}
				else
				{
					var p=$(n).parents(".Slider");
					p.find(".SliderRightButton").remove();
					p.find(".SliderLeftButton").remove();
					p.find(".SliderIndicator").remove();
				}
			});
	
	//onEndAnimation();
}



function gotoLeft(element , button)
{
	if (button)
	{
		var width=button.parents(".Slider").width();
		var current=button;
	}
	else
	{
		// CLICK donc ar�t d�filement:
			stopInterval($(this).parents(".Slider").find(".SliderContent").attr("idTimer"));
		
		var width=$(this).parents(".Slider").width();
		var current=$(this).parents(".Slider").find(".SliderContent");
	}
	
	
	current.attr("i" , parseInt(current.attr("i"))+1);
	
	var num_children=current.children().length;
	
	if (current.attr("i")<num_children)
	{
		dec=(width*current.attr("i"));
		current.animate({ 	left: "-"+dec+"px" }, SLIDER_TRANSITION_TIME  , SLIDER_TRANSITION_TYPE , onEndAnimation);
		
		
		indicatorChange(current);
	}
	else
	{
		current.attr("i" , parseInt(current.attr("i"))-1);
		decalageOnLeft(current);
		indicatorChange(current);
	}
	
}

function gotoRight()
{
	// CLICK donc ar�t d�filement:
		stopInterval($(this).parents(".Slider").find(".SliderContent").attr("idTimer"));
	
	// d�finition des variables
	var width=$(this).parents(".Slider").width();
	
	var current=$(this).parents(".Slider").find(".SliderContent");
	current.attr("i" , parseInt(current.attr("i"))-1);
	
	var num_children=current.children().length;
	
	if (current.attr("i")>-1)
	{
		dec=(width*current.attr("i"));
		current.animate({ 	left: "-"+dec+"px" },   SLIDER_TRANSITION_TIME  , SLIDER_TRANSITION_TYPE , onEndAnimation);
		indicatorChange(current);
	}
	else
	{
		current.attr("i" , parseInt(current.attr("i"))+1);
		decalageOnRight(current);
		indicatorChange(current);
	}
}

function decalageOnLeft(current)
{
	var first=$(current.children()[0]);
	var x=parseInt(current.css("left"))+current.parent().width();
	dec=(current.parent().width()*current.attr("i"));
	
	// supression du premier fils
	first.remove();
	
	// centrage du curent
		current.css("left" , x);
	
	// rajout du premier fils
		current.append(first);
		
	// recherche du dec
		current.attr("i" , 0);
		
		current.animate({ 
		 	left: "-"+dec+"px"
		}, 
		 SLIDER_TRANSITION_TIME  , SLIDER_TRANSITION_TYPE ,
		onEndDecalageOnLeft);
	
}

function onEndDecalageOnLeft()
{
	current=$(this);
	
	children=current.children();
	for (var i=0 ; i<children.length-1 ;i++)
	{
		var first=$(current.children()[0]);
		first.remove();
		
		var x=parseInt(current.css("left"))+current.parent().width();
		current.css("left" , x);
		
		current.append(first);
	}
	
	onEndAnimation();
}

function decalageOnRight(current)
{

	var last=$(current.children()[current.children().length-1]);
	
	// supression du dernier fils
	last.remove();

	// centrage du curent
		var x=parseInt(current.css("left"))-current.parent().width();
		current.css("left" , x);
	
	// rajout du premier fils
		current.prepend(last);
	
	// recherche du dec
		dec=(current.parent().width()*current.attr("i"));
		current.attr("i" , current.children().length-1);
		
		current.animate({ 
		 	left: "-"+dec+"px"
		}, 
		 SLIDER_TRANSITION_TIME  , SLIDER_TRANSITION_TYPE ,
		onEndDecalageOnRight);	
}

function onEndDecalageOnRight()
{
	current=$(this);
	
	children=current.children();
	for (var i=children.length-1 ; i>0;i--)
	{
		var last=$(current.children()[current.children().length-1]);
		last.remove();
		
		var x=parseInt(current.css("left"))-current.parent().width();
		current.css("left" , x);
		
		current.prepend(last);
	}
	
	onEndAnimation();
}

function initIndicator()
{
	$.each($(".SliderIndicator") ,  function(i , n)
			{
				//recuperation du nombre d'�l�ment
					var number= $(n).parent().find(".SliderContent").children().length;
					
				// cr�ation des div
					for (var i=0 ; i<number ; i++)
					{
						var div=$("<div/>");
						div.addClass("SliderIndicatorButton");
						div.attr("i" , i);
						div.css("width" , $(n).width()/number);
						if (i==0)
							div.addClass("SliderIndicatorButtonSelected");
						div.click(gotoI);
						$(n).append(div);
						
					}
			});
}

function indicatorChange(sliderContent)
{
	var indicator=sliderContent.parents(".Slider").find(".SliderIndicator");
	
	indicator.find(".SliderIndicatorButtonSelected").removeClass("SliderIndicatorButtonSelected");
	$(indicator.children()[sliderContent.attr("i")]).addClass("SliderIndicatorButtonSelected");
}

function gotoI()
{
	
	var width=$(this).parents(".Slider").width();
	
	$(this).parents(".Slider").find(".SliderContent").attr("i" , $(this).attr("i"));
	$(this).parents(".Slider").find(".SliderContent").animate({ 
	 	left: "-"+(width*$(this).attr("i"))+"px"
	}, 
	SLIDER_TRANSITION_TIME  , SLIDER_TRANSITION_TYPE , onEndAnimation);
	
	indicatorChange($(this).parents(".Slider").find(".SliderContent"));
}

function onEndAnimation()
{
	var t=-parseInt( $( $(".Slider").find(".SliderContent") ).position().left ) / 960;
	
	children = $( $(".Slider").find(".SliderContent") ).children();
	for ( var i=0 ; i<children.length ; i++)
	{
		if ( fonction = $(children[i]).attr("fonction") )
		{
			if (i==t)
			{
				SLIDER_CHILD_TAB[fonction]["IN"]( $(children[i]) );
			}
			else
			{
				SLIDER_CHILD_TAB[fonction]["OUT"]( $(children[i]) );
			}
		}
	}
}


/**************************************
*			TIMER
***************************************/
function initTimers(i , n)
{
	$(n).attr("idTimer" , i);
	timer[i]=setInterval( "onTime("+i+")" , $(n).attr("time"));
}


function onTime(n)
{
	gotoLeft( "test" , $(".SliderContent[idTimer="+n+"]"));
}

function stopInterval(i)
{
	clearInterval(timer[i]);
}
