// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
// ---------------------------------------------------------------------------
// Backup version 20061108

var WinOpenParams = "scrollbars=1, resizable=1,status=1,toolbar=0,location=0,menubar=0,directories=0,width=500,height=120"
var metersToMiles=0.0006213712;
var UseMeters = true;
var IsGetFeatureInfoWMSFormat = true;
var scalebar_width_min = 12;
var scalebar_width_max = 150;
var TypedWidth = new Array(13); 
    TypedWidth[0] = 0.5;   
    TypedWidth[1] = 1;  
    TypedWidth[2] = 5;
    TypedWidth[3] = 10;
    TypedWidth[4] = 50;
    TypedWidth[5] = 100;
    TypedWidth[6] = 500;
    TypedWidth[7] = 1000;
    TypedWidth[8] = 5000;
    TypedWidth[9] = 10000;
    TypedWidth[10] = 50000;
    TypedWidth[11] = 100000;
    TypedWidth[12] = 500000;
     
function appTrace(msg)
{
    document.getElementById("divTrace").innerHTML+=msg+"<br />";
}

function GetZoomLevel(obj,zoomLevel)
{
    return obj.zoomLevels[zoomLevel]*parseFloat(obj.container.style.width.split('px')[0]);
}

function setFixedZoom(obj, zoomval)
{
  //  alert("setFixedZoom = zoomLevel="+obj.zoomLevel+" zoomval="+zoomval);
    if (zoomval>1)
    {
        if (obj.zoomLevel-1>=0)
            obj.zoomLevel = obj.zoomLevel-1;
    }       
    else
    {
         if (obj.zoomLevel+1<obj.zoomLevels.length)
            obj.zoomLevel = obj.zoomLevel+1;
    }  
 
    return parseFloat(obj.zoomLevels[obj.zoomLevel]) * parseFloat(obj.container.style.width.split('px')[0]);
}

function setZoomLevel(obj, zoom)
{
    var zoomLevel=0;
    var counter=0;    
    //appTrace("setZoomLevel start - zoom:"+zoom);
    if (zoom<=GetZoomLevel(obj,0))
    {
        obj.zoomLevel=0;
        //appTrace("setZoomLevel if 1 - :"+ GetZoomLevel(obj,obj.zoomLevel));
        return;
    }
    for(counter=0; counter<obj.zoomLevels.length; counter++)
    {
        if (zoom==GetZoomLevel(obj,counter))
        {
            obj.zoomLevel=counter;
        //    appTrace("setZoomLevel if 2 - :"+ GetZoomLevel(obj,obj.zoomLevel));
            return;
        }
        if (zoom>GetZoomLevel(obj,counter))
            if (counter<obj.zoomLevels.length)
                if (zoom<GetZoomLevel(obj,counter+1))
                {                   
                    obj.zoomLevel=counter+1;
      //              appTrace("setZoomLevel if 3 - :"+ GetZoomLevel(obj,obj.zoomLevel));
                    return;
                }
    }
    obj.zoomLevel = obj.zoomLevels.length-1;
    //appTrace("setZoomLevel end - :"+ GetZoomLevel(obj,obj.zoomLevel));
}

function Reset() {
   tStart = null;

   document.theTimer.theTime.value = "00:00";
}

function SharpMap_Init(container, toolTipLayer,map1,map2,statusbar,statustext, imgLoading, InZoomLevels)
{
	var obj = new Object();
	obj.zoomLevel = InZoomLevels.length-1;
	obj.zoomLevels = InZoomLevels;
	obj.currTool = "pan";
	obj.currMap = 1;
	obj.mapReady = 1;
	obj.zoomEnded = 1;
	obj.zoomAmount = 3.0;
	obj.hiddenLayers = "none";
	obj.clickEvent = null;
	obj.toolTip = null;
	obj.clickEventActive = false;
	obj.ToolTips = new Array();
	obj.ImageLoading = WebForm_GetElementById(imgLoading);
	obj.toggleClickEvent = function() { obj.clickEventActive = (!obj.clickEventActive); }
	obj.disableClickEvent = function() { obj.clickEventActive = false; }
	obj.enableClickEvent = function() { obj.clickEventActive = true; }
	obj.setClickEvent = function(fnc) { obj.clickEvent = fnc; }
	obj.container = WebForm_GetElementById(container);
	obj.toolTipLayer = WebForm_GetElementById(toolTipLayer);
	obj.map1 = WebForm_GetElementById(map1);
	obj.map2 = WebForm_GetElementById(map2);
	obj.LayerActive = "Mapa";
	SetLayerActiveByButtonState(obj);
    obj.ResponsePage = "maphandler.aspx";
    obj.GetFeatureInfoXML;
	if(statusbar!='') { obj.statusbar = WebForm_GetElementById(statusbar); obj.statusText = statustext; }
	//Methods
	obj.VisibleMap = function() { if(obj.currMap==1) return obj.map1; else return obj.map2; }
	obj.HiddenMap =  function() { if(obj.currMap==2) return obj.map1; else return obj.map2; }
	obj.GetCenter =  function() { return SharpMap_GetCenter(obj); }
	//Events
	obj.container.onmousemove = function(event) { SharpMap_MapMouseOver(event,obj); }
	if(statusbar!='') obj.container.onmouseout = function(event) { obj.statusbar.innerHTML = ''; }
	obj.container.onmousewheel = function(event) { SharpMap_MouseWheel(event,obj); return false;}
	if(obj.container.addEventListener)
		obj.container.addEventListener('DOMMouseScroll', function(event) { SharpMap_MozillaMouseWheel(event,obj); }, false);
	obj.container.onresize = function(event) { SharpMap_ResizeTimeout(event,obj); }
	obj.container.onselectstart = function() { return false; }
	obj.container.ondrag = function(event) { return false; }
	obj.container.onmousedown = function(event) { SharpMap_MouseDown(event,obj); return false; }
	obj.container.onmouseup = function(event) { SharpMap_MouseUp(event,obj); return false; }
	document.onmouseup = function(event) { Outside_MouseUp(event,obj); return false; }
	return obj;
	}

/* Lets the user select which tool to use */
function SharpMap_ToolSelect(obj, tool){
	obj.currTool=tool;
    if (obj.currTool=="pan")
    {
        obj.container.style.cursor = "url('img/grab.cur'), pointer";
    }
    else
    {
        obj.container.style.cursor = "";
    }
}

