// determinano il browser e la versione
var isIE = (document.all?true:false);
var isNav = !isIE;
var isNav4 = false;
var isIE4 = false;
var is5up = false;
var isMoz = (((navigator.userAgent.indexOf("Gecko") >= 0) &&(navigator.userAgent.indexOf("Netscape") < 0))?true:false);

if (isNav) {
	if (parseFloat(navigator.appVersion)<5) {isNav4=true;}
	else{is5up = true;}
} 
else {
	if (navigator.appVersion.indexOf("MSIE 5")>0) {is5up = true;}
	else{isIE4 = false;}	
}	

// dimensione del bordo del rettangolo della overview
var ovBoxSize = 2;
// colore del rettangolo per la zoom box
var zoomBoxColor = '#ff0000';

thePageWin = window;
thePageDoc = document;

// Dimensioni finestra
var winWidth;
var winHeight;

// Dimensioni mappa

//Se si cambia la dimensione della mappa cambiare anche
//le dimensioni nelllo stile #mainDiv in style.css
var mapWidth =  1000;
var mapHeight = 500;//610px per risoluzione 1280 x 1024;
var borderWidth = 1;

/*
//posizioni finestre
//barH e' l'altezza delle barre delle finestre floating.
//cambiarla in questo file non ne cambia la dimensione.
//E'stata qui definita questa costante per rendere piu' chiari i calcoli 
//per il posiozionamento delle finestre rispetto alla mappa
var barH = 18;
var ovWidth = 176;
var ovHeight = 110;
var ovX = 0;
var ovY = mapHeight - (ovHeight + 3 + barH)//440;
var searchL = 264;
var searchH = 140;
//Si veda commento sulla TOC
var searchX = mapWidth - (264 + 3); //733;
var searchY = 1;
var mappeX = 1;
var mappeY = 1;
var mappeL = 295;
var mappeH = 84;
var tocL = 250;
var tocH = 150;
//attenzione l'altezza del footer della TOC non cambia cambiando questo parametro.
//L'altezza del footer della TOC non e' definita da alcuna parte, ma e' determinata
//dal suo contenuto. Essendo quest'ultimo fisso (non ha contenuti dinamici) possiamo 
//considerarlo come una costante.
var tocFooterH = 30; 
//Siccome la TOC e' posizionata nel lato basso destro della mappa esprimo il suo posizionamento
//come differenza tra la larghezza mazzima della mappa e la larghezza della TOC stessa
//piu' qualche pixel come distanziatore dal bordo e idem per l'altezza, con riferimento l'altezza della mappa.
//Se si desidera una maggiore distanza dal bordo aumentare il valore del distanziatore.
var tocX = mapWidth - (tocL + 3); //747;
var tocY = mapHeight - (tocH + tocFooterH + 5 + barH); //374;
var legendaX = (mapWidth-385)/2;
var legendaY = 160;
var legendaL = 302;
var legendaH = 300;
var wmsX = (mapWidth-385)/2;
var wmsY = 160;
var wmsL = 368;
var wmsH = 300;
var wmsInfoX = (mapWidth-385)/2;
var wmsInfoY = 160;
var wmsInfoL = 368;
var wmsInfoH = 300;
var mis_selX = (mapWidth-385)/2;
var mis_selY = 1;
var mis_selL = 368;
var infoX = (mapWidth-385)/2;
var infoY = 160;
var infoL = 368;
var urlX = (mapWidth-385)/2;
var urlY = 160;
var urlL = 368;
var urlH = 130;
var pluginX = 1;
var pluginY = 85; 
var pluginL = 176;
var pluginH = 53;
*/ 
 

// offset orizzontale e verticale
var hspc;
var vspc = 123;

// tolleranza per la selezione
var pixelTolerance=2;

var panFactor = 85/100;
var zoomFactor = 2
	
var layerTag = (isNav4?"layer":"div");
var flagLayer = false;
var hostName = window.location.host;

