// JavaScript Document

// ---------------------------------------------------------------------------------------------------- //
// Scripts Atiweb
// ---------------------------------------------------------------------------------------------------- //

// ---------------------------------------------------------------------------------------------------- //
// Nombres de connectés sur le site
// ---------------------------------------------------------------------------------------------------- //
function NbrConnectes(div) {
	setTimeout("afficheNbrConnectes('" + div + "')", 10000);	
}
function afficheNbrConnectes(div) {
	$.ajax({
		async : true,
		url: '/includes/modules/nbr-connectes.php',
		type: "GET",
		cache:false,
		success: function(data) {
			num_old = $('#' + div + ' strong').html();			
			$('#' + div).html(data);
			num = $('#' + div + ' strong').html();	
			if(num != num_old) {
				$('#' + div + ' span').css("background-color", "#F26D0B");
				setTimeout("animNbrConnectes('" + div + "')", 500);
			}			
			NbrConnectes(div);
		}
	});
}
function animNbrConnectes(div) {
	$('#' + div + ' span').css("background-color", "");
}

// ---------------------------------------------------------------------------------------------------- //
// Suppression des enchères auto
// ---------------------------------------------------------------------------------------------------- //
function supprBot(id ,id_article) {
	var test = confirm("Voulez vous supprimer cette enchère automatique ?");
	if(test && (id_article == "" || id_article == undefined)) window.location.href = "/ut-encheres-auto.html?action=suppr&id=" + id;
	else if(test) window.location.href = "/ut-encheres-auto.html?action=suppr&id=" + id + "&retour=1&id_article=" + id_article; 
}

// ---------------------------------------------------------------------------------------------------- //
// Texte défilant sur la page d'accueil
// ---------------------------------------------------------------------------------------------------- //
var tda_cu = 0;
var tda_ne = 1;
function afficheTDA(div) {
	nb_div = $('#' + div).children("div").length;	
	if(tda_cu > nb_div - 1) tda_cu = 0;
	if(tda_ne > nb_div - 1) tda_ne = 0;		
	$('#' + div).children("div").eq(tda_cu).fadeOut("slow",function() {
		$('#' + div).children("div").eq(tda_ne).fadeIn("slow");
		tda_cu++;
		tda_ne++;
	});
}

// ---------------------------------------------------------------------------------------------------- //
// Galerie photos
// ---------------------------------------------------------------------------------------------------- //
function masqueApGal() {
	nb_img = $('#gd_img_ap').children("img").length;
	for(i=0;i<nb_img;i++) $('#gd_img_ap').children("img").eq(i).fadeTo(100, 0.5);	
}
function afficheApGal(img) {
	$('#gd_img_ap').children("img").eq(img).fadeTo(100, 1);	
}
function changeImageGalerie(img,src) {
	$('#gd_img_grand').children("img").eq(0).attr("src", src);
}

// ---------------------------------------------------------------------------------------------------- //
// Calcul du temps
// ---------------------------------------------------------------------------------------------------- //
var ts_enchere = "";
var ts_enchere_tab = new Array();

function calculTemps(ts, id_article) {
	
	if(isNaN(ts) || String(parseInt(ts)) == "NaN") return ts;
	
	// Initialisation
	if(id_article != "" && id_article != undefined) ts_enchere_tab[id_article] = ts;
	else ts_enchere = ts;
	
	// Reste
	reste = ts % 3600;
	// Heures (avec les jours)
	heures = (ts - reste) / 3600;
	// Secondes
	ts = reste % 60;
	// Minutes
	minutes = (reste - ts) / 60;
	// Jours
	jours = Math.floor(heures / 24);
	// Heures (sans les jours)
	heures = heures % 24;
	
	// Affichage
	temps = "";
	if(jours > 0) temps = jours + "<span class=\"f_20\">j</span>";
	if(heures > 0) temps += heures + "<span class=\"f_20\">h</span>";
	if(minutes > 0) temps += minutes + "<span class=\"f_20\">m</span>";
	if(ts < 10) ts = "0" + ts;
	temps += ts + "<span class=\"f_20\">s</span>";
	
	// Mise à jour du temps
	if(id_article != "" && id_article != undefined) ts_enchere_tab[id_article] --;
	else ts_enchere --;
	
	// Retour
	return temps;
	
}

