function setCenterInterval() {
    if (!running) {
        setCenter();
        running = setInterval('setCenter()', 500);
    } else {
        clearInterval(running);
        running = false;
    }    
}

function resetElements(evt) {
    if ((evt.type == "keyup" && evt.keyCode == 13) || (evt.type != "keyup")) {
        var i;
        for (i=0; i < this.elements.length; i++) {
            var element = this.elements[i];
            element.reset();
        }
        controlRouteDiv(false);
    }
}

function resetSearch() {
    searchFloatingPanel.reset();
}

function loadCompleteRouting() {	
    map.setCenter(map.getCenter(),map.getZoom()-1);
    dirs.showManovreMarkers(false);
}

function loadComplete() {
    document.getElementById('addrInput').value=document.getElementById('searchInput').value;  
    showBottomDiv();
    controlRouteDiv(false);
    dirs.showManovreMarkers(false);
    //toggleSideBar('close');
}

function loadFailure() {
    showBottomDiv();
    controlRouteDiv(false);
    toggleSideBar('open');
}

function find() {
	reset_misure();
	document.getElementById('risultati').innerHTML='&nbsp;';   
    var optionSearch = document.getElementById('optionsSearch').value;
	
	if (optionSearch==1) {
	   document.getElementById('addrSel').value='';	
	   var address = document.getElementById('searchInput').value;
	   address=translate(address);
	   if ((address != '') && (address != 'ad es. via Roma, Cagliari') && (address != 'ad es. Losa') && (address != 'ad es. Albergo, Cagliari')) {
	        addr.load(address);
	   }     	   
	} else	if (optionSearch==2) {		
		 var address = document.getElementById('searchInput').value;
		  if ((address != '') && (address != 'ad es. via Roma, Cagliari') && (address != 'ad es. Losa') && (address != 'ad es. Albergo, Cagliari')) {
		 	gazetteerSearch(1);
		 }
		
	} else if (optionSearch==3) {
		 var address = document.getElementById('searchInput').value;
		  if ((address != '') && (address != 'ad es. via Roma, Cagliari') && (address != 'ad es. Losa') && (address != 'ad es. Albergo, Cagliari')) {
		 	poiSearch(1);
		 }		
	} 
}

function route() {
	reset_misure();
	dirs.reset();
	document.getElementById('startSel').value='';   
    document.getElementById('endSel').value='';   
   	document.getElementById('partenza').value='';   
    document.getElementById('arrivo').value='';  
    document.getElementById('viaPoint1Sel').value='';   
    document.getElementById('viaPoint2Sel').value=''; 
    document.getElementById('viaPoint1').value='';   
    document.getElementById('viaPoint2').value=''; 
      
	
    document.getElementById('risultati').innerHTML='&nbsp;';
    var options = document.getElementById('type').options;
    var selIndex = document.getElementById('type').options.selectedIndex;
    var type =options[selIndex].text;

    var start=document.getElementById('start').value;
    start=translate(start);
    var viaPoint1='';
    if (document.getElementById('viaPoint1') != null)
    {
        viaPoint1=document.getElementById('viaPoint1').value;
        viaPoint1=translate(viaPoint1);
    }
        
    var viaPoint2='';
    if (document.getElementById('viaPoint2') != null)
    {
        viaPoint2=document.getElementById('viaPoint2').value;
        viaPoint2=translate(viaPoint2);
    }
    var end=document.getElementById('end').value;
    end=translate(end);
    
    if (viaPoint1 == '') {viaPoint1 = viaPoint2;viaPoint2 = '';}
    // check sui parametri passati al route
    // alert(start+'\n'+viaPoint1+'\n'+viaPoint2+'\n'+end);

    if ((start != 'ad es. via Roma, Cagliari') && (end != 'ad es. via Roma, Cagliari') && (start != 'ad es. via Milano, Sassari') && (end != 'ad es. via Milano, Sassari') && (start != '') && (end != '')) {
        if (viaPoint1 != '') {
            if (viaPoint2 != '') dirs.load(start,viaPoint1,viaPoint2,end,type);
            else dirs.load(start,viaPoint1,end,type);}
        else dirs.load(start,end,type);

        dirs.display(true);
//        dirs.markersLayer.setVisibility(true);
        toggleSideBar('open');
    }
    
    document.getElementById('partenza').value=document.getElementById('start').value;   
    document.getElementById('arrivo').value=document.getElementById('end').value;   
    
}

