//Déclaration de l'objet
var Slider = {
	Slider_obj 	: '',
	Models_objs : '',
	Button_objs : '',
	Reference 	: '',
	Nbinterval	: 0,
	Arrow		: false,
	NbElement	: 0,
	Interval	: 0,
	Position	: 0,
	Offset		: 0,
	Play		: false,

	init : function (SlideObj, ModelsObj, ButtonObj, OffSet, NbInter , Arr, PlayVar){
		this.Slider_obj = SlideObj;
		this.Models_objs 	= ModelsObj;
		this.Button_objs	= ButtonObj;
		this.Reference 		= $(ModelsObj)[0];
		this.Nbinterval		= NbInter;
		this.Arrow			= Arr;
		this.NbElement 		= this.getNumElement();
		this.Position		= 0;
		this.Offset			= OffSet;
		this.Play			= PlayVar;
		
		// initialisation boutons
		this.initPlayerButton();
		this.arrowInit();
		this.buttonInit();
		
		if(this.Play)
			this.Interval 		= setInterval(function(){ Slider.animateSlide();}, this.Nbinterval);
		else 
			$("#numeros_player").addClass('stop');
		this.begin();
	},
	
	arrowSuivant : function(){
		this.Position++;
		this.move();
		this.setButtonPositionOnClick(this.Position);
		this.setArrowPosition();
	},
	
	arrowPrecedant : function(){
		this.Position--;
		this.move();
		this.setButtonPositionOnClick(this.Position);
		this.setArrowPosition();
	},
	
	arrowInit: function(){
		if (Slider.asSliderObj()){
			if (Slider.Arrow){
				$("#suivant").click(function() { 
					clearInterval(Slider.Interval);
					Slider.arrowSuivant();
					if (Slider.Play) Slider.Interval = setInterval(function(){ Slider.animateSlide();}, Slider.Nbinterval);
					return false;
				});
				
				$("#precedant").click(function() {
					clearInterval(Slider.Interval);
					Slider.arrowPrecedant();
					if (Slider.Play)Slider.Interval = setInterval(function(){ Slider.animateSlide();}, Slider.Nbinterval);
					return false;
				});
			}
		}
	},
	
	initPlayerButton : function(){
		$('#numeros_player').click(function(){
			Slider.playerButtonClick();
			return false;
		});	
	},
	
	playerButtonClick : function(){
		if(this.Play){
			clearInterval(this.Interval);
			$('#numeros_player').addClass('stop');
			this.Play =false;
		}else{
			$('#numeros_player').removeClass('stop');
			this.Play = true;
			this.Interval = setInterval(function(){ Slider.animateSlide();}, this.Nbinterval);
		}
	},
	
	buttonClick : function(Button){
		this.Position = Button;
		this.move();
		this.setButtonPositionOnClick(Button);
		this.setArrowPosition();
	},
	
	begin : function(){
		this.Position = 0;
		$(this.Models_objs).width( $(this.Reference).width() ) 
		$(this.Models_objs).height( $(this.Reference).height() ) 
		$(this.Models_objs).css("overflow", "hidden");
		this.setArrowPosition();
		this.setButtonPositionOnClick(0);
	},
	
	asSliderObj : function(){
		if (($(this.Slider_obj).length > 0) && (this.Slider_obj != ''))return true;
		else return false;
	},
	
	asButtonObj : function(){
		if (($(this.Button_objs).length > 0) && (this.Button_objs != ''))return true;
		else return false;
	},
	
	getNumElement :	function (){
		if (this.asSliderObj())return $(this.Models_objs).length
		else return false;
	},
	
	setArrowPosition : function(){
		if (this.Arrow){
			//Flèche précédant
			if (this.Position > 0) $("#precedant").css("visibility","visible");
			else $("#precedant").css("visibility","hidden");
			
			//Flèche Suivant
			if (this.Position < (this.NbElement-1)) $("#suivant").css("visibility","visible");
			else $("#suivant").css("visibility","hidden");
		}
	},
	
	buttonInit:function(){
		if (Slider.asSliderObj()){
			if (Slider.asButtonObj()){
				var count=0;
				$(Slider.Button_objs).each(function(i){
					id = this.id;
					count++;
					$('#'+id).click(function() {
						clearInterval(Slider.Interval);
						var pos = (this.id.split('_')[1])-1;
						Slider.buttonClick(pos);
						if(Slider.Play)Slider.Interval = setInterval(function(){ Slider.animateSlide();}, Slider.Nbinterval);
						return false;
					}); 
				});
			}
		}	
	},
	
	setButtonPosition : function(){
		//Suppression de la classe "bouton active"
		if (this.asButtonObj()){
			//alert(this.Position);
			$('.numero').removeClass("on");
			//Activation du bon bouton 
			var obj = $(this.Button_objs).children[this.Position].className;
			$('#'+$(this.Button_objs).children[this.Position].id).addClass('on');
		}
	},
	
	setButtonPositionOnClick : function(Button){
		//Suppression de la classe "bouton active"
		if (this.asButtonObj()){
			$('.numero').removeClass("on");
			$('.numero').each(function(i){
				if(this.id == 'numeros_'+(Button+1)){
					$('#'+this.id).addClass('on');
				}
			});
			
		}
	},
	move : function(){
		$('div#slider_titre_0').css('display','none');
		$('div#slider_titre_1').css('display','none');
		$('div#slider_titre_2').css('display','none');
		$('div#slider_titre_3').css('display','none');
		$('div#slider_titre_'+this.Position).css('display','block');
		$(this.Slider_obj).animate({ 
			marginLeft : - (($(this.Reference).width()+this.Offset) * this.Position) },1500
		);
	},
	
	animateSlide : function(){
		//Arret de l'animation en cour
		clearInterval(this.Interval);
		if(this.Position < (this.NbElement-1) ) this.Position++;
		else this.Position = 0;
		this.move();
		this.setButtonPositionOnClick(this.Position);
		this.setArrowPosition();
		if(this.Play)this.Interval = setInterval(function(){ Slider.animateSlide();}, this.Nbinterval);
	}
	
	
};


$(document).ready(function() { 
	//Interval
	Nbinreval=8000;
		
	// ANIMATION AUTO
	if ($("#modules").length > 0) {
		//(SlideObj, ModelsObj, ButtonObj, OffSet, NbInter , Arr, PlayVar)
		Slider.init("#modules", ".articlehome", ".numero",  40, Nbinreval , true, true);
	}

});