// ----------------------------------------------------------------
//     				   ArcWeb Services Site Starter Client
// ----------------------------------------------------------------
// Purpose:  Manages "active" navigation functionality.  
//           + Manages map coordinate logic.
//           + Uses DHTML to produce rubber-banding effect 
//             when user zooms in or out.  This is handled
//             by the zoomBox class.
// ----------------------------------------------------------------
// Notes:		
// -------------------------------------------------------------------

// The following variables are global to the page.
	// These vars reference HTML tags in the page whose
	// values may be read or set dynamically.
    var m_radZoomIn;
    var m_radRecenter;
    var m_txtXCoord;
    var m_txtYCoord;
    var m_imgLoading;
    var m_imgMapCanvas;
    var m_divMapBorder;
    var m_divZoomBox;
	var m_divMapTools;
	var m_divNorth;
	var m_divSouth;
	var m_divEast;
	var m_divWest;
    var	m_hvMinX;
    var	m_hvMinY;
    var	m_hvMaxX;
    var m_hvMaxY;
    var m_hvDefaultMinX;
    var m_hvDefaultMinY;
    var m_hvDefaultMaxX;
    var m_hvDefaultMaxY;
    var m_hvMapPage;
    var m_hvMapObjectStorageKey;
    var m_hvImgPath;

    var m_hvActiveTool;
    var m_toolSelect;
    var m_toolZoomIn;
    var m_toolZoomTo;
    var m_toolZoomFull;
    var m_toolZoomOut;
    var m_toolMove;
    var m_toolInfo;

    var m_MoveStartX;
    var m_MoveStartY;
    var m_MoveEndX;
    var m_MoveEndY;
    var m_MoveInProgress = 0;

    var m_prefix;
    var m_hash;
    var m_delta = 0;

	// These vars represent javascript classes that will
	// handle logic.
    var m_mapViewer;  // Object of type map.  Handles map coordinate management.

    var m_zbxZoom;      // Object of type zoombox.  Handles drawing the zoombox.  
				        // Operates entirely within page coordinates and has no 
				        // awareness of the map space or even the image that 
				        // represents the map on the page.

    var m_ToolMode;    // Indicates the currently selected tool, where
				        //				+ zoom in  = 1
                        //				+ recenter = 2
                        // available velues are:
    // select
    // zoomIn
    // zoomOut
    // pan                        
				        

	var m_sClientBrowserType;	// Possible values: "IE", "Netscape"
	var m_sClientPlatform;		// Possible values: "Windows","Mac"
    
    		           // The array that stores the level extent widths
	var m_ExtentWidths = new Array(600, 3200, 14000, 57087, 153000, 456697);

	var m_lTimerID = 1; // used only for hiding the "Wait" image for Netscape 6.x

	// Determine browser type and platform.

	if (navigator.appName.indexOf("Netscape")>=0) {
		m_sClientBrowserType = 'Netscape';
	} else if (navigator.appName.indexOf('Microsoft') >=0) {
		m_sClientBrowserType = 'IE';
	} else {
	    //window.location = "unsupported.htm";
	    //assume IE
	    m_sClientBrowserType = 'IE';
	}

	if (navigator.userAgent.indexOf("Win") >= 0) {
		m_sClientPlatform = 'Windows';
	} else if (navigator.platform.indexOf("Mac") >= 0) {
		m_sClientPlatform = 'Mac';
	} else {
    	//window.location = "unsupported.htm";
	    //assume Windows
	    m_sClientPlatform = 'Windows';
	}
	
	var m_icons=[];
	var m_icons_count = 0;

	var m_iconsHidden = 1;

	var m_wheelInProgress = false;

	var m_showInfo = true;

// *******************************************************************
// *********************** STARTUP FUNCTION **************************
// *******************************************************************

	function AddIcon(Name, X, Y, W, H, dX, dY) {
	    var elm = document.getElementById(Name);
	    m_icons[m_icons_count] = new icon(elm, Name, X, Y, W, H, dX, dY);
	    m_icons_count = m_icons_count + 1;
	}

