/* GDPixelRectangle class
	by: randy bacon
	10/03/06
	Used to contain a rectangle based on a pixels - using google GPoint() for x,y
*/
function GDPixelRectangle()
{
	//class members (consider _ members private)
	this._LeftTop;
	this._RightBottom;
	
	//inline constructor
	this.Init(arguments);
}

//GDPixelRectangle :: Init :: constructor of the sorts
GDPixelRectangle.prototype.Init = function(args)
{
	if( args.length == 2)
	{
		//assume we have 2 google points
		this._LeftTop = args[0];
		this._RightBottom = args[1];
	}
	else( args.length == 4)
	{
		this._LeftTop = new GPoint(args[0], args[1]);
		this._RightBottom = new GPoint(args[2], args[3]);
	}
}

//GDPixelRectangle :: GetRightBottom :: get for the right bottom member
GDPixelRectangle.prototype.GetRightBottom = function()
{
	return this._RightBottom;
}

//GDPixelRectangle :: GetLeftTop :: get for the left top member
GDPixelRectangle.prototype.GetLeftTop = function()
{
	return this._LeftTop;
}

//GDPixelRectangle :: toString :: override object toString
GDPixelRectangle.prototype.toString =  function()
{
	return "LeftTopX: " + this._LeftTop.x + " LeftTopY: " + this._LeftTop.y + " RightBottomX: " + this._RightBottom.x + " RightBottomY: " + this._RightBottom.y;
}

//GDPixelRectangle :: InBox :: see if a X,Y point is inside this box
//caller is responsible for having box set up correctly
GDPixelRectangle.prototype.InBox = function(pointX, pointY)
{
	if ( pointX > this._LeftTop.x && pointX < this._RightBottom.x && pointY < this._RightBottom.y && pointY > this._LeftTop.y )
		return true;
	else
		return false;
}

//GDPixelRectangle :: DrawDivBox :: draws our box as a div positioned relative to an element
//mostly for debugging - element must be an element object
GDPixelRectangle.prototype.DrawDivBox = function(elem)
{
	if( elem != null )
	{
		//create the div
		var div = document.createElement("div");
 			div.style.border = "1px solid red";
		div.style.position = "absolute";
		div.style.width = (this._RightBottom.x - this._LeftTop.x) + "px";
		div.style.height = (this._RightBottom.y - this._LeftTop.y) + "px";
		div.style.left = this._LeftTop.x + "px";
		div.style.top = this._LeftTop.y + "px";
		//attach it to our elem
		elem.appendChild(div);
	}
}
//end --------- GDPixelRectangle

/*  GDGoogleMap class
	by: randy bacon
	10/03/06
	Control and instance of a google map and the search interface
*/

//GDGoogleMap CONSTANTS
var GDMAPOBJ;
var GOOGMAP_ELEM = "gdGoogleMap";
var GOOGMAPOVERLAY_ELEM = "gdGoogleMapOverlay";
var GDZOOMTOOL_ELEM = "gdMapToolBox";
var GDDEFAULTTOOL_ELEM = "gdMapToolDrag";
var GDREMOVEBOX_ELEM = "gdRemoveMapBox";
var GDREMOVENOBOX_ELEM = "gdRemoveMapBoxDisabled";
var ZOOMAREA_KEYCODE = 90; //z
var MOUSELOCATION_X = 0;
var MOUSELOCATION_Y = 0;
var IMAGEBASE_URL = "http://images.graphicaldata.com/zoommaps/";
var GOOGLEMAPO;
var GDWAITBNDRAWS = 800;
var GDTOOLDEFAULT = 0;
var GDTOOLZOOMBOX = 1;
var GDCURRTOOL = GDTOOLDEFAULT;
var GDBGON = 1;
var GDBGOFF = 2;
var GDBGNA = 3;
var GDTOOLCLASSES = new Array();
GDTOOLCLASSES[GDTOOLDEFAULT] = new Array();
GDTOOLCLASSES[GDTOOLDEFAULT][0] = GDDEFAULTTOOL_ELEM;
GDTOOLCLASSES[GDTOOLDEFAULT][GDBGON] = null;
GDTOOLCLASSES[GDTOOLDEFAULT][GDBGOFF] = null;
GDTOOLCLASSES[GDTOOLZOOMBOX] = new Array();
GDTOOLCLASSES[GDTOOLZOOMBOX][0] = GDZOOMTOOL_ELEM;
GDTOOLCLASSES[GDTOOLZOOMBOX][GDBGON] = null;
GDTOOLCLASSES[GDTOOLZOOMBOX][GDBGOFF] = null;

