function getAction(paginaRisultati,lang){
	var t = document.getElementById("user_ricerca_pi3_tipo").value;
	var idZona = document.getElementById("user_ricerca_pi3_idZona").value;
	var idLocalita = document.getElementById("user_ricerca_pi3_idLocalita").value;
	var idTema = document.getElementById("user_ricerca_pi3_idTema").value;
	var tipoStr = document.getElementById("user_ricerca_pi3_tipoStr").value;
	var stelle = document.getElementById("user_ricerca_pi3_stelle").value;		
	
	var host = 'http://'+window.location.hostname+'/'; 					
	var action = host+"index.php?id="+paginaRisultati+"&user_ricerca_pi3[idZona]="+idZona+"&user_ricerca_pi3[idLocalita]="+idLocalita+"&user_ricerca_pi3[tipo]="+t+"&user_ricerca_pi3[idTema]="+idTema+
						"&user_ricerca_pi2[tipoStr]="+tipoStr+"&user_ricerca_pi2[stelle]="+stelle+"&L="+lang;
						
	return action;
}


function getActionJSON(paginaRisultati,lang){
	var t = document.getElementById("user_ricerca_pi3_tipo").value;
	var idZona = document.getElementById("user_ricerca_pi3_idZona").value;
	var idLocalita = document.getElementById("user_ricerca_pi3_idLocalita").value;
	var idTema = document.getElementById("user_ricerca_pi3_idTema").value;
	var tipoStr = document.getElementById("user_ricerca_pi3_tipoStr").value;
	var stelle = document.getElementById("user_ricerca_pi3_stelle").value;		
	
	var host = 'http://'+window.location.hostname+'/'; 					
	var action = host+"index.php?id="+paginaRisultati+"&user_ajaxsearch_pi1[idZona]="+idZona+"&user_ajaxsearch_pi1[idLocalita]="+idLocalita+"&user_ajaxsearch_pi1[tipo]="+t+"&user_ajaxsearch_pi1[idTema]="+idTema+
						"&user_ajaxsearch_pi1[tipoStr]="+tipoStr+"&user_ajaxsearch_pi1[stelle]="+stelle+"&L="+lang;
						
	return action;	
}


					
function doRedirect(paginaRisultati,lang){
	window.location = getAction(paginaRisultati,lang);
}


function doRedirectJSON(paginaRisultati,lang){
	window.location = getActionJSON(paginaRisultati,lang);
}				
			

function lookup(inputString, arrCached) {
	
	var html = '';
	var zoneInserted = false;
	
	for(var i in arrCached){
		zoneInserted = false;
		for(var j in arrCached[i]){
			if(j == 'label'){
				if( (arrCached[i][j].toLowerCase().indexOf(inputString.toLowerCase()) != -1 || inputString == '') && zoneInserted == false){
					html += '<li class="zonaInSearch" onClick="fill(&quot;'+arrCached[i][j]+'&quot;, '+i+', 1); if(typeof(refreshable) != &quot;undefined&quot;){$(&quot;#struttureSubmit&quot;).trigger(&quot;click&quot;);}">'+arrCached[i][j]+'</li>';
					zoneInserted = true;
				}
			}
			else{
				if(arrCached[i][j].toLowerCase().indexOf(inputString.toLowerCase()) != -1 || inputString == ''){
					if(zoneInserted == true){
						html += '<li class="locInSearch" onClick="fill(&quot;'+arrCached[i][j]+'&quot;, '+j+', 2); if(typeof(refreshable) != &quot;undefined&quot;){$(&quot;#struttureSubmit&quot;).trigger(&quot;click&quot;);}">'+arrCached[i][j]+'</li>';
					}
					else{
						html = '<li class="locInSearch" onClick="fill(&quot;'+arrCached[i][j]+'&quot;, '+j+', 2); if(typeof(refreshable) != &quot;undefined&quot;){$(&quot;#struttureSubmit&quot;).trigger(&quot;click&quot;);}">'+arrCached[i][j]+'</li>' + html;
					}
				}
			}
		}
	}
	
	if(html != ''){
		$('#suggestions').show();
		$('#autoSuggestionsList ul').html(html);
	}
	else{
		$('#suggestions').hide();
	}
} 




function lookupStrutture(inputString, arrCached) {
	
	var html = '';
	
	for(var i in arrCached){
			
		if( arrCached[i]['nome'].toLowerCase().indexOf(inputString.toLowerCase()) != -1 || inputString == '' ){
			//html += '<li class="zonaInSearch" onClick="fillStrutture(&quot;'+arrCached[i]+'&quot;, '+i+');">'+arrCached[i]+'</li>';
			html += '<li class="struttureSearch" onClick="fillStrutture(&quot;'+arrCached[i]['nome']+'&quot;, '+i+'); return true;">'+arrCached[i]['link']+'</li>';
		}
	}
	
	if(html != ''){
		$('#suggestionsStrutture').show();
		$('#autoSuggestionsListStrutture ul').html(html);
	}
	else{
		$('#suggestionsStrutture').hide();
	}
} 










