$(function(){
	var root = $('base').attr('href');
	
	// ANIMATION DE MENU
    $('li.rubrique').mouseover(function(){$('ul', this).show()});
    $('li.rubrique').mouseout(function(){$('ul', this).hide()});
	
	// FENTRE DE CONNECTION
	var connect;
    if ($('.connect').length > 0) {
		connect = $('<div>', {
			title: $('.connect:first').attr('title'),
			id: 'connect'
		}).load(root + 'account/start.html', function(){
			$(this).appendTo('body');

			$(this).dialog({
				autoOpen: false,
				draggable: false,
				resizable: false,
				width: 'auto'
			});
			
			$('.connect-toggle').click(function(){
				$('.connect-bloc').slideToggle(500);
				return false;
			});
			
			$('.connect').click(function(){
				connect.dialog("open");
				return false
			});
			
			$('.lostpwd').click(function(){
				bloc = $(this).parents('.connect-bloc-content:first');
				email = $('[name=email]', bloc);
				$.post(root + "account/lostpwd.html", {email : email.val()}, function(data){
					if (data.status == "success") bloc.slideUp(250, function(){$(this).html(data.html).slideDown(250)});
				}, 'json');
			});
		});
	}
	
	var connect_presse;
    if ($('.connect-presse').length > 0) {
		connect_presse = $('<div>', {
			title: $('.connect-presse:first').attr('title'),
			id: 'connect-presse'
		}).load(root + 'account/presse.html', function(){
			$(this).appendTo('body');

			$(this).dialog({
				autoOpen: false,
				draggable: false,
				resizable: false,
				width: 'auto'
			});
			
			$('.connect-presse').click(function(){
				connect_presse.dialog("open");
				return false
			});

		});
	}
	
	var lignesupprimee = $('<div>', {
		id : 'lignesupprimee'
	}).load(root + 'shop/lignesupprimee.html', function(){
		$(this).appendTo('body');

		$(this).dialog({
			autoOpen: false,
			draggable: false,
			resizable: false,
			width: 'auto'
		});

		$('.lignesupprimee-non').click(function(){
			lignesupprimee.dialog("close");
			return false;
		});

		$('.lignesupprimee-oui').click(function(){
			lignesupprimee.dialog("close");
			return false;
		});
	})
	
	// AJOUT DE PRODUIT
	var ajouter = function(input, id_reference, quantite, callback){
		if (quantite > 0) {
			$.post(
				'shop/ajouter.html',
				{
					reference : id_reference,
					quantite : quantite
				},
				callback,
				'json'
			);
		}
		else {
			$('.lignesupprimee-oui').unbind('click').click(function(){
				lignesupprimee.dialog("close");
				$.post(
					'shop/ajouter.html',
					{
						reference : id_reference,
						quantite : quantite
					},
					callback,
					'json'
				);
				return false;
			});
			
			$('.lignesupprimee-non').unbind('click').click(function(){
				lignesupprimee.dialog("close");
				input.val(1);
				input.keyup();
				return false;
			});
		
			lignesupprimee.dialog('open');
		}
	}
	    
	// PAGE PRODUIT.	
	if ($('.ajouter').length > 0) {
		var produitajoute = $('<div>', {
			id: 'produitajoute'
		}).load(root + 'shop/produitajoute.html', function(){
			$(this).appendTo('body');

			$(this).dialog({
				autoOpen: false,
				draggable: false,
				resizable: false,
				width: 'auto'
			});

			$('.produitajoute-close').click(function(){
				produitajoute.dialog("close");
				return false;
			});
		});
		
		$('.ajouter').live('click', function(){
			var blocparent = $(this).parents('.produit-bloc').first();
			var id_reference = $(this).attr('href').substr(1);
			var quantite = $('.panier-ligne-quantite', blocparent);

			ajouter(quantite, id_reference, 1, function(data) {
				if (data.status == "success") {
					if (data.vide){
						$('.panier-lien').hide();
						$('.panier-vide').show();
					}
					else {
						if ($('.panier-lien').is(':hidden')) {
							$('.panier-lien').show();
							$('.panier-vide').hide();
						}
						
						$('.panier-ligne', blocparent).slideDown(500);
						$('.ajouter', blocparent).slideUp(500);
						
						$('.panier-total').text(data.total);
						
						$('.panier-ligne-quantite', blocparent).val(data.quantite);
						
						produitajoute.dialog("open");
					}
				}
			});

			return false;
		});
	}
	
	// PAGE PANIER.
	$('.panier-ligne-quantite').live('keyup', function(){
		var blocparent = $(this).parents('.produit-bloc:first');
		var parent = $(this).parents('.panier-ligne:first');
		var id_reference = $('.panier-ligne-reference', parent).val();
		var quantite = $(this);
		
		ajouter(quantite, id_reference, quantite.val(), function(data) {
			if (data.status == "success") {
				if (data.vide) {
					// redirection la page d'accueil de la boutique.
					if (data.redirect) document.location.href = data.redirect;
					else {
						$(parent).slideUp(500);
						$('.ajouter', blocparent).slideDown(500);
					}
				}
				else {
					// Si la quantité est à zero, on cache la ligne et si on est en page produit, on réaffiche le bouton 'ajouter au panier'
					if (data.quantite == 0) {
						$(parent).slideUp(500);
						$('.ajouter', blocparent).slideDown(500);
					}

					// Montre les liens pour aller voir le panier.
					if ($('.panier-lien').is(':hidden')) {
						$('.panier-lien').show();
						$('.panier-vide').hide();
					}

					// Page panier, on affiche le total de la ligne ...
					$('.panier-ligne-total', parent).text(data.ligne_total);
					// ... et le total du panier ...
					$('.panier-total').text(data.total);
					// ... et le total de la tva.
					$('.panier-total-tva').text(data.total_tva);

					// Calcul des frais de port.
					$('#adresse_livraison').change();
				}
			}
			
			quantite.val(data.quantite);
		});
	});
	
	$('.plus').live('click', function(){
		var elmt = $('.panier-ligne-quantite', $(this).parents('.panier-ligne:first'));
		elmt.val(parseInt(elmt.val()) + 1);
		elmt.keyup();
		return false;
	});
	
	$('.moins').live('click', function(){
		var elmt = $('.panier-ligne-quantite', $(this).parents('.panier-ligne:first'));
		elmt.val(parseInt(elmt.val()) - 1);
		elmt.keyup();
		return false;
	});
	
	$('.supprimer').live('click', function(){
		var elmt = $('.panier-ligne-quantite', $(this).parents('.panier-ligne:first'));
		elmt.val(0);
		elmt.keyup();
		return false;
	});
	
	$('#adresse_livraison').change(function(){
		var id = $(this).val();
		$.post('shop/calculport.html', {id : id}, function(data){
			if (data.status == "success") {
				$('.panier-port').text(data.port);
				$('.panier-total-port').text(data.total);
				$('.adresse_livraison-html').html(data.html);
			}
		}, 'json');
	});
	
	$('#adresse_livraison').change();
	
	$('#adresse_facturation').change(function(){
		var id = $(this).val();
		$.post('shop/calculport.html', {id : id}, function(data){
			if (data.status == "success") {
				$('.adresse_facturation-html').html(data.html);
                
                if (data.pays != 1) {
                    $('#mode_reg').val(1);
                    $('#mode_reg option:last').hide();
                }
                else
                    $('#mode_reg option:last').show();
            }
		}, 'json');
	});
	
	$('#adresse_facturation').change();
	
	$('#cgv').click(function(){
		if ($(this).is(':checked')) {
			$('.panier-passer-commande').fadeIn(100);
			$('.panier-condition').fadeOut(100);
		}
		else {
			$('.panier-passer-commande').fadeOut(100);
			$('.panier-condition').fadeIn(100);
		}
	})
});