/* Called when the mousewheel-scroll event occurs on the map  */
function SharpMap_MouseWheel(event,obj){
    
	var e = event || window.event;
	if (e.type == 'mousewheel' && obj.mapReady && obj.zoomEnded==1) {

		var zoomval = obj.zoomAmount;
		if(e.wheelDelta<0) { zoomval = 1/obj.zoomAmount; }
		
		if (obj.zoom != obj.maxzoom && obj.zoom != obj.minzoom)
		{
		    // alert("obj.zoom=" + obj.zoom);
		    SharpMap_BeginZoom(obj,e.clientX,e.clientY,zoomval);
    
    	    // refresh small map image
    		SetSmallMapImageSrc(obj);
		}
	}
}

/* this intermediate wheel function is used for mousewheel compatibility in Mozilla browsers */
function SharpMap_MozillaMouseWheel(event,obj){
	var e = new Object;
	e.type = 'mousewheel';
	e.wheelDelta = -event.detail;
	e.clientX = event.screenX;
	e.clientY = event.screenY;
	SharpMap_MouseWheel(e,obj);
}

var startDrag = null;

/* MouseDown - Occurs when potentially starting a drag event */ 
function SharpMap_MouseDown(event,obj){
	var e = event || window.event;
	if(obj.zoomEnded==1 && obj.mapReady==1){
		if(!SharpMap_IsDefined(startDrag)){
			startDrag=SharpMap_GetRelativePosition(e.clientX,e.clientY,obj.container);
			switch(obj.currTool){
				case "boxZoom":
					obj.zoomLayer.style.visibility = "visible";
					obj.zoomLayer.style.left = startDrag.x+"px";
					obj.zoomLayer.style.top = startDrag.y+"px";
					obj.zoomLayer.style.width = "0px";
					obj.zoomLayer.style.height = "0px";
					obj.container.style.cursor = "crosshair";
					break;
				case "measure":
					obj.container.style.cursor = "crosshair";
					break;
			}
		}
	}
}

function ValidateBoxZoomDrag(startDrag,endDrag)
{
    var dx=endDrag.x-startDrag.x;
    var dy=endDrag.y-startDrag.y;		
    if(Math.abs(dx)>20 || Math.abs(dy)>20) //we are dragging  
         return true;
    else return false;
}

/* MouseUp - Occurs during a drag event or when doing a click */ 
function SharpMap_MouseUp(event,obj)
{
    var e = event || window.event;
    if(obj.zoomEnded==1 && obj.mapReady==1 && SharpMap_IsDefined(startDrag) && SharpMap_IsDefined(endDrag))
	{
		switch(obj.currTool)
		{
			case "pan":
				var dx=endDrag.x-startDrag.x;
				var dy=endDrag.y-startDrag.y;				
				if(Math.abs(dx)>4 || Math.abs(dy)>4)
				{
				    //we are dragging
					SharpMap_PanEnd(obj,dx,dy);
			    }
				else
				{
				    // we are not dragging so lets do the click event				    
				    if(obj.clickEventActive && obj.clickEvent!=null)
				    {				        
				      obj.clickEvent(e,obj);
    				}    				
				    else 
				    {				    
				        if (obj.zoom != obj.minzoom && obj.zoom != obj.maxzoom)
				        {				            
    					    SharpMap_BeginZoom(obj,e.clientX,e.clientY,obj.zoomAmount);										
    					}
	                }
	            }
				break;
			case "boxZoom":
				obj.zoomLayer.style.visibility = "hidden";
				obj.container.style.cursor = "default";
				var zoomBox = SharpMap_GetBoundingBox(obj,startDrag,endDrag);
				if (ValidateBoxZoomDrag(startDrag,endDrag))
				    SharpMap_BoxBeginZoom(obj,zoomBox.centerX,zoomBox.centerY,zoomBox.zoomAmount);				    
				break;
			case "measure":
				jg.clear();
				obj.measure.style.visibility="hidden";
				obj.container.style.cursor = "default";
				break;
		}
	}
	else 
	{	    
    	if(obj.clickEventActive && obj.clickEvent!=null)
    	{
    	    obj.clickEvent(e,obj);   		    		
        }
	    else
	    // we are not dragging so lets do the click event
	    {
	        SharpMap_BeginZoom(obj,e.clientX,e.clientY,obj.zoomAmount);
	    }
	}
	
	startDrag=null;	
	endDrag=null;			
	
	// refresh small map image if needed
    SetSmallMapImageSrc(ajaxMapObj);
	
	return false;
}

function SharpMap_PanEnd(obj,dx,dy){
	var center = SharpMap_PixelToMap(obj.container.offsetWidth*0.5-dx,obj.container.offsetHeight*0.5-dy,obj);
	obj.minX=center.x-obj.zoom*0.5;
	obj.maxY=center.y+obj.zoom/obj.container.offsetWidth*obj.container.offsetHeight*0.5;
	obj.mapReady = 0;
	SharpMap_BeginRefreshMap(obj,1);
	
	// refresh small map image if needed
	SetSmallMapImageSrc(ajaxMapObj);
}

function Outside_MouseUp(event,obj){
var e = event || window.event;
	//if(e.button==1 && obj.zoomEnded==1 && obj.mapReady==1 && SharpMap_IsDefined(startDrag) && SharpMap_IsDefined(endDrag))
	if( obj.zoomEnded==1 && obj.mapReady==1 && SharpMap_IsDefined(startDrag) && SharpMap_IsDefined(endDrag))
	{
		switch(obj.currTool){
			case "pan":
				var dx=endDrag.x-startDrag.x;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
				var dy=endDrag.y-startDrag.y;
								// if(Math.abs(dx)>4 || Math.abs(dy)>4) //we are dragging				
				if(Math.abs(dx)>4 || Math.abs(dy)>4) //we are dragging
					SharpMap_PanEnd(obj,dx,dy);
				else // we are not dragging so lets do the click event
					SharpMap_BeginZoom(obj,e.clientX,e.clientY,obj.zoomAmount);
				break;
			case "boxZoom":
				obj.zoomLayer.style.visibility = "hidden";
				obj.container.style.cursor = "default";
				var zoomBox = SharpMap_GetBoundingBox(obj,startDrag,endDrag);
				SharpMap_BoxBeginZoom(obj,zoomBox.centerX,zoomBox.centerY,zoomBox.zoomAmount);
				break;
			case "measure":
				jg.clear();
				obj.measure.style.visibility="hidden";
				obj.container.style.cursor = "default";
				break;
		}
	startDrag=null;	
	endDrag=null;
	}	
	return false;
}

