
$(document).ready(function(){

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

	$('body.verouiller a[href="#"]').click(function(){
		var position = $('ul#homeRotator').offset();

		obj_a = $('<a></a>')
			.attr('href','#')
			.addClass('enregistrer')
			.css('display','block')
			.css('position', 'absolute')
			.css('top', position.top)
			.css('left', position.left)
			.css('z-index', 102)
			.append($('<img>').attr('src','verouiller.jpg')).click(function(){
				fermer();
				jbEnregistrement();
			});
		obj_fond = $('<div></div>')
			.attr('id','popupBG')
			.css('display','block').click(function(){fermer();});

		$('body').append(obj_fond);
		$('body').append(obj_a);

		function fermer(){
			obj_a.remove();
			obj_fond.remove();
		}

		return false;
	});
	$('a.creerCompte').click(function(e){
		jbEnregistrement();
		e.stopPropagation();
		return false;
	});

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

	$('#identification form').submit(function(e){
		$.getJSON(souche+'/index/connexion/', {
			email: $('input[name="email"]', this).val(), 
			pwd: $('input[name="pwd"]', this).val()
		}, function(data){
			if (data.identified == true){
				document.location.href = souche;
			}else{
				jbconfirm({
					msg: data.error,
					type: 'alert',
					oui: 'Ok'
				});
			}
		});

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

	$('#menuProduit ul li a').click(function(e){
		$('#menuProduit ul li a').each(function(){
			if ($(this).hasClass('activ')){
				$(this).parent().find('ol').slideToggle('slow', function(){
					if ($(this).css('display') == 'none')
						$(this).parent().find('a:eq(0)').removeClass('activ');
				});
			}
		});

		var ol = $(this).parent().find('ol');
		$(this).addClass('activ');

		if ($('li:eq(0)', ol).get(0)){
			var ol = $(this).parent().find('ol');
			ol.slideToggle('slow', function(){
				if ($(this).css('display') == 'none')
					$(this).parent().find('a:eq(0)').removeClass('activ');
			});
			e.stopPropagation();
			return false;
		}
	});

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

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

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

	$('#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)+'&nbsp;&euro;<sup>HT</sup>');
		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();

	$('#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
	var panier = new AjaxPanier();
	panier.getPanier();

	// Bouton d'ajout au panier.
	$('a.commander[name!="nopanier"]').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 tmp = o.attr('href');
		var o = $(this).parent().parent();
		var id = tmp.substr(tmp.lastIndexOf('#')+1);
		jbconfirm({
			msg: 'Voulez-vous vraiment supprimer ce produit de votre panier ?', 
			oui: 'Oui', 
			non: 'Non', 
			ouiCallback: function(){
				if (panier.remove(id) != false){
					o.remove();
				}
			}
		});
		e.stopPropagation();
		return false;
	});

	// Bouton change quantite.
	$('a.quantity').click(function(e){
		var p = $(this).parent();
		var q = parseInt($('span', p).html());

		var id = p.attr('id');
		id = parseInt(id.substr(id.lastIndexOf('_')+1));

		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);

		if (r.alert.length < 1){
			$('span', p).html(r.qt);
			$('input', p).val(r.qt);
			$('#prix_'+id).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'});
		}
	});


	$('select.updateAdresse').change(function(){
		var id = $(this).val();
		//if (id == '')
		//	return false;
		var atr = 'fac';
		if ($(this).hasClass('liv')){
			atr = 'liv';
		}
		$('input[name="'+atr+'_nom"]').val(adresse[id]['nom']);
		$('input[name="'+atr+'_prenom"]').val(adresse[id]['prenom']);
		$('input[name="'+atr+'_societe"]').val(adresse[id]['societe']);
		$('input[name="'+atr+'_adresse"]').val(adresse[id]['adresse']);
		$('input[name="'+atr+'_cp"]').val(adresse[id]['cp']);
		$('input[name="'+atr+'_ville"]').val(adresse[id]['ville']);
		$('input[name="'+atr+'_pays"]').val(adresse[id]['pays']);
		$('input[name="'+atr+'_tel"]').val(adresse[id]['tel']);
		$('input[name="'+atr+'_mail"]').val(adresse[id]['mail']);
		$('input[name="'+atr+'_mail_conf"]').val(adresse[id]['mail']);
	});

});

