$(document).ready(function(){
	
	if( $('a.choix-tarif').length ){
		$('a.choix-tarif:first').addClass('courant');
		$('.bloc-tarif:first').addClass('courant');
		$('.bloc-tarif').not('.courant').hide();
	}
	
	//Choix du tarif
	$('a.choix-tarif').click(function(){
		var id = $(this).attr('rel');
		$('a.choix-tarif').removeClass('courant');
		$(this).addClass('courant');
		$('.bloc-tarif').hide().removeClass('courant');
		$('.bloc-tarif#'+id).addClass('courant').show();
		return false;
	});
	
	//Bouton ajouter au panier
	$('.ajouter-panier').click(function(){
		ajouter_panier();
		return false;
	});
	
	//Si on fait "Entrée" sur la quantité
	$('#form-boutique').submit(function(){
		if( ($('#quantite').attr('value') != '') && ($('#quantite').attr('value') >= 0) ){
			prix();
		}else{
			$('#quantite').attr('value', 1);
		}
		return false;
	});
	
	//Bouton supprimer du panier
	$('.supprimer-panier').live('click', function(){
		
		$.post('/boutique/panier/supprimer', {'rowid': $(this).attr('value')}, function(data){
			
			var retour = $.parseJSON(data);
			
			if( retour.erreur != 'false' ){
				$.get('/boutique/panier/actualiser', function(data){
					var panier = $.parseJSON(data);
					$('#nb-article').html(panier.nb_article);
					$('#caddie').html(panier.contenu);
					$('#line-'+retour.id).fadeOut(500);
					
					//Si panier vide
					if( panier.nb_article == 0 ){
						$('#liste-panier').html('<br /><br /><strong>Votre panier est vide.</strong><br /><br />');
						$('#total-commande').html($('#frais-port').text());
					}
				});
			}else{
				alert(retour.message);
			}
		});
		return false;
	});
	
	//Bouton afficher panier
	$('a#bt-caddie').click(function(){
		$('#caddie').slideToggle();
		return false;
	});
	
	//Listes déroulante references
	$('#liste-references select.criteres').live('change', function(){
		var select_id = $(this).attr('id');
		
		$.post('/catalogue/tidinox/ajax_liste_param/'+select_id, $('#form-criteres').formSubmit(), function(data){
			var retour = $.parseJSON(data);
			$.each(retour, function(key, value){
				$('#'+key).replaceWith(value);
			});
			
			$.ajax({
				type: 'post',
				async: false,
				url: '/catalogue/tidinox/ajax_filtre_reference',
				data: $('#form-criteres').formSubmit(),
				beforeSend: function(){
					$('#content-reference').html('<div class="align-center"><strong>Chargement en cours...<br /></strong></div>');
				},
				success: function(data){
					$('#content-reference').html(data);
				}
			});
		});
	});
	
	//Liste déroulante fiche produit
	$('#article select.criteres').live('change', function(){
		var select_id = $(this).attr('id');
		
		$.post('/catalogue/tidinox/ajax_liste_param/'+select_id, $('#form-boutique').formSubmit(), function(data){
			var retour = $.parseJSON(data);
			$.each(retour, function(key, value){
				$('#'+key).replaceWith(value);
			});
			prix();
			$.ajax({
				type: 'post',
				async: false,
				url: '/catalogue/tidinox/produits_associes',
				data: $('#form-boutique').formSubmit(),
				beforeSend: function(){
					$('#produits-associes').html('<div class="align-center"><strong>Chargement en cours...<br /></strong></div>');
				},
				success: function(data){
					$('#produits-associes').html(data);
				}
			});
		});
	});
	
	//Sélection quantité
	$('#article #quantite').keyup(function(){
		if( $('#quantite').attr('value') >= 0 ){
			prix();
		}else{
			$('#quantite').attr('value', 1);
		}
	});
	
	//Liste déroulante des pays
	$('select#pays').change(function(){
		$.post('/boutique/transporteurs/ajax_livraison', $('#form-transporteur').formSubmit(), function(data){
			var retour = $.parseJSON(data);
			
			$('#pays_id').val(retour.pays_id);
			$('#transporteur_id').val(retour.transporteur_id);
			$('#transporteurs').html(retour.transporteurs);
			$('#frais-port').html(retour.fraisport);
			$('#total-commande').html(retour.total);
			$('#franco').html(retour.franco);
		});
	});
	
	//Choix du transporteur
	if( $('input[name="transporteur_id"]').length > 1 ){
		$('input[name="transporteur_id"]').live('click', function(){
			$.post('/boutique/transporteurs/ajax_livraison', $('#form-transporteur').formSubmit(), function(data){
				var retour = $.parseJSON(data);
				
				$('#pays_id').val(retour.pays_id);
				$('#transporteur_id').val(retour.transporteur_id);
				$('#frais-port').html(retour.fraisport);
				$('#total-commande').html(retour.total);
				$('#franco').html(retour.franco);
			});
		});
	}
	
	//Connexion client
	$('button#submit-connexion').click(function(){
		
		$.post($('#form-connexion').attr('action'), $('#form-connexion').formSubmit(), function(data){
			var retour = $.parseJSON(data);
			if( retour.connecte ){ window.location = retour.redirection; }
			else{ alert(retour.erreurs); }
		});
		return false;
	});
});

function prix(){
	$.post('/catalogue/tidinox/get_prix', $('#form-boutique').formSubmit(), function(data){
		
		var retour = $.parseJSON(data);
		if( retour.prix_ttc ){
			$('#num-reference').html(retour.reference);
			$('#prix-ttc').html(retour.prix_ttc);
			$('#prix-ht').html(retour.prix_ht);
			$('#total').html(retour.prix_total);
			$('#unite').html(retour.unite);
		}
		else{ alert(retour.message); }
	});
}

function ajouter_panier(){
	$.ajaxSetup({ cache: false });
	$.post('/boutique/panier/ajouter', $('#form-boutique').formSubmit(), function(data){
		
		var retour = $.parseJSON(data);
		if( retour.erreur != true ){
			$.post('/boutique/panier/actualiser', function(data){
				
				var panier = $.parseJSON(data);
				$('html,body').animate({scrollTop:0}, 'slow');
				$('#nb-article').html(panier.nb_article);
				$('#caddie').html(panier.contenu).slideDown(600);
			});
		}
		else{
			alert(retour.message);
		}
	});
}
