var inc = 0;
var clics = 0;
var panier = new Array();

for(liste in produits){
    produits[liste][2] = 0;
}
/************************************************/
function plusPanier(id,nombre){
	if(produits[id]){
		var newLine = false;
		if(produits[id][2] == 0)
		{
			panier[inc] = id;
			produits[id][3] = inc;
			inc++;
			newLine = true;
		}

		if(newLine==true){
			addPanier(id);
		}
		produits[id][2] += parseInt(nombre);
		if(finalise){
			$('#prod'+id+' .commande_quantite').html("x"+produits[id][2]);
		}

		if(fini==true)
		{
			$.post("../ajax_panier.php", {
				prod_id : id,
				prod_total : produits[id][2]
			});
		
			var numProduit = 0;
			var numProduitOK = 0;
			$('#item li').each(function(){
				if(numProduitOK<=0){
					numProduit++;
				}
				if(id == $(this).attr('id').replace('item','')){
					numProduitOK = 1;
				}
			});
			if(scrollPanier>((numProduit-1)*14) || scrollPanier<((numProduit-4)*14)){
				if(scrollPanier>((numProduit-1)*14)){
					scrollPanier = (numProduit-1)*14;
					$('#item').scrollTo(scrollPanier+'px',200);
				}else{
					scrollPanier = (numProduit-4)*14;
					$('#item').scrollTo(scrollPanier+'px',200);
				}
				actualiseScrollPanier(0);
			}
			
			$('#item'+id).animate({
				'color':'#7aa631','backgroundColor':'#f7f7f7'},200).animate({
				'color':'#666666','backgroundColor':'#ffffff'},200).animate({
				'color':'#7aa631','backgroundColor':'#f7f7f7'},200).animate({
				'color':'#666666','backgroundColor':'#ffffff'},200).animate({
				'color':'#7aa631','backgroundColor':'#f7f7f7'},200).animate({
				'color':'#666666','backgroundColor':'#ffffff'},200);
		}
		$('#item'+id+' .nombre_panier').html(produits[id][2]);
		prixPanier();
	}
	return false;
}
/************************************************/
function moinsPanier(id,nombre)
{
    if(produits[id][0]){
        if(produits[id][2]>0)
        {
            produits[id][2]-=nombre;
            if(produits[id][2]<=0)
            {
                produits[id][2] = 0;
                delete panier[produits[id][3]];
                delete produits[id][3];
				if(finalise){
					$('#prod'+id).slideUp(function(){
						$(this).remove();
					});
				}
                $('#item'+id).slideUp(function()
                {
					if(scrollPanier>0){
						scrollPanier-=14;
						$('#item').scrollTo(scrollPanier+'px',200);
					}
					actualiseScrollPanier(14);
					
					if($(this).hasClass('last') && $('#item li').length>=1){
						$(this).remove();
						$('#item li').each(function(){
							$('#item li.last').removeClass('last');
							$(this).addClass('last');
						});
					}else{
						$(this).remove();
						if(finalise){
							redirectionVide();
						}
					}
                });
            }
        }
        $('#item'+id+' .nombre_panier').html(produits[id][2]);
		if(finalise){
			$('#prod'+id+' .commande_quantite').html("x"+produits[id][2]);
		}
		prixPanier();

        $.post("../ajax_panier.php", {
            prod_id: id,
            prod_total : produits[id][2]
        });
    }
    return false;
}
/************************************************/
function redirectionVide(){
	// chargement pas disant que le panier est vide
}
/************************************************/
function addPanier(id)
{
	$('#item .last').removeClass('last');
	var titreProduit = produits[id][0];
	if(titreProduit.length>=23){
		if(titreProduit.charAt(19)==' '){
			titreProduit = titreProduit.substr(0,19)+'...';
		}else{
			titreProduit = titreProduit.substr(0,20)+'...';
		}
	}
	$('#item').append('<li id="item'+id+'" class="last"><a href="#" class="delete_panier">x</a><a href="#" class="moins_panier">-</a><span class="nombre_panier">'+produits[id][2]+'</span><a href="#" class="plus_panier">+</a><span class="prix_panier">'+produits[id][1]+'€</span>'+titreProduit+'</li>');

	if(fini==true)
	{
		$('#item'+id).hide();
		$('#item'+id).slideDown();

		if(!finalise){
			$('#valider').slideDown();
		}
		// $('#prixPanier').slideDown();
		$('#videPanier').slideUp();
	}

	$('#item'+id+' .plus_panier').click(function()
	{
		var id = $(this).parent('li').attr('id').replace('item','');
		plusPanier(id,1);
		return false;
	})

	$('#item'+id+' .moins_panier').click(function()
	{
		var id = $(this).parent('li').attr('id').replace('item','');
		moinsPanier(id,1);
		return false;
	})

	$('#item'+id+' .delete_panier').click(function()
	{
		var id = $(this).parent('li').attr('id').replace('item','');
		moinsPanier(id,9999);
		return false;
	});


	var prod_list = '';
	$('#item li').each(function(){
		if(prod_list!=''){
			prod_list+=';';
		}
		prod_list+=$(this).attr('id').replace('item','');
	});

	if(fini==true){
		$.post("../ajax_panier.php", {
			'prod_list' : prod_list
		});
	}
	
}
/************************************************/

