/****************************************************************************************
 * File:		mouseEvents.js
 * Author:		Drew Eggers, Watauga County 2005
 * Revised:		Oct. 12 2005
 * Functions:	map_mouse_down() - determines what fn to call depending on the tool
 *				startpan() - entry point for panning functions, set up variables, 
 *						     and call functions depending on the mouse events
 *				doPan() - set up variables/offsets, call crop image function, and 
 *						  call move layer function
 *				stopPanWithMouseUp() - stop panning because mouseup has 'fired'
 *				stopPanWithMouseOut() - stop panning because user has dragged to 
 *										the point that the cursor has left the map area
 *				clipLayer(name, clipleft, cliptop, clipright, clipbottom) - takes the
 *								map image and crops it to the area that the map initially
 *								occupies
 *				getLayer(name) - returns the html element that is the map image
 *				moveLayer(name, x, y) - move the map image's upper lh corner to x and y
 *				zoomin_md() - start drawing 'rubberband'.  Handle mouseup and mousemove
 *							  events
 *				zoomin_mm() - Called when mouse is moving is dragging.  Draws 'rubberband'
 *							  from initial location to new location
 *				zoomin_mu() - Called onmouseup.  Makes 'rubberband' disappear and stores
 *							  the position of the initial click and the position of the
 *							  mouse when the button is released
 * Purpose:		Handles different mouse events by setting up variables and manipulating
 *				the browser's display, but NOT handling actual ActiveX Connector 
 *				events (DoZoomToExtent(), DoZoom(), DoZoomToFullExtent(), etc.)
 * Notes:		Rubberband idea taken loosely from:
 *				http://forums.esri.com/Thread.asp?c=64&f=783&t=89326&mc=14#245915
 *****************************************************************************************/

/* Global Variables */
var startx;		// Initial x location of click on window (not map)
var starty;		// Initial y location of click on window (not map)
var origx;		// Initial x location of upper left-hand corner of map
var origy;		// Initial y location of upper left-hand corner of map
var endx;		// x location of mouseup or last location before leaving map (window - not map)
var endy;		// y location of mouseup or last location before leaving map (window - not map)
var iWidth;		// width of the map
var iHeight;	// height of the map

/************************************************************************ 
 * Function:	map_mouse_down
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		none.
 * Returns:		none.	
 * Description: determines what function to call depending on the tool         
 ************************************************************************/
function map_mouse_down()
{
	switch (get_cookie("tool")) // find the tool to be used
	{
		case 'zoomin':			 // if tool is 'zoomin'
			Map_onMouseDown();		 // go to entry point of zoomin functions		
			break;
		case 'pan': 			 // if tool is 'pan'
			startpan();			 // go to entry point of pan funtions
	}
}

/************************************************************************ 
 * Function:	startpan
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		none.
 * Returns:		none.	
 * Description: entry point for panning functions, set up variables, 
 *				and call functions depending on the mouse events         
 ************************************************************************/
function startpan()
{
	var layer = getLayer("mainmap");	// get the html object (layer) named mainmap
	
	ns4 = (document.layers) ? true:false	// ns4 is true if browser is ns4 type
	
	origx = get_cookie("maplocx");			// Get the (x, y) of the map image
	origy = get_cookie("maplocy");
	iWidth = get_cookie("mapwidth");		// Get the diminsions of the map
	iHeight = get_cookie("mapheight");

    scrollY = document.body.scrollTop;		// Get the distance (pixels) that has been
											// scolled down the page

	layer.left = origx;					// set the coords of the map layer to the 
	layer.right = origy;				// coords of the map image
    layer.position = "absolute";		// allow image to be moved by pixels (can put anywhere)
     
    if (ns4)					// if ns4 type
    {
		startx = e.pageX;		// x coord of mouse click in pixels on the page (not map)
		starty = e.pageY;		// y coord of mouse click in pixels on the page (not map)
	}
    else
    {
		startx = event.x;		// x coord of mouse click in pixels on the page (not map)
		starty = event.y + scrollY; // (y coord) top is considered top of viewed area - not actual top
	}
		
	MapTable.onmousemove = doPan;				// while dragging, call doPan()
	MapTable.onmouseup   = stopPanWithMouseUp;	// call stopPanWithMouseUp() when dragging stops
	MapTable.onmouseout  = stopPanWithMouseOut; // call stopPanWithMouseOut() when mouse drags out of map area
}

