/**
*	CART
*/
var currentBtnAbo;
function actionCart(){
	$('a.delete_f_cart').each(function(){
		$(this).bind('click',function(e){
			e.preventDefault();
			var aElem = $(this);
			if(!aElem.data('clicked')){
				aElem.data('clicked',true);
				hRef = aElem.attr('href');
				arrHref = hRef.split('/');
				prodName = $(this).attr('rel');
				typeProd = arrHref[3];
				idxProd = $(this).attr('ref');
				typeProd = arrHref[3];
				url = window.location.pathname;
				$.postJSON('/action/delFromCartAJX',{idx: idxProd, url: url}, function(data, textStatus){
					if(textStatus=='success' || textStatus=='notmodified'){
 						$('#miniCarrello').empty().html(data.html);
						if(typeProd=='abbonamento'){
							$('#abo_' + data.idRiv).show().next().hide();
						} else {
							$('#prod_' + idxProd).show().next().hide();
							$('#in_mag').show();
							$('#out_mag').hide();
						}
						aElem.removeData('clicked');
						actionCart();
						$.growl("PRODOTTO ELIMINATO DAL CARRELLO", "Rimosso prodotto nel carrello acquisti: <strong>" + data.nome + "</strong>");
					} else {
						aElem.removeData('clicked');
						alert("Errore di trasmissione!\nRiprovare e controllare la propria connessione remota.");
					}
				});
			}
		});
	});
	
	$('a.addCart').each(function(){
		$(this).bind('click',function(e){
			e.preventDefault();
			var aElem = $(this);
			if(!aElem.data('clicked')){
				aElem.data('clicked',true);
				prodName = aElem.attr('rel');
				idxProd = aElem.attr('ref');
				url = window.location.pathname;
				$.postJSON('/action/addToCartAJX',{idx: idxProd, url: url}, function(data, textStatus){
					if(textStatus=='success' || textStatus=='notmodified'){
						$('#miniCarrello').empty().html(data.html);
						if(data.disp<=0) {
							$('#prod_' + idxProd).hide().next().show();
							$('#in_mag').hide();
							$('#out_mag').show();
						}
						aElem.removeData('clicked');
						actionCart();
						$.growl("QUANTITA' PRODOTTO INCREMENTATA", "Quantità prodotto aumentata nel carrello acquisti: <strong>" + data.nome + "</strong>");
					} else {
						aElem.removeData('clicked');
						alert("Errore di trasmissione!\nRiprovare e controllare la propria connessione remota.");
					}
				});
			}
		});
	});

	$('a.remCart').each(function(){
		$(this).bind('click',function(e){
			e.preventDefault();
			var aElem = $(this);
			if(!aElem.data('clicked')){
				aElem.data('clicked',true);
				prodName = aElem.attr('rel');
				idxProd = aElem.attr('ref');
				url = window.location.pathname;
				$.postJSON('/action/remFromCartAJX',{idx: idxProd, url: url}, function(data, textStatus){
					if(textStatus=='success' || textStatus=='notmodified'){
						$('#miniCarrello').empty().html(data.html);
						actionCart();
						$('#prod_' + idxProd).show().next().hide();
						$('#in_mag').show();
						$('#out_mag').hide();
						aElem.removeData('clicked');
						$.growl("QUANTITA' PRODOTTO DIMINUITA", "Quantità prodotto diminuita nel carrello acquisti: <strong>" + data.nome + "</strong>");
					} else {
						aElem.removeData('clicked');
						alert("Errore di trasmissione!\nRiprovare e controllare la propria connessione remota.");
					}
				});
			}
		});
	});
}

function showResponseAbbonamenti(data, statusText)  { 
	$('a.add_abo').colorbox.close();
	if(statusText=='success' || statusText=='notmodified'){
		$('#miniCarrello').empty().html(data.html);
		$('#abo_' + data.idRiv).hide().next().show();
		actionCart();
		$.growl("PRODOTTO AGGIUNTO AL CARRELLO", "Aggiunto prodotto nel carrello acquisti: <br /><strong>" + data.nome + "</strong>");
	} else {
		aElem.removeData('clicked');
		alert("Errore di trasmissione!\nRiprovare e controllare la propria connessione remota.");
	}
} 

$(document).ready(function() {
	
	$('a.add').each(function(){
		$(this).bind('click', function(e){
			e.preventDefault();
			var aElem = $(this);
			if(!aElem.data('clicked')){
				aElem.data('clicked',true);
				prodName = aElem.attr('rel');
				hRef = aElem.attr('href');
				arrHref = hRef.split('/');
				idProd = arrHref[4];
				typeProd = arrHref[3];
				idxProd = idProd + "_" + typeProd;
				url = window.location.pathname;
				$.postJSON('/action/insToCartAJX',{type: typeProd, id: idProd, url: url}, function(data, textStatus){
					if(textStatus=='success' || textStatus=='notmodified'){
						$('#miniCarrello').empty().html(data.html);
						if(data.disp <= 0) {
							$('#prod_' + idxProd).hide().next().show();
							$('#in_mag').hide();
							$('#out_mag').show();
						}
						aElem.removeData('clicked');
						actionCart();
						$.growl("PRODOTTO AGGIUNTO AL CARRELLO", "Aggiunto prodotto nel carrello acquisti: <br /><strong>" + data.nome + "</strong>");
					} else {
						aElem.removeData('clicked');
						alert("Errore di trasmissione!\nRiprovare e controllare la propria connessione remota.");
					}
				});
			}
		});
	});


	$('a.add_abo').colorbox({},function(){
		var options = {
			dataType: 'json',
			success: showResponseAbbonamenti  // post-submit callback 
		};
		$('#abbonamenti-form').ajaxForm(options);
	});

	actionCart();
});