/*Sets to point within the bounds of the control even if mouse is outside*/
function SharpMap_SetValid(p,obj){
	 if (p.x > obj.container.offsetWidth) p.x = obj.container.offsetWidth; 
	 if (p.x < 0) p.x = 0;
	 if (p.y < 0) p.y = 0;
	 if (p.y > obj.container.offsetHeight) p.y = obj.container.offsetHeight;
	 return p;
}

var endDrag = null;
//var endMeasure = null;

function SharpMap_MapMouseOver(event,obj){
	if(SharpMap_IsDefined(startDrag)) {
		var e = event || window.event;
		endDrag=SharpMap_GetRelativePosition(e.clientX,e.clientY,obj.container);
		endDrag=SharpMap_SetValid(endDrag,obj);
		switch(obj.currTool){
			case "pan":
				var dx=endDrag.x-startDrag.x;
				var dy=endDrag.y-startDrag.y;
                
				var img=obj.map1;
				if(obj.currMap==2) img=obj.map2;
				img.style.left=dx+'px';
				img.style.top=dy+'px';
                obj.toolTipLayer.style.left =img.style.left;
                obj.toolTipLayer.style.top =img.style.top;
				if (obj.toolTip!=null)
				{				    
                    endDragToolTip=SharpMap_GetRelativePosition(e.clientX,e.clientY,obj); 
                    var dxToolTip=endDragToolTip.x- parseInt( toolTip.getAttribute("StartDragX"));
				    var dyToolTip=endDragToolTip.y-parseInt( toolTip.getAttribute("StartDragY"));
				    obj.toolTip.style.left = dxToolTip;
                    obj.toolTip.style.top = dyToolTip;
                }
				obj.container.style.cursor="url('img/grabbing.cur'),move";
				// SharpMap_RedrawSmallMap(obj, dx, dy);
				break;
			case "boxZoom":
				var w=parseFloat(endDrag.x) - parseFloat(startDrag.x);
				var h=parseFloat(endDrag.y) - parseFloat(startDrag.y);

				if (w < 0)
					obj.zoomLayer.style.left = endDrag.x+"px";

				obj.zoomLayer.style.width = Math.abs(w)+"px";

				if (h < 0)
					obj.zoomLayer.style.top = endDrag.y+"px";

				obj.zoomLayer.style.height = Math.abs(h)+"px";
				break;
			case "measure":
				SharpMap_Measure(obj,startDrag,endDrag);
				break;
		}
	}
	var position = WebForm_GetElementPosition(obj.container);
	var scale;
	var e = event || window.event;
	var pos = SharpMap_PixelToMap(e.clientX-position.x,e.clientY-position.y,obj);
	var round = Math.floor(-Math.log(obj.zoom/obj.container.offsetWidth));
	var zoom = obj.zoom;
	if(round>0) {
		round = Math.pow(10,round);
		pos.x = Math.round(pos.x*round)/round;
		pos.y = Math.round(pos.y*round)/round;
		zoom = Math.round(zoom*round)/round;
	}
	else {
		pos.x = Math.round(pos.x);
		pos.y = Math.round(pos.y);
		zoom = Math.round(zoom);
	}
	obj.statusbar.innerHTML = ''+pos.x+', '+pos.y+' - Map width='+zoom+'';
	
}

/* Begins zooming around the point x,y */
function SharpMap_BeginZoom(obj,x,y,zoomval)
{
    var lower, upper;
    
    // get screen width to reach current map size
    var ScreenWidth = parseInt(obj.container.style.width.substring(0, obj.container.style.width.length-2));
        
    // and set ajaxMapObj.maxZoom according to map size
    switch (ScreenWidth)
    {
        case 350:
            upper = 358400; // 350 * 1024                        
            lower = 175;    // 350 * 0.5            
            break;
        case 500:
            upper = 512000; // 500 * 1024            
            lower = 250;    // 500 * 0.5            
            break;
        case 670:
            upper = 686080; // 670 * 1024            
            lower = 335;    // 670 * 0.5            
            break;
    }
    
    if (((zoomval > 1) && (obj.zoom > lower)) || ((zoomval < 1) && (obj.zoom < upper)))
    {
	    if(obj.zoomEnded==0) return;
	    obj.zoomEnded=0;
	    obj.container.style.cursor = 'wait';
	    var position = WebForm_GetElementPosition(obj.container);
    	var imgX = x-position.x;
    	var imgY = y-position.y;	
        var actualZoom = setFixedZoom(obj,zoomval);
        zoomval = obj.zoom/actualZoom;
    	if(obj.zoom/zoomval<obj.minZoom) zoomval = obj.zoom/obj.minZoom;
    	//if(obj.zoom/zoomval>obj.maxZoom) zoomval = obj.zoom/obj.maxZoom;
    	if(obj.zoom/zoomval>upper) zoomval = obj.zoom/upper;
    	var center = SharpMap_PixelToMap(imgX+(obj.container.offsetWidth*0.5-imgX)/zoomval,imgY+(obj.container.offsetHeight*0.5-imgY)/zoomval,obj);
    ///
        obj.zoom = actualZoom;
    ///// 
    //	obj.zoom = obj.zoom/zoomval;
	    obj.minX = center.x - obj.zoom*0.5;
	    obj.maxY = center.y + obj.zoom*obj.container.offsetHeight/obj.container.offsetWidth*0.5;
	    SharpMap_BeginRefreshMap(obj,1); //Start refreshing the map while we're zooming
	    obj.zoomEnded = 0;
	    SharpMap_DynamicZoom((position.x-x)*(zoomval-1),(position.y-y)*(zoomval-1),zoomval,0.0,obj);
	}
}

