/**********************************************************************
 *
 * $Id: startUp.js,v 1.5 2006/09/07 15:14:33 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.
 *
 * purpose: This is the sample ka-Map interface.  Feel free to use it 
 *          as the basis for your own applications or just to find out
 *          how ka-Map works.
 *
 * author: Lorenzo Becchi and Andrea Cappugi (www.ominiverdi.org)
 *
 * ka-Explorer interface has been developer for Food and Agriculture 
 * Organization of the United Nations (FAO-UN)
 *
 *
 **********************************************************************
 *
 * Copyright (c) 2006 Food and Agriculture Organization of the United Nations (FAO-UN)
 *
 * 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 = myKaPanoramio = myScalebar = myKaRuler = myBalloon = null;
var queryParams = null;

var myPropeditMapOverlay=null;
var myPropeditMode=1; // 0 - nije unet; 1 - po parcelama; 2 - pretraga
var myPropeditSearchArea; //minx,miny,maxx,maxy

/**
 * 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 if ((p == 'cps') && (typeof(default_x) != "undefined") && (typeof(default_y) != "undefined") && default_x && default_y) {
            return default_x+','+default_y+','+default_scale;
    } else {
        return '';
    }
}

function myOnLoad() {

	window.onresize=drawPage;

	myKaMap = new kaMap( 'viewport' );

	var szMap = getQueryParam('map');
	var iVerID = getQueryParam('ver_id');
    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 : false;
    
    var myKaLegend = new kaLegend( myKaMap, 'legend', false, legendOptions, typeof(layerTree) == "object" ? layerTree : false);
    var myKaKeymap = new kaKeymap( myKaMap, 'keymap' );
    myKaNavigator = new kaNavigator( myKaMap );
    myKaNavigator.activate();
//    myKaQuery = new kaQuery( myKaMap, KAMAP_POINT_QUERY );
    myPropedit = new kaQuery(myKaMap, KAMAP_POINT_QUERY);
    myKaPanoramio = new kaQuery( myKaMap, KAMAP_POINT_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, myPanoramioQuery );
    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');
	myToolTip = new kaToolTip( myKaMap );

	//toolTip = new kaToolTip( myKaMap );
	
	myKaUliceSearch = new kaUliceSearch( myKaMap );
    myKaRuler = new myKaRuler( myKaMap);
//test
	myBalloon = new kaBalloon( myKaMap, 'myBalloon');

	drawPage();
    //myKaMap.initialize( szMap, szExtents, szCPS );
    myKaMap.initialize( iVerID ? szMap + '&ver_id='+iVerID : 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 current_scale = default_scale;
    var szCPS = getQueryParam('cps');
    aCPS = szCPS.split(',');
    if (aCPS[2]) {
        current_scale = aCPS[2];
    }
    
	oSelect = document.forms[0].scales;
	if(oSelect){
		while( oSelect.options[0] ) oSelect.options[0] = null;
	    j=0;
	    for(var i in scales)
	    {
            var c = current_scale == scales[i] ? true : false;
            if (c) current_scale_index = j;
            var dc = default_scale == scales[i] ? true : false;
            oSelect.options[j++] = new Option("1:"+scales[i],scales[i],dc,c);
	    }
        var o = getRawObject("zoomslider_div");
        if (o) {
            var steps = oSelect.options.length-1
            o.style.display = 'block';
            $('#zoomslider').slider({ animate : true, min: steps, max: 0, steps: steps, startValue : current_scale_index, change: function(e,ui) { oSelect.selectedIndex = ui.value;mySetScale(oSelect.options[ui.value].value);} });
        }
	}
    
    

    
    //Activate query button
    switchMode('toolPan');
    
    
    
	
	/* 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);
		}
	}
    myKaStoppedQuery = new kaQuery(myKaMap, KAMAP_MOUSE_STOPPED, 1000);
    setKaStoppedQueryState();
    myKaMap.registerForEvent( KAMAP_MOUSE_STOPPED, null, myMouseOver);
    var activeTool=getQueryParam('active_tool');
	var bm = parseInt(getQueryParam('show_bm')); /*izborne jedinice*/
    var address=decodeURIComponent(getQueryParam('show_address'));
    if (address!='')
    {
        var tmp=address.split(';'); //first param is street name, second param is number [optional]
        switchService('toolSearch');
        if (tmp.length==1)
            tmp[1]='';
        var zoomLevel=getQueryParam('zoom_level');
        if (zoomLevel=='') {
            zoomLevel= tmp[1] == '' ? '2500' : '500';
        }
        myKaUliceSearch.zoomLevel=parseInt(zoomLevel);
        document.ulice_search.ulica.value = tmp[0];
        document.ulice_search.broj.value = tmp[1];
        myKaUliceSearch.searchUlice(tmp[0],tmp[1],true);
    } else if ((typeof(search) != 'undefined') && (typeof(obj) != 'undefined') && (obj == 'парцела')) {
        if (typeof(ko) == 'undefined') ko=0;
        myKaUliceSearch.searchParcele(search, ko, 0);
        //Activate service box
        switchService('toolSearch');
    } else if (activeTool=='toolIzbori') {
        switchService('toolIzbori');
    } else if (activeTool=='toolInvesticija') {
        switchService('toolInvesticija');
    } else {
        //Activate service box
        switchService('toolLegend');
    }

    if (bm>0)
    {
        myObjectShowObject('izborne_jedinice','bm='+bm,500);    
    }    
    

    myKaMap.zoomToScale(current_scale);
    
}

