//
// Global variables
//
var htmldocument=document;
var originWidth, originHeight;

// TEST GENERAL LAYERING

/*
// FOR HTML ONLY START
var chkArrayState = [ "chka" , "chkb" ] ;

// Load chkbox state on query string

if (chkArrayState) {
	for (var i = 0; i<chkArrayState.length; i++ ) {
		var chkItem = chkArrayState[i] ;
		var chkHtmlElem = document.getElementById( chkItem ) ;
		
	}
}
// END FOR HTML ONLY
*/
window.addMapLayer = ConsoleAddLayer ;
function ConsoleAddLayer ( arg0, arg1, arg2 )  {
	if (window.addLayerSvg) {
		window.addLayerSvg ( arg0, arg1, arg2 );
	} else if (window.addLayerHtml) {
		window.addLayerHtml ( arg0, arg1 );
	}  
	
}
// END TEST GENERAL LAYERING


// TEST HTML LAYERING
window.addLayerHtml = AddLayerHtml ;

function AddLayerHtml ( layerName, layerUrl ) {
	// MapGetHtmlLayer.aspx?
	layerUrl = "MapGetHtmlLayer.aspx?" + layerUrl + "&layer="+layerName+"&x1="+realRange.minX+"&y1="+realRange.minY+"&x2="+realRange.maxX+"&y2="+realRange.maxY ;
	// alert ( "addLayerHtml-"+layerName+"-"+layerUrl );
	// check if exists layerName
    var lyr = document.getElementById? document.getElementById(layerName): document.all? document.all[layerName]: null;
    if ( lyr && typeof lyr.innerHTML != "undefined" ) {
		// if exists, then remove
		if (lyr.innerHTML.length > 0 ) {
			lyr.innerHTML = '';	// se c'e' del contenuto allora azzeralo : comportamento ON/OFF
			// alert ( "addLayerHtml- innerHtml non vuoto. Azzeramento");
			return;
		} else {
			// ok, il layer esiste ed è vuoto
			// alert ( "addLayerHtml- innerHtml vuoto. Ok niente da fare");
		}
	} else {
		// il layer non esiste, dobbiamo preparare un div vuoto nel layer container
		// alert ( "addLayerHtml- layer non esiste. Aggiungiamolo");
		var lyrContainer = document.getElementById? document.getElementById('displayLayers'): document.all? document.all['displayLayers']: null;
		if ( lyrContainer && typeof lyrContainer.innerHTML != "undefined" ) {
			lyrContainer.innerHTML += "<div id=\""+ layerName + "\" ></div>" ;	// accodo un layer vuoto
		} else {
			// errore, non c'e' il layer container
			alert ("maputils.js::addLayerHtml::layer container does not exists");
			return;
		}
	}
	// Aggiungiamo il contenuto facendo una chiamata dinamica
	loadExternal( layerUrl, layerName ) ;
}

/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/
// 
// displayLayers -- DIV padre di tutti i layers
// 
function loadExternal(url, layerName) {
  // alert ( "loadExternal-"+url+"-"+layerName );
  if ( window.frames['htmlBuffer'] ) {
    window.frames['htmlBuffer'].location = url;
    var lyr = document.getElementById? document.getElementById(layerName): document.all? document.all[layerName]: null;
    if ( lyr && typeof lyr.innerHTML != "undefined" ) {
      lyr.innerHTML = '<img src="images/console/hourglass.gif" alt="">';
    }
    return false;
  } 
  return true; // other browsers follow link
}

// called when documents loaded into iframe (from their body's onload = displayExternal('layerName') attribute)
function displayExternal(layerName) {
  // alert ( "displayExternal-"+layerName );
  var lyr = document.getElementById? document.getElementById(layerName): document.all? document.all[layerName]: null;
    if ( window.frames['htmlBuffer'] && lyr && typeof lyr.innerHTML != "undefined" ) {
      lyr.innerHTML = window.frames['htmlBuffer'].document.body.innerHTML;
    }
}


// END HTML LAYERING




//
// Used to provide communication from svg to document
//
var realRange;		// Range class
function setCurrent_Range ( newRange ) {	// Range class
	realRange = newRange;
}
window.setCurrentRange = setCurrent_Range;

//
// This function can be customized, as it produces the resulting
//	html for highlighting. 
//	Note: the element id "highlight" must be present on the current page.
// 
function BoxEvidence_Show ( title, subtitle, summary, imgurl ) {
	// var msgArea = document.all["highlight"];
	var msgArea = document.getElementById("highlight");
	var s = "" ; // innerhtml
	if (msgArea) {
		if (imgurl.length > 0) {
			s = "<img border=\"0\" width=\"100\" src=\"" + imgurl + "\" ><br><b>"+title+"</b>"+"<br><i>"+subtitle+"</i>";
		} else {
			s = "<b>"+title+"</b>"+"<br><i>"+subtitle+"</i>";
		}
		if (summary.length > 0 ) s += "<br>"+summary;
		msgArea.innerHTML = s;
	}
}

//
// Remove evidence html box
//
function BoxEvidence_Hide ( ) {
	var msgArea = document.getElementById("highlight");
	// var msgArea = document.all["highlight"];
	msgArea.innerHTML = "";
}

window.doBoxEvidenceShow = BoxEvidence_Show;
window.doBoxEvidenceHide = BoxEvidence_Hide ;

var gOldStyle = null;	// old style when making evidence