/* Zoom to the original extents of the map */
//function SharpMap_ZoomExtents(obj)
//{
//    zoomval = obj.zoom/obj.maxZoom;
//    
//    obj.zoom = obj.maxZoom;
//        	
//	obj.minX = obj.defMinX;
//	obj.maxY = obj.defMaxY;
//	
//	setZoomLevel(obj, obj.zoom);	
//		
//	SharpMap_BeginRefreshMap(obj, 1); //Start refreshing the map while we're zooming
//	obj.zoomEnded = 0;
//	SharpMap_DynamicZoom(obj.minX*zoomval, obj.maxY*zoomval, zoomval, 0.0, obj);
//	
//	SetSmallMapImageSrc(ajaxMapObj);
//}

/*update the scale bar based on current view*/
function SharpMap_RedrawScaleBar(obj){    
    if (UseMeters)
    {        
        var scale =  (obj.zoom/obj.container.offsetWidth);    
        var temp;
        var width;
        var TypedWidthSelected = -1;

        for (i=0;i<TypedWidth.length;i++)
        {
            temp = TypedWidth[i]/scale;
            if (temp>=scalebar_width_min && temp<=scalebar_width_max )
            {   
                width = temp;
                TypedWidthSelected = i;
            }
        }     
         obj.scalebar.style.width = parseInt(width) + "px";         
         if (TypedWidth[TypedWidthSelected]>=1000)
         {
            obj.scaletext.innerText = TypedWidth[TypedWidthSelected]/1000 +" km";
            obj.scaletext.textContent = TypedWidth[TypedWidthSelected]/1000 +" km";
         }
         else
         {
            obj.scaletext.innerText = TypedWidth[TypedWidthSelected] +" m";
            obj.scaletext.textContent = TypedWidth[TypedWidthSelected] +" m";
         }
    }
    else
    {
	    var scale = (obj.zoom/obj.container.offsetWidth) * metersToMiles;
	    if(scale>0.049) {
		    obj.scalebar.style.width = 5/scale;
		    obj.scaletext.innerText = "5 mi";
		    obj.scaletext.textContent = "5 mi";
	    } else if(scale>0.016) {
		    obj.scalebar.style.width = 1/scale;
		    obj.scaletext.innerText = "1 mi";
		    obj.scaletext.textContent = "1 mi";		    
	    } else if(scale>0.005) {
		    obj.scalebar.style.width = 0.5/scale;
		    obj.scaletext.innerText = "0.5 mi";
		    obj.scaletext.textContent = "0.5 mi";
	    } else if(scale>0.0018){
		    obj.scalebar.style.width = 0.1893939/scale;
		    obj.scaletext.innerText = "1000 ft";
		    obj.scaletext.textContent = "1000 ft";
	    } else if(scale>0.0006){
		    obj.scalebar.style.width = 0.09469697/scale;
		    obj.scaletext.innerText = "500 ft";
		    obj.scaletext.textContent = "500 ft";
	    } else if(scale>0.0002){
		    obj.scalebar.style.width = 0.01893939/scale;
		    obj.scaletext.innerText = "100 ft";
		    obj.scaletext.textContent = "100 ft";
	    } else {
		    obj.scalebar.style.width = 0.009469697/scale;
		    obj.scaletext.innerText = "50 ft";
		    obj.scaletext.textContent = "50 ft";
	    }
	 }
}

/* Recursive method started by SharpMap_BeginZoom */
function SharpMap_DynamicZoom(tox,toy,toscale,step,obj)
{
    //alert(" tox:"+tox+" toy:" +y+" toscale:"+toscale+" step:"+step +" obj:"+obj);
	step = step + 0.2;
	var imgd = obj.VisibleMap();
	var width = Math.round(obj.container.offsetWidth * ((toscale-1.0)*step+1.0)) +'px';
	var height = Math.round(obj.container.offsetHeight * ((toscale-1.0)*step+1.0))+'px';
	var left = Math.round(tox*step)+'px';
	var top = Math.round(toy*step)+'px';
	imgd.style.width = width;
	imgd.style.height = height;
	imgd.style.left = left;
	imgd.style.top = top;
	
	if(step < 0.99)
	{
		var delegate = function()
		{
		    SharpMap_DynamicZoom(tox,toy,toscale,step,obj);
		};
	
		setTimeout(delegate,obj.zoomSpeed);
	}
	else
	{
		obj.zoomEnded=1;
		if(obj.mapReady==1)
		{
		    SharpMap_BeginFade(obj);
		}
	}
	
	SharpMap_RedrawScaleBar(obj);
}

/* Starts the fading from one image to the other */
function SharpMap_BeginFade(obj)
{
    // alert("SharpMap_BeginFade: obj.zoom = " + obj.zoom + ", obj.minZoom = " + obj.minZoom + ", obj.maxZoom=" + obj.maxZoom);

	obj.container.style.cursor = 'wait';
	var to=obj.HiddenMap();
	var from=obj.VisibleMap();
	to.style.zIndex = 10;
	from.style.zIndex = 9;
	to.style.width = '';
	to.style.height = '';
	to.style.left = '';
	to.style.top = '';
	from.onload = ''; //Clear the onload event
	SharpMap_SetOpacity(to,0);
	to.style.visibility='visible';
	if(obj.onViewChange)
		obj.onViewChange();
	if(obj.currMap==2) { obj.currMap=1; } else { obj.currMap=2; }
	SharpMap_Fade(20,20,from,to,obj);
}

/* Recursive method started from SharpMap_BeginFade */
function SharpMap_Fade(value,step,from,to,obj){
	SharpMap_SetOpacity(to,value);
	if(value < 100) { 
		var delegate = function() { SharpMap_Fade((value+step),step,from,to,obj); };
		setTimeout(delegate,obj.fadeSpeed);
	}
	else {
		from.style.visibility='hidden';
		if (obj.currTool=="pan")
            obj.container.style.cursor = "url('img/grab.cur'), pointer";            
        else 
         obj.container.style.cursor = 'auto';
	}
}

/* Resize handle and method of responding to window/map resizing */
var resizeHandle;
function SharpMap_ResizeTimeout(event,obj){
	/*
	if (resizeHandle!=0) { clearTimeout(resizeHandle); }
	var delegate = function() { SharpMap_BeginRefreshMap(obj,1); };
	resizeHandle = setTimeout(delegate,500);
	*/
}