var usePan=true;
var useZoomIn=true;
var useZoomOut=true;
var useFullExtent=true;
var useZoomActive=true;
var useZoomLast=true;
var useIdentify=true;
var useSelect=true;
var useFind=true;

var useIdentifyAll=false;
if (useIdentify) useIdentifyAll=false;

var ScaleBarPrecision = 2;
var numDecimals = ScaleBarPrecision;

function checkParams(componente) {
	if (isNav4) {
		componente.captureEvents(Event.MOUSEMOVE);
		componente.captureEvents(Event.MOUSEDOWN);
		componente.captureEvents(Event.MOUSEUP);
	}
	componente.onmousemove = getMouse;
	componente.onmousedown = mapTool;
	componente.onmouseup = chkMouseUp;
	clickFunction(parent.PostFrame.document.theForm.toolName.value);
	
	// Evento associato al resize della finestra
	//parent.MapFrame.onresize = onResizeMapFrame;
	window.onresize = onResizeMapFrame;
}

/*
Questa funzione effetua una riscrittura della pagina per la gestione errore, prodotta tramite xsl,
nella pagina principale del CTW Floating. Questo perch? la post arriva alla frame "MapServlet"
ma gli errori devono essere gestiti dal padre....
*/
function errorRedirect(){
//il primo if serve per discriminare il caso in cui siamo al  momento del login e quindi ancora senza frame
if(parent.PostFrame)
if(parent.PostFrame.document.theForm.is_floating.value=="1"){
	var tmpLayerContent=document.body.innerHTML;
	parent.MapFrame.document.body.innerHTML=tmpLayerContent;
	}
}
function getElementTopPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return offsetTop;
}

function getElementLeftPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return offsetLeft;
}

function showFloatingLayer(steps,currStep, currTrasp) 
{
	//la prima volta che viene chiamata riposiziona le finestra floating nella corretta posizione
	
	if(currStep==0){
		
		// Ricalcolo il valore dell'offset orizzontale
		winWidth = parent.MapFrame.getMapWidth();
		winHeight = parent.MapFrame.getMapHeight();
	
		vspc=Math.abs(getElementTopPosition('mainDiv'))+1;
		hspc=Math.abs(getElementLeftPosition('mainDiv'))+2;
		
		// Reimposto le propriet? di dragging per le finestre
		parent.MapFrame.setDHTML();
		// Riallineo le finestre proporzionalmente
//parent.MapFrame.realignWindows(false);
		// Imposto i metodi per le operazioni di Drag & Drop
		setDragDropMethods();
		
		//Reimposto le splashs
		loadBannerLeft = parseInt((mWidth - 93)/2);
	    loadBannerTop = parseInt((mHeight - 63)/2);
		parent.MapFrame.document.getElementById('LoadMap').style.left=loadBannerLeft;
		parent.MapFrame.document.getElementById('LoadMap').style.top=loadBannerTop;
		parent.MapFrame.document.getElementById('LoadData').style.left=loadBannerLeft;
	    parent.MapFrame.document.getElementById('LoadData').style.top=loadBannerTop;

		// Reimposto lo z-index della mappa
		parent.MapFrame.document.getElementById(parent.MapFrame.getActiveLayerName()).style.zIndex = 1;
		parent.MapFrame.document.getElementById(parent.MapFrame.getInactiveLayerName()).style.zIndex = 0;
		parent.PostFrame.document.theForm.service_action.value = "";
		
		
	}
	
	var incrementoTrasparency = 100/steps;
	var valoreAttualeTrasparency=currTrasp-(incrementoTrasparency/100); 
	 
	var containtLayer=parent.MapFrame.document.getElementById('resizingSplash');
	//qui imposto la trasparenza per i diversi browser
	containtLayer.style.filter = "alpha(opacity="+(valoreAttualeTrasparency*100)+")";
	containtLayer.style.MozOpacity= valoreAttualeTrasparency;
	containtLayer.style.opacity=valoreAttualeTrasparency;    
	currStep++;
	
	if (currStep <= steps)
	{
	    setTimeout("showFloatingLayer("+steps+","+currStep+","+valoreAttualeTrasparency+")",5);
	}
	else{
	hideLayer("resizingSplash");
	parent.MapFrame.document.getElementById('resizingSplash').style.display="none";	
	refresh();	
	}
}