var tva = 0;
function prixPanier()
{
    var prix = 0;
	var ma_commande = "";
    $('#item li').each(function(){
		prix += produits[$(this).attr('id').replace('item','')][1]*produits[$(this).attr('id').replace('item','')][2];
		ma_commande += $(this).attr('id').replace('item','')+','+produits[$(this).attr('id').replace('item','')][2]+";";
    });
	$('#prixPanier span').html(prix+' &euro;');
	if(finalise){
		$('#commande_htva').html(prix+' &euro;');
		var prixTva = Math.floor(prix*tva*100)/100;
		$('#commande_tva').html(prixTva+' &euro;');
		$('#commande_total').html(Math.floor((prix+prixTva)*100)/100+' &euro;');
		$('#form_commande').val(ma_commande);
		if(prix==0){
			redirectionVide();
		}
	}
	if(fini==true){
		if(prix>0){
			if(!finalise){
				$('.panier_valid').slideDown();
			}
			// $('#prixPanier').slideDown();
			$('#videPanier').slideUp();
		}else{
			if(!finalise){
				$('.panier_valid').slideUp();
			}
			// $('#prixPanier').slideUp();
			$('#videPanier').slideDown();
		}
	}else{
		if(prix>0){
			if(!finalise){
				$('.panier_valid').show();
			}else{
				$('.panier_valid').hide();
			}
			// $('#prixPanier').show();
			$('#videPanier').hide();
		}else{
			if(!finalise){
				$('.panier_valid').hide();
			}
			// $('#prixPanier').hide();
			$('#videPanier').show();
		}
	}
}
/************************************************/
var scrollPanier = 0;
function actualiseScrollPanier(decalageBas){
	if(scrollPanier<=0){
		$('.panier_before').fadeTo('fast',0.3);
		$('.panier_before').css({'cursor':'default'});
	}else{
		$('.panier_before').fadeTo('fast',1);
		$('.panier_before').css({'cursor':'pointer'});
	}
	if(scrollPanier>=(($('#item li').size()*14)-56-decalageBas)){
		$('.panier_after').fadeTo('fast',0.3);
		$('.panier_after').css({'cursor':'default'});
	}else{
		$('.panier_after').fadeTo('fast',1);
		$('.panier_after').css({'cursor':'pointer'});
	}
}