function startUp(prefix) {
// Purpose: Fires when the page first loads.

    m_prefix = prefix;

		// Find the HTML tags that will be used throughout the
		// page.
	m_radZoomIn = document.getElementById("radZoomIn");
	m_radRecenter = document.getElementById("radRecenter");
	m_txtXCoord = document.getElementById(prefix + "uxXCoord");
	m_txtYCoord = document.getElementById(prefix + "uxYCoord");
	m_imgMapCanvas = document.getElementById("uxMapImage");
    m_imgLoading = document.getElementById("imgLoading");
	m_divZoomBox = document.getElementById("divZoomBox");
	m_divMapBorder = document.getElementById("divMapBorder");
  	m_divNorth = document.getElementById("divNorth");
  	m_divSouth = document.getElementById("divSouth");
  	m_divEast = document.getElementById("divEast");
  	m_divWest = document.getElementById("divWest");
  	m_divMapTools = document.getElementById("divMapTools");
	m_hvMinX = document.getElementById(prefix + "hvMinX");
	m_hvMinY = document.getElementById(prefix + "hvMinY");
	m_hvMaxX = document.getElementById(prefix + "hvMaxX");
	m_hvMaxY = document.getElementById(prefix + "hvMaxY");

	m_hvDefaultMinX = document.getElementById(prefix + "hvDefaultMinX");
	m_hvDefaultMinY = document.getElementById(prefix + "hvDefaultMinY");
	m_hvDefaultMaxX = document.getElementById(prefix + "hvDefaultMaxX");
	m_hvDefaultMaxY = document.getElementById(prefix + "hvDefaultMaxY");

	m_hvMapPage = document.getElementById(prefix + "hvMapPage");
	m_hvMapObjectStorageKey = document.getElementById(prefix + "hvMapObjectStorageKey");
	m_hvImgPath = document.getElementById(prefix + "hvImgPath");

	m_hvActiveTool = document.getElementById(prefix + "hvActiveTool");
	m_toolSelect = document.getElementById(prefix + "uxSelectTool");
	m_toolZoomIn = document.getElementById(prefix + "uxZoomIn");
	m_toolZoomOut = document.getElementById(prefix + "uxZoomOut");
	m_toolZoomTo = document.getElementById(prefix + "uxZoomTo");
	m_toolZoomFull = document.getElementById(prefix + "uxZoomFull");
	m_toolMove = document.getElementById(prefix + "uxMove");
	//m_toolInfo = document.getElementById(prefix + "uxToggleInfo");

	// Create a map coordinate manager.
	m_mapViewer = new map(findPosX(m_imgMapCanvas),
  						  findPosY(m_imgMapCanvas),
						  m_imgMapCanvas.width,	
						  m_imgMapCanvas.height,
						  m_ExtentWidths,
						  new rect(m_hvMinX.value, m_hvMinY.value, m_hvMaxX.value, m_hvMaxY.value),
						  new rect(m_hvDefaultMinX.value, m_hvDefaultMinY.value, m_hvDefaultMaxX.value, m_hvDefaultMaxY.value)
						  );

		// Create a zoom box.
	m_zbxZoom = new zoomBox(m_divZoomBox);

    //Orient other page elements - Map Border, Map Tools, Loading Image, Border Navigation.
//############################################################################################################           
//    posLoadingImage();
//    posBorder();
//    posBorderNavigation(); //must be called BEFORE posTools()
//    posTools();  //must be called AFTER posBorderNavigation()
//############################################################################################################           
		// Initialize the tool mode. 
	handleToolClick(returnActiveTool());
		
	    // Initialize the X,Y coordinate display.
	m_txtXCoord.value = "X:"
	m_txtYCoord.value = "Y:"

		// Assign custom event handlers to mousedown, mousemove, 
		// and mouseup.
	if (m_sClientBrowserType == 'Netscape') {	    
	    if (document.addEventListener) {
	        /** DOMMouseScroll is for mozilla. */
	        document.addEventListener('DOMMouseScroll', handleMouseWheel, false);	        
	    }

	}
	m_imgMapCanvas.onmousedown = handleMouseDown;
	m_imgMapCanvas.onmousemove = handleMouseMove;
	m_imgMapCanvas.onmouseup = handleMouseUp;
	m_divZoomBox.onmouseup = handleMouseUp;
	document.onmousewheel = handleMouseWheel;
	// Assign an event handler to the m_imgMapCanvas onload event.
	m_imgMapCanvas.onload = hideWaitImage;
	
	    //Request a map.
	submit();

	//render object icons
	repositionIcons(0, 0);
//	if (readCookie('LDKKMapInfoISOn')) {
//	    handleToolClick('toggleInfo');	    
//	}
//	switchIcoToolTips(m_showInfo);

}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}


function switchIcoToolTips(enable) {
    if (m_icons != null && m_icons.length > 0 && m_iconsHidden == 0) {
       
        for (var i = 0; i < m_icons.length; i++) {
            var icon = m_icons[i];
            //var elm = document.getElementById(icon.name);
            var elm = icon.elm;
            if (elm != null) {
                if (enable) {
                    elm.setAttribute('title', elm.getAttribute('alt'));
                }
                else {
                    elm.setAttribute('title', '');
                }
            }
        }

    }
}