/************************************************************************ 
 * Function:	doPan
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		none.
 * Returns:		none.	
 * Description: set up variables/offsets, call crop image function, and 
 *				call move layer function         
 ************************************************************************/
function doPan()
{
	ns4 = (document.layers) ? true:false		// ns4 is true if browser is netscape 4 type
		
    scrollY = document.body.scrollTop;		// Get the distance (pixels) that has been
											// scolled down the page

    if (ns4)
    {
		curx = e.pageX;				// x coord of current mouse position in pixels on the page (not map)
		cury = e.pageY;				// y coord of current mouse position in pixels on the page (not map)
	}
    else
    {
		curx = event.x;				// x coord of current mouse position in pixels on the page (not map)
		cury = event.y + scrollY;	// (y coord)top is considered top of viewed area - not actual top	
	}
	
	/****************************************************************************************************
	 * Note that all numbers are relative to the INITIAL click - NOT the previous position of the mouse *
	 ****************************************************************************************************/
	var xoffset = startx - curx;	// number of pixels mouse has dragged horizontally
	var yoffset = starty - cury;	// number of pixels mouse has dragged vertically
	var cLeft = xoffset;			// number of pixels to crop from left side of map image
	var cTop = yoffset;				// number of pixels to crop from top of map image
	var cRight = iWidth;			// number of pixels to crop from right of map image
	var cBottom = iHeight;			// number of pixels to crop from bottom of map image

	if (xoffset < 0)				// if mouse has moved to the right (from where map was clicked)
	{
		cLeft = 0;							// don't want to crop left side at all 
		cRight = iWidth - (-xoffset);		// crop right side amount mouse has dragged to the right
	}
	if (yoffset < 0)				// if mouse has moved down the page
	{
		cTop = 0;							// don't want to crop top at all
		cBottom = iHeight - (-yoffset);		// crop bottom amount mouse has dragged down the page 
	}		

	clipLayer("mainmap", cLeft, cTop, cRight, cBottom);			// crop image to map's initial area and...
	moveLayer("mainmap", origx - xoffset, origy - yoffset);		// drag the image around the page

}

/************************************************************************ 
 * Function:	stopPanWithMouseUp
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		none.
 * Returns:		none.	
 * Description: stop panning because mouseup has 'fired'
 *				actual pan/reload of image is handled by tools.asp       
 ************************************************************************/
function stopPanWithMouseUp()
{
	ns4 = (document.layers) ? true:false	// ns4 is true if browser is netscape 4 type
	
	scrollY = document.body.scrollTop;		// Get the distance (pixels) that has been
											// scolled down the page

    if (ns4)
    {
		endx = e.pageX;					// x coord of current mouse position in pixels on the page (not map)
		endy = e.pageY;					// y coord of current mouse position in pixels on the page (not map)
	}
    else
    {
		endx = event.x;					// x coord of current mouse position in pixels on the page (not map)
		endy = event.y + scrollY;		// (y coord) top is considered top of viewed area - not actual top
	}

	document.cookie = "startx=" + startx;		// store xcoord's starting position of pan
    document.cookie = "starty=" + starty;		// store y coord's starting position of pan
    document.cookie = "endx=" + endx;			// store x coord's ending position
    document.cookie = "endy=" + endy;			// store y coord's ending position
    
    /* toolform will now submit because of mouseup.  tools.asp handles this event */
}

/************************************************************************ 
 * Function:	stopPanWithMouseOut
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		none.
 * Returns:		none.	
 * Description: stop panning because user has dragged to 
 *				the point that the cursor has left the map area     
 *				actual pan/reload of image is handled by tools.asp   
 ************************************************************************/
