var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var drawing=false;
var blnMultiPick=false;
var StairsVisible=false;
var ZoomBoxVisible=false;
var MaxX=0;
var MaxY=0;
var MinX=0;
var MinY=0;
var MinBoxSizeForZoom=10;
var BoxTopLayer, BoxLeftLayer, BoxRightLayer, BoxBottomLayer, AnchorLayer;
var NavStairsLayer1, NavStairsLayer2, NavStairsLayer3, NavStairsLayer4;
var StairsLayer;
var StairsImage;
var StairsImageWidth=120
var StairsStepIncrement=30;
var StairsImageTopSpacer=10;
var StairsImage1=new Image();
var StairsImage2=new Image();
var StairsImage3=new Image();
var StairsImage4=new Image();
var MagDiff;
var BoxBorderWidth=2;

StairsImage1.src="images/ZoomStairs1.gif";
StairsImage2.src="images/ZoomStairs2.gif";
StairsImage3.src="images/ZoomStairs3.gif";
StairsImage4.src="images/ZoomStairs4.gif";

function getMouseMapCoords(evt) {

	switch(browser){
		case "NAV6":
		case "NAV7":
			//BoxLeftLayer is the last of the four box layers, so it is the primary target.  Map is
			//target until immediately after drawing begins
			if (evt.target.id=="BoxLeftLayer" || evt.target.id=="Map") {
				mouseX=evt.layerX;
				mouseY=evt.layerY;
				if (mouseY>=MapHeight) mouseY=MapHeight;
				if (mouseX>=MapWidth) mouseX=MapWidth;
			}
			else if (evt.target.id == "ZoomStairsImage") { 
				//for zooming out - the event coorindates are based upon the ZoomBoxImage DIV, instead of
				//the positioning DIV, so the current left and top values are added.
				mouseX = (evt.target.offsetParent.offsetLeft - CaptureLayer.offsetLeft) + evt.layerX;
				mouseY = (evt.target.offsetParent.offsetTop - CaptureLayer.offsetTop) + evt.layerY;
				//mouseX=evt.layerX + parseInt(stairsX);
				//mouseY=evt.layerY + parseInt(evt.target.offsetParent.style.top);
			}
			break;
		case "IE4":
		case "IE5":
		case "IE6":
		case "IE7":
			//This if statement is to prevent incorrect elements from passing coordinates when the mouse moves
			//The map processes zooming in
			if (event.srcElement.id=="Map") {
				mouseX=event.offsetX;
				mouseY=event.offsetY;
				}
			else if (event.srcElement.id=="ZoomStairsImage") {
				//for zooming out - the event coordinates are based upon the ZoomBoxImage DIV, instead of
				//the positioning DIV, so the current left and top values are added.
				mouseX=event.offsetX+event.srcElement.parentElement.style.pixelLeft;
				mouseY=event.offsetY+event.srcElement.parentElement.style.pixelTop;
				}
			break;

	}

}

function getMouse(evt) {

	getMouseMapCoords(evt);

	x2=mouseX;
	y2=mouseY;

	if (drawing==true){
		if ((x2<x1) && (y2<y1)) {
			hideBox();
			setStairs();
		}
		else {
			hideStairs();
			setClip();
		}
	}

	if (((MaxX-MinX)>MinBoxSizeForZoom) && (drawing==true)){
		showZoomBoxInfo(evt);
	}

	return false;
	
	
}

function setMinMax(){

	var MouseBorderOffset=1; //This Offsets the Mouse from the Border so that there is no overlap
							//and prevents the OffsetX,OffsetX number from being captured by another element
	
	if (x1>=x2) {
		MaxX=x1;
		MinX=x2-MouseBorderOffset;
	}
	else {
		MinX=x1;
		MaxX=x2-MouseBorderOffset;
	}
	
	if (y1>=y2) {
		MaxY=y1;
		MinY=y2-MouseBorderOffset;
	}
	else {
		MinY=y1;
		MaxY=y2-MouseBorderOffset;
	}

}

function clearMinMax() {

	MaxX=0;
	MinX=0;
	MaxY=0;
	MinY=0;

}