function icoClick(id) {
    __doPostBack(m_prefix + 'uxActionButton', 'IconSelect#' + id);
}

function repositionIcons(X, Y) {
    if (m_icons != null && m_icons.length > 0 && m_iconsHidden == 1) {
        var scale = getIcoScaleFactor();
        if (scale == 1) {
            for (var i = 0; i < m_icons.length; i++) {
                var icon = m_icons[i];
                //var elm = document.getElementById(icon.name);
                var elm = icon.elm;
                if (elm != null) {
                    var pagepoint = m_mapViewer.toMapCanvasPoint(icon.mapX, icon.mapY)
                    elm.style.left = (icon.deltaX + X + pagepoint.x) + 'px';
                    elm.style.top = (icon.deltaY + Y + pagepoint.y) + 'px';
                    elm.style.width = icon.width + 'px';
                    elm.style.height = icon.heigth + 'px';
                    elm.style.display = '';
                }
            }
        }
        else {
            for (var i = 0; i < m_icons.length; i++) {
                var icon = m_icons[i];
                //var elm = document.getElementById(icon.name);
                var elm = icon.elm;
                if (elm != null) {
                    var pagepoint = m_mapViewer.toMapCanvasPoint(icon.mapX, icon.mapY)
                    elm.style.left = (scale * icon.deltaX + X + pagepoint.x) + 'px';
                    elm.style.top = (scale * icon.deltaY + Y + pagepoint.y) + 'px';
                    elm.style.width = (icon.width * scale) + 'px';
                    elm.style.height = (icon.heigth * scale) + 'px';
                    elm.style.display = '';
                }
            }
        }
        m_iconsHidden = 0;
    }
}

function hideIcons() {
    if (m_icons != null && m_icons.length > 0 && m_iconsHidden == 0) {
       
    for (var i = 0; i < m_icons.length; i++) {
        var icon = m_icons[i];
        //var elm = document.getElementById(icon.name);
        var elm = icon.elm;
        if (elm != null) {            
            elm.style.display = 'none';            
        }
    }
    m_iconsHidden = 1;
       
    }
}

function getIcoScaleFactor() {
     var currentLevel = m_mapViewer.getLevel();
     if (currentLevel > m_ExtentWidths.length - 2) {
         return 0.7;
     }
     else {
         return 1;
     }
}
// *******************************************************************
// *********************** EVENT HANDLERS  ***************************
// *******************************************************************

function handleMouseWheel(event) {
    if (!event) var event = window.event;
    if (!m_wheelInProgress) {
        var delta = 0;
        var posx = 0;
        var posy = 0;        
        if (event.pageX || event.pageY) {
            posx = event.pageX;
            posy = event.pageY;
        }
        else if (event.clientX || event.clientY) {
        posx = event.clientX + document.body.scrollLeft
			    + document.documentElement.scrollLeft;
        posy = event.clientY + document.body.scrollTop
			    + document.documentElement.scrollTop;
    }
    var mapPoint = m_mapViewer.toMapPoint(posx, posy);
    // Check to see if mouse scroll is over map
    if (!(m_mapViewer.isEventOverMap(posx, posy))) {	
    	return;
    }
        if (!event) /* For IE. */
            event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta / 120;
            /** In Opera 9, delta differs in sign as compared to IE.
            */
            if (window.opera)
                delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
            /** In Mozilla, sign of delta is different than in IE.
            * Also, delta is multiple of 3.
            */
            delta = -event.detail / 3;
        }
        /** If delta is nonzero, handle it.
        * Basically, delta is now positive if wheel was scrolled up,
        * and negative, if wheel was scrolled down.
        */
        if (delta) {
            
            handleMouseWheelDelta(delta, mapPoint, new point(posx, posy));

        }
    }
    /** Prevent default actions caused by mouse wheel.
    * That might be ugly, but we handle scrolls somehow
    * anyway, so don't bother here..
    */
    if (event.preventDefault)
        event.preventDefault();
    event.returnValue = false;

}

