/* DOM READY
--------------------------------------------------------------------------------------------------------------------------------------*/
var compteurImages = 1;
var nbImages = 1;
var numeroImage = 0; //position image galerie
var tempsSlideshow = 3000;
var actuel = 0; //galerie
var largeurConteneur = 0;
var largeur_actuel = 0;

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function changeBG() {
	urlBG = $("#bg-cache span:nth("+compteurImages+")").html();
	//console.log( $("#bg-cache span:nth("+compteurImages+")"));
	if(compteurImages >= nbImages-1){
		compteurImages = 0;
	}else{
		compteurImages++;
	}
	//$("img#background").attr("src",urlBG);
	
	$("img#background").animate({opacity:0},500,function(){
		$(this).attr("src",urlBG).animate({opacity:1},900);
		//console.log(urlBG);
	});
	
	
	$("img#background").fadeOut(function(){
		$(this).stop();
		$(this).attr("src",urlBG).fadeIn(300);
	},200);
}

function slideshow() {
	totalImages = $(".galerie_thumbs li").length;
	if(numeroImage >= totalImages){
		numeroImage= 0;
	}else{
		numeroImage++;	
	}
	$(".galerie_thumbs li:eq("+numeroImage+") a").trigger("click");
	////console.log(numeroImage);
}

//hover automatique	   
function goLeft() {
	if(actuel > 0){
		actuel -= 1;
		$(".galerie_thumbs ul").css({
			left: "-"+actuel+"px"	
		});
	}
	//console.log("goLeft");
}
function goRight() {
	largeurConteneur = $("#bloc-droit-galerie").width();
	if(largeur_actuel-largeurConteneur > actuel){
		actuel += 1;
		$(".galerie_thumbs ul").css({
			left: "-"+actuel+"px"
		
		});
	}
	//console.log("goRight");
}

