//FUNZIONI

//CONTROLLO CAPSULE
function ControlloCapsule(confirmMsg){
	var qty_cap = ContaQuantita("qty_cap");
	var qty_bev = ContaQuantita("qty_bev");
	var qty_mac = ContaQuantita("qty_mac");
	qty = (qty_cap + qty_bev);
	if(((qty % 50 == 0) && (qty > 0)) || ((qty_mac > 0) && (qty == 0))){
		return true;
	}else{
		//"The total number of capsules must be a multiple of 50."
		//alert($("#mac_shop").css("display"));
		var messaggio = "Attenzione! Non hai selezionato nessuna macchina o il totale delle capsule selezionate non è pari o multiplo di 50.\n";
		if($("#mac_shop").css("display") == "block"){
			messaggio += "Seleziona una macchina.";
		}else{
			if(qty == 0){
				messaggio += "Selezionare almeno 50 capsule.";
			}else{
				messaggio += "Capsule mancanti: " + (50 - (qty % 50));
			}
		}
		alert(messaggio);
		return false;
	}
}

//CONTEGGIO QUANTITA'
function ContaQuantita(classe){
	var conteggio = 0;
	$("select." + classe).each(function(i){
		if($(this).val()>0){
			conteggio = conteggio + eval($(this).val());
		}
	});
	return conteggio;
}

//CONTROLLO MACCHINE
function ControlloMacchine(){
	var qty = ContaQuantita("qty_mac");
	if((qty <= 10)){
		return true;
	}else{
		return false;
	}
}

//TAB SHOP
function tabShop(){
	function switchTab(myTab){
		if((myTab != '#mac_shop') && (myTab != '#bev_shop') && (myTab != '#cap_shop') && (myTab != '#acc_shop')){
			$("#mac_shop, #bev_shop, #acc_shop").fadeOut("normal");
		}else{
			$("#mac_shop, #bev_shop, #cap_shop, #acc_shop").not(myTab).fadeOut("normal");
			$(myTab).fadeIn("normal");
		}
	}
	
	var url = location.href;
	var myTab = url.substr(location.href.indexOf("#"),location.href.length);
	switchTab(myTab);
	
	$("span.sublink a").click(function(){
		myTabA = $(this).attr("href");
		switchTab(myTabA);
		return false;
	});
}


//DOM READY
$(document).ready(function(){
	tabShop();
	window.scrollTo(0,0);
	//var promoNautilusAttivata = false;
	
	$("#cap_shop div.col_shop:last, #mac_shop div.col_shop:last, #bev_shop div.col_shop:last").css("border-right","none");
	
	// funzione al cambio della select
	$(".col_shop select").change(function(){
		if ($(this).val()!="0") {
			$(this).addClass("on");
		} else {
			$(this).removeClass("on");
		}
	});
	
	// funzione delle select al dom ready
	$(".col_shop select").each(function(){
		if ($(this).val()!="0") {
			$(this).addClass("on");
		} else {
			$(this).removeClass("on");
		}
	});
	
	//CONTROLLO ORDINE
	$("#Ordina").click(function(){
		var ordine = true;
		
		if(!ControlloMacchine()){
			ordine = false;
			alert("Attenzione! Non è possibile acquistare più di 10 macchine.");
		}
		//alert(ordine);
		if(!ControlloCapsule(confirmMsg)){
			ordine = false;
		}

		//ORDINE
		if(ordine){
			var messaggio = "";
			
			if(messaggio != ""){
				messaggio += "\n\nClicca su annulla per tornare allo shop o su ok per proseguire l\'ordine."
			}else{
				var messaggio = "Vuoi proseguire l\'ordine?";
			}
			
			if(confirm(messaggio)){
				//alert(messaggio);
				//$("#form_ordine").submit();
			}else{
				return false;
			}
		}else{
			return false;
		}
	});

	//CARATTERISTICHE TECNICHE
	$("a.car_tech").click(function(){
		//alert(this.id);
		var qs = "?KeepThis=true&TB_iframe=true&height=450&width=600";
		var titolo = $(this).attr("title");
		switch (this.id){
			case "ct_14":
				tb_show(titolo, 'Templates/Macchine/amante_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_16":
				tb_show(titolo, 'Templates/Macchine/evolution_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_17":
				tb_show(titolo, 'Templates/Macchine/k111d_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_18":
				tb_show(titolo, 'Templates/Macchine/k111_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_34":
				tb_show(titolo, 'Templates/Macchine/nautilus_white_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_35":
				tb_show(titolo, 'Templates/Macchine/nautilus_silver_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_36":
				tb_show(titolo, 'Templates/Macchine/nautilus_rossa_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_37":
				tb_show(titolo, 'Templates/Macchine/so2hs_black_caratteristiche.htm' + qs, 'gallery');
				break;
			case "ct_40":
				tb_show(titolo, 'Templates/Macchine/so2hs_silver_caratteristiche.htm' + qs, 'gallery');
				break;	
			case "promozioni":
				tb_show(titolo, 'PromozioniRiepilogo.asp' + qs, titolo);
				break;
			default:
				return false;
		}
	});
	
});