function hideFloatingLayer(newWidth,newHeight,steps,currStep, currTrasp) 
{
	var incrementoTrasparency = 100/steps;
	var valoreAttualeTrasparency=currTrasp+(incrementoTrasparency/100); 

	//var containtLayer=parent.MapFrame.document.getElementById('container');
	var containtLayer=parent.MapFrame.document.getElementById('resizingSplash');

    //qui imposto la trasparenza per i diversi browser
	containtLayer.style.filter = "alpha(opacity="+(valoreAttualeTrasparency*100)+")";
	containtLayer.style.MozOpacity= valoreAttualeTrasparency;
	containtLayer.style.opacity=valoreAttualeTrasparency;	
	currStep++;
	
	if (currStep <= steps)
	{
	    setTimeout("hideFloatingLayer("+newWidth+","+newHeight+","+steps+","+currStep+","+valoreAttualeTrasparency+")",5);
	}
	else{
		
		var tmpLayer=parent.MapFrame.document.getElementById('mainDiv');
		var navDiv= parent.MapFrame.document.getElementById('navDiv');
		var headerDiv= parent.MapFrame.document.getElementById('header');
		var footerDiv= parent.MapFrame.document.getElementById('footer');
		var containerDiv= parent.MapFrame.document.getElementById('container');
			
	   
	    //applico la variazione della larghezza a tutti i layer
		tmpLayer.style.width=newWidth+'px';
		navDiv.style.width=newWidth+'px';
		headerDiv.style.width=newWidth+'px';
		footerDiv.style.width=newWidth+'px';
	    containerDiv.style.width=newWidth+'px';
	    //applico la variazione della altezza al solo layer della mappa ed al layer della trasparenza...
		tmpLayer.style.height=newHeight+'px';
												
		//imposto le variabili che contengono la dimensione della mappa
		mWidth=newWidth;
	    mHeight=newHeight;
		mapWidth=mWidth;
		mapHeight=mHeight;
			
		iWidth = mWidth;
		iHeight = mHeight;
		
		//Imposto le dimensioni dei due layer che si occupano della gestione delle img
		var mapLayer = parent.MapFrame.document.getElementById(parent.MapFrame.getInactiveLayerName());
		mapLayer.style.height=mapHeight;
		mapLayer.style.width=mapWidth;
			
		mapLayer = parent.MapFrame.document.getElementById(parent.MapFrame.getActiveLayerName());
		mapLayer.style.height=mapHeight;
		mapLayer.style.width=mapWidth;
		
		mapLayer = parent.MapFrame.document.getElementById('mapLayer_evidenzia');
		mapLayer.style.height=mapHeight;
		mapLayer.style.width=mapWidth;
		
		
		 //e cambio la dimensionde della img per ottenere l'effetto strecth...
	    mapLayer = parent.MapFrame.document.getElementById("mapImage");
		if(mapLayer){
		    mapLayer.height=mapHeight;
	        mapLayer.width=mapWidth;
	       }
	    //e cambio la dimensione della img di evidenziazione   
	    mapLayer = parent.MapFrame.document.getElementById("mapImage2");
		if(mapLayer){
		    mapLayer.height=mapHeight;
	        mapLayer.width=mapWidth;
	       }
		//chiamo la nuova img
		 setTimeout("showFloatingLayer("+steps+","+0+","+1+")",25);
	}
}



/**
 * 
 */
