/**********************************************************************
 *
 * $Id: startUp.js,v 1.2 2006/08/11 19:39:30 lbecchi Exp $
 *
 * purpose: start up code to bootstrap initialization of kaMap within
 *          the sample interface.  Examples of using many parts of
 *          the kaMap core api.
 *
 * author: Lorenzo Becchi and Andrea Cappugi
 *
 * contributions by Paul Spencer (pspencer@dmsolutions.ca)
 *
 * TODO:
 *
 **********************************************************************
 *
 * Copyright (c) 2005, DM Solutions Group Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 **********************************************************************/

/******************************************************************************
 *
 * To customize startUp:
 *
 * 1) modify toolbar Layout
 *  act on screen.css file and modify the funcion myMapInitialized().
 *  If you change pan and identifyer images edit switchMode() function too.
 *
 *****************************************************************************/

/*var myKaMap = myKaNavigator = myKaQuery = myScalebar = null;*/
var myKaMap = myKaNavigator = myKaQuery = null;
var queryParams = null;

/**
 * parse the query string sent to this window into a global array of key = value pairs
 * this function should only be called once
 */
function parseQueryString() {
    queryParams = {};
    var s=window.location.search;
    if (s!='') {
        s=s.substring( 1 );
        var p=s.split('&');
        for (var i=0;i<p.length;i++) {
            var q=p[i].split('=');
            queryParams[q[0]]=q[1];
        }
    }
}

/**
 * get a query value by key.  If the query string hasn't been parsed yet, parse it first.
 * Return an empty string if not found
 */
function getQueryParam(p) {
    if (!queryParams) {
        parseQueryString();
    }
    if (queryParams[p]) {
        return queryParams[p];
    } else {
        return '';
    }
}

function myOnLoad() {
    initDHTMLAPI();

	window.onresize=drawPage;

	myKaMap = new kaMap( 'viewport' );

	var szMap = getQueryParam('map');
    var szExtents = getQueryParam('extents');
    var szCPS = getQueryParam('cps');

    var legendOptions = {};
    legendOptions.visibility = typeof gbLegendVisibilityControl != 'undefined' ? gbLegendVisibilityControl : true;
    legendOptions.opacity = typeof gbLegendOpacityControl != 'undefined' ? gbLegendOpacityControl : true;
    legendOptions.order = typeof gbLegendOrderControl != 'undefined' ? gbLegendOrderControl : true;
    legendOptions.query = typeof gbLegendQueryControl != 'undefined' ? gbLegendQueryControl : true;
    
   // var myKaLegend = new kaLegend( myKaMap, 'legend', false, legendOptions);
    var myKaKeymap = new kaKeymap( myKaMap, 'keymap' );
    myKaNavigator = new kaNavigator( myKaMap );
    myKaNavigator.activate();
    myKaQuery = new kaQuery( myKaMap, KAMAP_RECT_QUERY );
    myKaRubberZoom = new kaRubberZoom( myKaMap );
    myKaTracker = new kaMouseTracker(myKaMap);
    myKaTracker.activate();
    
    myKaMap.registerForEvent( KAMAP_INITIALIZED, null, myInitialized );
    myKaMap.registerForEvent( KAMAP_MAP_INITIALIZED, null, myMapInitialized );
    myKaMap.registerForEvent( KAMAP_SCALE_CHANGED, null, myScaleChanged );
    myKaMap.registerForEvent( KAMAP_EXTENTS_CHANGED, null, myExtentChanged );
    myKaMap.registerForEvent( KAMAP_LAYERS_CHANGED, null, myLayersChanged );
    myKaMap.registerForEvent( KAMAP_LAYER_STATUS_CHANGED, null, myLayersChanged );
    myKaMap.registerForEvent( KAMAP_QUERY, null, myQuery );
    myKaMap.registerForEvent( KAMAP_MAP_CLICKED, null, myMapClicked );
    myKaMap.registerForEvent( KAMAP_MOUSE_TRACKER, null, myMouseMoved );

    /*myScalebar = new ScaleBar(1);
    myScalebar.divisions = 3;
    myScalebar.subdivisions = 2;
    myScalebar.minWidth = 150;
    myScalebar.maxWidth = 200;
    myScalebar.place('scalebar');*/

	//toolTip = new kaToolTip( myKaMap );
	
	//myKaSearch = new kaSearch( myKaMap );

	drawPage();
	myKaMap.initialize( szMap, szExtents, szCPS );
}

