var changeQte = function(e, from,article) {
	var tr = $(e).parents('tr');
	var qte = $(e).attr('value');
	var data = $(e).attr('name') + "=" + $(e).attr('value');
	
	$.post("/action/ajax/panier/ajouter/", data,function(data) {

		$.get("/ajax/panier/boxAlert",function(html) {
			alertBox(html);
		});	
		
		if(from == 'panier') {
		
			$.getJSON("/ajax/panier/actualisationPrix/"+article,function(data){
				$.each(data, function(article,item){
					if(item.qte == "0" ) {
						killLignePanier(article);
					} else {
						$.each(item, function(key,valeur){
							tr.children('td.'+key).children('span').html(valeur);
						});
					}
				});
			});
			$("table.panier tfoot").load("/ajax/panier/footer");
	
		} else if(from == 'article') {
			
		
			$.getJSON("/ajax/article/"+article,function(data){
				$.each(data, function(article,item){
		
					if(item.qte == "0" ) {
						tr.removeClass('panier');
					} else {
						tr.addClass('panier');
					}
					
					tr.children('td.qte').children('input').val(item.qte);
					tr.children('td.remise').html(item.remise + ' %');
					tr.children('td.prixNet').children('span').html(item.prixNet);
						
						
					});
				});
			

				
		}			

		
	});


}

var changeEtq = function(e) {
	var qte = $(e).attr('value');
	data = $(e).attr('name') + "=" + $(e).attr('value');
	$.post("/action/ajax/panier/ajouter/", data);

} 

$(window).load(function() {

	
	
	/**
	 * carousel promo
	 */
	$("#carousel").css('display', 'block');
	var n = $("#carousel .ss_promos ul li").length;
	if(n > 2) n = 2;
	$("#carousel .ss_promos").jCarouselLite({
		visible: n,
		btnNext: ".next",
		btnPrev: ".prev"
	});
	$("#carousel-loader").css('display', 'none');	

	/**
	 * carousel produit
	 */	
	$(".produitImages.carousel").each(function (i) {
		$(this).css('display','block');
		$(this).jCarouselLite({
			visible: 1,
			btnNext: $(this).children(".next"),
			btnPrev: $(this).children(".prev")
		});
		
	});
	
	/**
	 * animation menu
	 */
	 /*
	$('#header ul li').ahover({moveSpeed: 100, hoverEffect: function() {
        $(this)
            .animate({opacity: 0.6}, 750)
            .animate({opacity: 1.0}, 1000)
            .dequeue();
        $(this).queue(arguments.callee);
    }});
	*/
	

});



$(document).ready(function() {
	
	
	$('#articleRef').focus();

	
	// pour eviter un target _blank
	$('a.lienExterne').attr("target", "_blank");
	
	//$(".tableProduit").tablesorter();
	
	$("#keywd").focus(function() {
		fieldFocus(this, "Recherche...")
	});

	$("#keywd").blur(function() {
		fieldBlur(this, "Recherche...")
	});

	$("#identificationLogin").focus(function() {
		fieldFocus(this, "login")
	});

	$("#identificationLogin").blur(function() {
		fieldBlur(this, "login")
	});

	$("#identificationMotdepasse").focus(function() {
		fieldFocus(this, "password")
	});

	$("#identificationMotdepasse").blur(function() {
		fieldBlur(this, "password")
	});

	/*$('.lightBox').each(function (i) {
		$(this).lightBox();
	});*/

	$('.produitImages a.lightBox').prettyPhoto({ showTitle: true });
	
	$('.apercu_promo a.lightBox').prettyPhoto({ showTitle: true });
	
	
	
	$('table.tableProduit a.lightBox').live('click', function() {
		var href = $(this).attr('href');
		$(this).parents('div.produit').children('div.produitImages').children('ul').children('li').children('a.lightBox[href="'+href+'"]').trigger('click');
		return false;
	});

	

	
	
	
	$('#historique_commandes').accordion({autoheight: false});

	
	$('.formProduit').submit(function() {
		data = '<div class="element"><h4>Actualisation du panier</h4><img src="/img/Shopcart-Apply_64.png" <p>votre panier est à jour</p>';
		alertBox(data);
		return false;
	});

	$('#formAjoutPanier').submit(function() {
		var data = $("#formAjoutPanier").serialize();
		$.post("/action/ajax/panier/ajoutSimple/", data,function() {
			$.get("/ajax/panier/boxAlert",function(data) {
				alertBox(data);
			});	
			refreshContenuPanier();
			
		});
		document.getElementById('formAjoutPanier').reset();
		document.getElementById('articleRef').focus();
		return false;
		
	});
	$("#boxPanier .cross").click(function() {
		$('#boxPanier').fadeOut('slow');	
	});
	
	
	
	$('#ALTADL').change(function() {
		$('#inputAltAdl').show();
	});
	

	evenementPanier();

});


function evenementPanier() {

	$("table.panier tbody tr").live('hover', function(){
		article = $(this).attr('article');
		
		$("table.panier tbody tr.article"+article).addClass("light");
	}, function(){
		$("table.panier tbody tr.article"+article).removeClass("light");
	});
	
	/*
	$("table.panier tbody tr").click(function(){
		$(this).toggleClass("light2");
	});
	*/
	
	$(".btn_croix15").live('click', function() {
		var reg=new RegExp("([0-9]+)/$", "");
		arr = reg.exec($(this).attr('href'));
		article = arr[1];

		var reg=new RegExp("(action)", "g");
		$.get($(this).attr('href').replace(reg,'action/ajax'),function() {
			
			killLignePanier(article);
			$("table.panier tfoot").load("/ajax/panier/footer");
			$.get("/ajax/panier/boxAlert",function(data) {
				alertBox(data);
			});	
						
			
		});
		return false;
	});
}

function fieldFocus(e, text) {
	$(e).css("color", "#000");
	if(e.value == text) e.value = "";
}
	
function fieldBlur(e, text) {
	if(e.value == "") {
		e.value = text;
		$(e).css("color", "#999");
	}
}
	


function alertBox(data) {	
	data = $(data).appendTo("#boxAlertPanier").css('display','none');
	
	$(data).slideDown(500,function () {
		
        var t = setTimeout(function(){
        	$(data).slideUp(500,function() {
        		clearTimeout(t); 
        	});
        }, 2000);  
	

	});
}
	

function refreshContenuPanier() {
	$("table.panier tbody").load("/ajax/panier/contenu");
	$("table.panier tfoot").load("/ajax/panier/footer");
}


function killLignePanier(article) {
	
	var tr = $("table.panier tr.article"+article);
		$(tr).children('td').css('background-color',"#FFC0C0");
		$(tr).fadeOut(3000,function(){
			$(tr).remove();
			if( $("table.panier tbody tr").length==0 ) {
				refreshContenuPanier();
			}
		});			
}