//postavlja u js aktivni lejer na osnovu selektovanog dugmeta za prikaz
function SetLayerActiveByButtonState(obj)
{
    var lnkPrikaz_Mapa = document.getElementById("lnkPrikaz_Mapa");
    var lnkPrikaz_Hibrid = document.getElementById("lnkPrikaz_Hibrid");
    var lnkPrikaz_FotoKarta= document.getElementById("lnkPrikaz_FotoKarta");
	
	if (lnkPrikaz_Mapa.style.fontWeight == "bold")
	{
	    obj.LayerActive = "Mapa";
	    // PageMethods.ChangeLayerActive("Mapa");
	}
	else
	{
	    if (lnkPrikaz_Hibrid.style.fontWeight == "bold")
	    {
	        obj.LayerActive = "Hibrid";
	        // PageMethods.ChangeLayerActive("Hibrid");
	    }
	    else
	    {
	        if (lnkPrikaz_FotoKarta.style.fontWeight == "bold")
	        {
	            obj.LayerActive = "FotoKarta";
	            // PageMethods.ChangeLayerActive("FotoKarta");
	        }
	        else
	        {
	            obj.LayerActive = "Hibrid";
	            
	            // lnkPrikaz_Hibrid.style.fontWeight = "bold";           
                // imgPrikaz_Hibrid.src = "img/DugmeOrange.png";	            
	            
	            // PageMethods.ChangeLayerActive("Hibrid");
	        }
	    }
	}
}

/* Requests a new map from the server using async callback and starts fading when the image have been retrieved*/
function SharpMap_BeginRefreshMap(obj, dofade)
{
    // alert("SharpMap_BeginRefreshMap: obj.zoom = " + obj.zoom + ", obj.minZoom = " + obj.minZoom + ", obj.maxZoom=" + obj.maxZoom + ", dofade=" + dofade);
    
  SetLayerActiveByButtonState(obj);
    
    var width, height;
    
    if ((typeof(obj.container.offsetWidth) == "undefined") || (typeof(obj.container.offsetHeight) == "undefined"))
    {
        width = 500;
        height = 500;   
    }
    else
    {
        width = obj.container.offsetWidth;
        height = obj.container.offsetHeight;
    }    

	var center = SharpMap_GetCenter(obj);
	
	var delegate = function(url) { SharpMap_GetCallbackResponse(url, obj, dofade); };	
	
	// WebForm_DoCallback(obj.container.id,center.x+';'+center.y+';'+obj.zoom+';'+obj.container.offsetWidth+';'+obj.container.offsetHeight+';'+obj.hiddenLayers+';'+'-1;'+'-1;'
	// + obj.LayerActive ,delegate,null,SharpMap_AjaxOnError,true)
	
	WebForm_DoCallback(obj.container.id,center.x+';'+center.y+';'+obj.zoom+';'+width+';'+height+';'+obj.hiddenLayers+';'+'-1;'+'-1;'
	+ obj.LayerActive ,delegate,null,SharpMap_AjaxOnError,true);
	
	obj.mapReady=0;
	
	obj.container.style.cursor = 'wait';
	
	if(obj.onViewChanging)
		obj.onViewChanging();
}

function SharpMap_BeginRefreshMapNewCenter(obj,dofade, x, y)
{
    // SetLayerActiveByButtonState(obj);

    var width, height;
    
    if ((typeof(obj.container.offsetWidth) == "undefined") || (typeof(obj.container.offsetHeight) == "undefined"))
    {
        width = 500;
        height = 500;   
    }
    else
    {
        width = obj.container.offsetWidth;
        height = obj.container.offsetHeight;
    }

    //alert("SharpMap_BeginRefreshMapNewCenter: obj.zoom = " + obj.zoom + ", obj.minZoom = " + obj.minZoom + ", obj.maxZoom=" + obj.maxZoom + ", dofade=" + dofade);    
	var delegate = function(url) { SharpMap_GetCallbackResponse(url,obj,dofade); };
	// WebForm_DoCallback(obj.container.id,x+';'+y+';'+obj.zoom+';'+obj.container.offsetWidth+';'+obj.container.offsetHeight+';'+obj.hiddenLayers+';'+'-1;'+'-1;'
	// + obj.LayerActive ,delegate,null,SharpMap_AjaxOnError,true)
	WebForm_DoCallback(obj.container.id,x+';'+y+';'+obj.zoom+';'+width+';'+height+';'+obj.hiddenLayers+';'+'-1;'+'-1;'+ obj.LayerActive ,delegate,null,SharpMap_AjaxOnError,true)
	obj.mapReady=0;
	obj.container.style.cursor = 'wait';
	if(obj.onViewChanging)
		obj.onViewChanging();
}

function PointInView(obj,PushPin)
{
    var IsInView = false;
    if (obj.minX<PushPin.Point.x && obj.minX+obj.zoom>PushPin.Point.x &&
        obj.maxY>PushPin.Point.y && obj.maxY-obj.zoom<PushPin.Point.y)        
     {       
        var PointPixel = 
        SharpMap_MapToPixel(PushPin.Point.x,PushPin.Point.y,obj);
        PushPin.PointPixel.x =PointPixel.x;
        PushPin.PointPixel.y =PointPixel.y;
        IsInView = true;
     }
     return IsInView;
}

function AddToolTip(obj,num,x,y,HTML)
{
    var ppPushPin = new Object();
    var ptPoint = new Object();
    var ptPointPixel = new Object();
    
    ptPointPixel.x = -1;
    ptPointPixel.y = -1;
    
    ptPoint.x = parseFloat( x);
    ptPoint.y = parseFloat( y);
    
    ppPushPin.Point = ptPoint;
    ppPushPin.PointPixel = ptPointPixel;
    
    var divSmallPinContainer = document.createElement("div"); 
    divSmallPinContainer.style.position = "absolute";
    
    var divSmallPin = document.createElement("a"); 
    divSmallPin.style.position = "absolute"; 
    divSmallPin.innerHTML = num;   
    divSmallPin.style.zIndex = "7999";     
    divSmallPin.className  = "toolTipSmall";    
    
    var divBigPin = document.createElement("div"); 
    divBigPin.style.position = "absolute";    
    
    var divBigPinHTML = document.createElement("div"); 
    divBigPinHTML.style.position = "absolute";
    divBigPinHTML.innerHTML = HTML;           
    divBigPinHTML.style.zIndex = "8000";
    divBigPinHTML.style.top = "28px";
    divBigPinHTML.style.left = "10px";
    divBigPinHTML.style.width = "180px";    
    
    var imgBubble = document.createElement("img");
    imgBubble.src = "img/bubble.gif"
    imgBubble.style.position = "absolute";   
    imgBubble.width = 200;
    imgBubble.height = 82;
    imgBubble.style.zIndex = "7999";
 
 //   imgBack.onmouseover = function() { ShowBigPushPin(divBigPin, imgBack,event, obj); return false;}
 //   imgBack.onmouseout =  function() { HideBigPushPin(divBigPin, imgBack,event, obj); return false;}      
 
    divSmallPinContainer.onmouseover = function(event) { ShowBigPushPin(divBigPin, divSmallPin, event ); return false;}
 //       divSmallPinContainer.onmouseout =  function(event) { HideBigPushPin(divBigPin, divSmallPin, event ); return false;}    
    divSmallPinContainer.appendChild(divSmallPin);  
 
    divBigPin.appendChild(divBigPinHTML);
    divBigPin.appendChild(imgBubble);
 
    ppPushPin.ElementSmall = divSmallPinContainer;
    ppPushPin.Element = divBigPin;

    obj.ToolTips.push(ppPushPin);    
}