function setClip() {	

	var AdjustAmount;

	setMinMax();
	showBox();
		
	var ZoomXDiff=MaxX-MinX;
	var ZoomYDiff=MaxY-MinY;
	if (ZoomXDiff==0) ZoomXDiff=1;
	if (ZoomYDiff==0) ZoomYDiff=1;
	
	var MapXYRatio=parseInt((MapWidth*1000)/MapHeight);
	var ZoomBoxXYRatio=parseInt((ZoomXDiff*1000)/ZoomYDiff);
	if (ZoomBoxXYRatio<1) ZoomBoxXYRatio=1;
	
	if ((x1 != x2) && (y1 != y2)) {
		if (MapXYRatio>ZoomBoxXYRatio){
			AdjustAmount=parseInt(((ZoomXDiff * MapXYRatio) / ZoomBoxXYRatio - ZoomXDiff)/2);
			if (isNaN(AdjustAmount)==true) AdjustAmount=0;
			clipLayer(BoxTopLayer,MinY,MaxX+AdjustAmount,MinY+BoxBorderWidth,MinX-AdjustAmount);
			clipLayer(BoxLeftLayer,MinY,MinX-AdjustAmount+BoxBorderWidth,MaxY,MinX-AdjustAmount);
			clipLayer(BoxRightLayer,MinY,MaxX+AdjustAmount,MaxY,MaxX-BoxBorderWidth+AdjustAmount);
			clipLayer(BoxBottomLayer,MaxY-BoxBorderWidth,MaxX+AdjustAmount,MaxY,MinX-AdjustAmount);
		}
		else {
			AdjustAmount=parseInt(((ZoomYDiff * ZoomBoxXYRatio) / MapXYRatio - ZoomYDiff)/2);
			clipLayer(BoxTopLayer,MinY-AdjustAmount,MaxX,MinY+BoxBorderWidth-AdjustAmount,MinX);
			clipLayer(BoxLeftLayer,MinY-AdjustAmount,MinX+BoxBorderWidth,MaxY+AdjustAmount,MinX);
			clipLayer(BoxRightLayer,MinY-AdjustAmount,MaxX,MaxY+AdjustAmount,MaxX-BoxBorderWidth);
			clipLayer(BoxBottomLayer,MaxY-BoxBorderWidth+AdjustAmount,MaxX,MaxY+AdjustAmount,MinX);
		}
	}

}

function startMouseDraw(evt) {

	getMouse(evt);

	if (drawing!=true && (CursorMode=="Navigate" || CursorMode=="Start" || CursorMode=="Stop")){
		beginDraw(evt);
		hideZoomBoxInfo();
		if (browser=="NAV6" || browser=="NAV7") {
			if (is_mozilla || is_firefox) {
				evt.preventDefault();
			}
			else {
				evt.stopPropogation();
			}
		}			
		return false;
		}
	else if (CursorMode=="GetInfo") {
		switch (browser) {
			case "NAV6":
			case "NAV7":
				if (evt.ctrlKey==true || evt.shiftKey==true) blnMultiPick=true;
				break;
			case "IE4":
			case "IE5":
			case "IE6":
			case "IE7":
				if (event.ctrlKey==true || event.shiftKey==true) blnMultiPick=true;
				break;
		}
		beginDraw(evt);
		endBox(evt);
	}
	else {
		return true;
		}
		
}

function setupLayers() {

	BoxTopLayer=findLayer("BoxTopLayer",window.document);
	BoxLeftLayer=findLayer("BoxLeftLayer",window.document);
	BoxRightLayer=findLayer("BoxRightLayer",window.document);
	BoxBottomLayer=findLayer("BoxBottomLayer",window.document);
	ZoomBoxMagLayer=findLayer("ZoomBoxMag",window.document);
	
	switch(browser) {
		case "NAV6":
		case "NAV7":
		case "IE4":
		case "IE5":
		case "IE6":
		case "IE7":
			StairsLayer=findLayer("ZoomBoxStairs",window.document)
			StairsImage=document.images["ZoomStairsImage"];
			break;
	}
}