function handleMouseWheelDelta(delta, mapPoint, pagePoint) {
        
    if (delta > 0 && m_mapViewer.getLevel() != 1) {
        m_mapViewer.setLevel(m_mapViewer.getLevel() - 1);
        m_wheelInProgress = true;
        //hideIcons();
        //m_mapViewer.recenter(point);
        m_mapViewer.positionPoint(mapPoint, pagePoint);
        submit();
    }
    else if (delta < 0 && !m_mapViewer.isFullZoomOut()) {
        if (m_mapViewer.getLevel() != m_ExtentWidths.length) {
            m_mapViewer.setLevel(m_mapViewer.getLevel() + 1);
        }
        m_wheelInProgress = true;
        //hideIcons();
        //m_mapViewer.recenter(point);
        if (m_mapViewer.getLevel() == m_ExtentWidths.length) {
            m_mapViewer.reset();
        }
        else {
            m_mapViewer.positionPoint(mapPoint, pagePoint);
        }
        submit();
    }
        
    
        
//    if (m_delta < 19) {
//    
//        
//        m_delta += delta;
//        var zoom = (1 - 0.05 * m_delta)
//        newWidth = m_mapViewer.getTagWidth() / zoom;
//        newHeight = m_mapViewer.getTagHeight() / zoom;

//        m_imgMapCanvas.style.width = newWidth + "px";
//        m_imgMapCanvas.style.height = newHeight + "px";

//        m_imgMapCanvas.style.left = ((m_mapViewer.getTagWidth() - newWidth) / 2) + "px";
//        m_imgMapCanvas.style.top = ((m_mapViewer.getTagHeight() - newHeight) / 2) + "px";


//        //m_mapViewer.zoom(1 - 0.05 * m_delta);    
//        //repositionIcons(0,0);

//        
//        setTimeout("delayedZoom(" + m_delta + ");", 500);
//    }

}

function delayedZoom(delta) {
    if (m_delta == delta) {
        m_mapViewer.zoom(1 - 0.05 * delta);
        m_delta = 0;        
        submit();
    }
}

function handleMouseDown(evt) {

    var posx = 0;
    var posy = 0;
    if (!evt) var evt = window.event;
    if (evt.pageX || evt.pageY) {
        posx = evt.pageX;
        posy = evt.pageY;
    }
    else if (evt.clientX || evt.clientY) {
        posx = evt.clientX + document.body.scrollLeft
			    + document.documentElement.scrollLeft;
        posy = evt.clientY + document.body.scrollTop
			    + document.documentElement.scrollTop;
    }
    if (evt.preventDefault) {
        evt.preventDefault();
    }
    
    // Check to see if click is over map
    //	if (!(m_mapViewer.isEventOverMap(eventX, eventY))) {	
    //		return;
    //    }
    
   
	switch(m_ToolMode) {

	    case "select":
	        {
	            //__doPostBack(m_toolSelect, 'My Argument');
	            var ptUserClick = m_mapViewer.toMapPoint(posx, posy);
	            reportCoords(ptUserClick); // update the textbox to reflect
	            __doPostBack(m_prefix + 'uxActionButton', 'Select');
	            // document.forms[0].submit();
	            break;
	        }
	    
	    case "zoomTo":
	        {
	            //__doPostBack(m_toolSelect, 'My Argument');
	            var ptUserClick = m_mapViewer.toMapPoint(posx, posy);
	            reportCoords(ptUserClick);
	            __doPostBack(m_prefix + 'uxActionButton', 'ZoomTo');
	            // document.forms[0].submit();
	            break;
	        }

	    case "zoomOut":
	        {
	            //alert(m_mapViewer.getLevel());
	            var ptUserClick = m_mapViewer.toMapPoint(posx, posy);

	            if (!m_mapViewer.isFullZoomOut()) {
	                if (m_mapViewer.getLevel() == m_ExtentWidths.length) {
	                    m_mapViewer.reset();
	                }
	                else {
	                    m_mapViewer.setLevel(m_mapViewer.getLevel() + 1);
	                    if (m_mapViewer.getLevel() == m_ExtentWidths.length) {
	                        m_mapViewer.reset();
	                    }
	                    else {
	                        m_mapViewer.recenter(ptUserClick);
	                    }
	                }
	                submit();
	            }
	            //alert(m_mapViewer.getLevel());
	            break;
	        }
	    case "move":
	        {
	            m_MoveStartX = posx;
	            m_MoveStartY = posy;
	            m_MoveEndX = 0;
	            m_MoveEndY = 0;
	            m_MoveInProgress = 1;
	            if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
	                // For IE, a call to "setCapture" is made to ensure that
	                // new IE dragging events do not interfere with capturing 
	                // the mousemove events -- effectively, this turns off 
	                // drag and drop events temporarily.
	                m_imgMapCanvas.setCapture();
	            }
	            //m_imgMapCanvas.className = "MapCanvas Grabbing";
	            m_imgMapCanvas.style.cursor = "url(" + m_hvImgPath.value + "cursor-grabbing.cur), move";
	            hideIcons();
	            break;
	        }

	    case "zoomIn": // zoom in mode
	        if (m_imgMapCanvas.setCapture) {
	            // For IE, a call to "setCapture" is made to ensure that
	            // new IE dragging events do not interfere with capturing 
	            // the mousemove events -- effectively, this turns off 
	            // drag and drop events temporarily.
	            m_imgMapCanvas.setCapture();	            
	        }
	        m_zbxZoom.show();
	        m_zbxZoom.start(posx, posy);	        
	        break;
		case "recenter": // recenter mode
		    var ptUserClick = m_mapViewer.toMapPoint(posx, posy);
			m_mapViewer.recenter(ptUserClick);
			//Request a new map.
			submit();
			break;
		
	}
}

