﻿// JScript File
       var m_zbxZoom;
       var m_divZoomBox;
        // Variables
        var tempX = 0;
        var tempY = 0;
        var image;
        var zoomInProgress = false;
        var borderWidth = 0
        // Test if the browser is IE
        // If it is not IE, we assume that the browser is NS.
        var IE = document.all?true:false
function captureMouse() {        // Capture the mouse events
        // NS
        if (!IE) {
            document.captureEvents(Event.MOUSEMOVE); 
            document.captureEvents(Event.MOUSEUP);
		    document.captureEvents(Event.MOUSEDOWN);   
		    borderWidth = 4; 
        
        }
        
        // Set-up to use getMouseXY function onMouseMove
        document.onmousemove = getMouseMove;
        document.onmousedown = getStartBox;
        document.onmouseup = getEndBox;
}        

// MJ
function submitZoomRequest(e){
 
 //if the target of the click was not "glassDiv", which is the map, then do nothing
 if ((!IE) && (e.target.id != "glassDiv")) {
        return;
 }else{
    //__doPostBack(null, null); this is one potential way to submit postback
    // first test to see if we should submit a zoom box or a click (currently 5 pixel min for a zoom)
    if ((Math.abs(m_zbxZoom.getStartX() - m_zbxZoom.getEndX()) < 5) && (Math.abs(m_zbxZoom.getStartY() - m_zbxZoom.getEndY()) < 5))  {	
	    document.form1.navAction.value="click"
    }else{
	    document.form1.navAction.value="zoom"
    }
    // submits the whole form, app will recognize as a postBack 
    // with no extra events so must test in page_load event of the map control
        toggle_loadingImage('on');
        document.form1.submit(); 
 }   
}

        
    // Triggered on the page load
    function StartUp(){
        // Create a zoom box.MJ
        m_divZoomBox = document.getElementById("divZoomBox");
        m_zbxZoom = new zoomBox(m_divZoomBox);
        //image = document.getElementById("imgMapCanvas");  
        image = document.form1.imgMapCanvas;    
        if (!IE) {
            captureMouse();
        }
        // turn off the loading GIF if it is on
        toggle_loadingImage('off');
    }
   function getStartBox(e) { 
            // If the cursor is over the image start capturing the box      
            if(IsOverMapImage() == true){
                document.form1.CoordStart.value = getImageXCoord() + ',' + getImageYCoord();
                // Only works in IE, the glass layer is for all other browsers
                
                if (IE == true) {
                     image.setCapture();
                }
                // show zoombox and set the beginning coords to PAGE coords, not image coords.MJ
              
               
                m_zbxZoom.show();
			    m_zbxZoom.start(tempX,tempY);
                zoomInProgress = true;
            }else{
             //  alert("Help");
             //  routeEvent(e);       
            }   
        }
        
        function getEndBox(e) {
            // Only get the end if the zoom box is in progress
            // Note: The end coordinate will be captured even if the mouse
            //       is not over the image, but the max x and/or y coordinate
            //       of the image extent will be used.       
            if(zoomInProgress == true){
                var endX = getImageXCoord();
                var endY = getImageYCoord();
                if (IE == true) {
                    document.releaseCapture();
                }  
                                       
                document.form1.CoordEnd.value = endX + ',' + endY;
                // stop zoom box and do more stuff like zoom the map.MJ
                	m_zbxZoom.stop();
                	m_zbxZoom.hide();
                zoomInProgress = false;
                 submitZoomRequest(e);
            }  
           
        }        
        
        // Main function to retrieve mouse x-y pos.s
        function getMouseMove(e) {
          if (IE) { // grab the x-y pos.s if browser is IE
            tempX = event.clientX + f_scrollLeft(); //+ document.body.scrollLeft;
            tempY = event.clientY + f_scrollTop(); //+ document.body.scrollTop;
          } else {  // grab the x-y pos.s if browser is NS
            tempX = e.pageX;
            tempY = e.pageY;
          }  
          
          // catch possible negative values in NS4
          if (tempX < 0){tempX = 0}
          if (tempY < 0){tempY = 0}  
          // show the position values in the form named Show
          // in the text fields named MouseX and MouseY
          document.form1.MouseX.value = tempX;
          document.form1.MouseY.value = tempY;
          
          // Get the image XY Coords
          getMapImageMove();
          
          return true
        }
        
        function getMapImageMove(){
            var imageX = (getMouseXCoord() - getImageLocationX());
            var imageY = (getMouseYCoord() - getImageLocationY());
            
            // Test if the cursor is outside of the image bounds
            // This will set the min/max x/y if the cursor is beyond
            //  the bounds of the image.
            if(imageX > image.width){
                imageX = image.width;
            }
            if(imageX < 0){
                imageX = 0;
            }
            if(imageY > image.height){
                imageY = image.height;
            }
            if(imageY < 0){
                imageY = 0;
            }
            
            //Set text boxes
            document.form1.overImage.value = IsOverMapImage();
            document.form1.ImageX.value = imageX;
            document.form1.ImageY.value = imageY; 
            
            // Set the cursor to crosshair 
            if(IsOverMapImage(image) == true){
                image.style.cursor = "crosshair"; 
                		// if zooming then update the zoom box with PAGE COORDS not Image coords.MJ
                		if (m_zbxZoom.isInProgress) {
			                    m_zbxZoom.update(getMouseXCoord(),getMouseYCoord());
		                } 
            }  
        }
        
     
        
        // Tests if the cursor is over the map image
        function IsOverMapImage(){
            //TEMP - REMOVE
            //document.form1.TMP.value = 'Width: ' + image.width; 
            //document.form1.TMP.value += ' - Height: ' + image.height; 
            //TEMP - REMOVE
            
            var xVal;
            var yVal;
            xVal = getMouseXCoord() - getImageLocationX();
            yVal = getMouseYCoord() - getImageLocationY();

            // To return true, both the x and y coordinate need to be over the image    
            // Test the x coord        
            if((xVal < 0) || (xVal > image.width)){
                return false
            }
            
            // Test the y coord
            if((yVal < 0) || (yVal > image.height)) {
                return false
            }
            
            return true
        }
        
        // Gets the image X location and accounts for the offset
        function getImageLocationX() {
            var xPos = image.offsetLeft;
            tempEl = image.offsetParent;
            while (tempEl != null) {
                xPos += tempEl.offsetLeft;
                tempEl = tempEl.offsetParent;
            }  
            
            return xPos          
        }
        
        // Gets the image Y location and accounts for the offset
        function getImageLocationY() {
            var yPos = image.offsetTop;
            tempEl = image.offsetParent;
            while (tempEl != null) {
	            yPos += tempEl.offsetTop;
	            tempEl = tempEl.offsetParent;
            }  
            
            return yPos          
        }
        
        // Gets the coordinates from the text boxes (will most likely be hidden?)
        function getImageXCoord() {
            var xCoord = document.form1.ImageX.value
            return xCoord
        }
        
        function getImageYCoord() {
            var yCoord = document.form1.ImageY.value
            return yCoord    
        }
        
        function getMouseXCoord() {
            var xCoord = document.form1.MouseX.value;
            return xCoord
        }
        
        function getMouseYCoord() {
            var yCoord = document.form1.MouseY.value;
            return yCoord
        }
        function f_scrollLeft() {
	        return f_filterResults (
		        window.pageXOffset ? window.pageXOffset : 0,
		        document.documentElement ? document.documentElement.scrollLeft : 0,
		        document.body ? document.body.scrollLeft : 0
	        );
        }
        function f_scrollTop() {
	        return f_filterResults (
		        window.pageYOffset ? window.pageYOffset : 0,
		        document.documentElement ? document.documentElement.scrollTop : 0,
		        document.body ? document.body.scrollTop : 0
	        );
	    }
	    
        function f_filterResults(n_win, n_docel, n_body) {
	        var n_result = n_win ? n_win : 0;
	        if (n_docel && (!n_result || (n_result > n_docel)))
		        n_result = n_docel;
	        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
        }        
 
 function toggle_loadingImage(state) {
    var e = document.getElementById('imgLoading');
    
    if(state == 'on')
        e.style.display = 'block';
    else
        e.style.display = 'none';
}  