function stopPanWithMouseOut()
{
	ns4 = (document.layers) ? true:false	// ns4 is true if browser is netscape 4 type
	
	scrollY = document.body.scrollTop;		// Get the distance (pixels) that has been
											// scolled down the page

    if (ns4)
    {
		endx = e.pageX;					// x coord of current mouse position in pixels on the page (not map)
		endy = e.pageY;					// y coord of current mouse position in pixels on the page (not map)
	}
    else
    {
		endx = event.x;					// x coord of current mouse position in pixels on the page (not map)
		endy = event.y + scrollY;		// (y coord) top is considered top of viewed area - not actual top
	}

    document.cookie = "startx=" + startx;	// store xcoord's starting position of pan
    document.cookie = "starty=" + starty;	// store y coord's starting position of pan
    document.cookie = "endx=" + endx;		// store x coord's ending position
    document.cookie = "endy=" + endy;		// store y coord's ending position
    
    toolform.submit();					// form even will not have 'fired' since there was never a mouseup
										// event.  Must manually submit the form to be processed by tools.asp
}

/************************************************************************ 
 * Function:	clipLayer
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		name - id of the layer to be clipped
 *				clipleft - num pixels to left side to crop
 *				cliptop - num pixels on top to crop
 *				clipright - num pixels on right to crop
 *				clipbottom - num pixels on bottom to crop
 * Returns:		none.	
 * Description: takes the map image and crops it to the area that the 
 *				map initially occupies				       
 ************************************************************************/
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) 
{
	ns4 = (document.layers) ? true:false	// ns4 is true if browser is netscape 4 type
	
	var layer = getLayer(name);				// Get the html object (layer) to move
	
	if (ns4) 
	{
		/* Crop each side of the image (Netscape 4 style) */
		layer.clip.left   = clipleft;
		layer.clip.top    = cliptop;
		layer.clip.right  = clipright;
		layer.clip.bottom = clipbottom;
	}
	else
	{
		/* Crop the image to a rectangle (IE style) */
		layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	}
}

/************************************************************************ 
 * Function:	moveLayer
 * Author:		Drew Eggers, Watauga County
 * Date:		2005
 * Params:		name - name of the html object (layer) to move
 *				x - new x location for the top left corner of the layer
 *				y - new y location for the top left corner of the layer
 * Returns:		none.	
 * Description: move the map image's upper lh corner to x and y      
 ************************************************************************/
function moveLayer(name, x, y) 
{
	ns4 = (document.layers) ? true:false	// ns4 is true if browser is netscape 4 type
	
  	var layer = getLayer(name);				// Get the html object (layer) to move

  	if (ns4)
    	layer.moveTo(x, y);					// move the upper left hand corner of layer to x,y
	else 
	{
    	layer.left = x + "px";				// move layer horizontally x pixels
		layer.top  = y + "px";				// move layer vertically y pixels
  	}
}

/***********************************************************************
 * Next 3 functions display XY coordinates on status bar
 ***********************************************************************/
function showXY()
{
	var numDecimals = 8;
	
	getImageXY()
	getMapXY(window.event.clientX,window.event.clientY)
	var u = Math.pow(10,numDecimals);
	var uX = Math.round(mapX * u) / u
	var uY= Math.round(mapY * u) / u
	var statDisplay = ""

	//make sure the mouse is within the image to display other wise 
	//clear the status. 
	if ((mouseX<get_cookie("mapwidth")) && (mouseY<get_cookie("mapheight"))&&(mouseX>0)&&(mouseY>0)){
		window.status = "X:" +uX + "  Y:" + uY;
	}else{
			window.status = "";
	}
}

function getImageXY()
{
    ns4 = (document.layers) ? true:false
    
	if (ns4) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	}
	// subtract offsets from page left and top
	mouseX = mouseX-get_cookie("maplocx");
	mouseY = mouseY-get_cookie("maplocy");
}

function getMapXY(xIn,yIn) {
		pixelX = Math.abs(get_cookie("currentExtentRight")  -(get_cookie("currentExtentLeft"))) / get_cookie("mapwidth");
		mapX = pixelX * xIn + (+get_cookie("currentExtentLeft"));
		pixelY = Math.abs(get_cookie("currentExtentTop")-(get_cookie("currentExtentBottom"))) / get_cookie("mapheight");
		mapY = pixelY * (get_cookie("mapheight") - yIn) + (+get_cookie("currentExtentBottom"));
}