function handleMouseMove(evt) {

    var posx = 0;
    var posy = 0;
    if (!evt) var evt = window.event;
    if (evt.pageX || evt.pageY) {
        posx = evt.pageX;
        posy = evt.pageY;
    }
    else if (evt.clientX || evt.clientY) {
        posx = evt.clientX + document.body.scrollLeft
			    + document.documentElement.scrollLeft;
        posy = evt.clientY + document.body.scrollTop
			    + document.documentElement.scrollTop;
    }
    if (evt.preventDefault) {
        evt.preventDefault();
    }
    

    // Check to see if click is over map
    //	if (!(m_mapViewer.isEventOverMap(eventX, eventY))) {	
    //		return;
    //    }

    reportCoords(m_mapViewer.toMapPoint(posx, posy)); // update the textbox to reflect	
	//reportCoords(new point(eventX, eventY)); // update the textbox to reflect
																																												 // the current coordinates.
    // If the current tool is zoom in and a zoombox is in progress, update
    // the zoom box.
    if (m_ToolMode == "zoomIn") {
        if (m_zbxZoom.isInProgress) {
            m_zbxZoom.update(posx, posy);
		}
    }
    if (m_ToolMode == "move") {
        if (m_MoveInProgress == 1) {
            //m_imgMapCanvas.style.position = 'relative';
            m_MoveEndX = posx - m_MoveStartX;
            m_MoveEndY = posy - m_MoveStartY;
            m_imgMapCanvas.style.left = m_MoveEndX + "px";
            m_imgMapCanvas.style.top = m_MoveEndY + "px";
            //window.status = "Xx:" + m_MoveEndX + "; Yy:" + m_MoveEndY;
            //repositionIcons(m_MoveEndX, m_MoveEndY);
        }
    }
}

function handleMouseUp(evt) {
    //window.status = "Mouseup";
    //handle only active tools:
    //zoomIn, move
    if (!(m_ToolMode == "zoomIn" && m_zbxZoom.isInProgress())
        &&
        !(m_ToolMode == "move" && m_MoveInProgress == 1)
        ) {
        return;
    }

    var posx = 0;
    var posy = 0;
    if (!evt) var evt = window.event;
    if (evt.pageX || evt.pageY) {
        posx = evt.pageX;
        posy = evt.pageY;
    }
    else if (evt.clientX || evt.clientY) {
        posx = evt.clientX + document.body.scrollLeft
			    + document.documentElement.scrollLeft;
        posy = evt.clientY + document.body.scrollTop
			    + document.documentElement.scrollTop;
    }
    if (evt.preventDefault) {
        evt.preventDefault();
    }

    if (m_ToolMode == "move") {
        if (m_MoveInProgress == 1) {            
            m_MoveInProgress = 0;
            // Release the capture that was set in the mousedown
            // handler (again, this is something that we have to do because
            // of drag events in IE).
            if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
                document.releaseCapture();
            }
            //m_imgMapCanvas.className = "MapCanvas Grab";
            m_imgMapCanvas.style.cursor = "url(" + m_hvImgPath.value + "cursor-grab.cur), move";
            //recenter map at new position
            if (m_MoveEndX != 0 && m_MoveEndY != 0) {
                var ptUserMove = m_mapViewer.moveVector(m_MoveEndX, m_MoveEndY);                
                submit();
            } 
        }
        return;
    }

    if (m_ToolMode == "zoomIn") {
        if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
            document.releaseCapture();
        }

        // Stop the current effect.
        m_zbxZoom.stop();

        // If the width of the zoom box is less than five pixels,
        // treat is as a single click and zoom in one map level.
        // Otherwise, set the extent to the zoom box.
        if (m_mapViewer.getLevel() != 1) {
            if ((Math.abs(m_zbxZoom.getStartX() - m_zbxZoom.getEndX()) < 5) && (Math.abs(m_zbxZoom.getStartY() - m_zbxZoom.getEndY()) < 5)) {
                var ptUserClick = m_mapViewer.toMapPoint(m_zbxZoom.getStartX(), m_zbxZoom.getStartY());
                if (m_mapViewer.getLevel() == 1) {   //if we reach the minimum zoom level, zoom a fixed amount.
                    //don't zoom further
                    //m_mapViewer.zoom(0.5);
                } else {
                    m_mapViewer.setLevel(m_mapViewer.getLevel() - 1);
                }
                m_mapViewer.recenter(ptUserClick);
            } else {
                borderWidth = 0;
                if (m_sClientBrowserType == 'Netscape') {
                    borderWidth = 4; // 2 times border width
                }
                var ptStart = m_mapViewer.toMapPoint(m_zbxZoom.getStartX() + borderWidth, m_zbxZoom.getStartY() + borderWidth);
                var ptEnd = m_mapViewer.toMapPoint(m_zbxZoom.getEndX() + borderWidth, m_zbxZoom.getEndY() + borderWidth);                
                m_mapViewer.setExtent(new rect(ptStart.x, ptStart.y, ptEnd.x, ptEnd.y));
            }

            //Request a new map.
            submit();
        }
        else {
            m_zbxZoom.hide();
        }
    }

}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curleft, curtop];
    }
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            if (obj.offsetLeft > 0) {
                curleft += obj.offsetLeft;
            }
            if (obj.clientLeft > 0) {
                curleft += obj.clientLeft;
            }
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            if (obj.offsetTop > 0) {
                curtop += obj.offsetTop;
            }
            if (obj.clientTop > 0) {
                curtop += obj.clientTop;
            }
            obj = obj.offsetParent;
        }
    } else
        if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}