function routeXSL() {
	reset_misure();
	dirs.reset();
	document.getElementById('partenza').value='';   
    document.getElementById('arrivo').value='';  
    var a = document.getElementById('start').value;
    var b = document.getElementById('end').value;
    var v1 = document.getElementById('viaPoint1').value;
    var v2 = document.getElementById('viaPoint2').value;

    if (document.getElementById('startSel').value!='') {       
       document.getElementById('start').value= document.getElementById('startSel').value;   
    }
    if (document.getElementById('endSel').value!='') {       
       document.getElementById('end').value= document.getElementById('endSel').value;   
    }   
    if (document.getElementById('viaPoint1Sel').value!='') {       
       document.getElementById('viaPoint1').value= document.getElementById('viaPoint1Sel').value;   
    }
    if (document.getElementById('viaPoint2Sel').value!='') {       
       document.getElementById('viaPoint2').value= document.getElementById('viaPoint2Sel').value;   
    }

      document.getElementById('risultati').innerHTML='&nbsp;';
    var options = document.getElementById('type').options;
    var selIndex = document.getElementById('type').options.selectedIndex;
    var type =options[selIndex].text;

    var start=document.getElementById('start').value;
    start=translate(start);
    var viaPoint1='';
    if (document.getElementById('viaPoint1') != null)
    {
        viaPoint1=document.getElementById('viaPoint1').value;
        viaPoint1=translate(viaPoint1);
    }
        
    var viaPoint2='';
    if (document.getElementById('viaPoint2') != null)
    {
        viaPoint2=document.getElementById('viaPoint2').value;
        viaPoint2=translate(viaPoint2);
    }
    var end=document.getElementById('end').value;
    end=translate(end);
    
    if (viaPoint1 == '') {viaPoint1 = viaPoint2;viaPoint2 = '';}
    // check sui parametri passati al route
    // alert(start+'\n'+viaPoint1+'\n'+viaPoint2+'\n'+end);

    if ((start != 'ad es. via Roma, Cagliari') && (end != 'ad es. via Roma, Cagliari') && (start != 'ad es. via Milano, Sassari') && (end != 'ad es. via Milano, Sassari') && (start != '') && (end != '')) {
        if (viaPoint1 != '') {
            if (viaPoint2 != '') dirs.load(start,viaPoint1,viaPoint2,end,type);
            else dirs.load(start,viaPoint1,end,type);}
        else dirs.load(start,end,type);

        dirs.display(true);
//        dirs.markersLayer.setVisibility(true);
        toggleSideBar('open');
    }
    
    document.getElementById('start').value=a;   
    document.getElementById('end').value=b;   
    document.getElementById('viaPoint1').value=v1;   
    document.getElementById('viaPoint2').value=v2;     
    document.getElementById('partenza').value=document.getElementById('start').value;   
    document.getElementById('arrivo').value=document.getElementById('end').value;   
}



function translate(html)
{
	
    html = replaceAll(html,document.getElementById('caratterea').value,"%E0");
    html = replaceAll(html,document.getElementById('caratteree').value,"%E8");
    html = replaceAll(html,document.getElementById('caratterei').value,"%EC");
    html = replaceAll(html,document.getElementById('carattereo').value,"%F2");
    html = replaceAll(html,document.getElementById('carattereu').value,"%F9");
    return html;
}

function replaceAll(OldString,FindString,ReplaceString) {
   var SearchIndex = 0;
   var NewString = ""; 
   while (OldString.indexOf(FindString,SearchIndex) != -1)    {
     NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
     NewString += ReplaceString;
     SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);         
  }
   NewString += OldString.substring(SearchIndex,OldString.length);
   return NewString;
}


/**
*/
function useResponse(div,img) {
    if (document.getElementById(div).style.display == 'none')
    {
        document.getElementById(img).src = OpenLayers.ImgPath + 'routing/open.gif';
        document.getElementById(div).style.display = 'block';
    } else {
        document.getElementById(img).src = OpenLayers.ImgPath + 'routing/closed.gif';
        document.getElementById(div).style.display = 'none';
    }
}

/*  ***********************************
***********************************  */
function correctZoombarPosition()
{
    stato=document.getElementById('toggle').value;
    if (stato=='open')  {
        zoombar.div.style.left='14px';
    } else if (stato=='close') {
//        zoombar.div.style.left='14px';
        zoombar.div.style.left='310px';
    }
    
}



/*  ***********************************
***********************************  */
function correctPannelloPosition()
{
    stato=document.getElementById('toggle').value;
    if (stato=='open')  {
    	infoPanel.div.style.left='45px';
    } else if (stato=='close') {
//    	infoPanel.div.style.left='45px';
    	infoPanel.div.style.left='341px';
    }
    
}



/*  ***********************************
***********************************  */
function reverseDirection() {

    var Loc = document.getElementById('start').value;
    document.getElementById('start').value=document.getElementById('end').value;
    document.getElementById('end').value=Loc;
    Loc=document.getElementById('viaPoint1').value;
    document.getElementById('viaPoint1').value=document.getElementById('viaPoint2').value;
    document.getElementById('viaPoint2').value=Loc;
    
    var Loc = document.getElementById('startSel').value;
    document.getElementById('startSel').value=document.getElementById('endSel').value;
    document.getElementById('endSel').value=Loc;
    Loc=document.getElementById('viaPoint1Sel').value;
    document.getElementById('viaPoint1Sel').value=document.getElementById('viaPoint2Sel').value;
    document.getElementById('viaPoint2Sel').value=Loc;  
    nero();
    routeXSL();
};