//
// Perform evidence for an item on map
//
function evidenceInMap ( iditem ) {
	if (! htmldocument.embeds[0] ) {	// if no embed available
		return;
	}
	var svgdoc=htmldocument.embeds[0].getSVGDocument();
	
	var obj = svgdoc.getElementById( "obj" + iditem ); 
	if (obj == null) {  return;	}
	var svgimages = obj.getElementsByTagName('image');
	var nimages = svgimages.length;
	
	if (nimages > 0) {
		var currWidth = svgimages.item(0).getAttributeNS ( null, "width" ) ;
		originWidth = currWidth;
		currWidth = parseFloat ( currWidth ) ;
		currWidth = currWidth * 2.0;

		var currHeight = svgimages.item(0).getAttributeNS ( null, "height" ) ;
		originHeight = currHeight;
		currHeight = parseFloat ( currHeight ) ;
		currHeight = currHeight * 2.0;
		
		svgimages.item(0).setAttributeNS(null,"width", currWidth);
		svgimages.item(0).setAttributeNS(null,"height",currHeight);
	} else {
		// evidence on area.
		// var oldStyle = obj.getAttributeNS( null, "class");
		// obj.setAttributeNS(null,"class","testSelected" );
		
		// obj.setAttributeNS(null,"fill","red");
		// obj.setAttributeNS(null,"fill-opacity","0.4");

		gOldStyle = obj.getAttribute("class") ;
		obj.setAttribute("class", "EvidenceObject");
		
		// obj.setAttribute("fill-opacity","0.4");
		// obj.setAttribute("fill","red");
		
		// obj.getStyle().setProperty("fill","red");
		// obj.getStyle().setProperty("fill-opacity","0.4");

	}
}
		
//
// Perform unevidence for icons that are on map, normally becaouse of a mouse-out event
//		
function unEvidence ( iditem ) {
	if (! htmldocument.embeds[0] ) {	// if no embed available
		return;
	}
	var svgdoc=htmldocument.embeds[0].getSVGDocument();
	var obj = svgdoc.getElementById( "obj" + iditem ); 
	if (obj == null) return;
	
	var svgimages = obj.getElementsByTagName('image');
	if (svgimages.length > 0 ) {
		// var nimages = svgimages.length;
		svgimages.item(0).setAttributeNS(null,"width", originWidth);
		svgimages.item(0).setAttributeNS(null,"height",originHeight);
	} else {
		// unevidence on area.
		// obj.setAttribute("fill","none");
		// obj.getStyle().setProperty("fill","none");
		obj.setAttribute("class", gOldStyle );
	}
}



//
// Reload the current page, performing the substitution on the query string
//	Description...: Perform a substitution on the query string (if exists) or perform an addition
//						if not exists. qsParamName is the variable name on the query string and
//						qsParamValue is the new value (if just exists) or the added value (if not exits)
//	Used on.......: map control
//
function reloadWithNewParameter (qsParamName, qsParamValue  ) {
	var newUrl = "";
	var sSearch = window.location.search;
	var found = false;
	if (sSearch.length > 1) {
		sSearch = sSearch.substring(1, sSearch.length  ) ;	// remove first ?
		var arrNameVal = sSearch.split("&");
		var numNameVal = arrNameVal.length;
		var i;
		for (i=0; i < numNameVal; i++ ) {
			var arrItem = arrNameVal[i].split("=");
			if (arrItem.length == 2) {
				if (arrItem[0] == qsParamName) {
					arrNameVal[i] = qsParamName+"="+qsParamValue;
					found = true;
				}
			}
		}
		aSearch = "?" + arrNameVal.join("&");
		if (!found) aSearch = aSearch + "&" + qsParamName + "=" + qsParamValue;
		window.location.search = aSearch;
	} else {
		aSearch = "?" + qsParamName + "=" + qsParamValue;
		window.location.search = aSearch ;
	}
	if (realRange) {
		aSearch = addOrUpdateQsParam ( aSearch, "x1", realRange.minX );
		aSearch = addOrUpdateQsParam ( aSearch, "y1", realRange.minY );
		aSearch = addOrUpdateQsParam ( aSearch, "x2", realRange.maxX );
		aSearch = addOrUpdateQsParam ( aSearch, "y2", realRange.maxY );
	}
	
	// Save chkbox state on query string
	/*
	if (chkArrayState) {
		for (var i = 0; i<chkArrayState.length; i++ ) {
			var chkItem = chkArrayState[i] ;
			var chkHtmlElem = document.getElementById? document.getElementById(chkItem): document.all? document.all[chkItem]: null;
			alert ( chkHtmlElem );
			if (chkHtmlElem) {
				if (chkHtmlElem.checked) {
					aSearch = addOrUpdateQsParam ( aSearch, chkItem, "1" );
				} else {
					aSearch = addOrUpdateQsParam ( aSearch, chkItem, "0" );
				}
			}
		}
	}
	*/
	window.location = window.location.pathname + aSearch ;
}

function addOrUpdateQsParam ( qsSearch , qsParamName, qsParamValue ) {
	var newUrl = "";
	var sSearch = qsSearch ;
	var found = false;
	if (sSearch.length > 1) {
		sSearch = sSearch.substring(1, sSearch.length  ) ;	// remove first ?
		var arrNameVal = sSearch.split("&");
		var numNameVal = arrNameVal.length;
		var i;
		for (i=0; i < numNameVal; i++ ) {
			var arrItem = arrNameVal[i].split("=");
			if (arrItem.length == 2) {
				if (arrItem[0] == qsParamName) {
					arrNameVal[i] = qsParamName+"="+qsParamValue;
					found = true;
				}
			}
		}
		aSearch = "?" + arrNameVal.join("&");
		if (!found) aSearch = aSearch + "&" + qsParamName + "=" + qsParamValue;
	} else {
		aSearch = "?" + qsParamName + "=" + qsParamValue;
	}
	return aSearch;
}