function handleToolClick(iToolMode) {
    //m_imgMapCanvas.className = "MapCanvas";   
	switch(iToolMode) {
   
//	    case "toggleInfo":
//	        {
//	            m_showInfo = !m_showInfo;
//	            switchIcoToolTips(m_showInfo);
//	            if (m_showInfo) {
//	                m_toolInfo.className = "Selected " + m_toolInfo.className;
//	                createCookie('LDKKMapInfoISOn', 'yes', 1);
//	            }
//	            else {
//	                m_toolInfo.className = m_toolInfo.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
//	                eraseCookie('LDKKMapInfoISOn');
//	            }	            
//	            iToolMode = m_ToolMode;
//	            break;
//	        }

	    case "zoomIn":
	        {
	            m_imgMapCanvas.style.cursor = "crosshair";
	            m_toolZoomIn.className = "Selected " + m_toolZoomIn.className;
	            m_toolZoomTo.className = m_toolZoomTo.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomOut.className = m_toolZoomOut.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolMove.className = m_toolMove.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolSelect.className = m_toolSelect.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            break;
	        }
	    case "zoomOut":
	        {
	            m_imgMapCanvas.style.cursor = "crosshair";
	            m_toolZoomOut.className = "Selected " + m_toolZoomOut.className;
	            m_toolMove.className = m_toolMove.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolSelect.className = m_toolSelect.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomIn.className = m_toolZoomIn.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomTo.className = m_toolZoomTo.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            break;
	        }
	    case "zoomTo":
	        {
	            m_imgMapCanvas.style.cursor = "pointer";
	            m_toolZoomTo.className = "Selected " + m_toolZoomTo.className;
	            m_toolZoomOut.className = m_toolZoomOut.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolMove.className = m_toolMove.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolSelect.className = m_toolSelect.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomIn.className = m_toolZoomIn.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            break;
	        }
	    case "zoomFull":
	        {
	            //m_imgMapCanvas.style.cursor = "crosshair";
	            //m_toolZoomFull.className = "Selected";
	            //Postback to sinchronize envelope
	            //__doPostBack(m_prefix + 'uxActionButton', 'ZoomFull');
	            ZoomToLevel(m_ExtentWidths.length);	            
	            iToolMode = m_ToolMode;
	            break;
	        }
	        
	    case "select":
	        {
	            m_imgMapCanvas.style.cursor = "crosshair";
	            m_toolSelect.className = "Selected " + m_toolSelect.className;
	            m_toolZoomIn.className = m_toolZoomIn.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomTo.className = m_toolZoomTo.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomOut.className = m_toolZoomOut.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolMove.className = m_toolMove.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            break;
	        }
	    case "move":
	        {
	            //m_imgMapCanvas.style.cursor = null;
	            //m_imgMapCanvas.className = "MapCanvas Grab";	            
	            m_imgMapCanvas.style.cursor = "url(" + m_hvImgPath.value + "cursor-grab.cur), move";
	            m_toolMove.className = "Selected " + m_toolMove.className;
	            m_toolSelect.className = m_toolSelect.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomIn.className = m_toolZoomIn.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomTo.className = m_toolZoomTo.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            m_toolZoomOut.className = m_toolZoomOut.className.replace(" Selected", "").replace("Selected ", "").replace("Selected", "");
	            break;
	        }
	}
	m_ToolMode = iToolMode;
	m_hvActiveTool.value = iToolMode;
}