function GDGoogleMap()
{
	//class members (consider _ members private)
	this._gdMap;	
	this._gdMGoogleZoomSC;
	this._gdMGoogleOverMC;
	this._PolyOutline = null;		
	this._gdMapInitCenter;
	this._gdMapInitZoom;
	this._SearchResultsView = false;
	this._gdRemoveBoxLink;
	this._gdNoRemoveBoxLink;
			
	//class props
	this.GDMPixelOffset;
	this.GDMBounds;
	this.MapParams = new GDMapParams();
	this.GDZoomBox = new GDZoomBox();
	this.PlyPoints;
	this.BoxLineColor = "#CC0202";
	this.BoxLineWidth = 2;
	this.BoxLineTrans = .65;
	this.GDMapOverlay;	
}
	
//GDGoogleMap :: Init :: create our map instance and build
//call with (center lat, center long, zoom, polyPoints) OR (NE lat, NE long, SW lat, SW long, polyPoints)
GDGoogleMap.prototype.Init = function()
{
	if( arguments.length == 4 || arguments.length == 5 )
	{
		//map elem
		this._gdMap = document.getElementById(GOOGMAP_ELEM);
		//shared map set up
		this.GDMapOverlay = document.getElementById(GOOGMAPOVERLAY_ELEM);
		//create map
		GOOGLEMAPO = new GMap2(this._gdMap);
		//add controls
		GOOGLEMAPO.addControl(new GLargeMapControl());
		GOOGLEMAPO.addControl(new GScaleControl());
		//var OMap = new GOverviewMapControl();
     	//	GOOGLEMAPO.addControl(OMap);
		//OMap.hide(); 
		//allow google zooms
		this.EnableG();
	}
	if( arguments.length == 4)
	{
		//now set the center and zoom in
		GOOGLEMAPO.setCenter(new GLatLng(arguments[0], arguments[1]), arguments[2]); 
		this.PlyPoints = arguments[3];
		this._gdMapInitCenter = new GLatLng(arguments[0], arguments[1]);
		this._gdMapInitZoom = arguments[2];
	}
	else if( arguments.length == 5)
	{
		//get the bounds and zoom in
		var neCrn = new GLatLng(arguments[0], arguments[1]);
		var swCrn = new GLatLng(arguments[2], arguments[3]);
		var rsBounds = new GLatLngBounds(swCrn, neCrn);
		
		//build map and center
		GOOGLEMAPO.setCenter(rsBounds.getCenter(), GOOGLEMAPO.getBoundsZoomLevel(rsBounds));
		this.PlyPoints = arguments[4];
		this._gdMapInitCenter = rsBounds.getCenter();
		this._gdMapInitZoom = GOOGLEMAPO.getBoundsZoomLevel(rsBounds);
	}
	
	if( arguments.length == 4 || arguments.length == 5 )
	{
		//initalize tools and zoom
		this._ZoomInit();
	}
	
	//draw box if we are told to
	if( arguments.length == 5 )
	{
		this.PlyPoints = arguments[4];
		this.DrawPolyBox(true, true);
		//fire poly bounds changed
		this.LPolyBoundsChanged();
	}
}
	
