var box_static_x = 0;
var box_static_y = 0;
var mousedown = 0;

function show_box(e)
{
	//define objects
	var box = document.getElementById('box');
	var map = document.getElementById('map');
	
	//find map top left
	var map_top = find_pos_y(map);
	var map_left = find_pos_x(map);
	
	//find mouse 
	var mouse_x = get_mouse_x(e);
	var mouse_y = get_mouse_y(e);
	
	//find top and left
	var box_top = mouse_y - map_top;
	var box_left = mouse_x - map_left;
	
	//set stationary points of box
	box_static_y = box_top;
	box_static_x = box_left;
	
	//set top and left and height and width
	box.style.width = 0;
	box.style.height = 0;
	box.style.top = box_top;
	box.style.left = box_left;
	
	//set the mouse button to clicked
	mousedown = 1;
	
	//show box
	box.style.display = '';
}

function size_box(e)
{
	if ( mousedown == 1 )
	{
		//define objects
		var box = document.getElementById('box');
		
		//find map top left
		var map_top = find_pos_y(document.getElementById('map'));
		var map_left = find_pos_x(document.getElementById('map'));
		
		//find mouse relative to the map
		var rel_cur_mouse_y = get_mouse_y(e) - map_top;
		var rel_cur_mouse_x = get_mouse_x(e) - map_left;
		
		//find difference between current mouse and mouse on mousedown
		var diff_y = box_static_y - rel_cur_mouse_y;
		var diff_x = box_static_x - rel_cur_mouse_x;
		
		//set box height
		box.style.height = Math.abs(diff_y);
		box.style.width = Math.abs(diff_x);
		
		//set box to accept negative values
		if ( rel_cur_mouse_x <= box_static_x )
		{
			box.style.left = rel_cur_mouse_x;
		}
		if ( rel_cur_mouse_y <= box_static_y )
		{
			box.style.top = rel_cur_mouse_y;
		}
	}
}
	
	

function hide_box()
{
	//define objects
	var box = document.getElementById('box');
	var map = document.getElementById('map');
	
	//grab box details (a little trimming involved)
	var box_top = strip_units(box.style.top);
	var box_left = strip_units(box.style.left);
	var box_height = strip_units(box.style.height);
	var box_width = strip_units(box.style.width);
	
	//find absolute (x1, y1) and (x2, y2) for the map server
	var x1 = box_left;
	var y1 = box_top;
	var x2 = eval(box_left + " + " + box_width); //not sure why I need eval
	var y2 = eval(box_top + " + " + box_height);
	
	//set hidden variables to contain points
	document.dmg_map_request.ZoomBox_x1.value = x1;
	document.dmg_map_request.ZoomBox_y1.value = y1;
	document.dmg_map_request.ZoomBox_x2.value = x2;
	document.dmg_map_request.ZoomBox_y2.value = y2;
	
	//set tool so that if box is small than 7px square it becomes a click
	if ( strip_units(box.style.height) < 7 && strip_units(box.style.width) < 7 )
	{
		setActiveTool('ZoomIn');
	}
	else
	{
		setActiveTool('ZoomBox');
	}
	
	//submit_form
	document.dmg_map_request.submit();
	
	//show "zoom_alert" span
	document.getElementById('zoom_alert').style.display = '';
	
	//hide box
	box.style.display = 'none';
	
	//set the mouse to be unclicked
	mousedown = 0;
}

//----------------------------------
//ON LOAD - add the listeners     --
//----------------------------------
add_event(window, 'load', add_listeners, false);

//----------------------------------
//default behavior functions      --
//----------------------------------

//add listeners for the mouse rollovers
function add_listeners(e)
{
	
	if ( !document.getElementById )
	{
		return;
	}
	add_event(document.getElementById('map'), 'mousedown', show_box, false);
	add_event(document.getElementById('map'), 'mousemove', size_box, false);
	add_event(document.getElementById('map'), 'mouseup', hide_box, false);
}