/**
 * event handler for KAMAP_INITIALIZED.
 *
 * at this point, ka-Map! knows what map files are available and we have
 * access to them.
 */
function myInitialized() {
    //myMapInitialized( null, myKaMap.getCurrentMap().name );
}

/**
 * event handler for KAMAP_MAP_INITIALIZED
 *
 * the scales are put into a select ... this will be used for zooming
 */
function myMapInitialized( eventID, mapName ) {
    //get list of maps and populate the maps select box
    var aMaps = myKaMap.getMaps();
    // Update map selection list if one is available
    var oSelect = document.forms[0].maps;
    if (oSelect)
    {
        var j = 0;
        //var opt = new Option( 'select a map', '', true, true );
        //oSelect[j++] = opt;
        for(var i in aMaps)
		{
        	oSelect[j++] = new Option(aMaps[i].title,aMaps[i].name, false, false);
        }

        //make sure the map is selected ...
        var oSelect = document.forms[0].maps;
        if (oSelect.options[oSelect.selectedIndex].value != mapName) {
            for(var i = 0; i < oSelect.options.length; i++ ) {
                if (oSelect.options[i].value == mapName) {
                    oSelect.options[i].selected = true;	
                    break;
                }
           }
        }
    } 
	//update the scales select
    var currentMap = myKaMap.getCurrentMap();
    var scales = currentMap.getScales();
    var currentScale=myKaMap.getCurrentScale();
    
	oSelect = document.forms[0].scales;
	if(oSelect){
		while( oSelect.options[0] ) oSelect.options[0] = null;
	    j=0;
	    for(var i in scales)
	    {
	        oSelect.options[j++] = new Option("1:"+scales[i],scales[i],false,false);
	    }
	}
    //Activate query button
    switchMode('toolPan');
    //Activate service box
    switchService('toolMapinfo');
	/* handle request for layer visibility */
	var layers = getQueryParam('layers');
	if (layers != '') {
		var map = myKaMap.getCurrentMap();
		//turn off all layers
		var allLayers = map.getAllLayers();
		for (var i=0; i<allLayers.length; i++) {
			allLayers[i].setVisibility(false);
		}
		aLayers = layers.split(',');
		for (var i=0;i<aLayers.length; i++) {
			map.setLayerVisibility (unescape(aLayers[i]), true);
		}
	}
}

/**
 * handle the extents changing by updating a link in the interface that links
 * to the current view
 */
function myExtentChanged( eventID, extents ) {
	updateLinkToView();
}

function myMouseMoved( eventID, position) {
    var geopos = document.getElementById('geoPosition');
    if(geopos) geopos.innerHTML = 'x: ' + roundIt(position.x,2) + '<BR>y: ' + roundIt(position.y,2);
}

function myLayersChanged(eventID, map) {
	updateLinkToView();
}

function updateLinkToView()  {
	var port = (window.location.port)? window.location.port : 80;
	var url = window.location.protocol+'/'+'/'+window.location.host +':'+ port +''+window.location.pathname+'?';
	var extents = myKaMap.getGeoExtents();
	var cx = (extents[2] + extents[0])/2;
	var cy = (extents[3] + extents[1])/2;
	var cpsURL = 'cps='+cx+','+cy+','+myKaMap.getCurrentScale();
	var mapURL = 'map=' + myKaMap.currentMap;
    var theMap = myKaMap.getCurrentMap();
	var aLayers = theMap.getLayers();
	var layersURL = 'layers=';
	var sep = '';
	for (var i=0;i<aLayers.length;i++) {
		layersURL += sep + aLayers[i].name;
		sep = ',';
	}

	var link = getRawObject('linkToView');
	if(link) link.href = url + mapURL + '&' + cpsURL + '&' + layersURL;
	
	var linkContent = getRawObject('linkContent');
	if(linkContent) linkContent.value = myUrlEncode('This is a link:\n-------\n'+ url + mapURL + '&' + cpsURL + '&' + layersURL +'\n-------\n\nRemember to copy the entire link string.');


	//this should stay in an independant function
	var geoExtent = getRawObject('geoExtent');
	
	if(geoExtent) {
		geoExtent.innerHTML = 'minx: ' + roundIt(extents[0],2) +'<br>' +
							'miny: ' + roundIt(extents[1],2) +'<br>' +
							'maxx: ' + roundIt(extents[2],2) +'<br>' +
							'maxy: ' + roundIt(extents[3],2) +'<br>';
	}
}