function ResizeMap(sSize) {

    if (((sSize == 'SMALL') && (m_imgMapCanvas.className == 'SmallMap')) || 
        ((sSize == 'LARGE') && (m_imgMapCanvas.className == 'LargeMap'))) {
        return;
    }
    
    switch(sSize) { 
        case 'SMALL':
        m_imgMapCanvas.className = 'SmallMap';
        break;
        case 'LARGE':
        m_imgMapCanvas.className = 'LargeMap';
        break;
        default: 
        return; // invalid sSize value
    }

        // Create a new map coordinate manager.
    m_mapViewer = new map(m_imgMapCanvas.offsetLeft,
  				    m_imgMapCanvas.offsetTop,
				    m_imgMapCanvas.width,	
				    m_imgMapCanvas.height,
					m_ExtentWidths,
				    m_mapViewer.getExtent()
				    );


        //Orient other page elements - MapSizeImage, Map Border, Map Tools, Loading Image.
    switch(sSize) { 
        case 'SMALL':
        updateImgMapSize('LARGE');
        break;
        case 'LARGE':
        updateImgMapSize('SMALL');
        break;
    }
    
    posLoadingImage();
    posBorder();
    posBorderNavigation(); //must be called BEFORE posTools()
    posTools();  //must be called AFTER posBorderNavigation()

    submit();
}

function ZoomToLevel(iLevel) {
//    if (iLevel == m_ExtentWidths.length) {
//        __doPostBack(m_prefix + 'uxActionButton', 'ZoomFull');
//        return;
    //    }
    if (iLevel == m_ExtentWidths.length) {
        m_mapViewer.reset();
    }
    m_mapViewer.setLevel(iLevel);    
    //Request a new map.
    submit();
}

// *******************************************************************
// ********************** HELPER FUNCTIONS  **************************
// *******************************************************************

function hideWaitImage() {

    m_zbxZoom.hide();
    m_imgMapCanvas.style.top ='0px';
    m_imgMapCanvas.style.left = '0px';    
    m_imgMapCanvas.style.width = m_mapViewer.getTagWidth() + 'px';
    m_imgMapCanvas.style.height = m_mapViewer.getTagHeight() + 'px';

    setTimeout("repositionIcons(0,0);", 250);
    m_wheelInProgress = false;
    
//############################################################################################################           
//    m_imgLoading.style.visibility = 'hidden';
//############################################################################################################           
}

function hideWaitImageForNetscape6() {
	if (m_imgMapCanvas.complete) {
		clearInterval(m_lTimerID);
		m_divZoomBox.style.visibility = 'hidden';
		m_imgLoading.style.visibility = 'hidden';
	}
}

function moveEast() {
    m_mapViewer.moveEast();
    submit();
}

function moveNorth() {
    m_mapViewer.moveNorth();
    submit();
}

function moveSouth() {
    m_mapViewer.moveSouth();
    submit();
}

function moveWest() {
    m_mapViewer.moveWest();
    submit();
}

function persistExtent() {
	m_hvMinX.value = m_mapViewer.getExtent().getLeft();
	m_hvMinY.value = m_mapViewer.getExtent().getBottom();
	m_hvMaxX.value = m_mapViewer.getExtent().getRight();
	m_hvMaxY.value = m_mapViewer.getExtent().getTop();
}

function posBorder() {

    if (m_sClientBrowserType == 'Netscape') { 
		m_divMapBorder.style.left = m_imgMapCanvas.offsetLeft - parseInt(m_divMapBorder.style.borderWidth.replace('px',''));
		m_divMapBorder.style.top = m_imgMapCanvas.offsetTop - parseInt(m_divMapBorder.style.borderWidth.replace('px',''));
	} else { // browser must be IE
		m_divMapBorder.style.left = m_imgMapCanvas.offsetLeft;
		m_divMapBorder.style.top = m_imgMapCanvas.offsetTop;
	}
		
    m_divMapBorder.style.width = m_imgMapCanvas.width;
    m_divMapBorder.style.height = m_imgMapCanvas.height;
    m_divMapBorder.style.visibility = "visible";

}