function onResizeFromUser(isSubmit) {

/* calcolo la dimensione del vieport per determinare esattamente 
 * a quale dimensione devo ridimensionare il navigatore
 */
	
 	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	
	
	var tmpLayer=parent.MapFrame.document.getElementById('mainDiv');
	oldMapWidth = tmpLayer.offsetWidth;
    oldMapHeight = tmpLayer.offsetHeight;
	
	if(tmpLayer.offsetWidth >760){
        var newWidth =752;
        var newHeight = 450;
	}
	else
	{
		//tolgo 50 px, 25 per lato per mantenere almeno un p? di px di bordo
	     var newWidth =parseInt( x - 50);
	     var newHeight = parseInt(newWidth / 1.677777);	
		//devo controllare che non sono al di sotto delle dimensioni minime
		if( newWidth <752 || newHeight < 450){
			var newWidth =752;
	        var newHeight = 450;
		}
	
	}
	 
     //imposto le proprieta per il div che indica il resing in corso...
   	parent.MapFrame.document.getElementById('resizingSplash').style.left=0;
    parent.MapFrame.document.getElementById('resizingSplash').style.top=0;
	parent.MapFrame.document.getElementById('resizingSplash').style.width=x+"px";
    parent.MapFrame.document.getElementById('resizingSplash').style.height=y+"px";
 	parent.MapFrame.document.getElementById('resizingSplash').style.backgroundColor= "gray";
   	parent.MapFrame.document.getElementById('resizingSplash').style.filter = "alpha(opacity=0)";
	parent.MapFrame.document.getElementById('resizingSplash').style.MozOpacity=0;
	parent.MapFrame.document.getElementById('resizingSplash').style.opacity=0;
	parent.MapFrame.document.getElementById('resizingSplash').style.display="block";	
	showLayer("resizingSplash");
	hideFloatingLayer(newWidth,newHeight,15,0,0);
}

function getPageDimension(){
var x,y;
var test1 = document.body.scrollHeight;
var test2 = document.body.offsetHeight
if (test1 > test2) // all but Explorer Mac
{
	x = document.body.scrollWidth;
	y = document.body.scrollHeight;
}
else // Explorer Mac;
     //would also work in Explorer 6 Strict, Mozilla and Safari
{
	x = document.body.offsetWidth;
	y = document.body.offsetHeight;
}
return [x,y];
}


/* Tips per il resizing nei diversi browser */
function css_hack()
{
    var tmpLayer=parent.MapFrame.document.getElementById('mainDiv');
	tmpLayer.style.height='90%';
	tmpLayer.style.width='100%';
	
	if(tmpLayer.offsetHeight < 555)
	tmpLayer.style.height='555px';
	
	if(tmpLayer.offsetWidth <400)
	tmpLayer.style.width='400px';
	
	//Devo impostare i valori delle varibili che contengono la dimensione della mappa prendendo il valore della dimesione del div
	mWidth=parent.MapFrame.document.getElementById('mainDiv').offsetWidth;
    mHeight=parent.MapFrame.document.getElementById('mainDiv').offsetHeight;		

	mapWidth=mWidth;
	mapHeight=mHeight;
		
	iWidth = mWidth;
	iHeight = mHeight;

}


