var encours = 1;

var lot_encours = 1;

var total_lots = array_cat.length-2;

var cat_active = null;
var nom_cat_active = null;

$(document).ready(function(){
	$('.facebox').facebox();				   
	for(var i = 0; i<array_img_cat.length; i++) {
		$("<img>").attr("src", array_img_cat[i]);
	}

	$("#btn_suiv_cat").each(function() {
		$(this).click(function() {
			change_lot("plus");					   
		});
	});
	$("#btn_pre_cat").each(function() {
		$(this).click(function() {
			change_lot("moins");					   
		});
	});
	$("#btn_suiv").each(function() {
		$(this).click(function() {
			suivante();					   
		});
	});
	$("#btn_pre").each(function() {
		$(this).click(function() {
			precedente();					   
		});
	});
	$(".cubes").click(function() {
		retour_accueil();						   
	});
	
	if(document.getElementById("cat")) {
		place_cat();
		change_lot("on");
	}
	document.onkeyup = keyboardEvent;
	
	$(".loader").animate({
		opacity: 0
	},300);
});

$(window).resize(function(){
	place_cat();						   
});

function place_cat() {
	if(cat_active == null) {
		$("#cat_1").css({
			top: $(window).height()-530,
			left: ($(window).width()/2)-487
		});
		$("#cat_2").css({
			top: $(window).height()-530,
			left: ($(window).width()/2)-159
		});
		$("#cat_3").css({
			top: $(window).height()-530,
			left: ($(window).width()/2)+169
		});
	}
	else {
		$(".cat div").each(function() {
			$(this).css({"zIndex": 4});
			if(this.id != "cat_"+cat_active) {
				$(this).css({
					opacity: 0.6,	
					top: $(window).height()-134,
					marginLeft: 82,
					width: 154,
					height: 94
				});
			}
			else {
				$("#cat_"+cat_active).css({
					top: $(window).height()-124,
					marginLeft: 82,
					width: 164,
					height: 104,
					opacity: 1
				});	
			}
		});	
	}
}

function keyboardEvent(e)
{
	if(document.all)e = event;
	
	if(document.getElementById("1")) {
		if(e.keyCode==37){	// gauche
			precedente();
		}	
		if(e.keyCode==39){	// droite
			suivante();
		}
	}

}

function suivante() {
	a_venir = encours+1;
	
	if(a_venir == total) {
		a_venir = 1;	
	}
	
	$("#"+encours).animate({
		left: -400,
		opacity: 0
	},500);
	
	$("#"+a_venir).css({
		"display": "block",
		"left": $("body").width()+600,
		"opacity": 0
	});
	$("#"+a_venir).animate({
		left: $("body").width()/2,
		opacity: 1
	},500);
	
	encours = a_venir;
}

function precedente() {
	a_venir = encours-1;
	
	if(a_venir == 0) {
		a_venir = total-1;	
	}
	
	$("#"+encours).animate({
		left: $("body").width()+600,
		opacity: 0
	},500);
	
	$("#"+a_venir).css({
		"display": "block",
		"left": -400,
		"opacity": 0
	});
	$("#"+a_venir).animate({
		left: $("body").width()/2,
		opacity: 1
	},500);
	
	encours = a_venir;
}

function go_cat(num,quoi) {
	$("#cat_"+num).animate({
		top: $(window).height()-124,
		marginLeft: 82,
		width: 164,
		height: 104,
		opacity: 1
	},500);	
	
	$(".cat div").each(function() {
		$(this).css({"zIndex": 4});
		if(this.id != "cat_"+num) {
			$(this).animate({
				opacity: 0.6,	
				top: $(window).height()-134,
				marginLeft: 82,
				width: 154,
				height: 94
			},500);
		}
	});	
	
	$("#navig_cat").animate({
		bottom: 51,
		marginLeft: -461,
		width: 922
	},500);
	
	cat_active = num;
	nom_cat_active = num+array_cat[num-1];
	charg_liste(quoi);
}

function charg_liste(quoi) {
	var aleatoire = Math.random();
	
	$(".loader").animate({
		opacity: 1
	},300);

	var xhr_object = null;
	
	if(window.XMLHttpRequest) // Firefox
	   xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return;
	}
	
	var method   = "GET";
	var filename = "./include/ajax.php?cat="+quoi+"&a="+aleatoire;
	
	xhr_object.open(method, filename, true);
	
	xhr_object.onreadystatechange = function() {
	
		if(xhr_object.readyState == 1) {}
		
		if(xhr_object.readyState == 4) {
			var tmp = xhr_object.responseText;
			tmp = tmp.split("::##::");
			document.getElementById("liste_img").innerHTML = tmp[0];
			total = tmp[1];
			
			encours = 1;
			
			$("#liste_img").css({
				display: "block"					
			});
			$("#navig").css({
				display: "block"
			});
			
			$(".loader").animate({
				opacity: 0
			},300);
		}
	}	
	
	xhr_object.send(null);		
}

function retour_accueil() {
	$("#liste_img").css({
		display: "none"					
	});
	$("#navig").css({
		display: "none"					
	});
	$("#cat_1").animate({
		top: $(window).height()-530,
		left: ($(window).width()/2)-487,
		width: 316,
		height: 448,
		opacity: 1,
		marginLeft: 0
		
	});
	$("#cat_2").animate({
		top: $(window).height()-530,
		left: ($(window).width()/2)-159,
		width: 316,
		height: 448,
		opacity: 1,
		marginLeft: 0
		
	});
	$("#cat_3").animate({
		top: $(window).height()-530,
		left: ($(window).width()/2)+169,
		width: 316,
		height: 448,
		opacity: 1,
		marginLeft: 0
	});
	
	$("#navig_cat").animate({
		bottom: 300,
		marginLeft: -550,
		width: 1099
	},500);
	
	cat_active = null;
	nom_cat_active = null;
}

function change_lot(comment) {
	if(comment == "plus") {
		lot_encours = lot_encours + 3;	
	}
	if(comment == "moins") {
		lot_encours = lot_encours - 3;
	}
	
	if(lot_encours > total_lots) {
		lot_encours = 1;	
	}
	if(lot_encours <= 0) {
		lot_encours = total_lots;	
	}
	
	if(cat_active != null) {
		$(".cat div").each(function() {
			$(this).css({"zIndex": 4});
			$(this).animate({
				opacity: 0.6,	
				top: $(window).height()-134,
				marginLeft: 82,
				width: 154,
				height: 94
			},500);
		});
	}
	
	var a = 1;
	for(i=lot_encours;i<lot_encours+3;i++) {
		var iii = i-1;
		document.getElementById("img_cat_"+a).src = "./img_graph/cat/"+array_cat[iii]+".jpg";
		
		if(a+array_cat[iii] == nom_cat_active) {
			$("#cat_"+a).css({"zIndex": 4});
			$("#cat_"+a).animate({
				top: $(window).height()-124,
				marginLeft: 82,
				width: 164,
				height: 104,
				opacity: 1
			},500);
		}
		
		$("#cat_"+a).click(function() {
			var lid = this.id;
			var aa = lid.replace("cat_","");
			var aaa = ((lot_encours-1)+parseInt(aa))-1;
			go_cat(aa,array_cat[aaa]);			
		});
		a++;
	}
}