function sendLinkToView(email,body) {
	
	var mySubject = myUrlEncode('Authomatic ka-Map mail');
	var myBody = myUrlEncode(body);
		
	location.replace( 'mailto:' + email + '?subject=' + mySubject + '&body=' + body);
}



/**
 * called when kaMap tells us the scale has changed
 */
function myScaleChanged( eventID, scale ) {


	var oSelect = document.forms[0].scales;
	if(oSelect){
	    for (var i=0; i<oSelect.options.length; i++)
	    {
	        if (oSelect.options[i].value == scale)
	        {
	            oSelect.options[i].selected = true;
	            //document.forms[0].zoomout.disabled = (i==0);
	            //document.forms[0].zoomin.disabled = (i==oSelect.options.length - 1);
	        }
	    }
    }
    //todo: update scale select and enable/disable zoomin/zoomout
    var currentMap = myKaMap.getCurrentMap();
    var scales = currentMap.getScales();
    for(var i in scales){
        var imgString = 'img'+scales[i];
        var scaleString = 'img'+scale;
        if(getRawObject(imgString)) {
            if(imgString == scaleString) {
                getRawObject(scaleString).src = 'images/pixel-red.png';
            } else {
                getRawObject(imgString).src = 'images/pixel-blue.png';
            }
        }
    }
    /*myScalebar.update(scale);
   
    
    if (scale >= 1000000) {
        scale = scale / 1000000;
        scale = scale + " Million";
    }
    var outString = 'current scale 1:'+ scale;
    getRawObject('scale').innerHTML = outString;
    */
}

/**
 * called when the user changes scales.  This will cause the map to zoom to
 * the new scale and trigger a bunch of events, including:
 * KAMAP_SCALE_CHANGED
 * KAMAP_EXTENTS_CHANGED
 */
function mySetScale( scale ) {
    myKaMap.zoomToScale( scale );
}

/**
 * called when the map selection changes due to the user selecting a new map.
 * By calling myKaMap.selectMap, this triggers the KAMAP_MAP_INITIALIZED event
 * after the new map is initialized which, in turn, causes myMapInitialized
 * to be called
 */
/*function mySetMap(name)
{
	myKaMap.selectMap(name);
	
}*/

function ChangeMap()
{
	var map = document.getElementById("maps").value; //map that was requested
	if (LastVisibleMap == "none") //page is just loaded, menu was not used yet
	{
		LastVisibleMap = map;
	}
	else //menu was used already
	{
		if (LastVisibleLayer != "none") //Ship menu was used 
		{
			myKaMap.setLayerVisibility (LastVisibleLayer, false); //turn off current layer
			myKaMap.setLayerVisibility (LastVisibleMap, true); //turn on layer with all lines for current region
		}
	}
	LastVisibleLayer = "none";
	LastVisibleMap = map;
	document.getElementById("ddlShip").selectedIndex = 0;
	document.getElementById("metadata").innerHTML = '<br /><img src="../images/cdiac_ball1.jpg" /><br />';
	window.focus();
	myKaMap.selectMap(map);
}

/*this function was customised and moved to mapquery.js
function myQuery( eventID, queryType, coords )
{
   	var szLayers = '';
   	var layers = myKaMap.getCurrentMap().getQueryableLayers();
    if (layers.length == 0)
	{
		alert ("No queryable layers at this scale and extent");
     	return;
    }
    for (var i = 0; i < layers.length; i++ )
	{
        szLayers = szLayers + "," + layers[i].name;
    }
    var extent = myKaMap.getGeoExtents();
    var scale = myKaMap.getCurrentScale();
    var cMap = myKaMap.getCurrentMap().name;
	var params='map='+cMap+'&q_type='+queryType+'&scale='+scale+'&groups='+szLayers+'&coords='+coords+'&extent='+extent[0]+'|'+extent[1]+'|'+extent[2]+'|'+extent[3];
	getRawObject('queryOut').innerHTML = '<h3>Processing query. <br> please wait...</h3><hr>';
	call('map_query_float.php?'+params,this, myQueryOutput);
    alert( "Map: " + cMap + " | Scale: " + scale + " | Extent: " + extent + " | QUERY: " + queryType + " " + coords + " on layers " + szLayers );
	
}
*/