//fin hover automatique
$(function(){
	//taille fenetre
	var settings = {
		autoReinitialise: true
	};
	
	
	var conteneurAScroller = $("#contenu");
	
	if($("#contenu").hasClass("galerie")){
		
		
		
		//galerie
		var hauteur = $(window).height() - 130;
		var largeur = $("#content").width() - 250;
		$("#contenu.galerie").css({"height":hauteur,"width":largeur})
		$("#sidebar").css({"height":hauteur})
		$("#footer").css({"width":largeur-140})
		//on redimentionne l'image du milieu
		
		max_size = hauteur;
		$("#contenu.galerie #photo img").bind('load', function (e) {
					// Do stuff on load
					
					 $(this).removeAttr("width"); 
					  $(this).removeAttr("height");
					  /*$(this).css("height","auto");
					   $(this).css("width","auto");*/
					   
					   $(this).css("height",0);
					   $(this).css("width",0);
					   //console.log(this);
					   //console.log("w:"+ this.width+" - h:"+ this.height);
					  if ( this.height >this.width) {
						var h = max_size*0.8;
						var w = Math.ceil(this.width/  this.height * max_size)*0.8;
						//alert("h grand");
					  } else {
						 
						var h = max_size*0.8;
						var w = Math.ceil(this.width/  this.height * max_size)*0.8;
						//alert("w grand");
						/* debug img panoramique*/
						 //console.log("contenu : "+$("#contenu").width()+" /////// w img :"+w + "- h:"+h);
						 if(w>$("#photo").width()){

							max_size = $("#photo").width()-30;
									

							w = max_size;
							h =  Math.ceil($(this).height() / $(this).width() * max_size);
							//console.log("contenu : "+$("#contenu").width()+" /////// w img :"+w + "- h:"+h);
						 }
					  }
					
					if(isNaN(h))
						h = "auto";
					if(isNaN(w))
						w = "auto";
						
					 $(this).css("height",h);
					   $(this).css("width",w);
					   	
					//$(this).css({ height: h, width: w });
					//console.log("ws 1 img :"+w + "- h:"+h);
					$(this).animate({"opacity":1},200);
					});
		
		//on genere les thumbs
		largeur_thumbs=100;
		largeur_thumbs_total=0;
		largeur_actuel = 0;
		largeur_margegauche = 5;
		$(".galerie_thumbs li").each(function(){
			$(this).css({"display":"block","position":"absolute","left":largeur_actuel+"px"});
			largeur_actuel += largeur_thumbs+largeur_margegauche;
		});
		
		//gestion du carousel
		actuel = 0;
		$(".slide_droit").click(function(){
			largeurConteneur = $("#bloc-droit-galerie").width();
			////console.log("largeur actuel : "+largeur_actuel);
			////console.log("max :" +(largeur_actuel-largeurConteneur));
			if(largeur_actuel-largeurConteneur > actuel){
				actuel += 200;
				$(".galerie_thumbs ul").animate({
					left: "-"+actuel+"px"
				
				},200);
			}
		});
		$(".slide_gauche").click(function(){
			if(actuel > 0){
				actuel -= 200;
				$(".galerie_thumbs ul").animate({
					left: "-"+actuel+"px"
				
				},200);
			}
		});
		
		
		// FONCTION SLIDE AUTO
		var n=0;
		var position = $(".galerie_thumbs").position();
		var largeur_over = 150;
		
		var debut = 230; //marge gauche de galerie thumbs
		var timer_goLeft, timer_goRight;
		
		
		$(".galerie_thumbs").mouseover(function(e){
			
			position = $(".galerie_thumbs").position();
		   
		    var x = e.pageX - this.offsetLeft;
			var y = e.pageY - this.offsetTop;
			
			if(x > debut && x < (debut+largeur_over)){
				//LEFT
				//console.log("left");
				timer_goLeft=setInterval("goLeft()", 1);
			}else if(x > ($(this).width() - largeur_over + 230) ){
				//Right
				//console.log("right");
				timer_goRight=setInterval("goRight()", 1);
			}else{
				//console.log("leave inside");
				if(timer_goLeft != undefined)
					clearInterval(timer_goLeft);
					
				if(timer_goRight != undefined)
					clearInterval(timer_goRight);
			}
			
     		 //console.log(x +', '+ y);
	  
	  
			
			
		}).mouseout(function(){
		  // console.log("leave");
		   if(timer_goLeft != undefined)
					clearInterval(timer_goLeft);
					
				if(timer_goRight != undefined)
					clearInterval(timer_goRight);
		 // clearInterval(timer_goRight);
		} );
			
		
		
		//FIN FONCTION SLIDE AUTO
		
		//fin gestion du carousel
		
		//affichage des bouton
		largeurConteneur = $("#bloc-droit-galerie").width();
		if(largeurConteneur > largeur_actuel){
			//on affiche pas les boutons et on centre les images
			$("#bloc-droit-galerie .slide").css("display","none");
			$("#bloc-droit-galerie ul").css({"position":"static","left":"auto","top":"auto"});
			$("#bloc-droit-galerie ul li").css({"display":"inline-block","position":"static","left":"auto"});
		}
		//fin affichage des boutons
		
		//gestion du click thumbs
		$(".galerie_thumbs a").click(function(){
			urlImg = $(this).attr("href");
			lienClickLI= $(this).parent();
			$("#photo img").animate({"opacity":0},200,function(){
				$(this).attr("src",urlImg);
				$(this).parent().attr("href",urlImg);
				$("#legende1").html("");
				$("#legende2").html("");
				positionLI = lienClickLI.index();//position
				numeroImage = positionLI; //numero de l'image
				//console.log(numeroImage);
				$(this).load(urlImg,function(){
					pix_desc = $("#galerie_desc div.pix_desc:eq("+positionLI+")");
					$("#legende1").html(pix_desc.find(".legende1").html());
					$("#legende2").html(pix_desc.find(".legende2").html());
					
					
					
					
					$("#contenu.galerie #photo img").bind('load', function (e) {
					// Do stuff on load
					
					 $(this).removeAttr("width"); 
					  $(this).removeAttr("height");
					  /*$(this).css("height","auto");
					   $(this).css("width","auto");*/
					   
					   $(this).css("height",0);
					   $(this).css("width",0);
					   //console.log(this);
					   //console.log("w:"+ this.width+" - h:"+ this.height);
					  if ( this.height >this.width) {
						var h = max_size*0.8;
						var w = Math.ceil(this.width/  this.height * max_size)*0.8;
						//alert("h grand");
					  } else {
						 
						var h = max_size*0.8;
						var w = Math.ceil(this.width/  this.height * max_size)*0.8;
						//alert("w grand");
						/* debug img panoramique*/
						 //console.log("contenu : "+$("#contenu").width()+" /////// w img :"+w + "- h:"+h);
						 if(w>$("#photo").width()){

							max_size = $("#photo").width()-30;
									

							w = max_size;
							h =  Math.ceil($(this).height() / $(this).width() * max_size);
							//console.log("contenu : "+$("#contenu").width()+" /////// w img :"+w + "- h:"+h);
						 }
					  }
					
					if(isNaN(h))
						h = "auto";
					if(isNaN(w))
						w = "auto";
						
					 $(this).css("height",h);
					   $(this).css("width",w);
					   	
					//$(this).css({ height: h, width: w });
					//console.log("ws 1 img :"+w + "- h:"+h);
					$(this).animate({"opacity":1},200);
					});

				
					
				});
				//alert("ok");
				
			});
			
			
			
			return false;	
		});
		
		$(".galerie_thumbs a:eq(0)").trigger("click");
		
		$("#photo a").fancybox({
			'titleShow'     : false
		});
		//fin gestion du click thumbs
		
		//gestion btn
		
	 
	   var timer_slideshow;
   
   
		$("#bloc-gauche-galerie li span").click(function(){
			if($(this).parent().hasClass("ico_info")){
				if($(this).parent().hasClass("actif")){
					$(this).parent().removeClass("actif");
					$(".desc-img").removeClass("cache");
				}else{
					$(this).parent().addClass("actif");
					$(".desc-img").addClass("cache");
				}
			}
			
			if($(this).parent().hasClass("ico_thumbnails")){
				if($(this).parent().hasClass("actif")){
					$(this).parent().removeClass("actif");
					$("#bloc-droit-galerie-fleches").addClass("cache");
					$("#bloc-droit-galerie").removeClass("cache");
				}else{
					$(this).parent().addClass("actif");
					$("#bloc-droit-galerie-fleches").removeClass("cache");
					$("#bloc-droit-galerie").addClass("cache");
				}
			}
			
			if($(this).parent().hasClass("ico_slideshow")){
				if($(this).parent().hasClass("actif")){
					$(this).parent().removeClass("actif");
					clearInterval(timer_slideshow);
				}else{
					$(this).parent().addClass("actif");
					timer_slideshow=setInterval("slideshow()", tempsSlideshow);
				}
			}
			
			
		});
		//fin gestion btn
		
		//btn nav
		$(".nav-gauche").click(function(){
			totalImages = $(".galerie_thumbs li").length;
			if(numeroImage==0){
				numeroImage= totalImages-1;
			}else{
				numeroImage--;	
			}
			$(".galerie_thumbs li:eq("+numeroImage+") a").trigger("click");	
		});
		
		$(".nav-droite").click(function(){
			totalImages = $(".galerie_thumbs li").length;
			if(numeroImage >= totalImages){
				numeroImage= 0;
			}else{
				numeroImage++;	
			}
			$(".galerie_thumbs li:eq("+numeroImage+") a").trigger("click");	
		});
		//fin btn nav
		
		
		$(window).resize(function() {
			
			hauteur = $(window).height() - 130;
			largeur = $("#content").width() - 250;
			$("#contenu.galerie").css({"height":hauteur,"width":largeur})
			$("#sidebar").css({"height":hauteur})
			$("#footer").css({"width":largeur-140})
			
			 max_size = hauteur;
			$("#contenu.galerie #photo img").each(function(i) {
			  $(this).removeAttr("width"); 
			  $(this).removeAttr("height");

			  if ($(this).height() > $(this).width()) {
				//console.log("portrait" + hauteur);
				max_size = max_size - 90;
				var h = max_size;
				var w = $(this).width() / $(this).height() * max_size;
			  } else {
				//console.log("paysage");
				max_size = max_size*0.8;
				max_size = max_size +90;
				var w = max_size;
				var h = ($(this).height() / $(this).width() )* max_size;
				
				if(w>$("#photo").width()){
							panoramil
							max_size = $("#photo").width()-20;
							w = max_size;
							h =  Math.ceil($(this).height() / $(this).width() * max_size);
							
							
							//console.log("contenu : "+$("#contenu").width()+" /////// w img :"+w + "- h:"+h);
							 
							
						 }
						 
			  }
			  
			  if(isNaN(h))
					h = "auto";
				if(isNaN(w))
					w = "auto";
					
				 $(this).css("height",h);
				   $(this).css("width",w);
				   
			  //$(this).css({ height: h, width: w });
			});
			
		});
		
		//fin galerie
	}else{
		//debut autre pages non galerie
		if($("#contenu.contenuScroll").length){
			conteneurAScroller = $("#contenu.contenuScroll");
			var contenuScroll = $('#contenu.contenuScroll').jScrollPane(settings);
		}
		
		if($("#bloc-scroll").length){
			conteneurAScroller = $("#bloc-scroll");
			var contenuScroll = $('#bloc-scroll').jScrollPane(settings);
		}
		
		
		
		var hauteur = $(window).height() - 150;
		var hauteurContenu = $(conteneurAScroller).height();
		if(hauteurContenu > hauteur){
			$(conteneurAScroller).css("height",hauteur);
		}
		
		//fin taille fenetre
		
		//$('.scroll-pane').jScrollPane();
	
		
		//fond home
		/*
		if($("body.home").length){
			
			nbImages = $("#bg-cache span").length;
			//$("#background").fullBg();
			
			var images =[] ;
			
			 $("#bg-cache").children('span').each(function() {
				var urlImageBG = $(this).text();
				images.push(urlImageBG);
			});
			 // The index variable will keep track of which image is currently showing
			var index = 0;
		
			// Call backstretch for the first time,
			// In this case, I'm settings speed of 500ms for a fadeIn effect between images.
			$.backstretch(images[index], {speed: 500});
		
			// Set an interval that increments the index and sets the new image
			// Note: The fadeIn speed set above will be inherited
			setInterval(function() {
				index = (index >= images.length - 1) ? 0 : index + 1;
				$.backstretch(images[index]);
			}, 5000);

			//var timer=setInterval("changeBG()", 8000);
			
		}*/
		
		$(window).resize(function() {
		  hauteur = $(window).height() - 150;
		  if(hauteurContenu > hauteur){
			$(conteneurAScroller).css("height",hauteur);
			}
		}); 
		//fin autre pages non galerie
		
		//full
		if($("#artiste_liste").length){
			
			var hauteur = $(window).height() - 130;
			var largeur = $("#content").width() - 250;
			$("#contenu").css({"height":hauteur,"width":largeur})
		
			conteneurAScroller = $("#contenu");
			element = conteneurAScroller.jScrollPane();
			
			
	
			
			$(window).resize(function() {
				var api = element.data('jsp');
			 	api.destroy();

				
				
				hauteur = $(window).height() - 130;
				largeur = $(window).width() - $("#sidebar").width() - 200;
				$("#contenu").css({"height":hauteur,"width":largeur})
				
				conteneurAScroller = $("#contenu");
				element = conteneurAScroller.jScrollPane();

				
			}); 
		}
	}
	
	if($(".contenu-full-milieu").length){
			
			var hauteur = $(window).height() - 130;
			var largeur = $(window).width() - $("#sidebar").width() - 200;
			$("#contenu").css({"height":hauteur,"width":largeur})
		
			conteneurAScroller = $("#contenu");
			element = conteneurAScroller.jScrollPane();
			
			
	
			
			$(window).resize(function() {
				var api = element.data('jsp');
			 	api.destroy();

				
				
				hauteur = $(window).height() - 130;
				largeur = $(window).width() - $("#sidebar").width() - 200;
				$("#contenu").css({"height":hauteur,"width":largeur})
				
				conteneurAScroller = $("#contenu");
				element = conteneurAScroller.jScrollPane();

				
			}); 
		}
	
	
	$(".btn-back").click(function(){
		history.back();
		return false;	
	});
	
	
});