function beginDraw(evt) {

	//getMouse(evt);
	x1=mouseX;
	y1=mouseY;
	x2=x1+1;
	y2=y1+1;
	setupLayers();
	showAnchor();	
	setClip();
	drawing=true;

}

function endBox(evt) {

	if ((drawing==false) || (drawing==null)) return;

	if (CursorMode=="Start" || CursorMode=="Stop") {
		var frmAction=document.DirectionsForm;
	}
	else {
		var frmAction=document.MapForm;
	}
		
	drawing=false;
	frmAction.FormAction.value="MapClick";
	if ((MaxX <MinX+MinBoxSizeForZoom) && (MinY < MaxY+MinBoxSizeForZoom)) {
		//recenter Map
		frmAction.NewMapX.value=mouseX;
		frmAction.NewMapY.value=mouseY;
		if (blnMultiPick==true) frmAction.MultiPick.value="True";
		frmAction.submit();
		return;
		}
	else {
		var width = Math.abs(MaxX - MinX);
		var height = Math.abs(MaxY - MinY);
		document.MapForm.NewMapX.value=MinX+(width/2);
		document.MapForm.NewMapY.value=MinY+(height/2);
		document.MapForm.NewMapMag.value=getNewMag();
		document.MapForm.submit();
		}
	
	clearMinMax();

}

function stopMouseDraw(evt) { 
	
	if (drawing==true){
		endBox(evt);
		return true;
	}
}

function showBox() {

	if (ZoomBoxVisible==false) {
		showLayer(BoxTopLayer);
		showLayer(BoxLeftLayer);
		showLayer(BoxRightLayer);
		showLayer(BoxBottomLayer);
		ZoomBoxVisible=true;
	}
}

function hideBox() {

	clearMinMax();
	hideLayer(BoxTopLayer);
	hideLayer(BoxLeftLayer);
	hideLayer(BoxRightLayer);
	hideLayer(BoxBottomLayer);
	ZoomBoxVisible=false;

}

function showStairs(){

	if (StairsVisible==false){
		StairsVisible=true;
		switch (browser) {
			case "NAV6":
			case "NAV7":
				stairsX = CaptureLayer.offsetLeft + x1 - getLayerWidth(StairsLayer);
				stairsY = CaptureLayer.offsetTop + y1 - getLayerHeight(StairsLayer);
				moveLayer(StairsLayer,stairsX,stairsY);
				setzIndex(StairsLayer,5);
				StairsImage.addEventListener("mousemove", getMouse, false);
				StairsImage.addEventListener("mouseup", stopMouseDraw, false);
				break;
			case "IE4":
			case "IE5":
			case "IE6":
			case "IE7":
				moveLayer(StairsLayer,x1-getLayerWidth(StairsLayer),y1-getLayerHeight(StairsLayer));
				break;
		}
		if (MagDiff<=0) {
			hideStairs();
			drawing=false;
		}
		else if (MagDiff<4 && MagDiff>0) {
			clipLayer(StairsLayer,StairsImageWidth-(MagDiff*StairsStepIncrement), getLayerWidth(StairsLayer),getLayerHeight(StairsLayer),getLayerWidth(StairsLayer)-x1);
		}
		else {
			clipLayer(StairsLayer,getLayerHeight(StairsLayer)-y1, getLayerWidth(StairsLayer),getLayerHeight(StairsLayer),getLayerWidth(StairsLayer)-x1);
		}
		showLayer(StairsLayer);
	}


}

function setStairs(){

	MagDiff=CurrentMag-MinimumMag;
	if (MagDiff <= 0 ) return;
	
	showStairs();
	setMinMax();

	var Increment=getStairIncrement();
	if (Increment>MagDiff && MagDiff!=0) Increment=MagDiff;
	
	switch (browser) {
		case "NAV6":
		case "NAV7":
		case "IE4":
		case "IE5":
		case "IE6":
		case "IE7":
			var NewStairsImage=eval("StairsImage"+Increment);
			if (StairsImage.src!=NewStairsImage.src) {
				StairsImage.src=NewStairsImage.src;
			}
			break;
	}
	
}