function myQueryOutput (szText){
	getRawObject('queryOut').innerHTML=szText;
}


function myMapClicked( eventID, coords ) {
    //alert( 'myMapClicked('+coords+')');
	//myKaMap.zoomTo(coords[0],coords[1]);
}

function myZoomIn() {
    myKaMap.zoomIn();
}

function myZoomOut() {
    myKaMap.zoomOut();
}

function myPrint(output_type) {
    var szLayers = '';
    
    var layers = myKaMap.getCurrentMap().getLayers();
    for (var i=0;i<layers.length;i++) {
        szLayers = szLayers + "," + layers[i].name;
    }
	
    var extent = myKaMap.getGeoExtents();
    var scale = myKaMap.getCurrentScale();
    var cMap = myKaMap.getCurrentMap().name;
    
    var img_width = '600';// pixel dimension. max_img_width set inside print_map.php
    
    //output_type
	var params='output_type='+output_type+'&map='+cMap+'&scale='+scale+'&img_width='+img_width+'&groups='+szLayers+'&extent='+extent[0]+'|'+extent[1]+'|'+extent[2]+'|'+extent[3];
 	
 	//create and download the output file
 	location.href='tools/print/print_map.php?'+params;
 
    //WOOpenWin( 'Print', 'tools/print/print_map.php?'+params, 'resizable=yes,scrollbars=yes,width=600,height=400' );
    
}

/**
 * drawPage - calculate sizes of the various divs to make the app full screen.
 */
