var galPho = {

	nbSlide : 0,
	nbCurrent : 0,
	elemCurrent : null,
	elem : null,

	init : function(elem){
		this.nbSlide = elem.find(".slide").length;
		
		// creer la pagination
		for(var i=1; i<=this.nbSlide; i++){
			var fichier = elem.find(".slide").eq(i-1).find("img").attr("src");
			fichier = fichier.split("/");
			$("#nav_pho").append('<li><a><img src="photos/thumbs/'+fichier[1]+'" /></a></li>').css({"width":(this.nbSlide*74)});
		}
		
		// au click sur vignette
		$("#nav_pho").find("li").click(function(){
			idx = $("#nav_pho li").index(this);
			galPho.gotoSlide(idx);
		});
		
		// initialisation de la galerie
		this.elem = elem;
		elem.find(".slide").hide();
		elem.find(".slide:first").show();
		this.elemCurrent = elem.find(".slide:first");
		$("#nav_pho li:first").addClass("encours");
		
		
		// Création du timer
		//galPho.play();
		
		// pause de l'anim au survol
		//elem.hover(galPho.stop,galPho.play);
		
	},
	
	gotoSlide : function(num){
		if(num==this.nbCurrent){return false;}
		
		// animation en FadeIn/FadeOut
		/*
		this.elemCurrent.fadeOut();
		this.elem.find(".slide:eq("+num+")").fadeIn();
		*/
		
		// animation en slider
		var sens = 1;
		if(num<this.nbCurrent){
			sens = -1;
		}
		var cssDeb = { "left" : sens*this.elem.width() };
		var cssFin = { "left" : -sens*this.elem.width() };
		this.elem.find(".slide:eq("+num+")").show().css(cssDeb);
		
		this.elem.find(".slide:eq("+num+")").animate({ "top" : 0, "left" : 0}, 500);
		this.elemCurrent.animate(cssFin, 500);
		
		$("#nav_pho li").removeClass("encours");
		$("#nav_pho li:eq("+num+")").addClass("encours");
		this.nbCurrent = num;
		this.elemCurrent = this.elem.find(".slide:eq("+num+")");
	},
	
	next : function(){
		var num = this.nbCurrent+1;
		if(num > (this.nbSlide)-1){
			num = 0;
		}
		this.gotoSlide(num);
	},
	
	prev : function(){
		var num = this.nbCurrent-1;
		if(num < 0){
			num = this.nbSlide;
		}
		this.gotoSlide(num);
	},
	
	play : function(){
		window.clearInterval(galPho.timer);
		this.timer = window.setInterval("galPho.next()", 7000);
	},
	
	stop : function(){
		window.clearInterval(galPho.timer);
	}

}


$(function() {
// tout démarre ici

	// galerie vidéo
	$(".video").each(function(){
		var Vid = $(this).attr('id');
		var Vtype = $(this).attr('class').split(" ");
		Vtype = Vtype['1'];
		$(this).vid({
        	type    : Vtype,
        	videoId : Vid
        });
     });
    
    
    // galPho
    galPho.init($("#gal_pho"));
    
    $("#nav_ar").addClass("inactive");
    
    // reculer les miniatures
    $("#nav_ar").click(function(){
    	max = 0;
    	var how = $("#nav_pho").css('left');
    	if(how == "auto"){
    		how = "0px";
    	}
		var reg = new RegExp("(px)", "g");
		how = how.replace(reg,"");
		how = parseInt(how);
    	how = how+(2*72);
    	if(how >= max){
    		$("#nav_ar").addClass("inactive");
    	}
    	if(how > max){
    		if($("#nav_av").hasClass("inactive")){
    			$("#nav_av").removeClass("inactive");
    		}
    		$("#nav_pho").animate({left: max },500);
    	} else {
    		if($("#nav_av").hasClass("inactive")){
    			$("#nav_av").removeClass("inactive");
    		}
    		$("#nav_pho").animate({left: how },500);
    	}
    });
    
    // avancer les miniatures
    $("#nav_av").not(".inactive").click(function(){
    	max = $("#gal_pho div").length;
    	max = (-max*72)+(6*72);
    	var how = $("#nav_pho").css('left');
    	if(how == "auto"){
    		how = "0px";
    	}
		var reg = new RegExp("(px)", "g");
		how = how.replace(reg,"");
		how = parseInt(how);
    	how = how-(2*72);
    	if(how <= max){
    		$("#nav_av").addClass("inactive");
    	}
    	if(how < max){
    		if($("#nav_ar").hasClass("inactive")){
    			$("#nav_ar").removeClass("inactive");
    		}
    		$("#nav_pho").animate({left: max },500);
    	} else {
    		if($("#nav_ar").hasClass("inactive")){
    			$("#nav_ar").removeClass("inactive");
    		}
    		$("#nav_pho").animate({left: how },500);
    	}
    });
});