/**
 * handle the extents changing by updating a link in the interface that links
 * to the current view
 */
function myExtentChanged( eventID, extents ) {
	updateLinkToView();
    setKaStoppedQueryState();
}

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();
    setKaStoppedQueryState();
}

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('Позиција на мапи ГИС Ниш:\n-------\n'+ url + mapURL + '&' + cpsURL + '&' + layersURL.replace(/ /g, '%20') +'\n-------\n\nИскопирајте линк у целини!.');


	//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('ГИС Ниш - позиција на мапи');
	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);
	        }
	    }
    }
    $('#zoomslider').slider("moveTo", oSelect.selectedIndex); 
    var o = getRawObject('zoomslider_scale');
    if (o) {
        o.title = scale;
    }
    //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;
    */
    setKaStoppedQueryState();
    myKaRuler.reset();
    if (myKaRuler.state) {
        myKaRuler.deactivate();
        myKaRuler.activate();
    }

    if (myBalloon && myBalloon.visible)
    {
        myKaMap.zoomTo(myBalloon.curr_geoX,myBalloon.curr_geoY);
        
        myBalloon.moveGeo(myBalloon.curr_geoX,myBalloon.curr_geoY);
    }

}

/**
 * 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 myPanoramioQuery( eventID, queryType, coords ) {
    myBalloon.move();
    var szLayers = '';
    for (var i=0;i<layers.length;i++) {
        szLayers = 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];
		
    var url = baseurl + 'manufaktura/kaPanoramioQuery.php?'+params;
//    alert(url);
	call(url,this, myPanoramioQueryOut);

}
function myPanoramioQueryOut(resp) {
    var o = getRawObject('statPanoramio_det');
    if (o) {
        o.innerHTML = '';
        var delim1 = "_::_";
        var delim2 = "-::-";
        var aresp = resp.split(delim1);
        var count = aresp.shift();
        var first = true;
        for (var i in aresp) {
            var adet = aresp[i].split(delim2);
            o.innerHTML += decodeURIComponent(adet[3]);  
            if (first) {
                showPanoramio(adet[0],adet[1],adet[2]);
                first = false;
            }
        }
    }

}
function showPanoramio(id, x,y) {
    var o;
    if (o = getRawObject('panoramio_det_'+id)) {
        myBalloon.resizeWidth(260);
        myBalloon.resizeHeight(320);

        myBalloon.setText(o.innerHTML);
        myBalloon.moveGeo(x,y);
    }
    if (myKaMap.currentTool.name != 'kaQuery') {
        switchService('toolPanoramio');
    }
    
}

function myGPQuery( eventID, queryType, coords ) {
    var szLayers = '';
    var layers = myKaMap.getCurrentMap().getQueryableLayers();
    if(layers.length==0) {
        var warn = 'На овом делу мапе нема лејера са информацијама!';
        if (queryable_groups && queryable_groups.length) {
             warn += '\nНа легенди укључите лејере: \n"' + queryable_groups.join('", "') +'"';
        }
        alert(warn);
        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>Тражење података. <br> сачекајте...</h3><hr>';
    var url = baseurl + 'manufaktura/kaGPQuery.php?'+params;
//    alert(url);
	call(url,this, myGPQueryOutput);
	
//    alert( "Map: " + cMap + " | Scale: " + scale + " | Extent: " + extent + " | QUERY: " + queryType + " " + coords + " on layers " + szLayers );
}

function myGPQueryOutput (szText){
    var atext = szText.split('_**_');
	getRawObject('queryOut').innerHTML=atext[0];
    if (atext.length > 1) {
        aids = atext[1].split('_');
        for (i=0;i<aids.length;i++) {
            popwindow('layer_info.php?i='+aids[i], 'gup'+aids[i], 800, 600);
        }
    }
}


function myMapClicked( eventID, coords ) {
    var layer = myKaMap.getCurrentMap().getLayer('panoramio');
    if (layer && layer.visible) {
        myPanoramioQuery(eventID, KAMAP_POINT_QUERY, coords);
    }
    //alert( 'myMapClicked('+coords+')');
	//myKaMap.zoomTo(coords[0],coords[1]);
}

function myZoomIn() {
    myKaMap.zoomIn();
    setKaStoppedQueryState();
}

function myZoomOut() {
    myKaMap.zoomOut();
    setKaStoppedQueryState();
}

function myPrint(output_type, posname) {
    var szLayers = '';
    var szOpacitys = '';
    
    var layers = myKaMap.getCurrentMap().getLayers();
    for (var i=0;i<layers.length;i++) {
            szLayers = szLayers + "," + layers[i].name;
            szOpacitys = szOpacitys + "," + layers[i].opacity;
    }

    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='popup=3&output_type='+output_type+'&map='+cMap+"&opacitys="+szOpacitys+'&scale='+scale+'&img_width='+img_width+'&groups='+szLayers+'&extent='+extent[0]+'|'+extent[1]+'|'+extent[2]+'|'+extent[3]+'&posname='+encodeURIComponent(posname);
 	
 	//create and download the output file
 	location.href='manufaktura/print/print_map.php?'+params;
 	
 	//or open it in a new window
    //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('ulice_search');
		var mapLegend = getRawObject('mapLegend');
		var content = getRawObject('content');
		var contentBackground = getRawObject('contentBackground');
		var contentText = getRawObject('contentText');
        var ruler = getRawObject('ruler');
        var toolbar = getRawObject('toolbar');
		
		
	//Set Viewport Width
    if(isIE4) {
        //terrible hack to avoid IE to show scrollbar
        page.style.width = (browserWidth -2) + "px";
    } else {
        page.style.width = browserWidth + "px";
    }
    
     if(isIE4) {
        //terrible hack to avoid IE to show scrollbar
        page.style.height = (browserHeight -2) + "px";
    } else {
        page.style.height = browserHeight + "px";
    }
    var tbh = parseInt(getObjectHeight(toolbar));
    tbh = tbh + 5;
	//layoutFrame
	layoutFrame.style.width = parseInt(page.style.width) + "px";
	layoutFrame.style.height = parseInt(page.style.height) -parseInt(getObjectHeight(explorer)) -tbh + "px";
	layoutFrame.style.top= parseInt(getObjectHeight(explorer)) +tbh + "px";
	layoutFrame.style.left="0";
	layoutFrame.style.right="0";
	
	//VIEWPORT
	viewport.style.width = parseInt(getObjectWidth(layoutFrame)) - parseInt(getObjectWidth(service))-2 + "px";
	viewport.style.height = parseInt(getObjectHeight(layoutFrame)) -1  + "px";
	viewport.style.top="0px";
	viewport.style.left= parseInt(getObjectWidth(service)) + "px";
	viewport.style.right="0px";
    
    var splash = getRawObject('splash');
    if (splash) {
        splash.style.top = parseInt((viewport.offsetHeight - splash.offsetHeight)/2) + 'px';
        splash.style.left = parseInt((viewport.offsetWidth - splash.offsetWidth)/2) + 'px';
    }
	
	//CONTENT
	//content.style.top = viewport.style.top;
	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) -20  + "px";
	contentBackground.style.height = parseInt(viewport.style.height) -20  + "px";
	contentText.style.height = parseInt(viewport.style.height) -65  + "px";
	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"; 
    ruler.style.height = parseInt(getObjectHeight(layoutFrame)) -2  + "px";
	
    if (myKaMap) {	
        myKaMap.resize();
        kaObjectResize();
    }
}

function showContent(url) {
	var content = getRawObject('content');
	var viewport = getRawObject('viewport');
	content.style.top = parseInt(viewport.style.top) + 10 + "px";
	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.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]);
}

function myInitStat()
{
    if (!g_filterInit) {
        call("manufaktura/filter_user.php?action=init", this, myInitStatResp); 
        g_filterInit = true;
    }
} 
function myInitStatResp(resp)
{
        var o = getRawObject('stat_list');
        o.innerHTML = resp;
        call("manufaktura/filter_user.php",this, myInitStatRespResp);
}
function myInitStatRespResp(resp)
{
    var o;
    if (o = getRawObject("stat_div")) {
        o.innerHTML=resp;
    }
    //myAddFilterDiv();
}
/**
 * switchMode
 * ...
 */