// ---------------------------------------------------------------------------------------------------- //
// Affiche temps multiple
// ---------------------------------------------------------------------------------------------------- //
var prix_enchere_tab = new Array();

function afficheTempsMultiple(articles, cadre_temps, cadre_prix, cadre_acheteur) {
	
	clearInterval(timer);
	
	$.ajax({
		async : true,
		url: '/encheres/affiche-temps-multiple.php',
		type: "GET",
		cache:false,
		data: "id=" + articles,
		success: function(data) {
			
			retour = data.split("[]");
			for(i=0;i<retour.length;i++) {
				donnees_enchere = retour[i].split("|"); 
				id_article = donnees_enchere[0];
				if(id_article != "") {
				
					$('#' + cadre_temps + id_article).html(calculTemps(donnees_enchere[1], id_article));					
					if(cadre_prix != "") $('#' + cadre_prix + id_article).html(donnees_enchere[4]);
					if(cadre_acheteur != "") {
						$('#' + cadre_acheteur + id_article).html(donnees_enchere[5]);
						$('#' + cadre_acheteur + id_article + "_2").html(donnees_enchere[6]);
						$('#' + cadre_acheteur + id_article + "_3").html(donnees_enchere[7]);
					}
					
					// Changement du chrono vers la fin de l'enchère
					if(donnees_enchere[2] != "") {
						$('#chrono_enchere' + id_article).removeClass();
						$('#chrono_enchere' + id_article).addClass('rouge');
					} else {
						$('#chrono_enchere' + id_article).removeClass('rouge');
					}
					
					// Changement du cadre pendant les dernières secondes
					if(donnees_enchere[3] != "") {
						$('#box_enchere_detail' + id_article).css('background-color', '#e87901');
						$('#prix_enchere' + id_article).css('color', '#FFCC00');
						$('#div_enchere' + id_article).css('color', '#FFFFFF');
					} else {
						$('#box_enchere_detail' + id_article).css('background-color', '');
						$('#prix_enchere' + id_article).css('color', '');
						$('#div_enchere' + id_article).css('color', '');
					}
					
					// Changement de couleur lors d'une enchère
					prix = donnees_enchere[4];
					if(prix_enchere_tab[id_article] != "" && prix_enchere_tab[id_article] != prix) {						
						$('#chrono_enchere' + id_article).css('color', '#FFF');
						$('#div_chrono' + id_article).css('background-color', '#e87901');		
						$('#verification' + id_article).removeClass();	
						// Synchro
						$('#chrono_enchere' + id_article).html(calculTemps(donnees_enchere[1], id_article));
					}
					else {
						$('#chrono_enchere' + id_article).css('color', '');
						$('#div_chrono' + id_article).css('background-color', '');
					}
					prix_enchere_tab[id_article] = prix;
					
					// Affichage d'un 2e chrono lors d'une enchère privée ou d'une enchère star
					if(donnees_enchere[8] != '' || donnees_enchere[9] != '') {
						$('#chrono2' + id_article).show();
						// Enchère privée
						if(donnees_enchere[8] != '') $('#chrono2' + id_article).html('<span>'+ donnees_enchere[8] +'</span><small>s</small>'); 
						// Enchère star
						if(donnees_enchere[9] != '') {
							if(donnees_enchere[9] < 0) {
								$('#chrono2' + id_article).addClass("chrono2_elimination");
								$('#chrono2' + id_article).html('<span>'+ donnees_enchere[9].substr(1) +'</span><small>s</small>');
							}
							else $('#chrono2' + id_article).html('<span>'+ donnees_enchere[9] +'</span><small>s</small>'); 
						}
					} else {
						$('#chrono2' + id_article).hide();
						$('#chrono2' + id_article).html('');
					}
					
					// Meilleurs enchérisseurs
					if(donnees_enchere[10] != undefined && donnees_enchere[10] != "") {
						texte_encherisseurs = "";
						var encherisseurs = donnees_enchere[10].split('##');
						for(j=0;j<encherisseurs.length-1;j++) {
							donnees_ench = encherisseurs[j].split('#');
							var ench_login = donnees_ench[0];
							var ench_mises = donnees_ench[1];
							texte_encherisseurs += "<div>";
							texte_encherisseurs += j + 1 + ". ";
							texte_encherisseurs += donnees_ench[0];
							if(parseInt(donnees_ench[1]) == 1) texte_encherisseurs += " - <span class='orange'>" + donnees_ench[1] + " mise / " + donnees_ench[2] + "</span>";
							else if(parseInt(donnees_ench[1]) > 0) texte_encherisseurs += " - <span class='orange'>" + donnees_ench[1] + " mises / " + donnees_ench[2] + "</span>";
							else texte_encherisseurs += " - <span class='orange'>-- mise</span>";
							texte_encherisseurs += "</div>";							
						}
						$('#liste_top_encherisseurs').html(texte_encherisseurs);
						$('#top_encherisseurs_info').html(donnees_enchere[11]);
					}
				}
			}
			timer = setInterval("afficheTempsMultiple(articles, cadre_temps, cadre_prix, cadre_acheteur)", 400);
		}
	});
}