function drawPage() {
    var browserWidth = getInsideWindowWidth();
    var browserHeight = getInsideWindowHeight();

    var viewport = getRawObject('viewport');
    var page = getRawObject('page');
    var layoutFrame = getRawObject('layoutFrame');
    var explorer = getRawObject('explorer');
    var service = getRawObject('service');

    var identifier = getRawObject('identifier');
	var print = getRawObject('print');
	
		var mapInfo = getRawObject('mapInfo');
		var legend = getRawObject('legend');
		var keymap = getRawObject('keymap');
		var link = getRawObject('link');
		var search = getRawObject('search');
		var mapLegend = getRawObject('mapLegend');
		var content = getRawObject('content');
		var contentBackground = getRawObject('contentBackground');
		var contentText = getRawObject('contentText');
		var output = getRawObject('output');
		var meta = getRawObject('meta');
		var metadata = getRawObject('metadata');
		var misha = getRawObject('misha');
		var QParam = getRawObject('QParam');
		
			
		
	//Set Viewport Width
    if(myKaMap.isIE4) {
        //terrible hack to avoid IE to show scrollbar
        page.style.width = (browserWidth -2) + "px";
    } else {
        page.style.width = browserWidth + "px";
    }
    
     if(myKaMap.isIE4) {
        //terrible hack to avoid IE to show scrollbar
        page.style.height = (browserHeight -2) + "px";
    } else {
        page.style.height = browserHeight + "px";
    }
	//layoutFrame
	layoutFrame.style.width = parseInt(page.style.width) + "px";
	layoutFrame.style.height = parseInt(page.style.height) -parseInt(getObjectHeight(explorer)) + "px";
	layoutFrame.style.top= parseInt(getObjectHeight(explorer)) + "px";
	layoutFrame.style.left="0";
	layoutFrame.style.right="0";
	
	//VIEWPORT
	viewport.style.width = parseInt(getObjectWidth(layoutFrame)) - parseInt(getObjectWidth(service)) - 10 + "px";
	viewport.style.height = parseInt(getObjectHeight(layoutFrame)) - 175  + "px"; //height of map
	viewport.style.top = "0px";
	viewport.style.left = parseInt(getObjectWidth(service)) + 8 + "px";
	viewport.style.right = "0px";
		
	//OUTPUT
	output.style.bottom = parseInt(getObjectHeight(service)) + 10 + "px";
	output.style.left = parseInt(getObjectWidth(service)) + 10 + "px";
	output.style.height = "155px";
	output.style.width = "400px";
	
	//META
	meta.style.bottom = parseInt(getObjectHeight(service)) + 10 + "px";
	meta.style.left = parseInt(getObjectWidth(service)) + 10 + parseInt(getObjectWidth(output)) + 10 + "px";
	meta.style.height = "155px";//parseInt(getObjectHeight(output)) + "px";
	meta.style.width = parseInt(getObjectWidth(viewport)) - (parseInt(getObjectWidth(output)) + 15) + "px";
		
	//METADATA
	metadata.style.height = 135 + "px";
	
	//mapTitle
	
	if (parseInt(page.style.width) > 1200)
	{
		misha.style.display = "block";
	}
	
	
	
	//CONTENT
	//content.style.top = "50px";
	content.style.left = parseInt(viewport.style.left) + 10  + "px";
	content.style.width = parseInt(viewport.style.width) - 20  + "px";
	//content.style.height = parseInt(viewport.style.height) + "px"; //-20
	content.style.height = "650px";
	contentBackground.style.height = "650px";
	//contentBackground.style.height = parseInt(viewport.style.height) + "px";//-20
	//contentText.style.height = parseInt(viewport.style.height) - 65  + "px";
	contentText.style.height = "650px";
	contentText.style.width = parseInt(viewport.style.width) - 50  + "px";
	
	//SERVICE - left space
	service.style.height = parseInt(getObjectHeight(layoutFrame)) - 2  + "px";
	//print.style.height = parseInt(getObjectHeight(layoutFrame)) -2  + "px"; 
	identifier.style.height = parseInt(getObjectHeight(layoutFrame)) - 2  + "px"; 
	//mapInfo.style.height = parseInt(getObjectHeight(layoutFrame)) - 2  + "px"; 
	mapLegend.style.height = parseInt(getObjectHeight(layoutFrame)) - 2  + "px"; 
	//link.style.height = parseInt(getObjectHeight(layoutFrame)) -2  + "px"; 
	//search.style.height = parseInt(getObjectHeight(layoutFrame)) -2  + "px";
	//mapInfo.style.height = parseInt(getObjectHeight(layoutFrame)) - 175 + 10 + parseInt(getObjectHeight(output)) + "px";
		
    myKaMap.resize();
}

function showContent(url) {
	var content = getRawObject('content');
	var viewport = getRawObject('viewport');
	content.style.top = parseInt(viewport.style.top) + 10 + "px";
	content.style.display = "block"; //added by Misha
	call(url,this, setContent);
}

function setContent(szContent){
	var contentText = getRawObject('contentText');
	contentText.innerHTML = szContent;
}
function hideContent() {
	var content = getRawObject('content');
	var viewport = getRawObject('viewport');
	content.style.display = "none"; //added by Misha
	content.style.top = parseInt(viewport.style.top) + parseInt(viewport.style.height) + "px";
}

/**
 * getFullExtent
 * ...
 */
function getFullExtent() {
    var exStr = myKaMap.getCurrentMap().defaultExtents.toString();
    var ex = myKaMap.getCurrentMap().defaultExtents;
    myKaMap.zoomToExtents(ex[0],ex[1],ex[2],ex[3]);
}

/**
 * switchMode
 * ...
 */
function switchMode(id)
{
    if (id=='toolQuery')
	{
        myKaQuery.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_2.png)';
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_1.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
    } 
	else if (id=='toolPan')
	{
        myKaNavigator.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
    } 
	else if (id=='toolZoomRubber')
	{
		myKaRubberZoom.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_1.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_2.png)';
	}
	else
	{
        myKaNavigator.activate();
    }
}


/**
 * switchService
 * ...*/
 
 