function switchMode(id) {

    if (id=='toolQuery') {
        myKaQuery.activate();
//        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_2.png)';
        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_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_1.png)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';        
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
    } else if (id=='toolPanoramio') {
        myKaPanoramio.activate();
        myKaMap.getCurrentMap().setLayerVisibility ('panoramio', true); 
        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_2.png)';
//        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_1.png)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';        
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
    } else if (id=='toolPan') {
        myKaNavigator.activate();
        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_1.png)';
//        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)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';                
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
    } else if (id=='toolZoomRubber') {
        myKaRubberZoom.activate();
//        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myPanoramioQuery );
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myToolMapClick);

        myKaMap.registerForEvent( KAMAP_QUERY, null, myPanoramioQuery );

        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_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)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';                
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
    } else if (id=='toolRuler') {
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_2.png)';
//        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_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';                
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
    } else if (id=='toolStat')
    {
        myInitStat();
//        myKaRubberZoom.activate();
//        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_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_1.png)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_2.png)';
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
    } else if (id=='toolHepatitis')
    {
//        myKaRubberZoom.activate();
//        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_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_1.png)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_2.png)';
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
    } else if (id=='toolIzbori') {
//        myKaRubberZoom.activate();
//        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_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_1.png)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_2.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
    } else if (id=='toolInvesticija') {
//        myKaRubberZoom.activate();
//        getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_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_1.png)';
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_2.png)';
    }
    else {
        myKaNavigator.activate();
    }
}