function posBorderNavigation() {

    var borderOffset = 0;    
    if (m_sClientBrowserType == 'Netscape') {
		borderOffset = parseInt(m_divMapBorder.style.borderWidth.replace('px','')) 
	}
   
          //align the NORTH DIV       
    m_divNorth.style.top = m_imgMapCanvas.offsetTop - (parseInt(m_divNorth.style.height.replace('px',''))) - borderOffset;
    m_divNorth.style.left = m_imgMapCanvas.offsetLeft + (m_imgMapCanvas.width / 2)- (parseInt(m_divNorth.style.width.replace('px','')) / 2);    

         //align the SOUTH DIV
    m_divSouth.style.top = m_imgMapCanvas.offsetTop + m_imgMapCanvas.height + borderOffset;
    m_divSouth.style.left = m_imgMapCanvas.offsetLeft + (m_imgMapCanvas.width / 2)- (parseInt(m_divSouth.style.width.replace('px','')) / 2);   
  
         //align the EAST DIV       
    m_divEast.style.top = m_imgMapCanvas.offsetTop + ((m_imgMapCanvas.height / 2) - (parseInt(m_divEast.style.height.replace('px','')) / 2));
    m_divEast.style.left = m_imgMapCanvas.offsetLeft + m_imgMapCanvas.width + borderOffset;    

         //align the WEST DIV       
    m_divWest.style.top = m_imgMapCanvas.offsetTop + ((m_imgMapCanvas.height / 2) - (parseInt(m_divWest.style.height.replace('px','')) / 2));
    m_divWest.style.left = m_imgMapCanvas.offsetLeft - (parseInt(m_divWest.style.width.replace('px',''))) - borderOffset;   
}

function posLoadingImage() {
    m_imgLoading.style.top = (parseInt(m_imgMapCanvas.style.top.replace('px','')) + (m_imgMapCanvas.height / 2) - (m_imgLoading.height / 2) );
    m_imgLoading.style.left = (parseInt(m_imgMapCanvas.style.left.replace('px','')) + (m_imgMapCanvas.width / 2) - (m_imgLoading.width / 2) );
}

function posTools() {
    m_divMapTools.style.top = parseInt(m_divSouth.style.top.replace('px','')) + parseInt(m_divSouth.style.height.replace('px',''));
    m_divMapTools.style.width = m_imgMapCanvas.width;
}

function reportCoords(ptReport) {
	// Note: Use round() instead of toFixed(), since the latter isn't supported
  //       in IE Mac.
	m_txtXCoord.value = "X: "+(Math.round(ptReport.x*10000.0)/10000.0).toString();
	m_txtYCoord.value = "Y: "+(Math.round(ptReport.y*10000.0)/10000.0).toString();
}

function returnActiveTool() {
//############################################################################################################
//    if (m_radZoomIn.checked) {
//        return 1;
//    } else if (m_radRecenter.checked) {
//        return 2;
//    }
//    else return 1;
    //############################################################################################################
    

    return m_hvActiveTool.value;
}

function showWaitImage() {
//############################################################################################################
//    m_imgLoading.style.visibility = 'visible';
//############################################################################################################           	
}

function submit() {
    hideIcons();
    //m_imgMapCanvas.src = "";    
    var sURL = m_hvMapPage.value +
		         "?XMIN=" + Math.round(m_mapViewer.getExtent().getLeft()) +
				     "&YMIN=" + Math.round(m_mapViewer.getExtent().getBottom()) +
						 "&XMAX=" + Math.round(m_mapViewer.getExtent().getRight()) +
						 "&YMAX=" + Math.round(m_mapViewer.getExtent().getTop()) +
						 "&WIDTH=" + m_mapViewer.getTagWidth() +
						 "&HEIGHT=" + m_mapViewer.getTagHeight() +
						 "&KEY=" + m_hvMapObjectStorageKey.value;
						 

  updateZoomLevel(m_mapViewer.getLevel());
	showWaitImage();

	if (navigator.userAgent.indexOf('Netscape6/6') > -1) {
		m_lTimerID = setInterval("hideWaitImageForNetscape6();",100);
	}

  m_imgMapCanvas.src = sURL;   	
 	  //Persist extent in Hidden variables
  persistExtent();  
}

function updateImgMapSize(sSize) {
    m_imgMapSize = document.getElementById("imgMapSize");
    switch(sSize) { 
        case 'SMALL':
            m_imgMapSize.name = 'SMALL';
            m_imgMapSize.src= 'images/smallmap.gif';
        break;
        case 'LARGE':
            m_imgMapSize.name = 'LARGE';
            m_imgMapSize.src= 'images/largemap.gif';
        break;
    }
}

function updateZoomLevel(inLevel) {
//############################################################################################################           
        var nLevels = m_ExtentWidths.length;
		for (i=1;i<nLevels+1;i++) {
		    document.getElementById('zoomLevel' + i).className = 'ZoomScaleButton';
			if (i==inLevel) {
			    document.getElementById('zoomLevel' + i).className = 'ZoomScaleButton Active';

			}        }
//############################################################################################################           			    
}