//GDGoogleMap :: _ZoomInit :: method to init our zoom tools and objs
GDGoogleMap.prototype._ZoomInit = function()
{
	//set up some element refs
	this.GDMPixelOffset = gdGloGetObjectOffset(this._gdMap);
	
	GDTOOLCLASSES[GDTOOLDEFAULT][GDBGON] = document.getElementById(GDDEFAULTTOOL_ELEM + "BGOn");
	GDTOOLCLASSES[GDTOOLDEFAULT][GDBGOFF] = document.getElementById(GDDEFAULTTOOL_ELEM + "BGOff");
	GDTOOLCLASSES[GDTOOLZOOMBOX][GDBGON] = document.getElementById(GDZOOMTOOL_ELEM + "BGOn");
	GDTOOLCLASSES[GDTOOLZOOMBOX][GDBGOFF] = document.getElementById(GDZOOMTOOL_ELEM + "BGOff");
	
	this._gdRemoveBoxLink = document.getElementById(GDREMOVEBOX_ELEM);
	this._gdNoRemoveBoxLink = document.getElementById(GDREMOVENOBOX_ELEM);
	
	//bind event for mouse and key down/up
	document.onmousemove = this.MouseMoveH;
	document.onkeyup = this.KeyUpH;
	document.onkeydown = this.KeyDownH;
	
	//set up our map pixel bounds
	this.GDMBounds = new GDPixelRectangle(this.GDMPixelOffset.x, this.GDMPixelOffset.y, this.GDMPixelOffset.x + parseFloat(this._gdMap.style.width), this.GDMPixelOffset.y + parseFloat(this._gdMap.style.height));

	//slider
	this._gdMGoogleZoomSC = new GDPixelRectangle(this.GDMBounds.GetLeftTop().x + 7, this.GDMBounds.GetLeftTop().y + 7, this.GDMBounds.GetLeftTop().x + 62, this.GDMBounds.GetLeftTop().y + 260);
	//overview expander
	this._gdMGoogleOverMC = new GDPixelRectangle(this.GDMBounds.GetRightBottom().x - 15, this.GDMBounds.GetRightBottom().y - 15, this.GDMBounds.GetRightBottom().x, this.GDMBounds.GetRightBottom().y);

	//tell zoom tool to go
	this.ChangeToolState(GDTOOLDEFAULT);
	
	//add listener for our mouse down to the overlay
	GEvent.addDomListener(this.GDMapOverlay, 'mousedown', this.MouseDownH); 
	GEvent.addDomListener(this.GDMapOverlay, 'mouseup', this.MouseUpH); 
	
	//add handler for zoom
	GEvent.addListener(GOOGLEMAPO, "zoomend", function()
	{
		glZoomChanged();
	});
	
	//add move end handler
	GEvent.addListener(GOOGLEMAPO, "moveend", glCenterChanged);
		
	//fire params listners
	this.LFireAllListners();
		
	//put focus on my frame
	document.body.focus();
}

//GDGoogleMap :: ChangeToolState :: changes the map tool state and selection
GDGoogleMap.prototype.ChangeToolState = function(selTool)
{
	if( selTool != GDCURRTOOL)
	{
		GDCURRTOOL = selTool;
		var tmpState = "";
		for(var i=0;i<GDTOOLCLASSES.length;i++)
		{
			if( i == GDCURRTOOL)
			{
				GDTOOLCLASSES[i][GDBGON].style.display = "block";
				GDTOOLCLASSES[i][GDBGOFF].style.display = "none";
			}
			else
			{
				GDTOOLCLASSES[i][GDBGON].style.display = "none";
				GDTOOLCLASSES[i][GDBGOFF].style.display = "block";							
			}
		}
	}
}

//GDGoogleMap :: SetMapType :: change the map type
GDGoogleMap.prototype.SetMapType = function(selectedType)
{
	//V2 map types: G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP
	switch(selectedType)
	{
		case "2":
			GOOGLEMAPO.setMapType(G_HYBRID_MAP);
			break;
		case "1":
			GOOGLEMAPO.setMapType(G_SATELLITE_MAP);
			break;
		default:
			GOOGLEMAPO.setMapType(G_NORMAL_MAP);
			break;
	}
}

//GDGoogleMap :: CurrentSearchBounds :: returns the current poly or the map bounds
GDGoogleMap.prototype.CurrentSearchBounds = function()
{
	if( this.MapParams.MLPolyBounds.value == "" )
		return this.MapParams.Corners();
	else
		return this.MapParams.MLPolyBounds.value;
}

//GDGoogleMap :: Dispose :: clean up our events
GDGoogleMap.prototype.Dispose = function()
{
	document.onmousemove = null;
	document.onkeyup = null;
	document.onkeydown = null;
}

//GDGoogleMap :: ResetSearch :: reset map to our initial settings
GDGoogleMap.prototype.ResetSearch = function()
{
	//clear map overlays
	GOOGLEMAPO.clearOverlays();
	//then recenter and zoom map
	this.CenterMap(this._gdMapInitCenter, this._gdMapInitZoom);
	this.ResetAllToolsNoReCenter();
}

//GDGoogleMap :: ResetAllToolsNoReCenter :: resets all the tools and borders and poly points without recenter or zoom
GDGoogleMap.prototype.ResetAllToolsNoReCenter = function()
{
	//change to default tool
	this.ChangeToolState(GDTOOLDEFAULT);
	//reset tools
	this.ResetTools(false, true);
	//clear poly points
	this.PlyPoints = null;
	//reset box complete
	this.GDZoomBox.ResetComplete(false);
	//clear draw box
	this.RemoveBorderBox();
	//then tell listner we have poly bounds changed
	this.LPolyBoundsChanged();
	//hide remove box option
	this._gdNoRemoveBoxLink.style.display = "block";
	this._gdRemoveBoxLink.style.display = "none";
}

