// JavaScript Document

function initFormIngredienti() {
	if ($("#formIngredienti").html()) {
		resetFormIngredienti();
		
		$(".deleteIngrediente").click(function() {
			var sectionToClear = $(this).parent().prev().children().attr("id").replace("thumb","");
			var defaultVal = $("select[name="+sectionToClear+"] > option[value='']").text();
			$("select[name="+sectionToClear+"]").parent().children("div").children("span").text(defaultVal);
		});
		
		$("#senzaGlutine").click(function() {
			if ($(this).attr("src").indexOf("_OFF") != -1) {
				$(this).attr("src",$(this).attr("src").replace("_OFF","_ON"));
			} else {
				$(this).attr("src",$(this).attr("src").replace("_ON","_OFF"));	
			}
		});
		
		setInterval(checkChangeSelectIngredienti,100);
		
		$("#avviaRicerca").click(function() {
			var pasta = $("select[name=pasta]").parent().children("div").children("span").text();
			if (pasta == "Scegli") pasta = "";
			var carne = $("select[name=carne]").parent().children("div").children("span").text();
			if (carne == "Scegli") carne = "";
			var latticini = $("select[name=latticini]").parent().children("div").children("span").text();
			if (latticini == "Scegli") latticini = "";
			var pesce = $("select[name=pesce]").parent().children("div").children("span").text();
			if (pesce == "Scegli") pesce = "";
			var ortaggi = $("select[name=ortaggi]").parent().children("div").children("span").text();
			if (ortaggi == "Scegli") ortaggi = "";
			var spezie = $("select[name=spezie]").parent().children("div").children("span").text();
			if (spezie == "Scegli") spezie = "";
			
			var senzaGlutine = $("#senzaGlutine").attr("src").indexOf("_ON");
			if (senzaGlutine == -1) { senzaGlutine = ""; } else {senzaGlutine = 1;  }

			if (pasta+carne+latticini+pesce+ortaggi+spezie+senzaGlutine == "") {
				BlockUI("Attenzione","Seleziona almeno un criterio di ricerca");
				return false;
			}
			
			var dataString = "";
				dataString += "pasta="+pasta;
				dataString += "|carne="+carne;
				dataString += "|latticini="+latticini;
				dataString += "|pesce="+pesce;
				dataString += "|ortaggi="+ortaggi;
				dataString += "|spezie="+spezie;
				dataString += "|glutine="+senzaGlutine;

			LoadingBlockUI();
				
			document.location.href = BASE_URL+"it/prodotto/ricerca/"+dataString;
			return false;
		});
	}
}

function resetFormIngredienti() {
	$("select[name=pasta]").parent().children("div").children("span").text($("select[name=pasta] > option[value='']").text());
	$("select[name=carne]").parent().children("div").children("span").text($("select[name=carne] > option[value='']").text());
	$("select[name=latticini]").parent().children("div").children("span").text($("select[name=latticini] > option[value='']").text());
	$("select[name=pesce]").parent().children("div").children("span").text($("select[name=pesce] > option[value='']").text());
	$("select[name=ortaggi]").parent().children("div").children("span").text($("select[name=ortaggi] > option[value='']").text());
	$("select[name=spezie]").parent().children("div").children("span").text($("select[name=spezie] > option[value='']").text());

}

var nowPasta = "";
var nowCarne = "";
var nowLatticini = "";
var nowPesce = "";
var nowOrtaggi = "";
var nowSpezie = "";

function checkChangeSelectIngredienti() {
	
	
	var valPasta = $("select[name=pasta]").parent().children("div").children("span").text();
	if (nowPasta != valPasta) {
		nowPasta = valPasta;
		var img = $("select[name=pasta] > option").filter(function() {
			return $(this).text() == valPasta;
		}).attr("ref");
		$("#thumbpasta").attr("src","cms/uploads/ingrediente/"+img);
		$("#thumbpasta").ifixpng();
	}
	
	var valCarne = $("select[name=carne]").parent().children("div").children("span").text();
	if (nowCarne != valCarne) {
		nowCarne = valCarne;
		var img = $("select[name=carne] > option").filter(function() {
			return $(this).text() == valCarne;
		}).attr("ref");
		$("#thumbcarne").attr("src","cms/uploads/ingrediente/"+img);
		$("#thumbcarne").ifixpng();
	}
	
	var valLatticini = $("select[name=latticini]").parent().children("div").children("span").text();
	if (nowLatticini != valLatticini) {
		nowLatticini = valLatticini;
		var img = $("select[name=latticini] > option").filter(function() {
			return $(this).text() == valLatticini;
		}).attr("ref");
		$("#thumblatticini").attr("src","cms/uploads/ingrediente/"+img);
		$("#thumblatticini").ifixpng();
	}
	
	var valPesce = $("select[name=pesce]").parent().children("div").children("span").text();
	if (nowPesce != valPesce) {
		nowPesce = valPesce;
		var img = $("select[name=pesce] > option").filter(function() {
			return $(this).text() == valPesce;
		}).attr("ref");
		$("#thumbpesce").attr("src","cms/uploads/ingrediente/"+img);
		$("#thumbpesce").ifixpng();
	}
	
	var valOrtaggi = $("select[name=ortaggi]").parent().children("div").children("span").text();
	if (nowOrtaggi != valOrtaggi) {
		nowOrtaggi = valOrtaggi;
		var img = $("select[name=ortaggi] > option").filter(function() {
			return $(this).text() == valOrtaggi;
		}).attr("ref");
		$("#thumbortaggi").attr("src","cms/uploads/ingrediente/"+img);
		$("#thumbortaggi").ifixpng();
	}
	
	var valSpezie = $("select[name=spezie]").parent().children("div").children("span").text();
	if (nowSpezie != valSpezie) {
		nowSpezie = valSpezie;
		var img = $("select[name=spezie] > option").filter(function() {
			return $(this).text() == valSpezie;
		}).attr("ref");
		$("#thumbspezie").attr("src","cms/uploads/ingrediente/"+img);
		$("#thumbspezie").ifixpng();
	}
	
}	