$(document).ready(function(){
	$('.panier_after').fadeTo('fast',0.5);
	$('#panier').disableSelection();
	$('.panier_after').click(function(){
		scrollPanier+=28;
		if(scrollPanier>=(($('#item li').size()*14)-56)){
			if(($('#item li').size()*14)-56>=0){
				scrollPanier=($('#item li').size()*14)-56;
			}else{
				scrollPanier = 0;
			}
		}
		$('#item').scrollTo(scrollPanier+'px',200);
		actualiseScrollPanier(0);
	});
	$('.panier_before').click(function(){
		scrollPanier-=28;
		if(scrollPanier<0){
			scrollPanier=0;
		}
		$('#item').scrollTo(scrollPanier+'px',200);
		actualiseScrollPanier(0);
	});
	
	$('#item').scrollTo('0px',100);
	actualiseScrollPanier(0);
	
	
	
	/* --- connect --- */
	$('#connect .submit').click(submitConnect);
	
	function submitConnect(){
		var user = "";
		var pass = "";
		if($('#connect_user').val()!=""){
			user = $('#connect_user').val();
		}else{
			$('#connect_user').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#connect_pass').val()!=""){
			pass = $('#connect_pass').val();
		}else{
			$('#connect_pass').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		$('#connect .error').hide();
		if(user!="" && pass!=""){
			$.post("../ajax_panier.php",{
				'connect_user': user,
				'connect_pass': pass
			},function(data){
				if(data==1){
					// ok
					window.location.href = url+"/"+langue+"/accueil.html";
				}else{
					// no
					$('#connect .error').show();
				}
			});
		}
	};
	$('#connect_pass').keypress(function(event){
		if (event.keyCode == 38) $('#connect_user').focus();
		if (event.keyCode == 13) submitConnect();
	});
	$('#connect_user').keypress(function(event){
		if (event.keyCode == 13 || event.keyCode == 40) $('#connect_pass').focus();
	});
	
	
	/* --- register --- */
	$('#register .submit').click(submitRegister);
	
	function submitRegister(){
		var user = "";
		var pass = "";
		var name = "";
		var firstname = "";
		var tel = "";
		
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		
		if($('#register_user').val()!=""){
			user = $('#register_user').val();
		}else{
			$('#register_user').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#register_pass').val()!=""){
			pass = $('#register_pass').val();
		}else{
			$('#register_pass').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#register_name').val()!=""){
			name = $('#register_name').val();
		}else{
			$('#register_name').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#register_firstname').val()!=""){
			firstname = $('#register_firstname').val();
		}else{
			$('#register_firstname').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#register_tel').val()!=""){
			tel = $('#register_tel').val();
		}else{
			$('#register_tel').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}

		$('#register .error').hide();
		if(!pattern.test($('#register_user').val()) && user!=""){
			$('#register .error').show();
			user="";
			$('#register_user').animate({'color':'#bc4141','backgroundColor':'#e7e7e7'},200).animate({'color':'#666666','backgroundColor':'#ffffff'},200).animate(
										{'color':'#bc4141','backgroundColor':'#e7e7e7'},200).animate({'color':'#666666','backgroundColor':'#ffffff'},200);
		}
		
		if(user!="" && pass!="" && name!="" && firstname!="" && tel!=""){
			$.post("../ajax_panier.php",{
				'register_name': name,
				'register_firstname': firstname,
				'register_tel': tel,
				'register_user': user,
				'register_pass': pass
			},function(data){
				window.location.href = url+"/"+langue+"/accueil.html";
			});
		}
	};
	$('#register_name').keypress(function(event){
		if (event.keyCode == 13 || event.keyCode == 40) $('#register_firstname').focus();
	});
	$('#register_firstname').keypress(function(event){
		if (event.keyCode == 38) $('#register_name').focus();
		if (event.keyCode == 13 || event.keyCode == 40) $('#register_user').focus();
	});
	$('#register_user').keypress(function(event){
		if (event.keyCode == 38) $('#register_firstname').focus();
		if (event.keyCode == 13 || event.keyCode == 40) $('#register_pass').focus();
	});
	$('#register_pass').keypress(function(event){
		if (event.keyCode == 38) $('#register_user').focus();
		if (event.keyCode == 13 || event.keyCode == 40) $('#register_tel').focus();
	});
	$('#register_tel').keypress(function(event){
		if (event.keyCode == 38) $('#register_pass').focus();
		if (event.keyCode == 13) submitRegister();
	});
	
	
	
	
	
	
	$('#profil .submit').click(submitProfil);
	
	function submitProfil(){
		var user = "";
		var pass = "";
		var name = "";
		var firstname = "";
		var tel = "";
		
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		
		if($('#profil_user').val()!=""){
			user = $('#profil_user').val();
		}else{
			$('#profil_user').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#profil_pass').val()!=""){
			pass = $('#profil_pass').val();
		}else{
			$('#profil_pass').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#profil_name').val()!=""){
			name = $('#profil_name').val();
		}else{
			$('#profil_name').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#profil_firstname').val()!=""){
			firstname = $('#profil_firstname').val();
		}else{
			$('#profil_firstname').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}
		if($('#profil_tel').val()!=""){
			tel = $('#profil_tel').val();
		}else{
			$('#profil_tel').animate({'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200).animate(
										{'backgroundColor':'#e7e7e7'},200).animate({'backgroundColor':'#ffffff'},200);
		}

		$('#profil .error').hide();
		if(!pattern.test($('#profil_user').val()) && user!=""){
			$('#profil .error2').show();
			user="";
			$('#profil_user').animate({'color':'#bc4141','backgroundColor':'#e7e7e7'},200).animate({'color':'#666666','backgroundColor':'#ffffff'},200).animate(
										{'color':'#bc4141','backgroundColor':'#e7e7e7'},200).animate({'color':'#666666','backgroundColor':'#ffffff'},200);
		}
		
		if(user!="" && pass!="" && name!="" && firstname!="" && tel!=""){
			$.post("../ajax_panier.php",{
				'profil_id': user_id,
				'profil_name': name,
				'profil_firstname': firstname,
				'profil_tel': tel,
				'profil_user': user,
				'profil_pass': pass
			},function(data){
				window.location.href = url+"/"+langue+"/accueil.html";
			});
		}
	};
	$('#profil_user').keypress(function(event){
		if (event.keyCode == 13 || event.keyCode == 40) $('#profil_pass').focus();
	});
	$('#profil_pass').keypress(function(event){
		if (event.keyCode == 38) $('#profil_user').focus();
		if (event.keyCode == 13 || event.keyCode == 40) $('#profil_name').focus();
	});
	$('#profil_name').keypress(function(event){
		if (event.keyCode == 38) $('#profil_pass').focus();
		if (event.keyCode == 13 || event.keyCode == 40) $('#profil_firstname').focus();
	});
	$('#profil_firstname').keypress(function(event){
		if (event.keyCode == 38) $('#profil_name').focus();
		if (event.keyCode == 13 || event.keyCode == 40) $('#profil_tel').focus();
	});
	$('#profil_tel').keypress(function(event){
		if (event.keyCode == 38) $('#profil_firstname').focus();
		if (event.keyCode == 13) submitProfil();
	});
	
	$('#deco_btn').click(function(){
		$.post("../ajax_panier.php",{'deco': 1},function(data){
			window.location.href = url+"/"+langue+"/accueil.html";
		});
	});
	
	if($("#datePicker").length>=1){
		var dateHeure = 12;
		var dateRetrait = "";
		$("#datePicker").datepicker({
			monthNames: dateMois,
			dayNamesMin: dateJour,
			minDate: +1,
			firstDay: 1,
			beforeShowDay: noWeekendsOrHolidays,
			onSelect: function(dateText, inst) {
				var dateChoisie = $("#datePicker").datepicker("getDate");
				if(dateChoisie.getDay()==6){
					$('#timePicker15').button("disable");
					$('#timePicker16').button("disable");
					$('#timePicker17').button("disable");
					if($("input[name='radio']:checked").val()>=15){
						dateHeure = 14;
						$('#timePicker').button("destroy");
						$('#timePicker14').attr('checked', true);
						$('#timePicker').buttonset();
					}
				}else{
					$('#timePicker15').button("enable");
					$('#timePicker16').button("enable");
					$('#timePicker17').button("enable");
				}
				actualiseDate();
			}
		});
		$("#timePicker").buttonset();
		$("#timePicker input").change(function(){
			dateHeure = $("input[name='radio']:checked").val();
			actualiseDate();
		});
	}
	var disabledDays = [
		"1-1",
		"25-12"];

	function actualiseDate(){
		var dateChoisie = $("#datePicker").datepicker("getDate");
		dateRetrait = dateChoisie.getTime() + (dateHeure*3600000);

		var nouvelleDate = new Date();
		nouvelleDate.setTime(dateRetrait);
		
		$('#form_date').html(nouvelleDate.getDate()+' '+dateMois[nouvelleDate.getMonth()]+' '+nouvelleDate.getFullYear()+dateTo+nouvelleDate.getHours()+dateH);
		$('#dateDeReception').val(dateRetrait);
	}
	function nationalDays(date) {
	  var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
	  for (i = 0; i < 3; i++) {
		if( disabledDays[i] == ((m+1) + '-' + d + '-' + y) || new Date() > date) {
		  return [false];
		}
	  }
	  return [true];
	}
	function noWeekendsOrHolidays(date) {
		actualiseDate();
		// alert(date.getDay());
		if(date.getDay()<=0){
			return [false];
		}else{
			return [true];
		}
	}

	
	$('#submit_commande').click(function(){
		var commande = "";
		$('.commande_liste').each(function(){
			if($(this).attr('id')!=""){
				if(commande!=''){
					commande+='|';
				}
				commande += $(this).attr('id').replace('prod','');
				commande += '_'+$(this).children('ul').children('.commande_quantite').html().replace('x','');
			}
		});
		
		var dateCommande = $('#dateDeReception').val();
		var dateCommandeTxt = $('#form_date').html();
		var remarques = $('#form_remarque').val();
		if(commande==""){
			alert('Votre panier est vide !');
		}else{
			if(dateCommandeTxt==""){
				if(langue=="fr"){
					alert('Choisissez une date de retrait pour valider la commande');
				}else if(langue=="en"){
					alert('Choose a date for withdrawal to validate the order');
				}else{
					alert('Kies een datum voor de terugtrekking tot de orde te valideren');
				}
			}
		}
		
		if(dateCommandeTxt!="" && commande!=""){
			$.post("../ajax_panier.php",{
				'langue': langue,
				'commande': commande,
				'dateCommandeTxt': dateCommandeTxt,
				'dateCommande': dateCommande,
				'remarques': remarques
			},function(data){
				if(langue=="fr"){
					alert("Votre commande a bien été prise en compte.\nMerci de votre confiance");
				}else if(langue=="en"){
					alert("Your order has been taken into account.\nThank you for your trust");
				}else{
					alert("Uw bestelling is rekening gehouden.\nDank u voor uw vertrouwen");
				}
				window.location.href = url+"/"+langue+"/accueil.html";
			});
		}
	});
});