function getStairIncrement() {

	var diff;
	
	if (x1-x2>y1-y2)
		diff=Math.ceil((x1-x2)/StairsStepIncrement);
	else
		diff=Math.ceil((y1-y2)/StairsStepIncrement);

	diff=(diff>4) ? 4 : diff;

	return diff;
	
}

function hideStairs() {

	clearMinMax();
	if (StairsVisible==true ){
		StairsVisible=false;
		switch (browser) {
			case "NAV6":
				StairsImage.removeEventListener("mousemove", getMouse, false);
				StairsImage.removeEventListener("mouseup", stopMouseDraw, false);
				setzIndex(StairsLayer,1);
			case "NAV7":
			case "IE4":
			case "IE5":
			case "IE6":
			case "IE7":
				hideLayer(StairsLayer);
		}
	}
	
}

function showAnchor(){
	
	AnchorLayer=findLayer("ZoomBoxAnchor",window.document);
	if (browser == "NAV7") {
		var anchorX = CaptureLayer.offsetLeft + x1 -(getLayerWidth(AnchorLayer)/2);
		var anchorY = CaptureLayer.offsetTop + y1 - (getLayerHeight(AnchorLayer)/2);
		moveLayer(AnchorLayer,anchorX,anchorY);
		setzIndex(AnchorLayer,5);
		AnchorLayer.addEventListener("mouseup", stopMouseDraw, false);
	}
	else {
		moveLayer(AnchorLayer,x1-(getLayerWidth(AnchorLayer)/2),y1-(getLayerHeight(AnchorLayer)/2));
	}
	showLayer(AnchorLayer);

}

function roundNumber(NumberToRound,DecimalPlaces,PadWithZero){

	var FactorToUse=Math.pow(10,DecimalPlaces);
	var TempNumber=Math.round(NumberToRound*FactorToUse)/FactorToUse;
	
	if (PadWithZero==false) return TempNumber;
	
	var NumberAsString=TempNumber.toString();
	var DecimalLoc=NumberAsString.indexOf(".");
	
	if (DecimalLoc==-1){
		NumberAsString=NumberAsString + ".";
		for (var i=0;i<DecimalPlaces;i++){
			NumberAsString=NumberAsString + "0";
		}
	}
	else {
		var LeftOfZero=NumberAsString.substr(DecimalLoc+1);
		var LeftOfZeroCount=LeftOfZero.length;
		if (LeftOfZeroCount>DecimalPlaces){
			LeftOfZero=LeftOfZero.substr(0, ((DecimalPlaces==0) ? 0 : DecimalPlaces+1));
			LeftOfZero=Math.round(parseInt(LeftOfZero)/10)
			NumberAsString=NumberAsString.substr(0,DecimalLoc)+ "." + LeftOfZero.toString();
		}
		else if (LeftOfZeroCount<DecimalPlaces){
			for (var i=LeftOfZeroCount;i<DecimalPlaces;i++){
				NumberAsString=NumberAsString + "0";
			}
		}
	}
	
	return NumberAsString;

}

function updateDisplay(){

	showMapCoordinates();

}

function showMapCoordinates(){

	var WriteLayer, WriteLayerDoc;
	var CurrentLat;
	var CurrentLon;
	var DecLoc;
			
	CurrentLat=mouseYToDegreeLat(false);
	CurrentLon=mouseXToDegreeLon(false);

	if (CurrentMag< 6){
		CurrentLat=roundNumber(CurrentLat,2,true)
		CurrentLon=roundNumber(CurrentLon,2,true)
	}
	else if (CurrentMag< 9){
		CurrentLat=roundNumber(CurrentLat,3,true)
		CurrentLon=roundNumber(CurrentLon,3,true)
	}
	else if (CurrentMag< 12){
		CurrentLat=roundNumber(CurrentLat,4,true)
		CurrentLon=roundNumber(CurrentLon,4,true)
	}
	else {
		CurrentLat=roundNumber(CurrentLat,5,true)
		CurrentLon=roundNumber(CurrentLon,5,true)
	}


	switch(browser){
		case "NAV6":
		case "NAV7":
		case "IE4":
		case "IE5":
		case "IE6":
		case "IE7":
			document.all.CurrentLatitude.value=CurrentLat;
			document.all.CurrentLongitude.value=CurrentLon;
			break;
	}
	
}