function onResizeMapFrame() {
	
	css_hack();
	
	//Imposto le dimensioni dei due layer che si occupano della gestione delle img
	var mapLayer = parent.MapFrame.document.getElementById(parent.MapFrame.getInactiveLayerName());
	mapLayer.style.height=mapHeight;
	mapLayer.style.width=mapWidth;
		
	mapLayer = parent.MapFrame.document.getElementById(parent.MapFrame.getActiveLayerName());
	mapLayer.style.height=mapHeight;
	mapLayer.style.width=mapWidth;
	
	//e cambio la dimensionde della img per ottenere l'effetto strecth...
     mapLayer = parent.MapFrame.document.getElementById("mapImage");
	if(mapLayer){
	mapLayer.height=mapHeight;
        mapLayer.width=mapWidth;
}
    // Ricalcolo il valore dell'offset orizzontale
	winWidth = parent.MapFrame.getMapWidth();
	winHeight = parent.MapFrame.getMapHeight();

	vspc=Math.abs(getElementTopPosition('mainDiv'))+1;
	hspc=Math.abs(getElementLeftPosition('mainDiv'))+2;
	
	// Reimposto le propriet? di dragging per le finestre
//parent.MapFrame.setDHTML();
	// Riallineo le finestre
//parent.MapFrame.realignWindows(false);
	// Ripristino la posizione delle finestre
	parent.MapFrame.restoreAll();
	// Imposto i metodi per le operazioni di Drag & Drop
	setDragDropMethods();


	var larghezzaToc=parent.MapFrame.document.getElementById('tocStatusBarLayer').offsetWidth;
	var mainDivOffsetLeft=parent.MapFrame.document.getElementById('mainDiv').offsetLeft;
	var mainDivOffsetTop=parent.MapFrame.document.getElementById('mainDiv').offsetTop;

	dd.elements.legendStatusBarLayer.moveTo(mWidth+mainDivOffsetLeft-(larghezzaToc+5), mainDivOffsetTop+300);
    //dd.elements.tocStatusBarLayer.moveTo(mWidth+mainDivOffsetLeft-(larghezzaToc+5), mainDivOffsetTop+1);

	//Reimposto le splashs
	loadBannerLeft = parseInt((mWidth - 93)/2);
        loadBannerTop = parseInt((mHeight - 63)/2);
	parent.MapFrame.document.getElementById('LoadMap').style.left=loadBannerLeft;
	parent.MapFrame.document.getElementById('LoadMap').style.top=loadBannerTop;
	parent.MapFrame.document.getElementById('LoadData').style.left=loadBannerLeft;
        parent.MapFrame.document.getElementById('LoadData').style.top=loadBannerTop;

	// Reimposto lo z-index della mappa
	parent.MapFrame.document.getElementById(parent.MapFrame.getActiveLayerName()).style.zIndex = 1;
	parent.MapFrame.document.getElementById(parent.MapFrame.getInactiveLayerName()).style.zIndex = 0;
	parent.PostFrame.document.theForm.service_action.value = "";
	
   //if(isSubmit || isIE)
   //if(isIE)
	resizeMap();
	
	
	
	// Ricalcolo il valore dell'offset orizzontale
	winWidth = parent.MapFrame.getMapWidth();
	winHeight = parent.MapFrame.getMapHeight();

	vspc=Math.abs(getElementTopPosition('mainDiv'))+1;
	hspc=Math.abs(getElementLeftPosition('mainDiv'))+2;

	/// Reimposto le propriet? di dragging per le finestre
	parent.MapFrame.setDHTML();
	// Riallineo le finestre
	parent.MapFrame.realignWindows(false);
	// Imposto i metodi per le operazioni di Drag & Drop
	setDragDropMethods();
	// Reimposto lo z-index della mappa
	parent.MapFrame.document.getElementById(parent.MapFrame.getActiveLayerName()).style.zIndex = 1;
	parent.MapFrame.document.getElementById(parent.MapFrame.getInactiveLayerName()).style.zIndex = 0;
	

}

function checkParamsOv(componente) {
	if (isNav4) {
		componente.captureEvents(Event.MOUSEMOVE);
		componente.captureEvents(Event.MOUSEDOWN);
		componente.captureEvents(Event.MOUSEUP);
	}
	componente.onmousemove = getOvMouse;
	componente.onmousedown = ovMapTool;
	componente.onmouseup = ovChkMouseUp;
}

function openLinkCore() {
    var newWin = new Object();
    newWin = window.open('http://www.corenet.it','','');
    newWin.focus();
}	

function openLink(url) {
    var newWin = new Object();
    newWin = window.open(url,'','');
    newWin.focus();
}