/**
 * switchMode
 * ...
 */
function switchService(id) {
	var service = getRawObject('service');

//    getRawObject('toolQuery').style.backgroundImage = 'url(images/icon_set_explorer/tool_query_1.png)';
    getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_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('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('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_1.png)';
    getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_1.png)';
    getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_1.png)';
    getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_1.png)';
    getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_1.png)';
    getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_1.png)';
    getRawObject('toolPropedit').style.backgroundImage = 'url(images/icon_set_explorer/objekti_1.png)';
		
    getRawObject('mapLegend').style.display = 'none'; 
    getRawObject('mapInfo').style.display = 'none';
    getRawObject('print').style.display = 'none';
    getRawObject('identifier').style.display = 'none';
    getRawObject('curtain').style.display = 'none';
    getRawObject('link').style.display = 'none';
    getRawObject('ulice_search').style.display = 'none';
    getRawObject('ruler').style.display = 'none';
    getRawObject('statInfo').style.display = 'none';
    getRawObject('statHepatitis').style.display = 'none';
    getRawObject('statIzbori').style.display = 'none';
    getRawObject('statPanoramio').style.display = 'none';
    getRawObject('searchInvesticija').style.display = 'none';
    getRawObject('propedit').style.display = 'none';

    //if (id!='toolStat')
