var xmlHttpPoi;
var xmlHttpSponsor;
var xmlHttpPoiComment;
var xmlHttpPoiInternal;
var xmlHttpExposition;
var xmlHttpExpositionComment;

var stringSearch;
var timer;

function search(str, id, typesearch, typepoi){
   clearTimeout(timer);
   timer=setTimeout(function validate(){searchTesto(str, id, typesearch, typepoi)},1000);
}

function findPoi(id,limit,letter) {
	document.getElementById("ajaxpoi").innerHTML='<div id="ajaxloading"><img src="/icon/ajax-loader.gif" ></div>';
	xmlHttpPoi=GetXmlHttpObject();
	
	if (xmlHttpPoi===null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 } 
	var searchLetter;
	var url="/?";
	var filter;
	var servizi;
	(document.getElementById("selectfilter")) ? filter = document.getElementById("selectfilter").selectedIndex : filter = 0;
	(document.getElementById("selectservizi")) ? servizi = document.getElementById("selectservizi").options[document.getElementById("selectservizi").selectedIndex].value : servizi = 0;
	url=url+"c=general&m=poicomune_ajax&comuneid="+id+"&filtropoi="+filter+'&servizi='+servizi+'&limit='+limit+'&letter='+letter;
	url=url+"&sid="+Math.random();
	xmlHttpPoi.onreadystatechange=stateChangedPoi ;
	xmlHttpPoi.open("GET",url,true);
	xmlHttpPoi.send(null);
} 

function findExposition() {
	document.getElementById("lista_espositori").innerHTML='<div id="ajaxloading"><img src="/icon/ajax-loader.gif" ></div>';
	xmlHttpExposition=GetXmlHttpObject();
	
	if (xmlHttpPoi===null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 } 
	var eventname = document.getElementById("eventname").value;
	//var url="/?c=general&m=exposition&comuneid="+id_comune+"&filtropoi="+filter+'&servizi='+servizi+'&limit='+limit+'&letter='+letter+"&sid="+Math.random();
	var url="/?c=general&m=exposition&comuneid="+id_comune+"&eventname="+eventname+"&sid="+Math.random();
	xmlHttpExposition.onreadystatechange=stateChangedExposition ;
	xmlHttpExposition.open("GET",url,true);
	xmlHttpExposition.send(null);
	
} 

function stateChangedExposition() { 
	if (xmlHttpExposition.readyState==4 || xmlHttpExposition.readyState=="complete"){ 
		document.getElementById("lista_espositori").innerHTML=xmlHttpExposition.responseText;
	} 
}

function findExpositionInternal(id) {
	document.getElementById("lista_espositori").innerHTML='<div id="ajaxloading"><img src="/icon/ajax-loader.gif" ></div>';
	xmlHttpExposition=GetXmlHttpObject();
	
	if (xmlHttpPoi===null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 } 
	var searchLetter;

	var url="/?c=general&m=expositioninternal&poi_internal="+id+"&sid="+Math.random();
	xmlHttpExposition.onreadystatechange=stateChangedExposition ;
	xmlHttpExposition.open("GET",url,true);
	xmlHttpExposition.send(null);
	
} 

function stateChangedPoi() { 
	if (xmlHttpPoi.readyState==4 || xmlHttpPoi.readyState=="complete"){ 
		document.getElementById("ajaxpoi").innerHTML=xmlHttpPoi.responseText;
	} 
}

function findSponsor(id,limit,letter) {
	document.getElementById("ajaxsponsor").innerHTML='<div id="ajaxloading"><img src="../../icon/ajax-loader.gif" ></div>';
	xmlHttpSponsor=GetXmlHttpObject();
	
	if (xmlHttpSponsor===null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 } 
	
	var url="/?";
	var sponsorfilter = document.getElementById("selectsponsor").selectedIndex;
	var filter = document.getElementById("sponsorfilter").selectedIndex;
	url=url+"c=general&m=sponsor_ajax&comuneid="+id+"&filtro="+filter+'&sponsorfilter='+sponsorfilter+'&limit='+limit+'&letter='+letter;
	url=url+"&sid="+Math.random();
	xmlHttpSponsor.onreadystatechange=stateChangedSponsor ;
	xmlHttpSponsor.open("GET",url,true);
	xmlHttpSponsor.send(null);
} 

function findSponsorInternal(id,limit,letter) {
	document.getElementById("ajaxsponsor").innerHTML='<div id="ajaxloading"><img src="../../icon/ajax-loader.gif" ></div>';
	xmlHttpSponsor=GetXmlHttpObject();
	
	if (xmlHttpSponsor===null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 } 
	
	var url="/?";
	var sponsorfilter = document.getElementById("selectsponsor").selectedIndex;
	var filter = document.getElementById("sponsorfilter").selectedIndex;
	url=url+"c=general&m=sponsor_ajaxinternal&comuneid="+id+"&filtro="+filter+'&sponsorfilter='+sponsorfilter+'&limit='+limit+'&letter='+letter;
	url=url+"&sid="+Math.random();
	xmlHttpSponsor.onreadystatechange=stateChangedSponsor ;
	xmlHttpSponsor.open("GET",url,true);
	xmlHttpSponsor.send(null);
} 

function stateChangedSponsor() { 
	if (xmlHttpSponsor.readyState==4 || xmlHttpSponsor.readyState=="complete"){ 
		document.getElementById("ajaxsponsor").innerHTML=xmlHttpSponsor.responseText;
	} 
}

function findDetailsInternal(id) {
	xmlHttpExpositionComment=GetXmlHttpObject();
	
	if (xmlHttpExpositionComment===null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 } 
	
	var url="/?c=general&m=expositionInternalDetails&internalId="+id;
	xmlHttpExpositionComment.onreadystatechange=stateChangedDetailsInternal;
	xmlHttpExpositionComment.open("GET",url,true);
	xmlHttpExpositionComment.send(null);
} 