/*  ***********************************
***********************************  */
function toggleSideBar(stato) {
    if (stato != 'close' && stato != 'open') 
        stato=document.getElementById('toggle').value;
 
     if (stato=='open') {    
        // imposto il parametro di stato per la barra, visualizzo il div di output e posiziono mappa e bottone
        document.getElementById('toggle').value='close';
        document.getElementById('output').style.display='block';
        document.getElementById('button').className='closed_button';
        //document.getElementById('map').style.marginLeft='300px';
        zoombar.div.style.left='310px';
        scaleline.div.style.left='420px';
        scale.div.style.left='320px';
        //mouseposition.div.style.left='320px';
        panzoombutton.div.style.left='341px';
        
        document.getElementById('clicker').src='img/chiudi.gif';
  	    document.getElementById('selectsearch').style.left='0px';  
 	    document.getElementById('selectsearch2').style.left='0px';  
		
		
     } else if (stato=='close') {
     	document.getElementById('risultatiricerca').style.backgroundColor='#FDDA94';
     	 if (document.getElementById('tematismi75')!=null){
		document.getElementById('tematismi75').style.backgroundColor='#FDDA94';
     	 }
   	    document.getElementById('distanzearee').style.backgroundColor='#FDDA94';
   	    document.getElementById('contenitore_risultati').style.display='none';
   	    document.getElementById('contenitore_risultati').style.visibility='hidden';
   	    document.getElementById('contenutidisponibili').style.display='none';
   	    document.getElementById('contenutidisponibili').style.visibility='hidden'; 	
		document.getElementById('contenitore_tematismi').style.display='none';
   	    document.getElementById('contenitore_tematismi').style.visibility='hidden';  
   	  //  document.getElementById('selectsearch').style.position='absloute';
   	  //  console.log(document.getElementById('socialid'));  
   	    //reset_misure();
        // imposto il parametro di stato per la barra, visualizzo il div di output e posiziono mappa e bottone
        document.getElementById('toggle').value='open';
        document.getElementById('output').style.display='none';
        document.getElementById('button').className='opened_button';
        //document.getElementById('map').style.marginLeft='0px';
        zoombar.div.style.left='14px';
        scaleline.div.style.left='124px';
        scale.div.style.left='24px';
        //mouseposition.div.style.left='24px';
        panzoombutton.div.style.left='45px';
        document.getElementById('clicker').src='img/apri.gif';    
  	    document.getElementById('selectsearch').style.left='-300px';  
 	    document.getElementById('selectsearch2').style.left='-300px';  

       
     }
     map.setCenter(map.center);
  
     document.getElementById('button').className = document.getElementById('button').className + ' btnclick';
     var layerTocActive = "";
     
     //verifico se almeno un layer è attivo nella toc:
     for (var i = 0; i < map.layers.length; i++) {
	        var layer = map.layers[i];
	        
	        if (layer.CLASS_NAME == 'OpenLayers.Layer.WMS' || (layer.CLASS_NAME == 'Roja.Layer.WMS' && layer.layers == null)) {
	        	if (layer.physicalName && layer.physicalName != '' && layer.visibility == true) {
           			var layerTocActive = "attivo";
           			break;
	        	}
	        }
	        else {
	        		if (layer.layers && layer.layers!=null && layer.layers.length>0) {
	        			for (var k = 0; k < layer.layers.length; k++) {
	        				var wmsLayer = layer.layers[k];
	        				if (wmsLayer.CLASS_NAME == 'Roja.WMSLayer' && wmsLayer.visibility == true) {
	        				 	var layerTocActive = "attivo";
	        		            break;
	        	            }
	        			}
	        		}
	        }
	    }
	    
    //verifico quale label attivare allo spostamento della sidebar 
    if ((document.getElementById('risultati').innerHTML!='') && (document.getElementById('risultati').innerHTML.toLowerCase()!='<div xmlns="http://www.w3.org/1999/xhtml" class="rojaaddress"></div>') && (document.getElementById('risultati').innerHTML.toLowerCase()!='<div class=rojaaddress xmlns="http://www.w3.org/1999/xhtml"></div>')) {
       toggleSideBarOutput('risultatiricerca');
     }
    else if (layerTocActive == "attivo")   
     {
     	 toggleSideBarOutput('tematismi');
     }
    else {
     	 toggleSideBarOutput('contenutidisponibili');
     }
//	sposto il bottone Info	########################################	
			correctPannelloPosition();
//	fine bottone Info 		########################################	
         
}