// ---------------------------------------------------------------------------------------------------- //
// Affiche temps
// ---------------------------------------------------------------------------------------------------- //
var encherisseur = "";
var prix_enchere = "";

function afficheTemps(article, cadre_temps, cadre_prix, cadre_acheteur, actualiser) {
	
	clearInterval(timer);	
	id_article = article;

	if( (actualiser != "" && actualiser != undefined) || actualiser == false) actualiser = false;
	else actualiser = true;
	
	$.ajax({
		async : true,
		url: '/encheres/affiche-temps.php',
		type: "GET",
		cache:false,
		data: "id=" + article,
		success: function(data) {

			retour = data.split("|");
			
			$('#' + cadre_temps).html(calculTemps(retour[0]));				
			if(cadre_prix != "") $('#' + cadre_prix).html(retour[3]);
			if(cadre_acheteur != "") $('#' + cadre_acheteur).html(retour[4]);

			// Historique
			var texte_historique = "";
			var historique = retour[5];
			if(historique != "") {
				var hist = historique.split('##');
				for(i=0;i<hist.length-1;i++) {
					donnees_hist = hist[i].split('#');
					var hist_temps = donnees_hist[0];
					var hist_pseudo = donnees_hist[1];
					var hist_type = donnees_hist[2];
					texte_historique += "<div class='colonne_hist'>";
					texte_historique += "<div class='colonne_100 centrer couleur_texte_1'>" + donnees_hist[0] + "</div>";
					texte_historique += "<div class='colonne_100 centrer couleur_texte_1'>" + donnees_hist[1] + "</div>";
					texte_historique += "<div class='colonne_70 centrer couleur_texte_1'>" + donnees_hist[2] + "</div>";
					texte_historique += "</div>";
				}
				$('#historique').html(texte_historique);
			}

			// Enchérisseurs
			if(retour[6] != "") {
				encherisseurs = retour[6].split(";");
				 $('#encherisseur_un').html(encherisseurs[0]);
				 encherisseur = encherisseurs[0];
				 $('#encherisseur_deux').html(encherisseurs[1]);
				 $('#encherisseur_trois').html(encherisseurs[2]);
			}
			
			// Changement du chrono vers la fin de l'enchère
			if(retour[1] != "") {
				$('#chrono_enchere' + id_article).addClass('rouge');
			} else {
				$('#chrono_enchere' + id_article).removeClass('rouge');
			}
			
			// Changement du cadre pendant les dernières secondes
			if(retour[2] != "") {
				$('#box_enchere_detail' + id_article).css('background-color', '#e87901');
				$('#prix_enchere' + id_article).css('color', '#FFCC00');
				$('#div_enchere' + id_article).css('color', '#FFFFFF');
			} else {
				$('#box_enchere_detail' + id_article).css('background-color', '');
				$('#prix_enchere' + id_article).css('color', '');
				$('#div_enchere' + id_article).css('color', '');
			}
			
			// Changement de couleur lors d'une enchère
			if(document.getElementById(cadre_prix)) {
				prix = document.getElementById(cadre_prix).innerHTML = retour[3];
				if(prix_enchere != "" && prix_enchere != prix) {
					$('#chrono_enchere' + id_article).css('color', '#FFF');
					$('#div_chrono' + id_article).css('background-color', '#e87901');		
					$('#verification' + id_article).removeClass();
					// Synchro
					$('#' + cadre_temps).html(calculTemps(retour[0]));
				}
				else {
					$('#chrono_enchere' + id_article).css('color', '');
					$('#div_chrono' + id_article).css('background-color', '');
				}			
				prix_enchere = prix;
			}
			
			// Affichage d'un 2e chrono lors d'une enchère privée ou d'une enchère star
			/*if(retour[7] != '0' || retour[8] != '0') {
				$('#chrono2').show();
				// Enchère privée
				if(retour[7] != '0') $('#chrono2').html('<span class="rouge f_26">Attention</span>, plus que <span>'+ retour[7] +'</span><small>s</small> pour enchérir et participer à la suite de l\'enchère');
				// Enchères star
				if(retour[8] != '0') {
					if(retour[8] < 0) $('#chrono2').html('<span class="rouge f_26">Attention</span>, plus que <span>'+ retour[8].substr(1) +'</span><small>s</small> avant la prochaine élimination');
					else $('#chrono2').html('<span class="rouge f_26">Attention</span>, plus que <span>'+ retour[8] +'</span><small>s</small> pour enchérir et participer à la suite de l\'enchère');
				}
			} else {
				$('#chrono2').hide();
				$('#chrono2').html('');
			}*/
			
			// Meilleurs enchérisseurs
			if(retour[9] != undefined && retour[9] != "") {
				texte_encherisseurs = "";
				var encherisseurs = retour[9].split('##');
				for(i=0;i<encherisseurs.length-1;i++) {
					donnees_ench = encherisseurs[i].split('#');
					var ench_login = donnees_ench[0];
					var ench_mises = donnees_ench[1];
					texte_encherisseurs += "<div>";
					texte_encherisseurs += i + 1 + ". ";
					texte_encherisseurs += donnees_ench[0];					
					if(parseInt(donnees_ench[1]) == 1) texte_encherisseurs += " - <span class='orange'>" + donnees_ench[1] + " mise</span>";
					else if(parseInt(donnees_ench[1]) > 0) texte_encherisseurs += " - <span class='orange'>" + donnees_ench[1] + " mises</span>";
					else texte_encherisseurs += " - <span class='orange'>-- mise</span>";
					texte_encherisseurs += "</div>";
				}
				$('#liste_top_encherisseurs').html(texte_encherisseurs);
				$('#top_encherisseurs_info').html(retour[10]);		
			}			
			
			if(actualiser) timer = setInterval("afficheTemps(article, cadre_temps, cadre_prix, cadre_acheteur)", 400);
			
		}
	});
	
}

