
window.onload=function(){enableTooltips("menu")};

function decompte(restant, id, pseudo, mise)
{
	$('#pseudo'+id).html(pseudo);
	$('#dmise'+id).html(mise);

	jour = parseInt((restant / (60 * 60 * 24)), 10);
    heure = parseInt((restant / (60 * 60) - jour * 24), 10);
	minute = parseInt((restant / 60 - jour * 24 * 60 - heure * 60), 10);
	seconde = parseInt((restant - jour * 24 * 60 * 60 - heure * 60 * 60 - minute * 60), 10);
	
	if (jour == 0) {
           $('#jours'+id).css('display', 'none');
           $('#j'+id).css('display', 'none');
         } else {
           $('#jours'+id).html(jour);
         }

	if (heure == 0 && jour == 0) {
           $('#heure'+id).css('display', 'none');
           $('#h'+id).css('display', 'none');
         } else {
           $('#heure'+id).html(heure);
         }

	if (minute == 0 && heure == 0 && jour == 0 && seconde !=0) {
           $('#minute'+id).css('display', 'none');
           $('#m'+id).css('display', 'none');
         } else {
		 
			//Mise en évidence fin d'enchere...
			if( (jour == 0 && heure == 0 && minute==2 && (seconde == 59||seconde ==0)) || (jour == 0 &&heure == 0 && minute==1 && (seconde == 59||seconde ==0)) ){
			$('#cpt'+id).fadeOut(300).fadeIn(300).addClass('rouge');
			}
			if(jour == 0 && heure == 0 && minute<=2 ){$('#cpt'+id).addClass('rouge');}
			$('#minute'+id).html(minute);
         }

	if (seconde <= 0 && minute == 0 && heure == 0 && jour == 0) {
           $('#seconde'+id).css('display', 'none');
           $('#s'+id).css('display', 'none');
		   $('#bValid'+id).css('display', 'none');
		   $('#cpt'+id).html("Terminé").addClass('rouge');
         } else {
		    //Mise en évidence fin d'enchere...
			if(jour == 0 && heure == 0 && minute==0 && seconde <=59 ){$('#cpt'+id).addClass('rouge');}
			$('#seconde'+id).html(seconde);
         }
	
	if (restant > 0)
		setTimeout('ajax('+id+');', 1000);
	else
	{
		$('#cpt'+id).html("Terminé").addClass('rouge');
		$('#bValid'+id).css('display', 'none');
		// Temps fini, on fait ce qu'on veux
		/*$('#jours'+id).html(0+' r'+restant);
		$('#heure'+id).html(0);
		$('#minute'+id).html(0);
        $('#seconde'+id).html(0);*/
		$('#timer-ench').html("Pause").fadeOut(300).fadeIn(300);
		setTimeout('ajax('+id+');', 1000);
	}
	/*
	if(jour==0 && heure==0 && minute==0 && seconde ==10)
	{
		//verification enchere auto
		verifenchauto(id);
	}*/
	
}

function verifenchauto(id)
{
	$.ajax({
	
    type: "GET",
    url: "verif_enchere_auto.php?id=" + id,
    success: function(retour){
	    alert('encherissement auto');
   }
});
}

function verifier(id)
{
		$('#cpt'+id).html("Pause").fadeOut(300).fadeIn(300);
		$('#bValid'+id).html("<img src='http://banner.codencheres.com/images/pause.png' alt='Pause' title='Enchère en pause'/>");
		$('#timer-ench').html("Pause").fadeOut(300).fadeIn(300);
		setTimeout('ajax('+id+');', 1000);
}

function ajax(id) {

$.ajax({
	
    type: "GET",
    url: "time.php?id=" + id,
    success: function(retour){
				
               if (retour != 0) {             
                var recup = retour;
                var elem = recup.split('|');
                tmp = elem[0];
                pseudo = elem[1];
                mise = elem[2];
                tp = elem[3];
				pause = elem[4];
				if(pause==1){
					verifier(id);
				}else{
					
					if( tp == 1 && pause ==0)
					{
						//j'affiche de nouveau chrono
						$('#cpt'+id).html("<span id=\"jours"+id+"\"></span> <span id=\"j"+id+"\" style=\"font-size: medium;\">j</span> <span id=\"heure"+id+"\" ></span> <span id=\"h"+id+"\" style=\"font-size: small;\">h</span> <span id=\"minute"+id+"\" ></span> <span id=\"m"+id+"\" style=\"font-size: x-small;\">m</span> <span id=\"seconde"+id+"\"></span> <span id=\"s"+id+"\" style=\"font-size: xx-small;\">s</span>");
						
					}
					decompte(tmp, id, pseudo, mise);
				}
                if (tp == 1 && pause ==0) {
                $('#bValid'+id).html("<input type='image' src='http://banner.codencheres.com/images/aduje.png' style='cursor:pointer;' onclick='encherir(" + id + ");' onmouseover=\"this.src='http://banner.codencheres.com/images/aduje_over.png'\" onmouseout=\"this.src='http://banner.codencheres.com/images/aduje.png'\" />");
                }
                
               }
   }/*,
	error:function (xhr, ajaxOptions, thrownError){
		alert(xhr.status);
		alert(thrownError);
		alert('test'+retour);
	}   */
});

}