function lookupOther(lang, tipo) {
	
		$.ajax({
				type: "POST",
				url: "/typo3conf/ext/user_api/listaTemi.php",
				data: "lang="+lang+"&tipo="+tipo,
				success: function(res){
					if(res.length > 0){
						$('#suggestions'+tipo).show();
						$('#autoSuggestionsList'+tipo+' ul').html(res);
					}
				}
		});
	
} 





	
function fill(thisValue,locId,locType) {
	$('#comboZone').val(thisValue);
	if(locType == 1){
		$('#user_ricerca_pi3_idZona').val(locId);
	}else if(locType == 2){
		$('#user_ricerca_pi3_idLocalita').val(locId);
	}
	$('#user_ricerca_pi3_tipo').val(locType);
	setTimeout("$('#suggestions').hide();", 200);
}




function fillTheme(thisValue,themeId) {
	$('#comboZoneTheme').val(thisValue);
	$('#user_ricerca_pi3_idTema').val(themeId);
	setTimeout("$('#suggestionsTheme').hide();", 200);
}

function fillStrutture(thisValue,strId) {
	$('#comboZoneStrutture').val(thisValue);
	$('#user_ricerca_pi3_struttura').val(strId);
	setTimeout("$('#suggestionsStrutture').hide();", 200);
}



function fillTipoStr(thisValue,tipoStrId) {
	$('#comboZoneTipoStr').val(thisValue);
	$('#user_ricerca_pi3_tipoStr').val(tipoStrId);
	setTimeout("$('#suggestionsTipoStr').hide();", 200);
}



function fillStelle(thisValue,numStelle) {
	$('#comboZoneStelle').val(thisValue);
	$('#user_ricerca_pi3_stelle').val(numStelle);
	setTimeout("$('#suggestionsStelle').hide();", 200);
}



function clickedOutsideElement(elemId, evt, elemInput) {
	
	var theElem = '';
	if(window.event){
		theElem = getEventTarget(window.event);
	}
	else{
		theElem = getEventTarget(evt);
	}
	
    while(theElem != null) {
        if(theElem.id == elemId || theElem.id.indexOf("testuali") != -1 || theElem.id == elemInput)
            return false;

        theElem = theElem.offsetParent;
    }

    return true;
}


function getEventTarget(evt) {
    var targ = (evt.target) ? evt.target : evt.srcElement;
	
    if(targ != null) {
		// Se è un text node dammi il padre
        if(targ.nodeType == 3)
            targ = targ.parentNode;
    }

    return targ;
}





function selectCurrent(){
	$("#autoSuggestionsList ul li").eq(currentSelection).trigger("click");
}


function setSelected() {
	$("#autoSuggestionsList ul li").each(function(){
			$(this).css("background-color", "#212427");
			$(this).css("color", "#fff");
		});
	
	var curUrl = window.location.href;
	var colorBack = "";
	if(curUrl.indexOf("trentino-sudtirol.info") != -1){
		colorBack = "#94C11F";
	}
	else{
		colorBack = "#11BBEE";
	}
	
   $("#autoSuggestionsList ul li").eq(currentSelection).css("background-color", colorBack);
   $("#autoSuggestionsList ul li").eq(currentSelection).css("color", "#000");
}



function navigate(direction) {
   
   if($("#suggestions").is(":hidden")) {
      currentSelection = -1;
   }
   
   if(direction == 'up' && currentSelection != -1) {
      if(currentSelection != 0) {
         currentSelection--;
      }
   } else if (direction == 'down') {
      if(currentSelection != $("#autoSuggestionsList ul li").size()-1) {
         currentSelection++;
      }
   }
   setSelected();
}


function getKeyEvent(value,arr,keyCode){
	
	lookup(value,arrZoneLoc);
	
	switch(keyCode){
			case 38:
				navigate('up');
				break;
			case 40:
				navigate('down');
				break;
			case 13:
				selectCurrent();
				break;
			default:
				currentSelection = -1;
				break;
	}
}





function selectCurrentStrutture(){
	$("#autoSuggestionsListStrutture ul li a").eq(currentSelectionStrutture).trigger("click");
	var redAddr = $("#autoSuggestionsListStrutture ul li a").eq(currentSelectionStrutture).attr("href");
	
	window.location = 'http://'+window.location.hostname+'/'+redAddr;
}


function setSelectedStrutture() {
	$("#autoSuggestionsListStrutture ul li a").each(function(){
			$(this).css("background-color", "#212427");
			$(this).css("color", "#fff");
		});
	
	var curUrl = window.location.href;
	var colorBack = "";
	if(curUrl.indexOf("trentino-sudtirol.info") != -1){
		colorBack = "#94C11F";
	}
	else{
		colorBack = "#11BBEE";
	}
	
   $("#autoSuggestionsListStrutture ul li a").eq(currentSelectionStrutture).css("background-color", colorBack);
   $("#autoSuggestionsListStrutture ul li a").eq(currentSelectionStrutture).css("color", "#000");
}



