// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

/*var dragObj = new Object();
dragObj.zIndex =1500;*/
//dragObj.zIndex =1001;

function create_obj(event,id){
	dragObj = new Object();
	dragObj.zIndex =1500;
	dragObj.id =id+"1";
	dragObj.width =document.getElementById(id).width;
	dragObj.height =document.getElementById(id).height;
	
	if (browser.isIE) {
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
   
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
  
  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
  maxRight=window.document.body.offsetWidth-dragObj.elNode.offsetWidth-30
 	maxTop=window.document.body.offsetHeight-15-dragObj.elNode.offsetHeight
   
	dragStart(event, id);
}
function dragStart(event, id) {
	
	//if (!id) id="divResultRouting"
  
  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
 
 	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = x
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = y
  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.


  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
   // document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
   // document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.
 
 

 /* if ((dragObj.elStartLeft + x - dragObj.cursorStartX) < maxRight && (dragObj.elStartLeft + x - dragObj.cursorStartX) > 10) dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  if ((dragObj.elStartTop + y - dragObj.cursorStartY) > 178) dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
 */
   
  if ((dragObj.elStartLeft + x - dragObj.cursorStartX) < maxRight && (dragObj.elStartLeft + x - dragObj.cursorStartX) > 17) dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
 // else dragStop(event)
  if ((dragObj.elStartTop + y - dragObj.cursorStartY) > 120 && (dragObj.elStartTop + y - dragObj.cursorStartY)<maxTop) dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
 //	else dragStop(event)
 
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}


// funzione di selezione / deselezione del background
// degli elementi in ogni menù

// dichiaro lo stato degli elementi (se selezionati o no)
var ArteArchitetturaStato=0;
var TermeBenessereStato=0;
var StoriaStato=0;
var SpiritoStato=0;
var EnoStato=0;
var ArtigianatoStato=0;
var NaturaStato=0;
var MareStato=0;
var SportStato=0;
var EventiStato=0;
var CongressiStato=0;
var COMUOVStato=0;
var DOVDORStato=0;
var DOVMANStato=0;
var TEMPLStato=0;
var INFOGENStato=0;
var ALTSERVStato=0;
var GEOBLOG1Stato=0;
var Percorsi_DBStato=0; //Aggiunta MDe Santis 22102007
var APTStato=0; 
// accendo il background
function SwitchBGOn(MenuItem,MenuColor) {

  if ( MenuColor == "rosso") {
  	document.getElementById(MenuItem).className="MenuRisorseOn";
  }
  if ( MenuColor == "blu") {
    document.getElementById(MenuItem).className="MenuInformazioniOn";
  }
}

// spengo il background

function SwitchBGOff(MenuItem,MenuColor) {
if (eval(MenuItem+"Stato")!=1) {
  if ( MenuColor == "rosso") {
    document.getElementById(MenuItem).className="MenuRisorse";
  }
  if ( MenuColor == "blu") {
    document.getElementById(MenuItem).className="MenuInformazioni";
  }
}
}

// seleziono il background
function SelectBG(MenuItem,MenuColor,idtem,idtema) {
  if (tipoRicerca=="ALL"){
  	tipoRicerca="";
  	id_tematismo="";
  	attrattori="";
  }
	if (MenuItem!="" && MenuItem!=undefined){
		  if (eval(MenuItem+"Stato")==0) {
		  	check_itinerario=0
		    if (MenuColor == "rosso") {
		      document.getElementById(MenuItem).className="MenuRisorseOn";
		    }
		    if ( MenuColor == "blu") {
		      document.getElementById(MenuItem).className="MenuInformazioniOn";
		    }
		    eval (MenuItem+"Stato=1;");
		     
		   	if (idtem!=undefined && !isNaN(idtem)){
		   	   
		   		 crea_query(idtem);
		   	}else if (idtem=="Geoblog") visualizza_geoblog()
		   	 else if (idtem=="Percorso") visualizza_percorsi(0); //MDe Santis 22/10/2007 
		  }
		  else
		  {
		    if ( MenuColor == "rosso") {
		      document.getElementById(MenuItem).className="MenuRisorse";
		    }
		    if ( MenuColor == "blu") {
		      document.getElementById(MenuItem).className="MenuInformazioni";
		    }
		    eval (MenuItem+"Stato=0;");
		    //cancellazione elementi nella query
		    
		    id_tematismo=id_tematismo.replace(""+idtem+",","")
		    
		    if (idtem!="undefined" && !isNaN(idtem)) crea_query("0");
		    else if (idtem=="Geoblog") nascondi_geoblog()
		    else if (idtem=="Percorso") nascondi_percorsi(); //MDe Santis 22/10/2007
		  }
	}
}
// piazza gli oggetti secondo le coordinate richieste

function placePanel(PanelName) {
  
    	 r=0
    	  
    /*if (PanelName=="PannelloRisorse") var rightMargin = 226;
    if (PanelName=="PannelloInformazioni") var rightMargin =422;
    if (PanelName=="PannelloCerca") var rightMargin = 2;*/
    if (PanelName=="PannelloRisorse") var rightMargin = 416+r;
    if (PanelName=="PannelloInformazioni") var rightMargin =600+r;
    if (PanelName=="PannelloCerca") var rightMargin = 223+r;
    if (PanelName=="BoxRicercaSpaziale") var rightMargin = 223+r;
    larghezza=window.document.body.scrollWidth
    //alert(larghezza)
    document.getElementById(PanelName).style.left=(parseFloat(larghezza)-rightMargin)+"px";
    
    //document.getElementById(PanelName).style.right=rightMargin+"px"
}





// menù a scomparsa	
	var slideDownInitHeight = new Array();
	var slidedown_direction = new Array();

	var slidedownActive = false;
	var contentHeight = false;
	var slidedownSpeed = 3; 	// Higher value = faster script
	var slidedownTimer = 7;	// Lower value = faster script
	function slidedown_showHide(boxId,subBoxId,frecciaTarget)
	{
		
		var frecciaImg = document.getElementById(frecciaTarget);
		if(!slidedown_direction[boxId])slidedown_direction[boxId] = 1;
		if(!slideDownInitHeight[boxId])slideDownInitHeight[boxId] = 0;
		
		
		//TG 07/11/2007
		//Alla chiusura del menu a tendina chiudo anche il box ricerca
		if (frecciaTarget == 'frecciaGiallo')
		{	//Fase di chiusura
			if (slideDownInitHeight[boxId] != 0)
			{
				riduci_ricspaziale();
			}
		}
		//TG
		
		if(slideDownInitHeight[boxId]==0)slidedown_direction[boxId]=slidedownSpeed; else slidedown_direction[boxId] = slidedownSpeed*-1;
		
		slidedownContentBox = document.getElementById(boxId);
		var subDivs = slidedownContentBox.getElementsByTagName('DIV');
		for(var no=0;no<subDivs.length;no++){
			if(subDivs[no].className==subBoxId) slidedownContent = subDivs[no];	
		}
		
		contentHeight = slidedownContent.offsetHeight+3;
	
		slidedownContent.style.visibility='visible';
		if (frecciaTarget == 'frecciaBlu') frecciaImg.src='PK_layout/img/BluDx.gif';
		if (frecciaTarget == 'frecciaRosso') frecciaImg.src='PK_layout/img/RossoDx.gif';
		if (frecciaTarget == 'frecciaGiallo') frecciaImg.src='PK_layout/img/GialloDx.gif';
		slidedownActive = true;
		slidedown_showHide_start(slidedownContentBox,slidedownContent,frecciaTarget);
	}
	function slidedown_showHide_start(slidedownContentBox,slidedownContent,frecciaTargetHide)
	{
		if(!slidedownActive)
		{
						
			//TG 05/11/2007
			//NB FORZO QUESTA CHIAMATA(crea_buffer()) PER RENDERE VISIBILE LO SLIDER.
			//ALTRIMENTI, BISOGNEREBBE PRIMA CLICCARE SULL'ICONA DELLA RICERCA SPAZIALE.
			if (slideDownInitHeight['PannelloCerca']) 
			{
				crea_buffer();
			
				//disabilito la modalità di puntamento buffer
				try{drawControls["point"].deactivate();}catch(e){}
			}
			//TG
			
			return;
		}
		
		slideDownInitHeight[slidedownContentBox.id] = slideDownInitHeight[slidedownContentBox.id]/1 + slidedown_direction[slidedownContentBox.id];
		if(slideDownInitHeight[slidedownContentBox.id] <= 0){
			slidedownActive = false;	
			slidedownContent.style.visibility='hidden';
			if (document.getElementById('frecciaBlu').src != 'PK_layout/img/BluDx.gif')document.getElementById('frecciaBlu').src='PK_layout/img/BluDxSu.gif';
			if (document.getElementById('frecciaRosso').src != 'PK_layout/img/RossoDx.gif')document.getElementById('frecciaRosso').src='PK_layout/img/RossoDxSu.gif';
			if (document.getElementById('frecciaGiallo').src != 'PK_layout/img/GialloDx.gif')document.getElementById('frecciaGiallo').src='PK_layout/img/GialloDxSu.gif';
			slideDownInitHeight[slidedownContentBox.id] = 0;
		}
		if(slideDownInitHeight[slidedownContentBox.id]>contentHeight){
			slidedownActive = false;	
		}
		slidedownContentBox.style.height = slideDownInitHeight[slidedownContentBox.id] + 'px';
	//	slidedownContent.style.top = slideDownInitHeight[slidedownContentBox.id] - contentHeight + 'px';

		setTimeout('slidedown_showHide_start(document.getElementById("' + slidedownContentBox.id + '"),document.getElementById("' + slidedownContent.id + '"))',slidedownTimer);
	}
	
	function setSlideDownSpeed(newSpeed)
	{
		slidedownSpeed = newSpeed;
		
	}