function switchService(id) {
	var service = getRawObject('service');
	
    if (id=='toolQuery') {
        myKaQuery.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_2.png)';
        //getRawObject('toolLegend').style.backgroundImage = 'url(images/icon_set_explorer/tool_legend_1.png)';
        //getRawObject('toolMapinfo').style.backgroundImage = 'url(images/icon_set_explorer/tool_mapinfo_1.png)';
        //getRawObject('toolPrint').style.backgroundImage = 'url(images/icon_set_explorer/tool_print_1.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
		//getRawObject('toolLink').style.backgroundImage = 'url(images/icon_set_explorer/tool_link_1.png)';
		//getRawObject('toolSearch').style.backgroundImage = 'url(images/icon_set_explorer/tool_search_1.png)';
        
 		getRawObject('mapInfo').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('mapLegend').style.top = parseInt(getObjectHeight(service)) + "px";
		//getRawObject('print').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('identifier').style.top = '0';
		getRawObject('curtain').style.top =  parseInt(getObjectHeight(service)) + "px";
		//getRawObject('link').style.top =  parseInt(getObjectHeight(service)) + "px";
		//getRawObject('search').style.top =  parseInt(getObjectHeight(service)) + "px";
		
    } else if (id=='toolLegend') {
        myKaNavigator.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolMapinfo').style.backgroundImage = 'url(images/icon_set_explorer/tool_mapinfo_1.png)';
        getRawObject('toolLegend').style.backgroundImage = 'url(images/icon_set_explorer/tool_legend_2.png)';
        getRawObject('toolPrint').style.backgroundImage = 'url(images/icon_set_explorer/tool_print_1.png)';
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
		getRawObject('toolLink').style.backgroundImage = 'url(images/icon_set_explorer/tool_link_1.png)';
		getRawObject('toolSearch').style.backgroundImage = 'url(images/icon_set_explorer/tool_search_1.png)';
        
		getRawObject('mapLegend').style.top = '0';
		getRawObject('mapInfo').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('print').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('identifier').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('curtain').style.top =  parseInt(getObjectHeight(service)) + "px";
		getRawObject('link').style.top =  parseInt(getObjectHeight(service)) + "px";
		getRawObject('search').style.top =  parseInt(getObjectHeight(service)) + "px";
		
    } else if (id=='toolPrint') {
        myKaNavigator.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolLegend').style.backgroundImage = 'url(images/icon_set_explorer/tool_legend_1.png)';
        getRawObject('toolMapinfo').style.backgroundImage = 'url(images/icon_set_explorer/tool_mapinfo_1.png)';
        getRawObject('toolPrint').style.backgroundImage = 'url(images/icon_set_explorer/tool_print_2.png)'; 
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
		getRawObject('toolLink').style.backgroundImage = 'url(images/icon_set_explorer/tool_link_1.png)';
		getRawObject('toolSearch').style.backgroundImage = 'url(images/icon_set_explorer/tool_search_1.png)';
        
        getRawObject('mapLegend').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('mapInfo').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('print').style.top = '0';
		getRawObject('identifier').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('curtain').style.top =  parseInt(getObjectHeight(service)) + "px";
		getRawObject('link').style.top =  parseInt(getObjectHeight(service)) + "px";
		getRawObject('search').style.top =  parseInt(getObjectHeight(service)) + "px";
		
    } else if (id=='toolLink') {
        myKaNavigator.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolLegend').style.backgroundImage = 'url(images/icon_set_explorer/tool_legend_1.png)';
        getRawObject('toolMapinfo').style.backgroundImage = 'url(images/icon_set_explorer/tool_mapinfo_1.png)';
        getRawObject('toolPrint').style.backgroundImage = 'url(images/icon_set_explorer/tool_print_1.png)'; 
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
		getRawObject('toolLink').style.backgroundImage = 'url(images/icon_set_explorer/tool_link_2.png)';
		getRawObject('toolSearch').style.backgroundImage = 'url(images/icon_set_explorer/tool_search_1.png)';
        
		getRawObject('mapInfo').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('mapLegend').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('print').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('identifier').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('curtain').style.top =  parseInt(getObjectHeight(service)) + "px";
		getRawObject('link').style.top =   "0px";
		getRawObject('search').style.top =  parseInt(getObjectHeight(service)) + "px";
	
	} else if (id=='toolSearch') {
        myKaNavigator.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolLegend').style.backgroundImage = 'url(images/icon_set_explorer/tool_legend_1.png)';
        getRawObject('toolMapinfo').style.backgroundImage = 'url(images/icon_set_explorer/tool_mapinfo_1.png)';
        getRawObject('toolPrint').style.backgroundImage = 'url(images/icon_set_explorer/tool_print_1.png)'; 
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
		getRawObject('toolLink').style.backgroundImage = 'url(images/icon_set_explorer/tool_link_1.png)';
		getRawObject('toolSearch').style.backgroundImage = 'url(images/icon_set_explorer/tool_search_2.png)';
        
		getRawObject('mapLegend').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('mapInfo').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('print').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('identifier').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('curtain').style.top =  parseInt(getObjectHeight(service)) + "px";
		getRawObject('link').style.top =   parseInt(getObjectHeight(service)) + "px";
		getRawObject('search').style.top =  "0px";
		
    } else if (id=='toolMapinfo') {
        myKaNavigator.activate();
        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        //getRawObject('toolLegend').style.backgroundImage = 'url(images/icon_set_explorer/tool_legend_1.png)';
        //getRawObject('toolMapinfo').style.backgroundImage = 'url(images/icon_set_explorer/tool_mapinfo_2.png)';
        //getRawObject('toolPrint').style.backgroundImage = 'url(images/icon_set_explorer/tool_print_1.png)'; 
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
        getRawObject('toolZoomRubber').style.backgroundImage = 'url(images/icon_set_explorer/tool_rubberzoom_1.png)';
		//getRawObject('toolLink').style.backgroundImage = 'url(images/icon_set_explorer/tool_link_1.png)';
		//getRawObject('toolSearch').style.backgroundImage = 'url(images/icon_set_explorer/tool_search_1.png)';
        
		getRawObject('mapLegend').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('mapInfo').style.top = "0px";
		//getRawObject('print').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('identifier').style.top = parseInt(getObjectHeight(service)) + "px";
		getRawObject('curtain').style.top =  parseInt(getObjectHeight(service)) + "px";
		//getRawObject('link').style.top =   parseInt(getObjectHeight(service)) + "px";
		//getRawObject('search').style.top =  parseInt(getObjectHeight(service)) + "px";
		
    } else {
        myKaNavigator.activate();
    }
}