function navigateStrutture(direction) {
   
   if($("#suggestionsStrutture").is(":hidden")) {
      currentSelectionStrutture = -1;
   }
   
   if(direction == 'up' && currentSelectionStrutture != -1) {
      if(currentSelectionStrutture != 0) {
         currentSelectionStrutture--;
      }
   } else if (direction == 'down') {
      if(currentSelectionStrutture != $("#autoSuggestionsListStrutture ul li a").size()-1) {
         currentSelectionStrutture++;
      }
   }
   setSelectedStrutture();
}


function getKeyEventStrutture(value,arr,keyCode){
	
	lookupStrutture(value,arrStrutture);
	
	switch(keyCode){
			case 38:
				navigateStrutture('up');
				break;
			case 40:
				navigateStrutture('down');
				break;
			case 13:
				selectCurrentStrutture();
				break;
			default:
				currentSelectionStrutture = -1;
				break;
	}
}







document.onclick = function(evt) {
	
	if(clickedOutsideElement('suggestionsStelle',evt,'comboZoneStelle') && $("#suggestionsStelle").css("display") == "block"){
        $("#suggestionsStelle").css("display","none");
		if($("#comboZoneStelle").attr("value") == ''){
			$("#comboZoneStelle").attr("value", "stelle?");
			$("#user_ricerca_pi3_stelle").attr("value","0");
		}
	}
	
	if(clickedOutsideElement('suggestionsTipoStr',evt, 'comboZoneTipoStr') && $("#suggestionsTipoStr").css("display") == "block"){
        $("#suggestionsTipoStr").css("display","none");
		if($("#comboZoneTipoStr").attr("value") == ''){
			$("#comboZoneTipoStr").attr("value", "hotel,b&b,..altro?");
			$("#user_ricerca_pi3_tipoStr").attr("value","-1");
		}
	}
	
    if(clickedOutsideElement('suggestionsTheme',evt, 'comboZoneTheme') && $("#suggestionsTheme").css("display") == "block"){
        $("#suggestionsTheme").css("display","none");
		if($("#comboZoneTheme").attr("value") == ''){
			$("#comboZoneTheme").attr("value", "vacanze con bambini, benessere,..");
			$("#user_ricerca_pi3_tema").attr("value","-1");
		}
	}
		
	if(clickedOutsideElement('suggestions',evt, 'comboZone') && $("#suggestions").css("display") == "block"){
        $("#suggestions").css("display","none");
		if($("#comboZone").attr("value") == ''){
			$("#comboZone").attr("value", "zona?");
			$("#user_ricerca_pi3_idZona").attr("value","0");
			$("#user_ricerca_pi3_idLocalita").attr("value","0");
		}
		currentSelection = -1;
	}
	
	if(clickedOutsideElement('suggestionsStrutture',evt, 'comboZoneStrutture') && $("#suggestionsStrutture").css("display") == "block"){
        $("#suggestionsStrutture").css("display","none");
		if($("#comboZoneStrutture").attr("value") == ''){
			$("#comboZoneStrutture").attr("value", "nome struttura?");
			$("#user_ricerca_pi3_struttura").attr("value","0");
		}
		currentSelectionStrutture = -1;
	}	
	
		
    
	if(clickedOutsideElement('suggestionsOfferte',evt, 'comboZoneOfferte') && $("#suggestionsOfferte").css("display") == "block"){
        $("#suggestionsOfferte").css("display","none");
		if($("#comboZoneOfferte").attr("value") == ''){
			$("#comboZoneOfferte").attr("value", "zona?");
			$("#user_cercaofferte_pi3_idZona").attr("value","0");
			$("#user_cercaofferte_pi3_idLocalita").attr("value","0");
		}
		currentSelectionOfferte = -1;
	}	


	if(clickedOutsideElement('suggestionsStelleOfferte',evt,'comboZoneStelleOfferte') && $("#suggestionsStelleOfferte").css("display") == "block"){
        $("#suggestionsStelleOfferte").css("display","none");
		if($("#comboZoneStelleOfferte").attr("value") == ''){
			$("#comboZoneStelleOfferte").attr("value", "stelle?");
			$("#user_cercaofferte_pi3_stelle").attr("value","0");
		}
	}
	
	if(clickedOutsideElement('suggestionsTipoStrOfferte',evt, 'comboZoneTipoStrOfferte') && $("#suggestionsTipoStrOfferte").css("display") == "block"){
        $("#suggestionsTipoStrOfferte").css("display","none");
		if($("#comboZoneTipoStrOfferte").attr("value") == ''){
			$("#comboZoneTipoStrOfferte").attr("value", "hotel,b&b,..altro?");
			$("#user_cercaofferte_pi3_tipoStrOfferte").attr("value","-1");
		}
	}
	
    if(clickedOutsideElement('suggestionsThemeOfferte',evt, 'comboZoneThemeOfferte') && $("#suggestionsThemeOfferte").css("display") == "block"){
        $("#suggestionsThemeOfferte").css("display","none");
		if($("#comboZoneThemeOfferte").attr("value") == ''){
			$("#comboZoneThemeOfferte").attr("value", "vacanze con bambini, benessere,..");
			$("#user_cercaofferte_pi3_tema").attr("value","-1");
		}
	}



}