// this function determines whether the event is the equivalent of the microsoft 
// mouseleave or mouseenter events. 
function isMouseLeaveOrEnter(e, handler) 
{ 
   if (e.type != 'mouseout' && e.type != 'mouseover') 
    return false; 
  var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement; 
  while (reltg && reltg != handler) 
  reltg = reltg.parentNode; 
  return (reltg != handler);
}

function findPos(obj) {
	var pos = new Object();
	if (obj.offsetParent) {
	    pos.Left = obj.offsetLeft;
	    pos.Top = obj.offsetTop;
		while (obj = obj.offsetParent) {
			pos.Left += obj.offsetLeft
			pos.Top += obj.offsetTop
		}
	}
	return pos;
}

function GetEventPos(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
// posx and posy contain the mouse position relative to the document
// Do something with this information
	var Pos  = new Object();
	Pos.x = posx;
	Pos.y = posy;
	return Pos;
}

function getElementHeight(Elem) {
	return Elem.offsetHeight;	
}

function getElementWidth(Elem) {
    return Elem.offsetWidth;	
}

function ShowBigPushPin( elem, caller, event, obj)
{
       // obj.container.onmousemove = null;
        elem.style.display = "block";
        setTimeout(function(){  elem.style.display = "none"; return false; }, 2000);
}

function HideBigPushPin ( elem, caller, e , obj)
{   
  //  elem.style.display = "none";
 /*
    var pos = findPos(caller);
    var posEvent = GetEventPos(e);
    var Width = getElementWidth(caller);
    var Height = getElementHeight(caller);
    document.getElementById("divTrace").innerHTML = posEvent+" " +e + " " + pos+"<br>";
    document.getElementById("divTrace").innerHTML += e.clientX    + " " + e.clientY +"<br>"+
                                                    pos.Left + "  " +pos.Top+"<br>"+
                                                    posEvent.x + "  " +posEvent.y+"<br>"+
                                                    Width+ "  " +Height;
    var offset = 2;
    if (!(posEvent.x>=pos.Left+3 && posEvent.x<=pos.Left+Width-3 && posEvent.y>=pos.Top+3 && posEvent.y<=pos.Top+Height-3))
    {
        elem.style.display = "none";
        document.getElementById("divTrace").innerHTML +="<br>hide";
    }
    else
    {
        document.getElementById("divTrace").innerHTML +="<br>show";
    }
    */
        
       // obj.container.onmousemove = function(event) { SharpMap_MapMouseOver(event,obj); }
  /*  var IStoElement = false;
    var ISfromElement=false;
    if (event.toElement==caller) IStoElement = true;
    if (event.fromElement==caller) ISfromElement = true;
    document.getElementById("divTrace").innerHTML = "e.relatedTarget= "+ event.relatedTarget+
                                                    "<br>e.toElement= " + event.toElement+
                                                    "<br>e.fromElement" + event.fromElement+
                                                    "<br>e.toElement=caller "+ IStoElement+
                                                    "<br>e.fromElement=caller "+ ISfromElement;   
    if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	//if (tg.nodeName != 'DIV') return;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return;*/   
}

function SetToolTipLayer(obj)
{
    obj.toolTipLayer.style.top = "0px";
    obj.toolTipLayer.style.left = "0px";
    while(obj.toolTipLayer.firstChild) { obj.toolTipLayer.removeChild(obj.toolTipLayer.firstChild); } 
    for (q=0;q<obj.ToolTips.length;q++)
    {
        var ppPushPin = obj.ToolTips[q];
        if (PointInView(obj,ppPushPin)) 
        {
            ppPushPin.ElementSmall.style.left = parseInt(ppPushPin.PointPixel.x-10)+"px";
            ppPushPin.ElementSmall.style.top = parseInt(ppPushPin.PointPixel.y-8)+"px";
            ppPushPin.ElementSmall.style.zIndex = "130";
            obj.toolTipLayer.appendChild(ppPushPin.ElementSmall);
            ppPushPin.Element.style.left = parseInt(ppPushPin.PointPixel.x-43)+"px";
            ppPushPin.Element.style.top = parseInt(ppPushPin.PointPixel.y-1)+"px";
            ppPushPin.Element.style.display = "none";
            ppPushPin.Element.style.zIndex = "8000";
            obj.toolTipLayer.appendChild(ppPushPin.Element);
        }
    }
 }
 
/* Processes the response from the callback -
   The function sets up an onload-even for when the image should start fading if dofade==1 */