//GDGoogleMap :: PolyArray :: turns a correctly formatted string into an array of poly points
GDGoogleMap.prototype.PolyArray = function(plyStr)
{
	var plyArray = new Array()
	var indItems = plyStr.split(',');
	if( indItems.length > 0)
	{
		for(var i = 0; i<indItems.length; i++)
		{
			var innerItem = indItems[i].split('~');
			if( innerItem.length == 2)
			{
				plyArray.push(new GLatLng(innerItem[0], innerItem[1]));
			}
		}
	}
	else
	{
		return null;
	}
	return plyArray;
}

//GDGoogleMap :: DrawPolyBox :: method to draw our poly box on the map
GDGoogleMap.prototype.DrawPolyBox = function(isABox, closeItUp)
{
	this.RemoveBorderBox();
	if( this.PlyPoints != null )
	{
		var points;
		if( this.PlyPoints.length == 2 && isABox)
		{
			points = this.BoxCorners(this.PlyPoints);
		}
		else
		{
			points = this.PlyPoints;
			if( closeItUp )
				points.push(points[0]);
		}
		//now we draw the box
		this._PolyOutline = new GPolyline(points, this.BoxLineColor, this.BoxLineWidth, this.BoxLineTrans);
		GOOGLEMAPO.addOverlay(this._PolyOutline);
		//and we show the remove box link
		this._gdRemoveBoxLink.style.display = "block";
		this._gdNoRemoveBoxLink.style.display = "none";
	}
}

//GDGoogleMap :: BoxCorners :: expects a 2 point array will return an array of box points
GDGoogleMap.prototype.BoxCorners = function(points)
{
	var boxPoints = new Array();
	//pass box on corners above
	boxPoints[0] = new GLatLng(points[0].y, points[1].x);
	boxPoints[1] = points[0];
	boxPoints[2] = new GLatLng(points[1].y, points[0].x);
	boxPoints[3] = points[1];
	boxPoints[4] = new GLatLng(points[0].y, points[1].x);
	return boxPoints;
}

//GDGoogleMap :: RemoveBorderBox :: removes the border box from the map if there is one
GDGoogleMap.prototype.RemoveBorderBox = function()
{
	if( this._PolyOutline != null && this._PolyOutline != 'undefined')
		GOOGLEMAPO.removeOverlay(this._PolyOutline);
}

//GDGoogleMap :: PixelToLatLng :: return a lat/long from a gpoint pixel location
GDGoogleMap.prototype.PixelToLatLng = function(gPointVr)
{
	return GOOGLEMAPO.fromContainerPixelToLatLng(gPointVr)
}

//GDGoogleMap :: LatLngToPixel :: return a pixel from a gLatLong point
GDGoogleMap.prototype.LatLngToPixel = function(gLatLong)
{
	return GOOGLEMAPO.fromLatLngToDivPixel(gLatLong)
}

//GDGoogleMap :: FindPointXY :: find the point x,y of an item on the map by lat/long
GDGoogleMap.prototype.FindPointXY = function(gLatLongPoint)
{
	var pointOffset = this.LatLngToPixel(gLatLongPoint);
	var pointSW = GOOGLEMAPO.fromLatLngToDivPixel(GOOGLEMAPO.getBounds().getSouthWest());
	var pointNE = GOOGLEMAPO.fromLatLngToDivPixel(GOOGLEMAPO.getBounds().getNorthEast()); 
	var x =  pointOffset.x - pointSW.x;
	var y = pointOffset.y - pointNE.y; 
	return new GPoint(x, y);
}

//GDGoogleMap :: DisableG :: disable google drag and zoom
GDGoogleMap.prototype.DisableG = function()
{
	GOOGLEMAPO.disableDoubleClickZoom();
	GOOGLEMAPO.disableDragging();
}

//GDGoogleMap :: EnableG :: enable google drag and zoom
GDGoogleMap.prototype.EnableG = function()
{
	GOOGLEMAPO.enableDoubleClickZoom();
	GOOGLEMAPO.enableDragging();
}

//GDGoogleMap :: MouseDownH :: event handler for the mouse down event
GDGoogleMap.prototype.MouseDownH = function(e)
{
	if (!e) 
		e = window.event;
	if(!GDMAPOBJ.GDZoomBox.MClickEnabled)
		return;
	GDMAPOBJ.GDZoomBox.StartDraw(false);
}