function toggleSideBarOutput(id) {   

   if (id=='risultatiricerca') {  	      
   	   document.getElementById('risultatiricerca').style.backgroundColor='#ffcc66';
	   if (document.getElementById('tematismi75')!=null){
	   document.getElementById('tematismi75').style.backgroundColor='#FDDA94';
	   }
   	   document.getElementById('distanzearee').style.backgroundColor='#FDDA94';
   	   document.getElementById('contenitore_risultati').style.display='block';
   	   document.getElementById('contenitore_risultati').style.visibility='visible';
   	   document.getElementById('contenutidisponibili').style.display='none';
   	   document.getElementById('contenutidisponibili').style.visibility='hidden';  
	   document.getElementById('contenitore_tematismi').style.display='none';
   	   document.getElementById('contenitore_tematismi').style.visibility='hidden';  	   
   	   reset_misure();
   } else if(id=='tematismi'){
   /* disattivo tutti i blocchi e valorizzo quello dei tematismi e la legenda */
   document.getElementById('risultatiricerca').style.backgroundColor='#FDDA94';
    if (document.getElementById('tematismi75')!=null){
		document.getElementById('tematismi75').style.backgroundColor='#ffcc66';
    }
   	   document.getElementById('distanzearee').style.backgroundColor='#FDDA94';
   	   document.getElementById('contenitore_risultati').style.display='none';
   	   document.getElementById('contenitore_risultati').style.visibility='hidden';
   	   document.getElementById('contenutidisponibili').style.display='none';
   	   document.getElementById('contenutidisponibili').style.visibility='hidden';
	   document.getElementById('contenitore_tematismi').style.display='block';
   	   document.getElementById('contenitore_tematismi').style.visibility='visible';
   	   return true; 	  
   }
   else {
   	   document.getElementById('risultatiricerca').style.backgroundColor='#FDDA94';
   	    if (document.getElementById('tematismi75')!=null){
	   document.getElementById('tematismi75').style.backgroundColor='#FDDA94';
   	    }
   	   document.getElementById('distanzearee').style.backgroundColor='#ffcc66';
   	   document.getElementById('contenitore_risultati').style.display='none';
   	   document.getElementById('contenitore_risultati').style.visibility='hidden';
   	   document.getElementById('contenutidisponibili').style.display='block';
   	   document.getElementById('contenutidisponibili').style.visibility='visible'; 	  
   	    document.getElementById('contenitore_tematismi').style.display='none';
   	   document.getElementById('contenitore_tematismi').style.visibility='hidden';  
   }
}
/*  ***********************************
***********************************  */
function setSize() {
    var myHeight = 0;
    if( typeof( window.innerHeight) == 'number' ) {
        //Non-IE
        myHeight = window.innerHeight;
    } else if( document.documentElement && document.documentElement.clientHeight )  {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && document.body.clientHeight )  {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    var url = decodeURI(location.search);
	url = decodeParameters(url);
	var pos = url.indexOf('small=');	
	var small = '';
	if (pos!=-1) {
		small = url.substring(pos+6);
		pos = small.indexOf('&');
		if (pos!=-1) {
			small = small.substring(0,pos);
		}	 
	}
	if (small!='' && small ==1)
	{
		  myHeight = myHeight;
	} else {
		  myHeight = myHeight-88;
	}  
    document.getElementById('output').style.height=myHeight + 'px';
    document.getElementById('button').style.height=myHeight + 'px';
    document.getElementById('map').style.height=myHeight + 'px';
    //toggleSideBar();
    //map.setCenter(map.center);
}

/*  ***********************************
***********************************  */
function nero() {
    var val=document.getElementById('searchInput').value;
    if (val!='ad es. via Roma, Cagliari') document.getElementById('searchInput').style.color='black'
    else document.getElementById('searchInput').style.color='#999999';

    val=document.getElementById('start').value;
    if (val!='ad es. via Roma, Cagliari') document.getElementById('start').style.color='black'
    else document.getElementById('start').style.color='#999999';

    val=document.getElementById('end').value;
    if (val!='ad es. via Milano, Sassari') document.getElementById('end').style.color='black'
    else document.getElementById('end').style.color='#999999';    
   
}

/*  ***********************************
***********************************  */
function reset_init(cosa) {
	objects = null;
	addr.geocodedAddr=null;
	dirs.reset();
		  
    if (cosa=='route') {
    	
       	if (document.getElementById('searchInput').value!='ad es. via Roma, Cagliari' && document.getElementById('searchInput').value!='ad es. Losa'  && document.getElementById('searchInput').value!='ad es. Albergo, Cagliari' && document.getElementById('searchInput').value!='' ) {
    		 document.getElementById('start').value=document.getElementById('searchInput').value;
    	} else {
    		 document.getElementById('start').value='ad es. via Roma, Cagliari';
    	}
        document.getElementById('viaPoint1').value='';
        document.getElementById('viaPoint2').value='';
        if (document.getElementById('end').value!='ad es. via Milano, Sassari' && document.getElementById('end').value!='') {
       		document.getElementById('end').value=document.getElementById('end').value;
        }	
        else {
        	document.getElementById('end').value='ad es. via Milano, Sassari';
        }	
        document.getElementById('indicazioni').className='todo_bar pos';
        document.getElementById('cerca').className='outscreen';
        document.getElementById('start').focus();
	    document.getElementById('start').select();
	    
    }
    if (cosa=='address') {
        if (document.getElementById('start').value!='ad es. via Roma, Cagliari' && document.getElementById('start').value!='') {
       		document.getElementById('searchInput').value=document.getElementById('start').value;
        }	
        else {
        	document.getElementById('searchInput').value='ad es. via Roma, Cagliari';
        }	
        document.getElementById('indicazioni').className='outscreen';
        document.getElementById('cerca').className='todo_bar pos';
        document.getElementById('searchInput').focus();
	    document.getElementById('searchInput').select();
	    document.getElementById('optionsSearch').value = '1';
	    
    }
  
    document.getElementById('risultati').innerHTML='';
    nero();
}

function reset_misure() {
	 
    //disattivo i controlli per le misure  
 	var radios = document.getElementsByName('misure');
    for (i=0;i<radios.length;i++) {
       	var radio = radios[i];
       	radio.checked=false;   	       	
    }  
    
    var span = document.getElementById('line');
    if (span!=null) {
    	span.className = 'spandistanzearee';
    }
    span = document.getElementById('polygon');
    if (span!=null) {
    	span.className = 'spandistanzearee';
    }
    span = document.getElementById('coord');
    if (span!=null) {
    	span.className = 'spandistanzearee';
    }
    
    var span = document.getElementById(this.value);    
   
    for(key in measureControls) {
        var control = measureControls[key];
        control.deactivate();	        
    }
    if (document.getElementById('unitamisuraarea')!=null) {
    	document.getElementById('unitamisuraarea').value = 'm2';
    }        
    if (document.getElementById('unitamisuraperimetro')!=null) {
       	document.getElementById('unitamisuraperimetro').value = 'm';
    }        
     if (document.getElementById('unitamisura')!=null) {
    	document.getElementById('unitamisura').value = 'm';
    }
    
    distanze = {
	   	'm':'',
	   	'km':'',
	   	'mi':'',
	  	'nm':''
	};
	   
	aree = {
	   	'm2':'',
	   	'km2':'',
	   	'et':''
	};
	   
	perimetri = {
	   	'm':'',
	   	'km':''
	}; 
    
    //nascondo i div delle misure delle distanze e delle aree         
    var distanza = document.getElementById('misuredistanza');
    var area = document.getElementById('misurearea');
    var coord = document.getElementById('misurecoord');

    if (distanza!=null && area!=null && coord!=null) {    	 
	 	distanza.style.backgroundColor = 'white' ;
	 	area.style.backgroundColor = 'white' ;    	
	 	coord.style.backgroundColor = 'white' ;    	
	}
	
	var resultDistanza = document.getElementById('measuredistanza');
 	if (resultDistanza!=null)
 	{
 		 resultDistanza.style.display = 'none';
 	     resultDistanza.style.visibility = 'hidden';
 	}    	 	
 	
 	var resultArea = document.getElementById('measurearea');
 	
 	if (resultArea!=null)
 	{
 		resultArea.style.display = 'none';
 		resultArea.style.visibility = 'hidden';
 	} 	
    
     /* coord giamba */
	var resultCoord = document.getElementById('measurecoord');
 	if (resultCoord!=null)
 	{
 		 resultCoord.style.display = 'none';
 	     resultCoord.style.visibility = 'hidden';
   	 	 coordinateButton.deactivate();
	}    	 	


   //svuoto i campi input dei risultati per le distanze, le aree e i perimetri   
    if (document.getElementById('resultMeasure')!=null) {
    	document.getElementById('resultMeasure').value = '';  
    	//questa istruzione serve a spostare il focus fuori dal div delle misure
    	// senza questa istruzione il focus non viene inserito correttamente sui campi input 
    	// della ricerca in mappa  
    	document.getElementById('contenutidisponibili').blur();         	
    }       
    
    var element = document.getElementById('resultMeasurePerimetro');
    if (element!=null) {
    	element.value = '';    	
    }
    var element = document.getElementById('resultMeasureArea');
    if (element!=null) {
    	element.value = '';    	
    }
    
    //svuoto i risultati delle coordinate
    var element = document.getElementById('resultCoord');
    if (element!=null) {
    	element.value = '';    	
    }    
    var element = document.getElementById('resultLink');
    if (element!=null) {
    	element.value = '';    	
    }    
    
    //il poligono resta la figura accesa per default per misurare le aree 
    if (document.getElementById('cerchio')!=null) {
	    var img = document.getElementById('cerchio');	    
		img.src = OpenLayers.ImgPath + 'toolbar/external/cir_off.png';
    }
    if (document.getElementById('poligono')!=null) {
		var img = document.getElementById('poligono');
		img.src = OpenLayers.ImgPath + 'toolbar/external/pol_on.png';
    }
    if (document.getElementById('rettangolo')!=null) {
		var img = document.getElementById('rettangolo');
		img.src = OpenLayers.ImgPath + 'toolbar/external/rect_off.png';
    } 
}
/*  ***********************************
***********************************  */
function switchIndicazioniStradali(trigger) {
    if (trigger>1) {
        trigger = 'aggiungi' + trigger;
        document.getElementById(trigger).style.display='block';
        document.getElementById('addTappa').style.display='none';
        // nasconde le indicazioni stradali
        document.getElementById('percorso1').style.display='none';
        if (document.getElementById('percorso2')) document.getElementById('percorso2').style.display='none';
        if (document.getElementById('percorso3')) document.getElementById('percorso3').style.display='none';
        document.getElementById('switchIS').src='img/plus.gif';
    } else {
        if (document.getElementById('percorso1').style.display=='none') {
            // visualizza le indicazioni stradali
            document.getElementById('percorso1').style.display='block';
            if (document.getElementById('percorso2')) document.getElementById('percorso2').style.display='block';
            if (document.getElementById('percorso3')) document.getElementById('percorso3').style.display='block';
            document.getElementById('switchIS').src='img/minus.gif';
        } else {
            // nasconde le indicazioni stradali
            document.getElementById('percorso1').style.display='none';
            if (document.getElementById('percorso2')) document.getElementById('percorso2').style.display='none';
            if (document.getElementById('percorso3')) document.getElementById('percorso3').style.display='none';
            document.getElementById('switchIS').src='img/plus.gif';
        }
    }
}
/*  ***********************************
***********************************  */
function inviaRoute(evt){
    if (evt.type == "keyup" && evt.keyCode == 13) {
            route();
    }
}
function inviaRouteXSL(evt){
    if (evt.type == "keyup" && evt.keyCode == 13) {
            routeXSL();
    }
}
function inviaAddress(evt){
    if (evt.type == "keyup" && evt.keyCode == 13) {
            find();
    }
}


/*  ***********************************
***********************************  */
function activateKeyboardHandler()
{
    keyboardHandler.activate();
}

function deactivateKeyboardHandler()
{
    keyboardHandler.deactivate();
}
function coloraIstruzione(nr){
    sbiancaIstruzione();
    document.getElementById('istruzione'+nr).style.backgroundColor='#FFCC66';
    document.getElementById('istruzione'+nr).style.border='1px solid #F29E00';
};
function sbiancaIstruzione(){
    var i=2;
    while (document.getElementById('istruzione'+i)!= null) {
        document.getElementById('istruzione'+i).style.backgroundColor='#ffffff';
        document.getElementById('istruzione'+i).style.border='0px solid #ffffff';
        i++;
    }
};

/* *************************************
 ************************************ */
function SwitchLayer(layerName, backgroundColor) {
    
    map.setBaseLayer(map.getLayerByName(layerName));
    correctZoombarPosition();  
	

//	sposto il bottone Info	########################################	
	
			correctPannelloPosition();
		
//	fine bottone Info 		########################################	
	
//	console.log(map.baseLayer); 


    if (map.baseLayer.backgroundColor != null)
    {
        map.div.style.backgroundColor = map.baseLayer.backgroundColor;
    }
    else if (backgroundColor != null) 
    {
        map.div.style.backgroundColor = backgroundColor;
    }
    /*if (layerSwitcher.overlay)
    {
        layerSwitcher.overlay.setVisibility(false);
    }*/
    if (map.baseLayer.overlay && layerSwitcher)
    {
        layerSwitcher.overlay = map.getLayerByName(map.baseLayer.overlay);
        layerSwitcher.overlay.setVisibility(true);
    }
    
}

function stampa() {
	var olon = new Roja.Format.OLON();	
	olonSaveStrMap = olon.write(map, true);  
	if (printWin!=null)
	{
		printWin.close();
	}	         
   printWin = window.open('print.html','Anteprima_di_stampa');
   printWin.focus();	
} 

function toggleSearch() {
	var inputText = document.getElementById('searchInput');
	inputText.className = 'lungo text'
	var optionSearch = document.getElementById('optionsSearch').value;
	document.getElementById('risultati').innerHTML='';
	objects = null;
	addr.geocodedAddr=null;
	dirs.reset();
    if (optionSearch==1) {
		clearObjectMarkers();
		addr.reset();
       	if (inputText.value=='ad es. Losa'  || inputText.value=='ad es. Albergo, Cagliari' || document.getElementById('searchInput').value=='' ) {
  		     inputText.value = 'ad es. via Roma, Cagliari';
		     inputText.style.color = '#999999';
    	} 
	} else	if (optionSearch==2) {		
		clearObjectMarkers();
		addr.reset();
       	if (inputText.value=='ad es. via Roma, Cagliari'  || inputText.value=='ad es. Albergo, Cagliari' || document.getElementById('searchInput').value=='' ) {
			inputText.value = 'ad es. Losa';
			inputText.style.color = '#999999';
       	}
	} else if (optionSearch==3) {
		clearObjectMarkers();
		addr.reset();
       	if (inputText.value=='ad es. via Roma, Cagliari'  || inputText.value=='ad es. Losa' || document.getElementById('searchInput').value=='' ) {
			inputText.value = 'ad es. Albergo, Cagliari';
			inputText.style.color = '#999999';
       	}
	}	
}
/**
 */

function loadComuni() {
	var urlComunas= '';
	urlComunas= serviceUrl + '/www.comunas.it/xml/comuni_url.php';  
	//alert('urlComunas2: ' + urlComunas);
	try {	
		if (redirect && redirect!='' && redirect!=null) {
			  urlComunas= redirect + '/www.comunas.it/xml/comuni_url.php';  
	
		}
	}	
	catch(e)
    {
     
    }
	
   var url = urlComunas;	
   
   if (Roja.Proxy && urlComunas.startsWith("http")) {
		url = Roja.Proxy + Roja.Util.escapeUri(urlComunas);
	}
/* ************************************************ aggiungo per test in locale: ********************* */
	else{
	url='http://www.comunas.it/xml/comuni_url.php';
	}
/* *************************************************************************************************** */
	new OpenLayers.Ajax.Request(url,
	                         {   method: 'get',
	                             parameters: null,
	                             onComplete: successComuni,
	                             onFailure: failureComuni
	                          }
	 );  
}

/**
 */
function successComuni(request)
{   	    
	    if (request.responseXML == null){
	        return;
	    }
	    var xmlResponse = request.responseXML;
	    if (!xmlResponse || request.fileType!="XML") { //for IE compatibility
	        xmlResponse = OpenLayers.parseXMLString(request.responseText);       
	    }	    	      
	    var comuniNL = xmlResponse.getElementsByTagName('localita');
		for (i=0; i<comuniNL.length; i++)
		{			
			var comune = comuniNL[i];
			 
			var nomeNodo = comune.getElementsByTagName('nome');
			var urlNodo = comune.getElementsByTagName('url');
			
			var nome = '';
			var url = '';
			
			if (nomeNodo!=null && urlNodo!=null ) {
				nome = nomeNodo[0].firstChild.nodeValue;
				url = urlNodo[0].firstChild.nodeValue;				
			}
			
			if (nome!=null && url!=null && nome!='' && url!='') {
				hashTableComuni.put(nome.toUpperCase(),url);	
			}					
		}	    
}

/**
 */
function failureComuni(request)
{   	    
	   alert('Impossibile caricare il file dei comuni');
}

/**
 */
function clearObjectMarkers() {

	// ciclo i controlli della mappa per trovare infoPanel
	/*for(var i=0, len=map.controls.length; i<len; i++) {
                if (map.controls[i].id == "infoPanel"){
                	for(var j=0, len=map.controls[i].controls.length; j<len; j++) {
                		 if (map.controls[i].controls[j].id == "info"){
                		 	map.controls[i].controls[j].closeBaloon();
                		 	break;
                		 }//chiude if2 
                	
                	}//chiude for2
                break;
                }//chiude if1
            }//chiude for1*/
            
            
   var info = map.getControlsBy('id','info')[0];
   
   if (info!=null){
   info.closeBaloon();
   }
   
   var markerLayer = map.getLayerByName("objectMarkers");
   if(markerLayer != null) {
	  if (markerLayer.markers != null) {
            while(markerLayer.markers.length > 0) {
            	var marker = markerLayer.markers[0];
            	markerLayer.removeMarker(marker);
            	marker.destroy();
            }
        }
   }	   
}	

/**
 */
function openPopup(evt) {
	 var markerLayer = map.getLayerByName("objectMarkers");
	 if (markerLayer!=null && markerLayer.markers!=null)
	 {
	 	for (i=0;i< markerLayer.markers.length; i++) {
	 		markerLayer.markers[i].display(true);
	 		markerLayer.markers[i].popup.hide();
	 	}
	 }
	 
	if (this !=null) {
		this.display(true);	
		this.popup.show();	
	}
	
	if (map.layerMinZoomLevel != null && map.getZoom()>0)
  	{
    	//This x-y values is used to show div in best place
    	var extent = map.getExtent();

		var  mapcenter= map.getCenter();
		var  x= mapcenter.lon;
		var  y= mapcenter.lat;
		
		var maxBounds = map.getMaxExtent();  

		if ((this.lonlat.lon + this.popup.size.w/2 * map.getResolution()) > extent.right)
		{
			x = x + ((this.lonlat.lon + this.popup.size.w/2 * map.getResolution()) - extent.right);
			if (x>maxBounds.right) {
				x = maxBounds.right;
			}
		}
		if ((extent.left > this.lonlat.lon - this.popup.size.w/2 * map.getResolution()))
		{
			x = x - ((extent.left - this.lonlat.lon + this.popup.size.w/2 * map.getResolution()));
			if (x<maxBounds.left) {
				x = maxBounds.left;
			}
		}
		if ((this.lonlat.lat + this.popup.size.h * map.getResolution() + this.icon.size.h * map.getResolution()) > extent.top)
		{
			y = y + ((this.lonlat.lat + this.popup.size.h * map.getResolution() + this.icon.size.h * map.getResolution()) - extent.top);
			if (y>maxBounds.top) {
				y = maxBounds.top;
			}
		}

		map.setCenter(new OpenLayers.LonLat(x, y), map.getZoom());
  	}
	
}

/**
 */
function openMarker(evt) {
	
	 var markerLayer = map.getLayerByName("objectMarkers");
	 if (markerLayer!=null && markerLayer.markers!=null)
	 {
	 	for (i=0;i< markerLayer.markers.length; i++) {
	 		markerLayer.markers[i].display(true);
	 		markerLayer.markers[i].popup.hide();
	 	}
	 }
	 
	if (this.marker !=null) {
		this.marker.display(true);	
		this.marker.popup.show();	
	 	
	}
	
	if (this.marker.lonlat!=null) {
       	map.setCenter(this.marker.lonlat,map.getExtent());
    }    
    
    if (this.marker.map.layerMinZoomLevel != null && this.marker.map.getZoom()>0)
  	{
    	//This x-y values is used to show div in best place
    	var extent = this.marker.map.getExtent();

		var  mapcenter= this.marker.map.getCenter();
		var  x= mapcenter.lon;
		var  y= mapcenter.lat;

        var maxBounds = this.marker.map.getMaxExtent();  
        
		if ((this.marker.lonlat.lon + this.marker.popup.size.w/2 * this.marker.map.getResolution()) > extent.right)
		{
			x = x + ((this.marker.lonlat.lon + this.marker.popup.size.w/2 * this.marker.map.getResolution()) - extent.right);
			if (x>maxBounds.right) {
				x = maxBounds.right;
			}
		}
		if ((extent.left > this.marker.lonlat.lon - this.marker.popup.size.w/2 * this.marker.map.getResolution()))
		{
			x = x - ((extent.left - this.marker.lonlat.lon + this.marker.popup.size.w/2 * this.marker.map.getResolution()));
			if (x<maxBounds.left) {
				x = maxBounds.left;
			}
		}
		if ((this.marker.lonlat.lat + this.marker.popup.size.h * this.marker.map.getResolution() + this.marker.icon.size.h * this.marker.map.getResolution()) > extent.top)
		{
			y = y + ((this.marker.lonlat.lat + this.marker.popup.size.h * this.marker.map.getResolution() + this.marker.icon.size.h * this.marker.map.getResolution()) - extent.top);
			if (y>maxBounds.top) {
				y = maxBounds.top;
			}
		}

		this.marker.map.setCenter(new OpenLayers.LonLat(x, y), this.marker.map.getZoom());
  	}
    
}
function showHideDiv(id,visible) {
	if (visible) {
		document.getElementById(id).style.display='block';	
		document.getElementById(id).style.visibility='visible';
	} else {
		document.getElementById(id).style.display='none';	
		document.getElementById(id).style.visibility='hidden';
	}	
}

function addWmsLayers(map) {
		
		var layerUrl =  urlGeoserverSitr + 'wms';
		var bounds = new OpenLayers.Bounds(1159976,4299800,1570920,4577800);
					
		 // WMS options
        var options = {
        		resolutions: [0.5,1,2,4,8,16,32,64,128,256,512,1024,2048],
                maxResolution: "auto",
                maxExtent: bounds,
                /*maxResolution: 1173.046875,*/
                projection: "EPSG:3003",
                buffer: 0,
              /*  legendId: 'legenda',*/
                singleTile: true
            
            	
            };
            
		// WMS params
        var params = {
             srs: 'EPSG:3003',
            type: "png",
        	format: 'image/png',
             ratio: '1',
             transparent: 'true'
        };
	
		// WMS Layers
	    var layers = [  
	       	           
	         new Roja.WMSLayer({
	            physicalName: 'ras:IDT_AA01G_COMUNI',
	            logicalName: 'Comuni',
	            group:'Limiti Amministrativi',
	            visibility: false
	         }),
	         new Roja.WMSLayer({
	            physicalName: 'ras:IDT_AA02G_PROVINCE',
	            logicalName: 'Province',
	            group:'Limiti Amministrativi',
	            visibility: false
	         })		       
	    ];

            // Roja.Layer.WMS
            layerWMS = new Roja.Layer.WMS(
                'Limiti Amministrativi',
                layerUrl,
                params,
                //options,
                {},
                layers
            );
        
        layerWMS.displayInLayerSwitcher = false;
        layerWMS.setIsBaseLayer(false);
        map.addLayer(layerWMS);

        

}
function error() {
    alert('load configuration error');
}