function SharpMap_GetCallbackResponse(url,obj,dofade){
   
	if(url=='') 
	{
	alert("SharpMap_GetCallbackResponse url = ''");
	return;
	}
    // SharpMap_RedrawSmallMap(obj, null, null);
    SetToolTipLayer(obj);
	if(true)//dofade==1) //Nikola - forsirati uvek fade
	{	    
		var imgdnew = obj.HiddenMap();
		
		// var browser = navigator.appName;
		
		var finalUrl = url;
		
		if (obj.zoom < 4000)
		{
		    finalUrl = url+'&rand='+Math.random();
		}				
		
		imgdnew.onload = function(){ obj.mapReady=1; imgdnew.onload=''; if(obj.zoomEnded==1) { SharpMap_BeginFade(obj); } }		
		imgdnew.src = finalUrl;
		
//		switch (browser)
//		{
//		    case "Netscape":		
//		        imgdnew.onload = function(){ obj.mapReady=1; imgdnew.onload=''; if(obj.zoomEnded==1) { SharpMap_BeginFade(obj); } }		
//		        imgdnew.src = finalUrl;
//		        break;
//		    case "Microsoft Internet Explorer":
//		        imgdnew.src = finalUrl;
//		        imgdnew.onload = function(){ obj.mapReady=1; imgdnew.onload=''; if(obj.zoomEnded==1) { SharpMap_BeginFade(obj); } }		        
//		        break;
//		    default:
//		        imgdnew.onload = function(){ obj.mapReady=1; imgdnew.onload=''; if(obj.zoomEnded==1) { SharpMap_BeginFade(obj); } }		
//		        imgdnew.src = finalUrl;
//		        break;
//		}		        
	}
	else
	{
		obj.VisibleMap().src = url;
		obj.container.style.cursor = 'auto';
		obj.VisibleMap().onload = function(){ obj.mapReady=1; }
	}	 
}

function IsRequestGetFeatureInfo(url)
{
 //   if (url.length < (parseInt(url.indexOf("FeatureGuid="))+30))
    if (parseInt(url.indexOf("XMLData="))==-1 || url.length < (parseInt(url.indexOf("XMLData="))+30))
        return false;
    return true;
}

/* Returns the center of the current view */
function SharpMap_GetCenter(obj){
   var center = new Object();
   center.x = obj.minX+obj.zoom*0.5;
   center.y = obj.maxY-obj.zoom*obj.container.offsetHeight/obj.container.offsetWidth*0.5;
   return center;
}

/* Sets the opacity of an object (x-browser) */
function SharpMap_SetOpacity(obj,value){
	obj.style.opacity = value/100.0;
	obj.style.mozopacity = value/100.0;
	obj.style.filter = 'ALPHA(opacity=' + value + ')';
}

function SharpMap_AjaxOnError() { alert('Map refresh failed: ' + arg); }

/* Transforms from pixels coordinates to world coordinates */
function SharpMap_PixelToMap(x,y,obj){
	 var p=new Object();
	 p.x = obj.minX+x*obj.zoom/obj.container.offsetWidth; 
	 p.y = obj.maxY-y*obj.zoom/obj.container.offsetWidth;
	 return p;
}

function SharpMap_MapToPixel(x,y,obj){
	 var p=new Object();
	 p.x = (x - obj.minX)*obj.container.offsetWidth/obj.zoom;
	 p.y = (obj.maxY - y)*obj.container.offsetWidth/obj.zoom;
	 return p;
}

/* Returns the relative position of a point to an object */
function SharpMap_GetRelativePosition(x,y,obj){
	var position=WebForm_GetElementPosition(obj);
	var p=new Object();
	p.x=x-position.x;
	p.y=y-position.y;
	return p;
}

function SharpMap_IsDefined(obj){
	if (null == obj) { return false; }
	if ('undefined' == typeof(obj) ) { return false; }
	return true;
}

function SharpMap_GetBoundingBox(obj,p1,p2){
	var box=new Object();
	box.x1=(p1.x<p2.x)?p1.x:p2.x;
	box.x2=(p2.x<p1.x)?p1.x:p2.x;
	box.y1=(p1.y<p2.y)?p1.y:p2.y;
	box.y2=(p2.y<p1.y)?p1.y:p2.y;
	box.centerX=box.x1+((box.x2-box.x1)/2);
	box.centerY=box.y1+((box.y2-box.y1)/2);
	box.zoomAmount=(obj.container.offsetWidth/(box.x2-box.x1)<obj.container.offsetHeight/(box.y2-box.y1))?obj.container.offsetWidth/(box.x2-box.x1):obj.container.offsetHeight/(box.y2-box.y1);
	return box;
}

// ova se poziva kad se koristi "select box" tool
function SharpMap_BoxBeginZoom(obj,x,y,zoomval)
{
	if(obj.zoomEnded==0) return;
	obj.zoomEnded=0;
	obj.container.style.cursor = 'wait';	
//	 document.getElementById("divTrace").innerHTML += "obj.zoom " +obj.zoom+ "obj.minZoom " +obj.minZoom +"obj.maxZoom " +obj.maxZoom+ "<br>";
	
	if(obj.zoom/zoomval<obj.minZoom) zoomval = obj.zoom/obj.minZoom;		
	
	if(obj.zoom/zoomval>obj.maxZoom) zoomval = obj.zoom/obj.maxZoom;	
	
//	document.getElementById("divTrace").innerHTML += "x  "+x+" y "+y+"zoomval " +zoomval+ " obj.container.offsetWidth "+obj.container.offsetWidth + " obj.container.offsetHeight "+obj.container.offsetHeight +"<br>";
	
//	var center = SharpMap_PixelToMap(x+( (obj.container.offsetWidth*0.5)-x)/(zoomval*2),y+((obj.container.offsetHeight*0.5)-y )/(zoomval*2),obj);
	   //var center = SharpMap_PixelToMap(x+( (obj.container.offsetWidth*0.5)-x)/zoomval,y+((obj.container.offsetHeight*0.5)-y )/zoomval,obj);
	
	   //var center = SharpMap_PixelToMap(x ,y ,obj);
	   
	obj.zoom = obj.zoom/zoomval;
	setZoomLevel(obj, obj.zoom)
    var center = SharpMap_PixelToMap(x*zoomval, y*zoomval, obj);
	
//	 document.getElementById("divTrace").innerHTML += "center.x " +center.x+"center.y " +center.y+" obj.zoom " +obj.zoom+ "<br>";
	
	obj.minX = center.x - obj.zoom*0.5;
	obj.maxY = center.y + obj.zoom*obj.container.offsetHeight/obj.container.offsetWidth*0.5;
//	document.getElementById("divTrace").innerHTML += "x*(1-zoomval )" + (x*(1-zoomval ))+ " obj.minX "+(y*(1-zoomval )) + "<br>";
	
	SharpMap_BeginRefreshMap(obj, 1); //Start refreshing the map while we're zooming
	obj.zoomEnded = 0; 
	SharpMap_DynamicZoom((obj.container.offsetWidth*0.5)-x*zoomval, (obj.container.offsetHeight*0.5)-y* zoomval, zoomval, 0.0, obj);	 
}