//find mouse x and y
function get_mouse_x(e)
{
	if (document.all) 
	{
    	X = event.clientX + document.body.scrollLeft;
  	} 
  	else 
  	{
    	X = e.pageX;
  	}
  
  	return X;
}

function get_mouse_y(e)
{
	if (document.all) 
	{
    	Y = event.clientY + document.body.scrollTop;
  	} 
  	else 
  	{
    	Y = e.pageY;    
  	}
  	
  	return Y;
}
	

//add events function with feature sniffing
function add_event(elm, evType, fn, useCapture)
{
	if( elm.addEventListener )
	{
		elm.addEventListener( evType, fn, useCapture);
		return true;
	}
	else if ( elm.attachEvent )
	{
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else
	{
		elm['on' + evType] = fn;
	}
}

function find_pos_x(obj)
{
	var curLeft = 0;
	if ( obj.offsetParent )
	{
		do
		{
			curLeft += obj.offsetLeft;
		}
		while( obj = obj.offsetParent );
	}
	else if( obj.x )
	{
		curLeft += obj.x;
	}
	return curLeft;
}

function find_pos_y(obj)
{
	var curTop = 0;
	if ( obj.offsetParent )
	{
		do
		{
			curTop += obj.offsetTop;
			//alert(curTop);
		}
		while ( obj = obj.offsetParent )
	}
	else if ( obj.y )
	{
		curTop += obj.y;
	}
	
	return curTop;
}

function strip_units(obj)
{
	var stripped_obj = obj.replace(/px/,'').replace(/pt/,'');
	return stripped_obj;
}

/* Unused Code that may come in handy

		//THIS CODE WILL SET THE BOX PROPERTIES IN INDIVIDUAL QUADRANTS
		//0 degrees
		if ( rel_cur_mouse_x > box_static_x && rel_cur_mouse_y == box_static_y )
		{
			//alert('0');
			box.style.left = box_static_x;
			box.style.top = box_static_y;
		}
		//90 degrees
		else if ( rel_cur_mouse_x == box_static_x && rel_cur_mouse_y > box_static_x )
		{
			//alert('90');
			box.style.left = box_static_x;
			box.style.top = box_static_y;
		}
		//180 degrees
		else if ( rel_cur_mouse_x < box_static_x && rel_cur_mouse_y == box_static_y )
		{
			//alert('180');
			box.style.left = rel_cur_mouse_x;
			box.style.top = box_static_y;
		}
		//270 degrees
		else if ( rel_cur_mouse_x == box_static_x && rel_cur_mouse_y < box_static_y )
		{
			//alert('270');
			box.style.left = box_static_x;
			box.style.top = rel_cur_mouse_y;
		}
		//Quad I
		else if ( rel_cur_mouse_x > box_static_x && rel_cur_mouse_y > box_static_y )
		{
			box.style.left = box_static_x;
			box.style.top = box_static_y;
		}
		//Quad II
		else if ( rel_cur_mouse_x < box_static_x && rel_cur_mouse_y > box_static_y )
		{
			box.style.left = rel_cur_mouse_x;
			box.style.top = box_static_y;
		}
		//Quad 3
		else if ( rel_cur_mouse_x < box_static_x && rel_cur_mouse_y < box_static_y )
		{
			box.style.left = rel_cur_mouse_x;
			box.style.top = rel_cur_mouse_y;
		}
		//Quad 4
		else if ( rel_cur_mouse_x > box_static_x && rel_cur_mouse_y < box_static_y )
		{
			box.style.left = box_static_x;
			box.style.top = rel_cur_mouse_y;
		}
		//END QUAD CODE
		
		
		//THIS NEXT CODE FINDS THE HEIGHT OF THE MAP AND PLACES A MESSAGE OVER THE MAP WHILE RELOADING
		//TAKEN OUT TO ALSO LET SIDE NAV BUTTONS DISPLAY MESSAGE
		
		//find top for "Now Zooming" box
		var message_box_top = (strip_units(map.style.height) - 50) / 2;
		//create display for "Now Zooming" box
		map.innerHTML = "";
		
		//END DISPLAY CODE
*/