//GDGoogleMap :: MouseUpH :: event handler for the mouse up event
GDGoogleMap.prototype.MouseUpH =function(e)
{
	if (!e) 
		e = window.event;
	if(!GDMAPOBJ.GDZoomBox.MClickEnabled)
		return;
	GDMAPOBJ.GDZoomBox.EndDraw();
}

//GDGoogleMap :: MouseMoveH :: event handler for the mouse move
GDGoogleMap.prototype.MouseMoveH = function(e)
{
	//capture current mouse event
	if (!e) 
		e = window.event;
	
	//get mouse location
	if (e.pageX || e.pageY) 	
	{
		MOUSELOCATION_X = e.pageX;
		MOUSELOCATION_Y = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
		MOUSELOCATION_X = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		MOUSELOCATION_Y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	//see if we need to show/hide our mask
	if( GDMAPOBJ.GDZoomBox.MClickEnabled && !GDMAPOBJ.GDZoomBox.IsInSearch() )
		if( GDMAPOBJ.PointerOverMask() )
			GDMAPOBJ.GDMapOverlay.style.display = "none";
		else
			GDMAPOBJ.GDMapOverlay.style.display = "inline";
	
	//alert zoom to movement
	GDMAPOBJ.GDZoomBox.Move();
}

//GDGoogleMap :: KeyUpH :: event handler for the key up
GDGoogleMap.prototype.KeyUpH = function(e)
{
	if (!e) 
		e = window.event;
		
	if (e.keyCode != ZOOMAREA_KEYCODE)
		return;
	
	//tell zoom to end 
	GDMAPOBJ.GDZoomBox.EndDraw();
}

//GDGoogleMap :: KeyDownH :: event handler for the key down
GDGoogleMap.prototype.KeyDownH = function(e)
{
	if (!e) 
		e = window.event;
	if (e.keyCode == ZOOMAREA_KEYCODE)
	{
		//tell zoom tool start
		GDMAPOBJ.GDZoomBox.StartDraw(true);
	}
}

//GDGoogleMap :: ResetTools :: method to reset all our tools
GDGoogleMap.prototype.ResetTools = function()
{
	//set map items
	this._gdMap.style.cursor = "default";
	this.EnableG();
	//then reset tools
	this.GDZoomBox.Reset();
	this.GDZoomBox.ClickSearchState();
}

//GDGoogleMap :: LFireAllListners :: fire all listners to update map params
GDGoogleMap.prototype.LFireAllListners = function()
{
	this.LZoomChanged();
	this.LCenterChanged();
	this.LPolyBoundsChanged();
}

//GDGoogleMap :: LZoomChanged :: listner for zoom level changed
GDGoogleMap.prototype.LZoomChanged = function()
{
	this.MapParams.MLZoomLevel.value = GOOGLEMAPO.getZoom();
}

//GDGoogleMap :: LCenterChanged :: listner for center of map changed
GDGoogleMap.prototype.LCenterChanged = function()
{
	var mapLTLNBounds = GOOGLEMAPO.getBounds();
	this.MapParams.MLNECorner.value = mapLTLNBounds.getNorthEast().y + "~" + mapLTLNBounds.getNorthEast().x;
	this.MapParams.MLSWCorner.value = mapLTLNBounds.getSouthWest().y + "~" + mapLTLNBounds.getSouthWest().x;
	if( !this.GDZoomBox.IsInCenterAndZoom())
	{
		this.ResetAllToolsNoReCenter();
	}
}

//GDGoogleMap :: LPolyBoundsChanged :: poly bounds changed
GDGoogleMap.prototype.LPolyBoundsChanged = function()
{
	this.MapParams.MLPolyBounds.value = "";
	if( this.PlyPoints != null )
	{
		for(var i = 0; i<this.PlyPoints.length; i++)
		{
			if( i>0 )
				this.MapParams.MLPolyBounds.value += ",";
			this.MapParams.MLPolyBounds.value += this.PlyPoints[i].y + "~" + this.PlyPoints[i].x;
		}
	}
}

//GDGoogleMap :: UpdatePolyPointsL :: update the poly points listener WITHOUT searching
GDGoogleMap.prototype.UpdatePolyPointsL = function()
{
	this.MapParams.MLPolyBounds.value = "";
	if( this.PlyPoints != null )
	{
		for(var i = 0; i<this.PlyPoints.length; i++)
		{
			if( i>0 )
				this.MapParams.MLPolyBounds.value += ",";
			this.MapParams.MLPolyBounds.value += this.PlyPoints[i].y + "~" + this.PlyPoints[i].x;
		}
		if( this.PlyPoints.length == 2 )
		{
			this.GDZoomBox.ResetComplete(true);
		}
		else
		{
			this.GDZoomBox.ResetComplete(false);
		}
	}
}

//GDGoogleMap :: PointerInMapBounds :: sees if the mouse is currently within the map bounds
GDGoogleMap.prototype.PointerInMapBounds = function()
{
	return this.GDMBounds.InBox( MOUSELOCATION_X, MOUSELOCATION_Y );
}

//GDGoogleMap :: PointerOverMask :: sees if the mouse is currently over the mask
GDGoogleMap.prototype.PointerOverMask = function()
{
	//see if over the zoom slider box
	if( this._gdMGoogleZoomSC.InBox(MOUSELOCATION_X, MOUSELOCATION_Y) )
	{
		return true;
	}
	//see if over the map overview expand
	else if( this._gdMGoogleOverMC.InBox(MOUSELOCATION_X, MOUSELOCATION_Y) )
	{
		return true;
	}
	return false;	
}

//GDGoogleMap :: CenterMap :: method to zoom and center map on current bounds
//wants (GLatLng, zoomLevel) or (GLatLngBounds)
GDGoogleMap.prototype.CenterMap = function()
{
	if( arguments.length == 2 )
	{
		GOOGLEMAPO.setCenter(arguments[0], arguments[1]); 
	}
	else
	{
		GOOGLEMAPO.setCenter(arguments[0].getCenter(), GOOGLEMAPO.getBoundsZoomLevel(arguments[0])); 
	}
}

//global functions to give alerts for map
function glCenterChanged()
{
	//alert change
	GDMAPOBJ.LCenterChanged();
	//check if we need to get points
	if( !GDMAPOBJ.GDZoomBox.IsBoxComplete())
		GDMAPOBJ.LPolyBoundsChanged();
}

function glZoomChanged()
{
	//alert change
	GDMAPOBJ.LZoomChanged();
	//check if we need to get points
	if( !GDMAPOBJ.GDZoomBox.IsBoxComplete())
		GDMAPOBJ.LPolyBoundsChanged();
}
//end --------- GDGoogleMap

//functions for client interaction with a set up google map object
function UnloadGDMap()
{
	if(typeof(GDMAPOBJ) != "undefined")
		GDMAPOBJ.Dispose();
	GDMAPOBJ = null;
}

function zToolChangeHandler(selTool)
{
	GDMAPOBJ.ChangeToolState(selTool);
	GDMAPOBJ.ResetTools(true, true);
}

function ResetSearch()
{
	GDMAPOBJ.ResetSearch();
}

function ChangeMapType(sList)
{
	GDMAPOBJ.SetMapType(sList.value);
}

function RemoveMapBox()
{
	GDMAPOBJ.ResetAllToolsNoReCenter();
}

//disable select start in all for IE6 mostly
document.onselectstart = glDocSelStart;

function glDocSelStart(e)
{
	if (!e) 
		e = window.event;
	//see if we are text box
	if( e != null && e.srcElement != null && e.srcElement.type != null)
		return true;
	return false;
}
//end google map common

/*  GDMapParams class
	by: randy bacon
	10/04/06
	Container for map params
*/

//GDMapParams contants
var MLZOOMLEVEL_ELEM = "mLZoomLevel";
var MLNECORNER_ELEM = "mLNECorner";
var MLSWCORNDER_ELEM = "mLSWCorner";
var MLPOLYBOUNDS_ELEM = "mLPolyBounds";

function GDMapParams()
{
	/* inline constructor */
	this.MLZoomLevel = document.getElementById(MLZOOMLEVEL_ELEM);
	this.MLNECorner = document.getElementById(MLNECORNER_ELEM);
	this.MLSWCorner = document.getElementById(MLSWCORNDER_ELEM);
	this.MLPolyBounds = document.getElementById(MLPOLYBOUNDS_ELEM);
}

//GDMapParams :: Corners :: return the corners of our map in the right format (NE, SW)
GDMapParams.prototype.Corners = function()
{
	return this.MLNECorner.value + "," + this.MLSWCorner.value;
}

//GDMapParams :: toString :: overload to string to return useful information
GDMapParams.prototype.toString = function()
{
	var rtrStr = "";
	rtrStr = "NE Corner: ";
	rtrStr += this.MLNECorner.value;
	rtrStr += "\nSW Corner: ";
	rtrStr += this.MLSWCorner.value;
	rtrStr += "\nZoom Level: ";
	rtrStr += this.MLZoomLevel.value;
	rtrStr += "\nPoly Bounds: ";
	rtrStr += this.MLPolyBounds.value;
	return rtrStr;
}
//end --------- GDMapParams

/*  GDZoomBox class
	by: randy bacon
	10/04/06
	Zoom box tool and functions
*/

//GDZoomBox constants
var GDZOOMBOX_ELEM = "gdZoomBox";

function GDZoomBox()
{
	//private members
	this._InZoom = false;
	this._ZBoxStart;
	this._ZBoxEnd;
	this._ZStartBoxP;	//original pixel location
	this._IsComplete = false;
	this._Width = 0;
	this._Height = 0;
	this._BorderWidth = 4;
	this._InCenterAndZoom = false;
	
	//initialize
	this.Init();
	
	//properies
	this.MClickEnabled = false;
}

//GDZoomBox :: Init :: Initialize tool
GDZoomBox.prototype.Init = function()
{
	this.ZoomBox = document.getElementById(GDZOOMBOX_ELEM);
	//always hide the box on start
	this.ZoomBox.style.display = "none";
}

//GDZoomBox :: Reset:: method to reset this tool
GDZoomBox.prototype.Reset = function()
{
	this.ZoomBox.style.display="none";
	this._InZoom = false;
	this.ZoomBox.style.cursor = "default";
	this._ZBoxStart = null;
	this._ZStartBoxP = null;
	this._ZBoxEnd = null;
	this._Width = 0;
	this._Height = 0;
	this._InCenterAndZoom = false;
}

//GDZoomBox :: IsInCenterAndZoom:: returns InCenterAndZoom state
GDZoomBox.prototype.IsInCenterAndZoom = function()
{
	return this._InCenterAndZoom;
}

//GDZoomBox :: IsInSearch:: returns search state
GDZoomBox.prototype.IsInSearch = function()
{
	return this._InZoom;
}

//GDZoomBox :: IsBoxComplete:: returns true if we have a box on the map
GDZoomBox.prototype.IsBoxComplete = function()
{
	return this._IsComplete;
}

//GDZoomBox :: ResetComplete :: reset the complete status to state
GDZoomBox.prototype.ResetComplete = function(state)
{
	return this._IsComplete = state;
}

//GDZoomBox :: ClickSearchState :: enable/disable the click zoom function
GDZoomBox.prototype.ClickSearchState = function()
{
	if(GDCURRTOOL == GDTOOLZOOMBOX)
	{
		this.MClickEnabled = true;
		GDMAPOBJ.GDMapOverlay.style.display = "inline";
		GDMAPOBJ.GDMapOverlay.style.cursor = "crosshair";
		document.getElementById(GOOGMAP_ELEM).style.cursor = "crosshair";
		GDMAPOBJ.DisableG();
	}
	else
	{
		this.MClickEnabled = false;
		GDMAPOBJ.GDMapOverlay.style.display = "none";
		GDMAPOBJ.GDMapOverlay.style.cursor = "default";
		document.getElementById(GOOGMAP_ELEM).style.cursor = "default";
	}
}

//GDZoomBox :: StartDraw :: starts the box draw if parameters are correct
GDZoomBox.prototype.StartDraw = function()
{
	if( !this._InZoom && GDMAPOBJ.PointerInMapBounds() )
	{
		this._IsComplete = false;
		//if all good start the draw 
		this._InZoom = true;
		GDMAPOBJ.DisableG();
		this._ZBoxStart = GDMAPOBJ.PixelToLatLng( new GPoint(MOUSELOCATION_X - GDMAPOBJ.GDMPixelOffset.x, MOUSELOCATION_Y - GDMAPOBJ.GDMPixelOffset.y) );
		//get our original location
		this._ZStartBoxP = new GPoint(MOUSELOCATION_X, MOUSELOCATION_Y);
		//set zoom box position
		this.ZoomBox.style.left = (MOUSELOCATION_X -  GDMAPOBJ.GDMPixelOffset.x) + "px";
		this.ZoomBox.style.top = (MOUSELOCATION_Y  -  GDMAPOBJ.GDMPixelOffset.y) + "px";
		this.ZoomBox.style.width = 0;
		this.ZoomBox.style.height = 0;
		//show the zoom box
		this.ZoomBox.style.display = "inline";
		//set cursor to crosshair
		this.ZoomBox.style.cursor = "crosshair";
		document.getElementById(GOOGMAP_ELEM).style.cursor = "crosshair";
		
		if( typeof(this._ZBoxStart) != 'undefined')
			GDMAPOBJ.RemoveBorderBox();
	}
}
	
//GDZoomBox :: EndDraw :: ends the box draw
GDZoomBox.prototype.EndDraw = function()
{
	if( this._ZBoxStart == null || typeof(this._ZBoxStart) == 'undefined')
		return;
		
	if( this._InZoom )
	{
		if( GDMAPOBJ.PointerInMapBounds() )
		{
			//end the box and draw
			this._InZoom = false;
			//get the end of the box
			this._ZBoxEnd = GDMAPOBJ.PixelToLatLng( new GPoint(MOUSELOCATION_X - GDMAPOBJ.GDMPixelOffset.x, MOUSELOCATION_Y - GDMAPOBJ.GDMPixelOffset.y) );
			
			//see if our points got inversed
			if( this._ZBoxStart.y < this._ZBoxEnd.y )
			{
				var tempY = this._ZBoxStart.y;
				this._ZBoxStart.y = this._ZBoxEnd.y;
				this._ZBoxEnd.y = tempY;
			}
			
			if( this._ZBoxStart.x > this._ZBoxEnd.x )
			{
				var tempX = this._ZBoxStart.x;
				this._ZBoxStart.x = this._ZBoxEnd.x;
				this._ZBoxEnd.x = tempX;
			}
			
			//check our box size first
			if( this._Width > 5 ||  this._Height > 5 )
			{
				//tell we are done
				this._IsComplete = true;
				
				//set the zoom map bounds to find the center - put this in the zoom box class
				var neCrn = new GLatLng(this._ZBoxStart.y, this._ZBoxEnd.x);
				var swCrn = new GLatLng(this._ZBoxEnd.y, this._ZBoxStart.x);
				var rsBounds = new GLatLngBounds(swCrn, neCrn);
				
				//update our poly points
				var tempPoints = new Array();
				tempPoints[0] = neCrn;
				tempPoints[1] = swCrn;
				GDMAPOBJ.PlyPoints = tempPoints;
				GDMAPOBJ.LPolyBoundsChanged(tempPoints);			
								
				//redraw map - but tell zoom event we are in zoom and center
				this._InCenterAndZoom = true;
				GDMAPOBJ.CenterMap(rsBounds);
				this._InCenterAndZoom = false;
							
				//draw box on map
				GDMAPOBJ.DrawPolyBox(true, false);
			}
		}
		//regardless reset tools
		GDMAPOBJ.ResetTools();
	}
}

//GDZoomBox :: GDZBMove :: handles move in the zoom tool
GDZoomBox.prototype.Move = function()
{
	if (!this._InZoom)
		return;			
	
	if( this._ZBoxStart == null || typeof(this._ZBoxStart) == 'undefined')
		return;
		
	if(GDMAPOBJ.PointerInMapBounds())
	{
		//move our box
		var w = parseFloat(MOUSELOCATION_X) - parseFloat(this._ZStartBoxP.x);
		var h = parseFloat(MOUSELOCATION_Y) - parseFloat(this._ZStartBoxP.y);
		if (w < 0) 
		{
			w = Math.abs(w);
			this.ZoomBox.style.left = (MOUSELOCATION_X - GDMAPOBJ.GDMPixelOffset.x) + "px";
		}  
		this._Width = w;
		if( (this._Width - this._BorderWidth) > 0 )
			this.ZoomBox.style.width = (this._Width - this._BorderWidth) + "px";
		//then set height
		if (h < 0)
		{
			h = Math.abs(h);
			this.ZoomBox.style.top = (MOUSELOCATION_Y - GDMAPOBJ.GDMPixelOffset.y) + "px";
		}  
		this._Height = h;
		if( (this._Height - this._BorderWidth) > 0 )
			this.ZoomBox.style.height = (this._Height - this._BorderWidth) + "px";
	}
	else //not in bounds reset the tool
	{
		GDMAPOBJ.PlyPoints = null;
		GDMAPOBJ.LPolyBoundsChanged(GDMAPOBJ.PlyPoints);
		GDMAPOBJ.ResetTools();
	}
}
//end --------- GDZoomBox