function stateChangedDetailsInternal() { 
	if (xmlHttpExpositionComment.readyState==4 || xmlHttpExpositionComment.readyState=="complete"){ 
		document.getElementById("commento").innerHTML=xmlHttpExpositionComment.responseText;
	} 
}

function searchTesto(str, id, typesearch, typepoi){
    if (str.length > 3) {
	    document.getElementById("livesearch").innerHTML = '<img src="../../icon/ajax-loader.gif" />';
		document.getElementById("livesearch").style.visibility = "visible";
	    xmlHttp = GetXmlHttpObject();
    } else {
		document.getElementById("livesearch").innerHTML = "";
        document.getElementById("livesearch").style.border = "0px";
		document.getElementById("livesearch").style.visibility = "hidden"; // 16/03/2010 con 3 caratteri o meno > box livesearch hidden
        return;
	}
	
    document.getElementById("livesearch").innerHTML = '<img src="../../icon/ajax-loader.gif" />';
    xmlHttp = GetXmlHttpObject()
    
    if (xmlHttp === null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
	var url="/?";
    url = url + "q=" + str + "&c=general&m=ajax_search&dove=" + id+"&typesearch="+typesearch+"&typepoi="+typepoi;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	stringSearch = str;
}

function stateChanged(){
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById("livesearch").innerHTML = xmlHttp.responseText;
        document.getElementById("livesearch").style.border = "1px solid #A5ACB2";
		var myregexp = new RegExp("\\d{1,3}");
		if (xmlHttp.responseText.match(myregexp) != null) zoomOutSearchPoi(stringSearch);
		if (g_playerIsOpen) playerClose();
    }
}

function closeSearch(){
    document.getElementById("livesearch").innerHTML = "";
    document.getElementById("livesearch").style.border = "0px";
	document.getElementById("livesearch").style.visibility = "hidden";
	drawCustomBreak = false;
	clearPOICustom();
	drawCustomPoi();
    return;
}

function searchTestoBusiness(str, id, typesearch, typepoi){
    if (str.length > 3) {
	    document.getElementById("livesearchbusiness").innerHTML = '<img src="../../icon/loading.gif" >';
	    xmlHttp = GetXmlHttpObject()
    } else{
		document.getElementById("livesearchbusiness").innerHTML = "";
        document.getElementById("livesearchbusiness").style.border = "0px";
        return;
	}
	
    document.getElementById("livesearchbusiness").innerHTML = '<img src="../../icon/loading.gif" >';
    xmlHttp = GetXmlHttpObject()
    
    if (xmlHttp === null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
	var url="/?";
    url = url + "q=" + str + "&c=general&m=ajax_search&dove=" + id+"&typesearch="+typesearch+"&typepoi="+typepoi;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChangedBusiness;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function stateChangedBusiness(){
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById("livesearchbusiness").innerHTML = '<div class="close" align="right"><img src="../../icon/button/close.png" onclick="closeSearchBusiness();" ></div>' + xmlHttp.responseText;
        document.getElementById("livesearchbusiness").style.border = "1px solid #A5ACB2";
        
    }
}

function closeSearchBusiness(){
    document.getElementById("livesearchbusiness").innerHTML = "";
    document.getElementById("livesearchbusiness").style.border = "0px";
    return;
}

function getInternalView(id, limit, client){

	$.get("/?c=general&m=internalView", { poi: id, limit: limit },
	   function(data){
	   	 if (client == 'false') {
		 	// clienti paganti con moduli prodotti
				$("#poi_indoor_text_row").html('<div class="poi_indoor_cards_client" id="poi_indoor_cards" ></div><div class="poi_indoor_text_client" id="poi_indoor_text">' + label_indoor_istruzioni + '</div><div class="wrapper_poi_indoor_prodotti_clienti" id="wrapper_poi_indoor_prodotti_clienti"></div>');
				$("#wrapper_poi_indoor_prodotti_clienti").load("/general/returnMultimedia/"+id,function(){
							$(document).ready(function(){
								$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
							});

				});			
		 } else {
				// musei - casiṇ
				$("#poi_indoor_text_row").html('<div class="poi_indoor_cards" id="poi_indoor_cards" ></div><div class="poi_indoor_text" id="poi_indoor_text">' + label_indoor_istruzioni + '</div>');
		}
		
		 $("#poi_indoor_list_col").html(data);
		 /*
		 $.get("/general/returnCard/"+id, function (data) {
		 	$('#poi_indoor_cards').html(data);
			if (client == 'true') {
				//alert('adesso carico');
				var movlocal = thisMovie("banner");
				movlocal.loadXML('/general/getSponsor');		
			}				
		 });
		 */
		 $("#poi_indoor_cards").load("/general/returnCard/"+id);
		 // imposto l'altezza del wrapper lasciando spazio per il DIV con logo che viene caricato in append con la funzione returnLogoSmall
		 $("#wrapper_poi_indoor_list").height(mapHeightGlobal-100);	
	});	
} 

function getInternalViewComment(id) {
	$("#poi_indoor_text").load("/?c=general&m=internalViewComment&poi="+id);		
} 

function resetInternalView()	{
	document.getElementById("poi_indoor_text_row").innerHTML= 	'';
	document.getElementById("poi_indoor_list_col").innerHTML=	'';
	$("#poi_indoor_text_row").hide('fast');	
	$('#poi_indoor_list_col').hide();	
	mapInternalView = false;
	var elem = document.getElementById("poi_indoor_list_col");
	elem.style.height = (tableHeight+119)+'px';
}

function GetXmlHttpObject(){
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}