// ---------------------------------------------------------------------------------------------------- //
// Enchérir
// ---------------------------------------------------------------------------------------------------- //
function encherir(id_article, login, pseudo, url_prix, url_ench, url_solde, url_chrono, div_chrono) {
	$.ajax({
		url: "includes/modules/encherir.php",
		async:false,
		type: "POST",
		data: "id="+id_article+"&lg="+login,
		success: function(data) {
			if(data != '') alert(data);				
			else {
				$.ajax({
					async:false,
					url: '/encheres/affiche-temps.php',
					type: "GET",
					cache:false,
					data: "id=" + id_article,
					success: function(data) {
						retour = data.split("|");						
						// Prix
						$('#prix_enchere' + id_article).html(retour[3]);						
						// Enchérisseur
						$('#pseudo_enchere' + id_article).html(retour[4]);		
						// Synchro du chrono
						if(prix_enchere_tab.length > 0) $('#chrono_enchere' + id_article).html(calculTemps(retour[0], id_article));
						else $('#chrono_enchere' + id_article).html(calculTemps(retour[0]));
						// Couleurs : Chrono
						$('#chrono_enchere' + id_article).css('color', '#FFF');
						$('#div_chrono' + id_article).css('background-color', '#e87901');		
						$('#verification' + id_article).removeClass();
						// Couleurs : Cadre
						$('#box_enchere_detail' + id_article).css('background-color', '');
						$('#prix_enchere' + id_article).css('color', '');
						$('#div_enchere' + id_article).css('color', '');
						// Mise à jour du prix						
						if(prix_enchere_tab.length > 0) prix_enchere_tab[id_article] = retour[3];
						else prix_enchere = retour[3];
					}
				});
				
				// Mise à jour du solde
				$.ajax({
					async:false,
					url: '/includes/modules/solde.php',
					type: "GET",
					cache:false,
					data: "lg=" + login,
					success: function(data) {					
						// Mise à jour du solde					
						$('#solde_client').html(data);
					}
				});
				
				if(document.getElementById('cadre_achat_immediat')) $("#cadre_achat_immediat").load('/includes/ajax/cadre-achat-immediat.php?id=' + id_article);
			}
			
		}
	});
}

