var galerieOuverte = false;

function LancerLaGalerie(idCategorie){
	
	//$.history.load("galerie");
	$.address.value("/galerie/");
	
	galerieOuverte = true;
	
	var currentIndex = -1;
	var width;
	var height;
	var nbImages;
	var miniatureCourante = 0;
	
	//$('html').scrollTop(0);
	
	$("body")
		.append('<div id="galerie-background"></div>')
		.find('#galerie-background')
			.css({
				height:$(document).height(),
				width: "100%",
				opacity: 0,
				top:0
			})
			.animate({
					opacity: 1
				}, 500)
			.end()
		.append('<div id="galerie-view-conteneur"></div>')
		.find("#galerie-view-conteneur")
			.css({
				top: 0
			})
			.hide()
			.load("galerie-view.php?c="+idCategorie, function(){
				
				nbImages = $(this).find("#view-nb-photos").text();
				
				ChangerDePhoto(0);
				
				$(this)
					.find("#view-une-photo")
						.hide()
						.fadeIn(1000)
						.end()
					.fadeIn(500)
					.find("#view-conteneur-miniatures")
						.css({margin:'auto', overflow:"hidden"})
						.find("#view-contenu-miniatures")
							.css({position:"relative",left:0,width:(nbImages * (120 + 5 * 2))})
							.end()
						.find('a')
							.bind("click", function(){
								ChangerDePhoto($(this).parent().find(".view-infos-index").text());
								return false;
							})
							.end()
						.end()
					.find("#view-go-right")
						.bind("mouseenter", function(){
							$(this).fadeTo(300, 1);
						})
						.bind("mouseleave", function(){
							$(this).fadeTo(300, .2);
						})
						.bind("click", function(){
							if( miniatureCourante < nbImages - 5 ){
								var decalage =  (nbImages - miniatureCourante - 5 > 5) ? 5 : nbImages - miniatureCourante-5
								miniatureCourante+=decalage;
								$("#view-contenu-miniatures")
									.animate({left:"-="+ ( decalage * 130 ) +"px"})
							}
						})
						.end()
					.find("#view-go-left")
						.bind("mouseenter", function(){
							$(this).fadeTo(300, 1);
						})
						.bind("mouseleave", function(){
							$(this).fadeTo(300, .2);
						})
						.bind("click", function(){
							if(miniatureCourante > 0){
								var decalage = (miniatureCourante > 5) ? 5 : miniatureCourante;
								miniatureCourante-=decalage;
								$("#view-contenu-miniatures")
									.animate({left:"+="+ (decalage * 130) +"px"})
							}
						})
					
				
			})
			.end()
		.append('<div id="galerie-view-fermer">Fermer</div>')
		.find("#galerie-view-fermer")
			.hide()
			.fadeTo(1000, .5)
			.bind("mouseenter", function(){
				$(this).fadeTo(300, 1);
			})
			.bind("mouseleave", function(){
				$(this).fadeTo(300, .5);
			})
			.bind("click", function(){
				history.back();
			})
			

	function ChangerDePhoto(index){
		if(currentIndex == index)
			return;
		currentIndex = index;
		$("#view-une-photo")
			.find("#view-photo-description")
				.hide()
				.end()
			.find("img")
			.fadeOut(300, function(){
				$(this).parent()
					.empty()
					.append('<img src="./galerie/images/'+$("#view-contenu-miniatures .view-une-miniature:eq("+index+") .view-infos-pathName").text()+'" alt="Image" />')
					.append('<div id="view-photo-description">'+$("#view-contenu-miniatures .view-une-miniature:eq("+index+") .view-infos-description").text()+'</div>')
					.find('img')
						.hide()
						.one("load", function(){
							var dimensions = getSizeOfImage(index);
							$(this)
								.css({width:dimensions.width,height:dimensions.height})
								.fadeIn(300,function(){
									if($(this).parent().find("#view-photo-description").text() != '')
										$(this).parent().find("#view-photo-description").fadeIn(200);
								})
								.parent()
									.css({
										paddingTop:(600 -dimensions.height) / 2,
										height: 600 - ((600 -dimensions.height) / 2)
									})
									
									.find("#view-photo-description")
										.hide()
										.css({marginLeft:((800 - dimensions.width) / 2)})
						})
						.each(function(){
							if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
								$(this).trigger("load");
						})
						.one("click", function(){
							ChangerDePhoto((parseInt(index)+1)%nbImages);
						})
			})
	}
}

function getSizeOfImage(index){
	var width = $(".view-une-miniature:eq("+index+") .view-infos-width").text();
	var height = $(".view-une-miniature:eq("+index+") .view-infos-height").text();
	
	if(width > 800){
		height = (800 / width) * height; 
		width = 800;
	}
	if(height > 600){
		width = (600 / height) * width;
		height = 600;
	}
	
	return {width:width,height:height};
}

function getPageHeight(){ // From Facebox
	var windowHeight
	if (self.innerHeight) {	// all except Explorer
	 windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	 windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
	 windowHeight = document.body.clientHeight;
	}
		
	/*windowHeight = $(document).height();
	if($("body").innerHeight() > windowHeight)
		windowHeight = $("body").outerHeight();
	*/	
	return windowHeight
}


function FermerLaGalerie(){
	$("#galerie-view-conteneur")
		.slideUp(500, function(){
			$(this).remove();
		});
	$("#galerie-background")
		.fadeOut(500, function(){
		$(this).remove();
	})
	$("#galerie-view-fermer")
		.remove();
		
	galerieOuverte = false;
}