//        myKaMap.getCurrentMap().setLayerVisibility ('Statistika', false);
    

    if (id=='toolQuery') {
        myKaQuery.activate();
        getRawObject('identifier').style.display = 'block';
    } else if (id=='toolPanoramio') {
        
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myPanoramioQuery );
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myToolMapClick);

        myKaMap.registerForEvent( KAMAP_QUERY, null, myPanoramioQuery );

        myKaPanoramio.activate();
        myKaMap.getCurrentMap().getLayer('panoramio').setVisibility(true);
        myKaMap.setMapLayers(); 
        myKaMap.getCurrentMap().setLayerVisibility ('panoramio', true);

        getRawObject('toolPanoramio').style.backgroundImage = 'url(images/icon_set_explorer/tool_panoramio_2.png)';
        getRawObject('statPanoramio').style.display = 'block';
    } else if (id=='toolLegend') {
        myKaNavigator.activate();
		getRawObject('mapLegend').style.display = 'block';
    } else if (id=='toolPrint') {
        myKaNavigator.activate();
//        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('print').style.display = 'block';
    } else if (id=='toolLink') {
        myKaNavigator.activate();
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
		getRawObject('toolLink').style.backgroundImage = 'url(images/icon_set_explorer/tool_link_2.png)';
		getRawObject('link').style.display = "block";
	} else if (id=='toolSearch') {
        myKaNavigator.activate();
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
		getRawObject('toolSearch').style.backgroundImage = 'url(images/icon_set_explorer/tool_search_2.png)';
		getRawObject('ulice_search').style.display =  "block";
    } else if (id=='toolMapinfo') {
        myKaNavigator.activate();
        getRawObject('toolMapinfo').style.backgroundImage = 'url(images/icon_set_explorer/tool_mapinfo_2.png)';
        getRawObject('toolPan').style.backgroundImage = 'url(images/icon_set_explorer/tool_pan_2.png)';
		getRawObject('mapInfo').style.display = "block";
    } else if (id=='toolRuler') {
        myKaRuler.activate();
        getRawObject('toolRuler').style.backgroundImage = 'url(images/icon_set_explorer/tool_ruler_2.png)';
        getRawObject('ruler').style.display = 'block';
    } else if (id=='toolStat') {
        myInitStat();
        getRawObject('toolStat').style.backgroundImage = 'url(images/icon_set_explorer/tool_stat_2.png)';
        getRawObject('statInfo').style.display = 'block';
    } else if (id=='toolHepatitis') {
        getRawObject('toolHepatitis').style.backgroundImage = 'url(images/icon_set_explorer/vlo_2.png)';
        getRawObject('statHepatitis').style.display = 'block';
        myObjectInitSearch('zutica','statHepatitis','search'); 
    } else if (id=='toolIzbori') {
        getRawObject('toolIzbori').style.backgroundImage = 'url(images/icon_set_explorer/tool_izbori_2.png)';
        getRawObject('statIzbori').style.display = 'block';
        g_result_mark_size = 24;
        g_result_mark_on_size = 24;
        myObjectInitSearch('izborne_jedinice','statIzbori','empty_search'); 
    } else if (id=='toolInvesticija') {
        getRawObject('toolInvesticija').style.backgroundImage = 'url(images/icon_set_explorer/tool_izgradnja_2.png)';
        getRawObject('searchInvesticija').style.display = 'block';
        
        myKaMap.getCurrentMap().getLayer('investicije').setVisibility(true);
        myKaMap.setMapLayers(); 
        myKaMap.getCurrentMap().setLayerVisibility ('investicije', true);
        
        g_current_tool = 'investicija';
        
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myPanoramioQuery );
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myToolMapClick);
        
        myKaMap.registerForEvent( KAMAP_QUERY, null, myToolMapClick);

        g_result_mark_size = 24;
        g_result_mark_on_size = 24;
        
        myPropedit.activate();

        if (!g_search_initialized['investicija']) {
            myObjectInitSearch('investicija','searchInvesticija','search'); 
        }
    } else if (id=='toolPropedit') {
        getRawObject('toolPropedit').style.backgroundImage = 'url(images/icon_set_explorer/objekti_2.png)';
        getRawObject('propedit').style.display = 'block';
        g_current_tool = 'object_ni';
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myPanoramioQuery );
        myKaMap.deregisterForEvent( KAMAP_QUERY, null, myToolMapClick);
        g_result_mark_size = 24;
        g_result_mark_on_size = 24;

        myKaMap.registerForEvent( KAMAP_QUERY, null, myToolMapClick);
        myPropedit.activate();
        if (!g_search_initialized['object_ni']) {
            myObjectInitSearch('object_ni','propedit','search');
        }
    } 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;
}