/*
 *  applyPNGFilter(o)
 *
 *  Applies the PNG Filter Hack for IE browsers when showing 24bit PNG's
 *
 *  var o = object (this png element in the page)
 *
 * The filter is applied using a nifty feature of IE that allows javascript to
 * be executed as part of a CSS style rule - this ensures that the hack only
 * gets applied on IE browsers :)
 */
function applyPNGFilter(o) {
    var t="images/a_pixel.gif";
    if( o.src != t ) {
        var s=o.src;
        o.src = t;
        o.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+s+"',sizingMethod='scale')";
    }
}

//functions to open popup

function WOFocusWin( nn ) {
	eval( "if( this."+name+") this."+name+".moveTo(50,50); this."+name+".focus();" );
}

function WOOpenWin( name, url, ctrl ) {
    eval( "this."+name+"=window.open('"+url+"','"+name+"','"+ctrl+"');" );

    /*IE needs a delay to move forward the popup*/
    // window.setTimeout( "WOFocusWin(nome);", 300 );
}

function WinOpener() {
    this.openWin=WOOpenWin;
	this.focusWin=WOFocusWin;
}


//URL SYNTAX ENCODING
function myUrlEncode(string) {
  encodedHtml = escape(string);
  encodedHtml = encodedHtml.replace("/","%2F");
  encodedHtml = encodedHtml.replace(/\?/g,"%3F");
  encodedHtml = encodedHtml.replace(/=/g,"%3D");
  encodedHtml = encodedHtml.replace(/&/g,"%26");
  encodedHtml = encodedHtml.replace(/@/g,"%40");
  return encodedHtml;
};
  
function myUrlDecode(sz){
	return unescape(sz).replace(/\+/g," ");
};

//MATH FUNCTIONs
function roundIt(number,decimals){
	var base10 = 10;
	for(var i=0;i<decimals-1;i++)
		base10 = base10 *10;
	 
	return Math.round(number * base10)/base10;
}