var timeoutHnd;
var stato = "chiuso";
$(document).ready(function() {

	$(".addCarrello").click(function(){
		apri_carrello(this, "add");
	});
	
	$("#submit_ordine").click(function(){
		submit_ordine(this);
	});
	
	$("#submit_login").click(function(){
		submit_login(this);
	});
	
	$(".sceltaPagamento").click(function(){
		calcola_totale();
	});
	
	$(".sceltaSpedizione").click(function(){
		calcola_totale();
	});
	
	
});


function calcola_totale(){
	$.ajax({
		url: "/it/ajax/calcolaTotale.php",
		dataType: 'json',
		type: 'POST', 
		cache: false,
		success: function(data){
			var acquistabile = data.acquistabile.split("|");
			var ordinabile = data.ordinabile.split("|");
			var metodoPagamento = data.metodoPagamento;
			if (!ordinabile[1]){
				$(".prezzo_finale_ordinabile").hide();
			}
			$("#totaleValue").html(ordinabile[0]);
			$("#totaleArticoli").html(data.totaleArticoli);
			$("#totaleAcqIva").html(data.totaleAcqIva);
			$("#totaleOrdIva").html(data.totaleOrdIva);
			if (data.speseSpedizione != "0,00 €"){
				$("#speseSpedizione").html(data.speseSpedizione);
			} else {
				$(".speseSpedizione").hide();
			}
			if (data.commissioniPagamento != "0,00 €"){
				$("#commissioniPagamento").html(data.commissioniPagamento);
			} else {
				$(".commissioniPagamento").hide();
			}
			if (acquistabile[1]){
				$("#totaleAcquistabile").html(acquistabile[0]);
				$("#continua").attr('href', '/it/servizi_consegna');
				$("#continua_cons").attr('href', '/it/servizi_consegna_consumatore');
				if (acquistabile[1] < 50){
					$(".limitazione").show();
				}else{
					$(".limitazione").hide();
				}
				
				if ((metodoPagamento == "contrassegno") && (acquistabile[1] > 2500)){
					$(".limitazione_contrassegno").show();					
				}else{
					$(".limitazione_contrassegno").hide();					
				}

				
			} else {
				$(".prezzo_finale_consumer").hide();
				$(".prezzo_finale_carrello").hide();
			}
		}
	});
}


function submit_ordine(elem){
	var current = $(elem).attr("current");
	var next = $(elem).attr("next");
	var params = "current=" + current + "&next=" + next + "&" + $("#servizi_consegna").serialize();
	$.ajax({
	  url: "/it/ajax/submit_ordine.php",
	  data: params,
	  type: 'POST', 
	  cache: false,
	  success: function(html){
		  switch(html){
			  case "maxlimit":
			  	window.location = "/it/carrello";
			  break;
			  default:
				  window.location = next;
		  }
	  }
	});
}

function submit_login(elem){
	var current = $(elem).attr("current");
	var next = $(elem).attr("next");
	var params = "current=" + current + "&next=" + next + "&" + $("#accedi").serialize();
	$.ajax({
	  url: "/it/ajax/submit_ordine.php",
	  data: params,
	  type: 'POST', 
	  cache: false,
	  success: function(html){
		  if (html == "errore"){
			  alert("Login errato");
		  }else{
	    	window.location = next;
		  }
	  }
	});
}


function apriCarrelloTable(elem, operazione){
	chiudi_carrello();
	
	var params = "";
	if (elem != null){
		params = "id=" + $(elem).attr("rel") + "&prodotto=" + $(elem).attr("prodotto") + 
		"&quantita=" + $(elem).val();
	}
	params += "&operazione=" + operazione + 
		"&type=table";
	
	$.ajax({
	  url: "/it/ajax/carrello.php",
	  data: params,
	  type: 'POST', 
	  cache: false,
	  success: function(html){
		$("#carrelloTable").html(html);
		if (html == ""){
			window.location = "/it/";
		}
		
		var valore = $("#tipo_risposta").val();
		if (valore != ""){
			var lista = valore.split("|");
			for(var i=0;i<lista.length;i++){
				$("#" + lista[i]).show();
			}
		}
		
		$(".rimuoviInputTable").click(function(){
			 apriCarrelloTable(this, "delete");
		  });
		 $(".qtaInputTable").keyup(function(){
			  	var elem = this;
				if(timeoutHnd){
					clearTimeout(timeoutHnd)
				}
				timeoutHnd = setTimeout(function(){
					apriCarrelloTable(elem, "update");
				},500) ;

			  
		  });
		  calcola_totale();
	  }
	});
	
}


function apri_carrello(elem, operazione){
	
	
	$("#carrello").click(
		 function () {
			apri_carrello();
	});
	
	
	
	var params = "";
	
	if (elem != null){
		params = "id=" + $(elem).attr("rel") + "&prodotto=" + $(elem).attr("prodotto") + 
		"&prezzo=" + $(elem).attr("prezzo") + 
		"&quantita=" + $(elem).val() + 
		"&operazione=" + operazione;
	}
	
	
	$.ajax({
	  url: "/it/ajax/carrello.php",
	  data: params,
	  type: 'POST', 
	  cache: false,
	  success: function(html){
		  if (html == "") return chiudi_carrello();
		  $("#carrelloaperto").html(html);
		  $("#carrello").addClass("carello-selected");
		  $("#carrelloaperto").show();
		  $(".qtaInput").keyup(function(){
			  	var elem = this;
				if(timeoutHnd){
					clearTimeout(timeoutHnd)
				}
				timeoutHnd = setTimeout(function(){
					apri_carrello(elem, "update");
				},500) ;

			  
		  });
		  $(".carrelloaperto_close").click(function(){
			 chiudi_carrello();
		  });
		   $(".rimuoviInput").click(function(){
			 apri_carrello(this, "delete");
		  });

	  }
	});
	
	
	
}
function chiudi_carrello(){
	
	
	stato = "chiuso";
	
	$.ajax({
	  url: "/it/ajax/carrello.php",
	  data: "id=" + $(this).attr("rel") + "&chiudi=true",
	  cache: false,
	  success: function(html){
		 $("#carrello").removeClass("carello-selected");
		 $("#carrelloaperto").hide();
	  }
	});
	
	
	
}