function setKaStoppedQueryState() {
    if (typeof(myKaStoppedQuery) == 'undefined') return false;
    var layer;
    if (layer = myKaMap.getCurrentMap().getLayer('gup')) {
        if(layer.visible) {
            myKaStoppedQuery.activate();
        } else {
            myKaStoppedQuery.deactivate();
            hideToolTip();
        }
    }
}
function myMouseOver(eventID, queryType, coords) {
    if (coords == undefined) return false;
    myKaStoppedQuery.deactivate();
    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 params='map='+cMap+'&scale='+scale+'&groups='+szLayers+'&coords='+coords+'&extent='+extent[0]+'|'+extent[1]+'|'+extent[2]+'|'+extent[3];
    var url = baseurl + 'manufaktura/kaMouseOverQuery.php?'+params;
	call(url,this, myMouseOverOutput);

}
function myMouseOverOutput (szText) {
    if (szText != "empty") {
        var a = szText.split('_::_');
        var x = a.shift();
        var y = a.shift();
        var tip = '<b>' + 'Генерални план:' + '</b>';
        tip +='<br>';
        tip += a.join('<br>');
        tip += '<div class="close"><a class="ql" onclick="myToolTip.move();">затвори</a></div>';
        myToolTip.setText(tip);
        myToolTip.moveGeo(x, y);
//        setTimeout('hideToolTip()', 1000);
//        alert(tip);
    }
    setKaStoppedQueryState();
}
function hideToolTip() {
    myToolTip.move();
}
function showPopis() {
    var map = myKaMap.getCurrentMap();
    var layers = map.getLayers();
    var added = false;
    for (var i=0;i<layers.length;i++) {
        if (layers[i].name == "Popis") added = true;
    }
    if (!added) {
        map.addLayer(new _layer( { name:'Popis',visible:true,opacity:40,imageformata:'PNG',queryable:false,tileSource:'dynamic',redrawInterval:-1,refreshInterval:-1,scales: new Array('1','1','1','1','1','1','1','1','1','1','1')}));
        myKaMap.setMapLayers();
    }

}