// ---------------------------------------------------------------------------------------------------- //
// Affiche temps pour les jeux
// ---------------------------------------------------------------------------------------------------- //
function afficheTempsJeux(articles, cadre_temps, cadre_prix) {
	
	clearInterval(timer);
	
	$.ajax({
		async : true,
		url: '/encheres/affiche-temps-jeux.php',
		type: "GET",
		cache:false,
		data: "id=" + articles,
		success: function(data) {
			retour = data.split("[]");
			for(i=0;i<retour.length;i++) {
				donnees_enchere = retour[i].split("|"); 
				id_article = donnees_enchere[0];
				if(id_article != "") {
					if(!isNaN(donnees_enchere[1])) $('#' + cadre_temps + id_article).html(calculTemps(donnees_enchere[1], id_article) + ' <span class="f_12">avant le début de l\'enchère</span>');	
					else $('#' + cadre_temps + id_article).html(calculTemps(donnees_enchere[1], id_article));																									
					if(cadre_prix != "") $('#' + cadre_prix + id_article).html(donnees_enchere[2]);
				}
			}
			timer = setInterval("afficheTempsJeux(articles, cadre_temps, cadre_prix)", 400);
		}
	});
}

// ---------------------------------------------------------------------------------------------------- //
// Mise sur un jeu
// ---------------------------------------------------------------------------------------------------- //
function miseJeu(id_article) {
	$.ajax({
		url: "includes/modules/mise-jeu.php",
		async:false,
		type: "POST",
		data: "id="+id_article,
		success: function(data) {
			if(data != '') {
				alert(data);
				return false;
			} else $('#formulaire_' + id_article).submit();
		}
	});
}

// ---------------------------------------------------------------------------------------------------- //
// Anciens scripts
// ---------------------------------------------------------------------------------------------------- //

// RESET LE FORMULAIRE DE CONTACT
function effacer_contact() {
	document.getElementById('nom').value = "";
	document.getElementById('prenom').value = "";
	document.getElementById('mail').value = "";
	document.getElementById('adresse').value = "";
	document.getElementById('code_postal').value = "";
	document.getElementById('ville').value = "";
	document.getElementById('commentaire').value = "";
}

// ACHAT IMMEDIAT
function achatImmediat(id_article, login, prix)
{	
	$.ajax({
		url: "includes/modules/achat_immediat.php",
		async:true,
		type: "POST",
		data: "id="+id_article+"&lg="+login+"&prix="+prix,
		success: function(data){
			retour = data.split("|");
			if(typeof(retour[1]) != "undefined") statut = retour[1];
			else statut = "";
			alert(retour[0]);
			if(statut == "termine" || statut == "") window.location.href = '/ut-encheres-remportees.html';
		}
	});
}

// FERMETURE DU SITE
function fermetureSite()
{	
	$(document).load('includes/modules/fermeture.php')
}

// OUVERTURE DU SITE
function ouvertureSite()
{	
	$(document).load('includes/modules/ouverture.php')
}
