
var panier;

$(document).ready(function(){

	$('#top div p').glisse();

	$('a.blanck').attr('target','_blanck');
	$('a.icimail').html("npphrvy\100punhssntr-qvfpbhag\056se".replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);
	}));

	$('.homePromoDemi .middel ul:eq(0)').jbcarousel({
		'timeout': 4,
		'liste': '.homePromoDemi .middel ul.repaire',
		'width': 235
	});

	$('ul#homeRotator').jbRotator({ tempo: 5000 });

	$('ul.jbonglet').jbonglet({
		show: '',
		effect: '',
		minCarac: 45
	});

	$('.radia').click(function(){
		$('ul.jbonglet').ScrollTo(800).find('a[href="#o4"]').click();
		return false;
	});

	$('#formContact').submit(function(e){
		var result = true;
		$('label[for="nom"]', this).removeClass('error');
		if ($('input[name="nom"]', this).val() == ''){
			$('label[for="nom"]', this).addClass('error');
			result = false;
		}
		$('label[for="message"]', this).removeClass('error');
		if ($('textarea[name="message"]', this).val() == ''){
			$('label[for="message"]', this).addClass('error');
			result = false;
		}
		if (result == false){
			e.stopPropagation();
			return false;
		}
	});

	function updatePrix(o){
		var i = $(o).attr('id').substr(2);
		var q = parseInt($(o).val());
		var p = parseFloat($('input#qp'+i).val());
		if (q < 1){
			q = 1;
		}

		$('span.p'+i).html(p*q);
		var d = $('a.devis'+i).attr('href');
		$('a.devis'+i).attr('href', d+'&q='+q);
	}

	$('input[name="qt"]').keyup(function(e){
		//if ((e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode <= 8){
			updatePrix(this);
		//}
	});

	$('input:eq(0)').focus();

	$('a.devis').click(function(e){
		if ($('div.mcarre').length > 0){
			var qt = $('input[name="qt"]');
			if (parseInt(qt.val()) < 2){
				qt.css('background-color','yellow');
				qt.css('border', '1px solid red');
				e.stopPropagation();
				return false;
			}
		}
	});

	$('#formDevis').submit(function(e){
		var result = true;

		$('input[name="fac_nom"]', this).parent().removeClass('error');
		if ($('input[name="fac_nom"]', this).val() == ''){
			$('input[name="fac_nom"]', this).parent().addClass('error');
			result = false;
		}
		$('input[name="fac_prenom"]', this).parent().removeClass('error');
		if ($('input[name="fac_prenom"]', this).val() == ''){
			$('input[name="fac_prenom"]', this).parent().addClass('error');
			result = false;
		}
		$('input[name="fac_adresse"]', this).parent().removeClass('error');
		if ($('input[name="fac_adresse"]', this).val() == ''){
			$('input[name="fac_adresse"]', this).parent().addClass('error');
			result = false;
		}
		$('input[name="fac_cp"]', this).parent().removeClass('error');
		if ($('input[name="fac_cp"]', this).val() == ''){
			$('input[name="fac_cp"]', this).parent().addClass('error');
			result = false;
		}
		$('input[name="fac_ville"]', this).parent().removeClass('error');
		if ($('input[name="fac_ville"]', this).val() == ''){
			$('input[name="fac_ville"]', this).parent().addClass('error');
			result = false;
		}
		$('input[name="fac_mail"]', this).parent().removeClass('error');
		if ($('input[name="fac_mail"]', this).val() == ''){
			$('input[name="fac_mail"]', this).parent().addClass('error');
			result = false;
		}
		$('input[name="fac_mail_conf"]', this).parent().removeClass('error');
		if ($('input[name="fac_mail_conf"]', this).val() == ''){
			$('input[name="fac_mail_conf"]', this).parent().addClass('error');
			result = false;
		}else{
			if ($('input[name="fac_mail"]', this).val() != $('input[name="fac_mail_conf"]', this).val()){
				$('input[name="fac_mail_conf"]', this).parent().addClass('error');
				$('input[name="fac_mail"]', this).parent().addClass('error');
				result = false;
			}
		}

		if (result == false){
			e.stopPropagation();
			return false;
		}
		$('input[type="submit"]').attr('disabled','disabled');
	});

	$('#formCalpuissance').submit(function(e){
		var corespondance = new Array();
		corespondance[0] = new Array();
		corespondance[0][2] = 0;
		corespondance[0][4] = 0;
		corespondance[0][5] = 0;
		corespondance[0][7] = 0;
		corespondance[0][8] = 0;
		corespondance[0][9] = 0;
		corespondance[0][10] = 0;
		corespondance[0][12] = 0;
		corespondance[0][15] = 0;
		corespondance[200] = new Array();
		corespondance[200][2] = 2;
		corespondance[200][4] = 1;
		corespondance[200][5] = 1;
		corespondance[200][7] = 1;
		corespondance[200][8] = 1;
		corespondance[200][9] = 1;
		corespondance[200][10] = 1;
		corespondance[200][12] = 1;
		corespondance[200][15] = 0;
		corespondance[400] = new Array();
		corespondance[400][2] = 4;
		corespondance[400][4] = 2;
		corespondance[400][5] = 2;
		corespondance[400][7] = 2;
		corespondance[400][8] = 3;
		corespondance[400][9] = 2;
		corespondance[400][10] = 3;
		corespondance[400][12] = 3;
		corespondance[400][15] = 4;
		corespondance[600] = new Array();
		corespondance[600][2] = 6;
		corespondance[600][4] = 3;
		corespondance[600][5] = 3;
		corespondance[600][7] = 4;
		corespondance[600][8] = 5;
		corespondance[600][9] = 3;
		corespondance[600][10] = 4;
		corespondance[600][12] = 5;
		corespondance[600][15] = 6;
		corespondance[800] = new Array();
		corespondance[800][2] = 8;
		corespondance[800][4] = 4;
		corespondance[800][5] = 4;
		corespondance[800][7] = 6;
		corespondance[800][8] = 7;
		corespondance[800][9] = 4;
		corespondance[800][10] = 7;
		corespondance[800][12] = 7;
		corespondance[800][15] = 8;
		corespondance[1000] = new Array();
		corespondance[1000][2] = 10;
		corespondance[1000][4] = 5;
		corespondance[1000][5] = 5;
		corespondance[1000][7] = 7;
		corespondance[1000][8] = 9;
		corespondance[1000][9] = 0;
		corespondance[1000][10] = 9;
		corespondance[1000][12] = 9;
		corespondance[1000][15] = 9;
		corespondance[1200] = new Array();
		corespondance[1200][2] = 12;
		corespondance[1200][4] = 6;
		corespondance[1200][5] = 6;
		corespondance[1200][7] = 8;
		corespondance[1200][8] = 11;
		corespondance[1200][9] = 0;
		corespondance[1200][10] = 11;
		corespondance[1200][12] = 11;
		corespondance[1200][15] = 10;
		corespondance[1400] = new Array();
		corespondance[1400][2] = 14;
		corespondance[1400][4] = 0;
		corespondance[1400][5] = 7;
		corespondance[1400][7] = 0;
		corespondance[1400][8] = 13;
		corespondance[1400][9] = 0;
		corespondance[1400][10] = 13;
		corespondance[1400][12] = 12;
		corespondance[1400][15] = 0;
		corespondance[1600] = new Array();
		corespondance[1600][2] = 16;
		corespondance[1600][4] = 0;
		corespondance[1600][5] = 8;
		corespondance[1600][7] = 0;
		corespondance[1600][8] = 15;
		corespondance[1600][9] = 0;
		corespondance[1600][10] = 14;
		corespondance[1600][12] = 0;
		corespondance[1600][15] = 0;
		corespondance[1800] = new Array();
		corespondance[1800][2] = 18;
		corespondance[1800][4] = 0;
		corespondance[1800][5] = 9;
		corespondance[1800][7] = 0;
		corespondance[1800][8] = 17;
		corespondance[1800][9] = 0;
		corespondance[1800][10] = 15;
		corespondance[1800][12] = 0;
		corespondance[1800][15] = 0;
		corespondance[2000] = new Array();
		corespondance[2000][2] = 0;
		corespondance[2000][4] = 0;
		corespondance[2000][5] = 10;
		corespondance[2000][7] = 0;
		corespondance[2000][8] = 19;
		corespondance[2000][9] = 0;
		corespondance[2000][10] = 19;
		corespondance[2000][12] = 0;
		corespondance[2000][15] = 0;
		var departement = parseInt($('select[name="departement"]').val());
		var altitude = parseInt($('select[name="altitude"]').val());
		var temperature = parseInt($('select[name="temperature"]').val());
		var isolation = parseFloat($('select[name="isolation"]').val());
		var surface = parseInt($('input[name="surface"]').val());
		var hauteur = parseFloat($('select[name="hauteur"]').val());
		var kw = ((departement+corespondance[altitude][departement]+temperature)*isolation*surface*hauteur)/1000;
		$('input#kw').val(Math.round(kw*100)/100);
		$('#calculpuissance form p.result').show();
		e.stopPropagation();
		return false;
	});

	// ----------------------------------------------------------------------------
	// PANIER
	panier = new AjaxPanier();
	panier.getPanier();

	// Bouton d'ajout au panier.
	$('a.commander').click(function(e){
		var href = $(this).attr('href');
		var href = href.substr(href.lastIndexOf('#')+1);
		var q = $('input#qt'+href);
		if (q.length){
			q = parseInt($('input#qt'+href).val());
			panier.add(href, q);
		}else{
			panier.add(href, 1);
		}
		e.stopPropagation();
		return false;
	});

	// Bouton suppression.
	$('a.remove').click(function(e){
		var o = $(this);
		var href = o.attr('href');
		var subRef = 0;
		id = href.substr(href.lastIndexOf('#')+1);
		if (id.indexOf('_') > -1){
			subRef = parseInt(id.substr(id.indexOf('_')+1));
		}
		jbconfirm({
			msg: 'Voulez-vous vraiment supprimer ce produit de votre panier ?', 
			oui: 'Oui', 
			non: 'Non', 
			ouiCallback: function(){
				if (panier.remove(id, subRef) == true){
					o.parent().parent().remove();
				}
			}
		});
		e.stopPropagation();
		return false;
	});

	// Bouton change quantite.
	$('a.quantity').click(function(e){
		var p = $(this).parent();
		var q = parseInt($('span', p).html());
		var ref = p.attr('id');
		var id = 0;
		var subRef = 0;
		ref = ref.substr(ref.indexOf('_')+1);
		if (ref.indexOf('_') > -1){
			subRef = parseInt(ref.substr(ref.indexOf('_')+1));
			id = parseInt(ref.substring(0, ref.indexOf('_')));
		}else{
			id = parseInt(ref);
		}

		if ($(this).hasClass('moins') == true){
			if (q < 2){
				e.stopPropagation();
				return false;
			}
			q = q-1;
		}
		if ($(this).hasClass('plus') == true){
			q = q+1;
		}

		var r = panier.updateQuantity(id, q, subRef);

		if (r.alert.length < 1){
			$('span', p).html(r.qt);
			$('input', p).val(r.qt);
			$('#prix_'+ref).html(r.prix);
			if (r.port == 0)
				$('.port').html('Gratuit');
			else
				$('.port').html(r.port);
			$('.total').html(r.total);
		}else{
			alert(r.alert);
		}

		e.stopPropagation();
		return false;
	});

	// Masque le formulaire COORDONNEES DE LIVRAISON.
	if ($('input[name="coordonnees_livraison"][checked]').val() == '1'){
		$('input[name="coordonnees_livraison"][checked]').parent().parent().find('div:eq(0)').css({display: 'none'});
	}

	$('input[name="coordonnees_livraison"]').click(function(){
		if ($(this).val() == 1){
			$(this).parent().parent().find('div:eq(0)').css({display: 'none'});
		}
		if ($(this).val() == 2){
			$(this).parent().parent().find('div:eq(0)').css({display: 'block'});
		}
	});
});

