var article = "home";
var type = "VP1E";
var canRefresh = true;
var counterTotal0 = 0;
var counterTotal1 = 0;

//Met à jour les infos de tous les voyages
function updateStats() {
	$.post("engine/functions.php", {article: article, type:type}, function(xml) {
		var indexVoyage = 0;
		$(xml).find("article").each(function() {
			var articleId = $(this).attr("article");
			var articleTermine = 0;
			$(this).find("stat_0").each(function() { $("#stat_0_" + articleId).text($(this).text()); });
			$(this).find("stat_1").each(function() { $("#stat_1_" + articleId).text($(this).text()); });
			$(this).find("stat_2").each(function() { $("#stat_2_" + articleId).text($(this).text()); });
			$(this).find("stat_2b").each(function() { $("#stat_2b_" + articleId).text($(this).text()); });
			$(this).find("stat_3").each(function() { $("#stat_3_" + articleId).text($(this).text()); });
			$(this).find("stat_4").each(function() { $("#stat_4_" + articleId).text($(this).text()); });
			$(this).find("stat_5").each(function() { $("#stat_5_" + articleId).text($(this).text()); });
			$(this).find("termine").each(function() {
				articleTermine = $(this).text();
			});
			$(this).find("date_fin").each(function() {
				var dateExploded = $(this).text().split(":");
				
				if(indexVoyage == 0)
					counterTotal0 = dateExploded[6];
				else if(indexVoyage == 1)
					counterTotal1 = dateExploded[6];
				
				if(counterTotal0 == 0)
					articleTermine = 1;
				else if(counterTotal1 == 0)
					articleTermine = 1;
			});
			//counter = 223;
			
			//Article terminé ?
			if(articleTermine != "0") {
				if(canRefresh == true)
					window.location.reload();
				$("#articleButton" + articleId).hide();
				$("#Finestimation" + articleId).hide();
				$("#bandeauGagne" + articleId).show();
			}
			else {
				$("#articleButton" + articleId).show();
			}
			
			indexVoyage++;
		});
		setTimeout(updateStats, 15000);
	});
}

function enchere(articleEnchere, estimation) {
	if(uniqueEnchere > 0) {
		if(notIE6)
			$("#articleButtonSubmit").fadeTo(100, 0.33);
		else
			$("#articleButtonSubmit").hide();
		
		uniqueEnchere--;
		if(estimation == undefined)
			estimation = $("#articleInput").val();
		$.post("engine/functions.php", {article: articleEnchere, estimation: estimation}, function(xml) {
			enchereResult(xml);
		});
	}
}

function encheres(articleEnchere) {
	var estimationStart = $("#articleInputStart").val();
	var estimationEnd = $("#articleInputEnd").val();
	var estimationNb = $("#articleInputNb").val();
	var estimationPalier = $("#articleInputPalier").val();
	
	if(notIE6)
		$("#articleButtonSubmitFourchette").fadeTo(100, 0.33);
	else
		$("#articleButtonSubmitFourchette").hide();

	$.post("engine/functions.php", {article: articleEnchere, estimationStart: estimationStart, estimationEnd: estimationEnd, estimationNb: estimationNb, estimationPalier: estimationPalier}, function(xml) {
		enchereResult(xml);
	});
}

function noMoreCredits() {
	$("#userCredits").fadeIn(50).fadeOut(50).fadeIn(50).fadeOut(50).fadeIn(50).fadeOut(50).fadeIn(50).fadeOut(50).fadeIn(50).fadeOut(50).fadeIn(50, showPaiementLB);
}

function enchereResult(xml) {
	uniqueEnchere++;
	var erreur = "";
	var estimation = "";
	$(xml).find("estimationOk").each(function()  { window.location.href = "resultatEnchereGagnante.php?article=" + article; });
	$(xml).find("estimationNok").each(function() { window.location.href = "resultatEnchere.php"; });

	$(xml).find("erreurCredits").each(function() { noMoreCredits(); });
	$(xml).find("erreurPrix").each(function() 	 { erreur = "Le prix que vous avez saisi est incorrect."; });
	$(xml).find("erreurUser").each(function() 	 { erreur = "Pour estimer un voyage, vous devez vous identifier. Si vous n'avez pas de compte, cliquez sur \"S'inscrire\" et créez votre compte en 20 secondes !"; });
	$(xml).find("erreurActive").each(function()  { erreur = "Pour estimer un voyage, vous devez avoir un compte activé. Vous pouvez refaire une demande d'email d'activation dans le menu de gauche."; });
	$(xml).find("erreurArticle").each(function() { window.location.reload(); });
	$(xml).find("estimation").each(function() 	 { estimation = $(this).text(); });

	//$("#articleErreur").text(erreur);
	if(erreur != "") {
		alert(erreur);
		if(notIE6) {
			$("#articleButtonSubmit").fadeTo(100, 1.0);
			$("#articleButtonSubmitFourchette").fadeTo(100, 1.0);
		}
		else {
			$("#articleButtonSubmit").show();
			$("#articleButtonSubmitFourchette").show();
		}
	}

	//MAJ
	updateUser();
	updateStats();
}

function updateCounter() {
	//Calcul du temps restant en DD:HH:MM:SS
	counterTotal0 = counterTotal0 - 1;
	updateCounterFunction(counterTotal0, 0);
	
	//Calcul du temps restant en DD:HH:MM:SS
	counterTotal1 = counterTotal1 - 1;
	updateCounterFunction(counterTotal1, 1);
	
	//Calcul du temps restant en DD:HH:MM:SS
	updateCounterFunction(counterTotal0, "");
	
	//Update
	setTimeout(updateCounter, 1000);
}

function updateCounterFunction(counterStart, id) {
	var counter = counterStart;
	var counterD = 0;
	var counterH = 0;
	var counterM = 0;
	var counterS = 0;
	var counterDstr = "";
	var counterHstr = "";
	var counterMstr = "";
	var counterSstr = "";
/*
	
	if(counter <= 1)
		updateStats();
	
*/
	if(counter > 0) {
		while(counter >= 86400) {
			counter -= 86400;
			counterD++;
		}
		counterDstr = "" + counterD;
		if(counterDstr.length == 1)
			counterDstr = "0" + counterD;

		while(counter >= 3600) {
			counter -= 3600;
			counterH++;
		}
		counterHstr = "" + counterH;
		if(counterHstr.length == 1)
			counterHstr = "0" + counterH;

		while(counter >= 60) {
			counter -= 60;
			counterM++;
		}
		counterMstr = "" + counterM;
		if(counterMstr.length == 1)
			counterMstr = "0" + counterM;

		counterS = counter;
		counterSstr = "" + counterS;
		if(counterSstr.length == 1)
			counterSstr = "0" + counterS;

		$("#countdownD" + id).text(counterDstr);
		$("#countdownH" + id).text(counterHstr);
		$("#countdownM" + id).text(counterMstr);
		$("#countdownS" + id).text(counterSstr);
	}
}