function mouseYToDegreeLat(Format){

	var DegreeAll;
	var DegreeWhole;
	var DegreeMinutes;
	var DegreeSeconds;
	var DegreeDirection;

	if (isNaN(mouseY))
		return "";
		
	var MouseYInRadians=CenterLatInRadians-(mouseY-CenterY)/PixelsPerRadian;

	DegreeAll=MouseYInRadians/Math.PI * 180;
	if (DegreeAll<0)
		DegreeDirection="S";
	else
		DegreeDirection="N";
	
	if (Format==true){
		DegreeAll=Math.abs(DegreeAll);
		DegreeWhole=parseInt(DegreeAll);
		DegreeHour=(DegreeAll-parseInt(DegreeAll))*60;
		DegreeMinute=parseInt((DegreeHour-parseInt(DegreeHour))*60);
		if (isNaN(DegreeMinute))
			DegreeMinute=0;
		DegreeHour=parseInt(DegreeHour);
		if (isNaN(DegreeHour))
			DegreeHour=0;
		if (OS=="MAC")
			return '  ' + DegreeWhole.toString() + '°  ' + DegreeHour.toString() + ' ' + DegreeMinute.toString() + ' ' + DegreeDirection;
		else
			return DegreeWhole.toString() + '° ' + DegreeHour.toString() + '´ ' + DegreeMinute.toString() + '´´ ' + DegreeDirection;
	}
	else{
		return DegreeAll;
	}	
}	
	

function mouseXToDegreeLon(Format){

	var DegreeAll;
	var DegreeWhole;
	var DegreeMinutes;
	var DegreeSeconds;
	var DegreeDirection;

	if (isNaN(mouseX))
		return "";

	var MouseXInRadians=CenterLonInRadians+(mouseX-CenterX)/(PixelsPerRadian*Math.cos(CenterLatInRadians));
	
	if (MouseXInRadians < -(Math.PI)){
		MouseXInRadians+=(Math.PI*2);
	}
	
	if (MouseXInRadians>=Math.PI){
		MouseXInRadians-=(Math.PI*2);
	}
	
	DegreeAll=MouseXInRadians/Math.PI * 180;
	
	if (DegreeAll<0)
		DegreeDirection="W";
	else
		DegreeDirection="E";
	
	if (Format==true){
		DegreeAll=Math.abs(DegreeAll);
		DegreeWhole=parseInt(DegreeAll);
		DegreeHour=(DegreeAll-parseInt(DegreeAll))*60;
		DegreeMinute=parseInt((DegreeHour-parseInt(DegreeHour))*60);
		if (isNaN(DegreeMinute))
			DegreeMinute=0;
		DegreeHour=parseInt(DegreeHour);
		if (isNaN(DegreeHour))
			DegreeHour=0;
		if (OS=="MAC")
			return '  ' + DegreeWhole.toString() + '° ' + DegreeHour.toString() + ' ' + DegreeMinute.toString() + ' ' + DegreeDirection;
		else
			return DegreeWhole.toString() + '° ' + DegreeHour.toString() + '´ ' + DegreeMinute.toString() + '´´ ' + DegreeDirection;
	}
	else{
		return DegreeAll;
	}	
}	
	

function disableMouseCaptures(){

	switch (browser) {
		case "IE4":
		case "IE5":
		case "IE6":
		case "IE7":
			var CaptureLayer=document.all.PositioningLayer
			CaptureLayer.onmousemove='';
			CaptureLayer.onmousedown='';
			CaptureLayer.onmouseup='';
	}
}

function enableMouseCaptures(){

	switch (browser) {
		case "IE4":
		case "IE5":
			var CaptureLayer=document.all.PositioningLayer
			CaptureLayer.onmousemove=getMouse;
			CaptureLayer.onmousedown=boxDraw;
			CaptureLayer.onmouseup=stopBoxDraw;
	}
}