function ajaxspeed(id) {
	$.ajax({
	type: "GET",
    url: "timespeed.php?id=" + id,
    success: function(retour2){
	
	
	if (retour2 != 0) {                
		var recup2 = retour2;
		var elem2 = recup2.split('|');
		tmp2 = elem2[0];
		pseudo2 = elem2[1];
		mise2 = elem2[2];
		tp2 = elem2[3];
		pause2 = elem2[4];
		actif2 = elem2[5];
		relance2 = elem2[6];		
				
		if(actif2==0)
		{
			if(pause2==1){
				verifier(id);
			}else{
				relance2 = relance2 * 60;
				var chaine= "";
				jour2 = parseInt((relance2 / (60 * 60 * 24)), 10);
				heure2 = parseInt((relance2 / (60 * 60) - jour2 * 24), 10);
				minute2 = parseInt((relance2 / 60 - jour2 * 24 * 60 - heure2 * 60), 10);
				seconde2 = parseInt((relance2 - jour2 * 24 * 60 * 60 - heure2 * 60 * 60 - minute2 * 60), 10);
				
				if(jour2!=0){chaine=jour2+" j ";}
				if(heure2!=0){chaine=chaine+heure2+" h ";}
				if(minute2!=0){chaine=chaine+minute2+" m ";}
				if(seconde2!=0){chaine=chaine+seconde2+" s ";}
				$('#cpt'+id).html("<span style='color:green;'>START</span>: "+chaine);
				//decompte(relance2, id, pseudo2, mise2);
			}
		}else{
		
			if(pause2==1){
				verifier(id);
			}else{
				
				if( tp2 == 1 && pause2 ==0)
				{
					//j'affiche de nouveau chrono
					$('#cpt'+id).html("<span id=\"jours"+id+"\"></span> <span id=\"j"+id+"\" style=\"font-size: medium;\">j</span> <span id=\"heure"+id+"\" ></span> <span id=\"h"+id+"\" style=\"font-size: small;\">h</span> <span id=\"minute"+id+"\" ></span> <span id=\"m"+id+"\" style=\"font-size: x-small;\">m</span> <span id=\"seconde"+id+"\"></span> <span id=\"s"+id+"\" style=\"font-size: xx-small;\">s</span>");
					
				}
				decompte(tmp2, id, pseudo2, mise2);
			}
			if (tp == 1 && pause ==0) {			
				$('#bValid'+id).html("<input type='image' src='http://banner.codencheres.com/images/aduje.png' style='cursor:pointer;' onclick='encherir2(" + id + ");' onmouseover=\"this.src='http://banner.codencheres.com/images/aduje_over.png'\" onmouseout=\"this.src='http://banner.codencheres.com/images/aduje.png'\" />");
			}
		}
	}
	
	
	}
	})
}


function encherir(id2)
{

	$.ajax({
		type: "GET",
		url: "encherir.php?id=" + id2,
		success: function(retour){
		
		if (retour != "") {
		
			var recup = retour;
			var elem = recup.split('|');
			credit = elem[0];
			solde = elem[1];
			$('#Nbrcredit').html(credit);
			//$('#Nbeuro').html(solde);
			$('#bValid'+id2).html("<img src='http://banner.codencheres.com/images/aduje-ok.png' alt='ok' />");
			$('#minute'+id2).css('display','inline');
			$('#m'+id2).css('display','inline');			
		} else {
		   alert("Vous n'avez plus de crédits.");
		}

		}
	});


}


function encherir2(id2)
{

    

$.ajax({
    type: "GET",
    url: "encherir2.php?id=" + id2,
    success: function(retour){
	
    if (retour != "") {
	
		var recup = retour;
		var elem = recup.split('|');
		credit = elem[0];
		solde = elem[1];
        $('#Nbrcredit').html(credit);
		//$('#Nbeuro').html(solde);
		$('#bValid'+id2).html("<img src='http://banner.codencheres.com/images/aduje-ok.png' alt='ok' />");
		$('#cpt'+id2).html("<span id=\"jours"+id2+"\"></span> <span id=\"j"+id2+"\" style=\"font-size: medium;\">j</span> <span id=\"heure"+id2+"\" ></span> <span id=\"h"+id2+"\" style=\"font-size: small;\">h</span> <span id=\"minute"+id2+"\" ></span> <span id=\"m"+id2+"\" style=\"font-size: x-small;\">m</span> <span id=\"seconde"+id2+"\"></span> <span id=\"s"+id2+"\" style=\"font-size: xx-small;\">s</span>");
		ajaxspeed(id2);
    } else {
       alert("Vous n'avez plus de crédits.");
    }

    }
});


}

function total(ttc, val, taux) {
       montant = Math.round(ttc / taux * val);
       /*montant = Math.round(montant*100) / 100;  */
       $('#montantttc').html(montant);
}

function jsFormatNumber(number, decimals)
{
	number = Math.round(number * Math.pow(10, decimals));
	if (isNaN(number)) number = 0;
	number = String(number);
	while (number.length < 3) number = '0' + number;
	return number.substring(0, number.length - 2) + '.' + number.substring(number.length - 2, number.length);
}

function jsUpdatePaymentFee(value, t)
{
	$('#payment_fee').html($('#'+value).text());
	jsComputePayment(t);
}

function jsComputePayment(t)
{
        //var credits = parseFloat(parseInt($('Credits').value));
	var credits = $('#credits').val();

        var payment_fee = 0.50;

	var total_price = credits * t;
	var fee_amount = 0;
	var total_to_pay = total_price + fee_amount + payment_fee;

	$('#total_price').html(jsFormatNumber(total_price,  2));
	$('#payment_fee').html(jsFormatNumber(payment_fee,   2));
    
	$('#total_to_pay').html(jsFormatNumber(total_to_pay, 2));
}