function SharpMap_Measure(obj,p1,p2)
{
	jg.clear();
	jg.setColor("#ff0000"); // red
	jg.setStroke(2);
	jg.drawLine(p1.x,p1.y,p2.x,p2.y);
	jg.drawEllipse(p1.x-2,p1.y-2,4,4);
	jg.paint();
	obj.measure.style.visibility="visible";
	obj.measure.style.top=p2.y+"px";
	obj.measure.style.left=p2.x+"px";
	var d1 = SharpMap_PixelToMap(p1.x,p1.y,obj);
	var d2 = SharpMap_PixelToMap(p2.x,p2.y,obj);
    
    if (UseMeters)
	{
	    var dist = (Math.sqrt(Math.pow(d2.x-d1.x, 2)+Math.pow(d2.y-d1.y, 2)));
	    if(dist>1000)
	    {
		    obj.measure.innerText=roundNumber(dist/1000,2) + " km";
		    obj.measure.textContent=roundNumber(dist/1000,2) + " km";
		}
	    else
	    {
		    obj.measure.innerText=Math.round(dist) + " m";
		    obj.measure.textContent=Math.round(dist) + " m";
		}
	}
	else
	{
	    var dist = (Math.sqrt(Math.pow(d2.x-d1.x, 2)+Math.pow(d2.y-d1.y, 2)))*metersToMiles;
	    if(dist>1)
	    {
		    obj.measure.innerText=roundNumber(dist,2) + " miles";
		    obj.measure.textContent=roundNumber(dist,2) + " miles";
		}
	    else
	    {
		    obj.measure.innerText=Math.round(5280*dist) + " feet";
		    obj.measure.textContent=Math.round(5280*dist) + " feet";
		}   
	}
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function SharpMap_HiddenLayers(obj){
	var getstr = "";
	//var theForm = (!document.forms['Form1'])? document.Form1 : document.forms['Form1'];
	var theForm = document.forms[0];
	var inputs = theForm.getElementsByTagName("INPUT");
	
	for (i=0; i<inputs.length; i++)
		if (inputs[i].type == "checkbox" && inputs[i].checked == false) { 		
		getstr += inputs[i].nextSibling.value+ ","; 
	//	getstr += inputs[i].id.substring(3,inputs[i].id.length) + ","; 
		}

	if(getstr.length>1)
	{
		getstr = getstr.substring(0,getstr.length-1);
		obj.hiddenLayers = getstr;
	} else
		obj.hiddenLayers = "none";
		
	//alert(obj.hiddenLayers);
	SharpMap_BeginRefreshMap(obj,1);
}

/* disables members of the layergroup on Toggle*/
function togLayerGroup(cb,DivId)
{
    var myDiv = document.getElementById("myDiv" + DivId);
    if(myDiv!=null){
	var inputs = myDiv.getElementsByTagName("INPUT");
	
	for (i=0; i<inputs.length; i++)
		if (inputs[i].type == "checkbox") {inputs[i].disabled = !cb.checked;}
	}
}

/*toggle the toolbar*/
function togToolbar(toolId, DivId)
{
    var myDiv = document.getElementById(DivId);
    if(myDiv!=null){
	var tools = myDiv.getElementsByTagName("img");
	
	for (i=0; i<tools.length; i++)
		if (tools[i].id != toolId)
			tools[i].className = "button";
		else
			tools[i].className = "pressed";
	}
}

/*expand and collapse layergroup*/
function expandIt(id) {
    if (document.all) {
        whichEl = eval("myDiv" + id);
        whichIm = event.srcElement;
        if (whichEl.style.display == "none") {
            whichEl.style.display = "block";
            whichIm.src = "img/minus.gif";        
        }
        else {
            whichEl.style.display = "none";
            whichIm.src = "img/plus.gif";
        }
    }
    else if(document.all)//LOOKUP on the website below for a netscape reference
    {
        whichEl = eval("document.myDiv" + id);
        
        whichIm = eval("document.images['img" + id + "']");
        if (whichEl.visibility == "hide") {
            whichEl.visibility = "show";
            whichIm.src = "img/minus.gif";
        }
        else {
            whichEl.visibility = "hide";
            whichIm.src = "img/plus.gif";
        }
       // arrange(); this needs more work for propper results in netscape
       //see http://www.webreference.com/dhtml/column12/outALLtwo.html
    }
}

function GotToMapAndZoom(obj, x , y, zoom)
{
    var center=new Object();
    //obj.zoom = setFixedZoom(obj, obj.zoom/zoom);
     //= zoom;
    //if (zoom <= obj.maxzoom)    
    // if ((obj.zoom != obj.minzoom) && (obj.zoom != obj.maxzoom))
    {
        setZoomLevel(obj,zoom);
        obj.zoom = zoom;
        center.x = x; 
        center.y = y; 
        obj.minX= center.x-zoom*0.5;
        obj.maxY=center.y+zoom/obj.container.offsetWidth*obj.container.offsetHeight*0.5;
        SharpMap_BeginRefreshMapNewCenter(obj,1,center.x,center.y);        
        SharpMap_RedrawScaleBar(ajaxMapObj);
        
        // refresh small map image if needed
        SetSmallMapImageSrc(ajaxMapObj);
    }    
}

function GoToMap(obj, x , y)
{
    GotToMapAndZoom(obj, x , y, obj.zoom);
/*    var center=new Object();
    center.x = x; 
    center.y = y; 
    obj.minX= center.x-obj.zoom*0.5;
    obj.maxY=center.y+obj.zoom/obj.container.offsetWidth*obj.container.offsetHeight*0.5;
    SharpMap_BeginRefreshMapNewCenter(obj,1,center.x,center.y);*/
}

function activateLayer(obj, pnlLayersId)
{
    var spans = document.getElementById(pnlLayersId).getElementsByTagName("SPAN");
    for(i=0;i<spans.length;i++) {    
        spans[i].style.backgroundColor = '#ffffff';
        spans[i].style.fontWeight = 'normal';
    }
    obj.style.backgroundColor = '#dcdcdc';
    obj.style.fontWeight = 'bold';
}