﻿$(document).ready(function(){
	$("#fingr").submit(function(){
		add();
		return false;
	});
	$("#fingrscl").submit(function(){
		remove();
		return false;
	});
	$("#ingredienti").click(function(){
		if($("#ingredienti").val()=="ingredienti") {
			$("#ingredienti").val("");
			$("#ingredienti").css("color","#73AE1E");
		}
	});
	$("#ingredientiescl").click(function(){
		if($("#ingredientiescl").val()=="ingredienti") {
			$("#ingredientiescl").val("");
			$("#ingredientiescl").css("color","#FF0012");
		}
	});
	$("#esclusioni").click(function(){
		$("#esclusioni").addClass("ingrtabsel");
		$("#cucina").removeClass("ingrtabsel");
		
		$("#ingrsel").hide();
		$("#ingrescl").show();
		
		$("#dingr").hide();
		$("#dingrescl").show();
	});
	$("#cucina").click(function(){
		$("#cucina").addClass("ingrtabsel");
		$("#esclusioni").removeClass("ingrtabsel");
		$("#ingrsel").show();
		$("#ingrescl").hide();
		$("#dingr").show();
		$("#dingrescl").hide();
	});

	$("#ingredienti").autocomplete("/autocomplete.php", {
													delay:10,
													minChars:3,
													matchSubset:1,
													matchContains:1,
													cacheLength:10,
													onItemSelect:selectItem,
													onFindValue:findValue,
													formatItem:formatItem,
													autoFill:true
												}
							);
							
	$("#ingredientiescl").autocomplete("/autocomplete.php", {
													delay:10,
													minChars:3,
													matchSubset:1,
													matchContains:1,
													cacheLength:10,
													onItemSelect:selectItem,
													onFindValue:findValue,
													formatItem:formatItem,
													autoFill:true
												}
							);	
	$("#ingredienti").keyup(function(e) {
		if(e.keyCode == 13) {
			add();
		}
	});
	$("#ingredientiescl").keyup(function(e) {
		if(e.keyCode == 13) {
			remove();
		}
	});
});

function findValue(li) {
	if( li == null ) return alert("Ingrediente non trovato");
	
	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];
	
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);

	$("#ingredienti").focus();
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0];
}

function lookupAjax(){
	var oSuggest = $("#ingredienti")[0].autocompleter;
	oSuggest.findValue();
	return false;
}

function add(){
	var t = $("#ingredienti").val();	
	if(t.length < 2) return false;	
	$.ajax({
	   type: "get",
	   url: "/autocomplete.php",
	   data: "q="+t+"&test=true",
	   success: function(msg){
	     if(msg=="true") {
			var ingredienti = ingredientepath.split("|");
			for(i=0;i<ingredienti.length;i++){
				if(ingredienti[i].substring(0,1)=="!") {
					ingredienti[i] = ingredienti[i].substring(1);
				}
				if(ingredienti[i] == t) {
					alert("Ingrediente già presente");
					return false;
				}
			}
			ingredienti[ingredienti.length] = t;
			ingredienti.sort();
			for(i=0;i<ingredienti.length;i++){
				if(ingredienti[i]=="") {
					ingredienti.splice(i,1); 
				}
			}
			ingredientipath = ingredienti.join("|");
			document.location = "/"+currportatapath+"1/"+ingredientipath+"/";
	     } else {
			alert("Ingrediente non trovato");
	     }
	   }
	 });
	
	$("#ingredienti").val("");
	$("#ingredienti").focus();
	$("#ingredienti")[0].autocompleter.flushCache();
}

function remove(){
	var t = $("#ingredientiescl").val();	
	if(t.length < 2) return false;	
	$.ajax({
	   type: "get",
	   url: "/autocomplete.php",
	   data: "q="+t+"&test=true",
	   success: function(msg){
	     if(msg=="true") {
			var ingredienti = ingredientepath.split("|");
			for(i=0;i<ingredienti.length;i++){
				if(ingredienti[i] == "-"+t) {
					alert("Ingrediente già presente");
					return false;
				}
			}
			ingredienti[ingredienti.length] = "-"+t;
			ingredienti.sort();
			for(i=0;i<ingredienti.length;i++){
				if(ingredienti[i]=="") {
					ingredienti.splice(i,1); 
				}
			}
			ingredientipath = ingredienti.join("|");
			document.location = "/"+currportatapath+"1/"+ingredientipath+"/";
	     } else {
			alert("Ingrediente non trovato");
	     }
	   }
	 });
	
	$("#ingredientiescl").val("");
	$("#ingredientiescl").focus();
	$("#ingredientiescl")[0].autocompleter.flushCache();
}
