var nIncludeMargin = 6;

var nMapMargin = 13;

var oMOWaiting = null;
var oPOI = new Object();
oPOI.Type = 0;
oPOI.Legend = 1;
oPOI.Run = 2;
oPOI.Pic = 3;
oPOI.ParLat = 4;
oPOI.ParLon = 5;
oPOI.CamLat = 6;
oPOI.CamLon = 7;
oPOI.County = 8;
oPOI.Pin = 9;

function AlertNewMapData(win)
{
	var oMO = oMOWaiting;

	if (!win.bSessionAlive)
	{
		win.Announce();
		oMO.show();

		if (top.HideProView)
			top.HideProView();
	}
	if (win.sError)
	{
		alert(win.sError);
		oMO.show();
	}
	else if (win.oMapInfo)
		oMO.updateInfo(win.oMapInfo);

	oMOWaiting = null;
}


function MOEvent_Click(event)
{
	var oMO = this.object;
	if (oMO.bLoggedIn)
	{
		var e = arguments.length>0 ? event : window.event;
		var x=null;
		var y=null;
		if (e.offsetX)
		{
			x = e.offsetX;
			y = e.offsetY;
		}
		else
		{
			alert("Your browser can't determine where on an image you clicked.\nMicrosoft Internet Explorer 5.0 and later work best.");
		}

		if (x!=null && y!=null)
		{
			if (e.shiftKey)
				oMO.panMapXY(x,y);
			else
			{
				var lat = oMO.YToLat(y);
				var lon = oMO.XToLon(x);
				DriveToLatLon(lat, lon, oMO.nRegion);
			}
		}
	}
}

function MOEvent_POIClick()
{
	var oMO = this.oMO;
	var arrPOI = oMO.oMapInfo.arrPOI[this.nIndex];

	if (oDriveObj && arrPOI[oPOI.Run]!=null)
		oDriveObj.getByPOI(arrPOI, oMO);

	var oP = oMO.oPointerMark;

	oP.moveLatLon(arrPOI[oPOI.ParLat], arrPOI[oPOI.ParLon]);
	oP.node.nIndex = this.nIndex;
	oP.setMouseOverAndOut(this.onmouseover, this.onmouseout);

	oMO.arrCurPOI = arrPOI;

	var sLegend = arrPOI[oPOI.Legend];
	if (top.SetLegend)
		top.SetLegend(sLegend);

	oMO.updateLegend(sLegend);
}

function MOEvent_POIMouseOver(e)
{
	var event = (arguments.length==0) ? window.event : e;

	var nIndex = this.nIndex;
	var arrPOI = this.oMO.oMapInfo.arrPOI[nIndex];

	var eDiv = document.createElement("DIV");
	var s = arrPOI[oPOI.Legend];
	if (!s)
		s = "address unavailable";
	eDiv.appendChild(document.createTextNode(s));

	if (this.oMO.bGeocoding)
	{
		eDiv.appendChild(document.createElement("BR"));
		eDiv.appendChild(document.createTextNode("Lat: " + String(arrPOI[oPOI.ParLat])));
		eDiv.appendChild(document.createElement("BR"));
		eDiv.appendChild(document.createTextNode("Lon: " + String(arrPOI[oPOI.ParLon])));

		if (arrPOI.length>=oPOI.Pin-1)
		{
			eDiv.appendChild(document.createElement("BR"));
			eDiv.appendChild(document.createTextNode("PIN: " + arrPOI[oPOI.Pin]));
		}
	}

	if (arrPOI[oPOI.Run]==null)
	{
		eDiv.appendChild(document.createElement("BR"));
		eDiv.appendChild(document.createTextNode("Image not available"));
	}

	this.eToolTip = new ToolTip(event, eDiv);
}

function MOEvent_POIMouseOut(e)
{
	if (this.eToolTip)
	{
		this.eToolTip.remove();
		this.eToolTip = null;
	}
}

MapObject.prototype.setRegion = function(r) { this.nRegion=r; }
MapObject.prototype.getRegion = function() { return  this.nRegion ? this.nRegion : ((this.oMapInfo)?this.oMapInfo.nRegion:null); }
MapObject.prototype.getMapBase = MapObject_GetMapBase;
MapObject.prototype.getByCountyPin = MapObject_GetByCountyPin;
MapObject.prototype.getByLatLon = MapObject_GetByLatLon;
MapObject.prototype.getByRunPic = MapObject_GetByRunPic;
MapObject.prototype.getTaxMapByLatLon = MapObject_GetTaxMapByLatLon;

MapObject.prototype.hasNoInfo = function () { return this.oMapInfo==null; }
MapObject.prototype.clear = MapObject_Clear;
MapObject.prototype.updateInfo = MapObject_UpdateInfo;
MapObject.prototype.removePOI = MapObject_RemovePOI;
MapObject.prototype.updatePOI = MapObject_UpdatePOI;
MapObject.prototype.openPOI = MapObject_OpenPOI;
MapObject.prototype.findPOI = MapObject_FindPOI;

MapObject.prototype.isPointInMargins = function (nLeft,nTop) { return (nLeft > nMapMargin && nLeft < this.nHeight-nMapMargin && nTop > nMapMargin && nTop < this.nHeight-nMapMargin); }
MapObject.prototype.isPointInMarginsLatLon = function (nLat,nLon) { return this.isPointInMargins(this.LonToX(nLon), this.LatToY(nLat)); }
MapObject.prototype.keepMap = function (nLat, nLon) { return this.isPointInMarginsLatLon(nLat, nLon); }
MapObject.prototype.hasTaxIndex = function () { if (this.oMapInfo) return this.oMapInfo.bHasTaxIndex; return false; }
MapObject.prototype.getTaxMapName = function () { if (this.oMapInfo) return this.oMapInfo.sCurTaxMapName; return ""; }

MapObject.prototype.prepareMarks = MapObject_PrepareMarks;
MapObject.prototype.clearMarks = function() { this.oPointerMark.clear(); this.oTargetMark.clear(); this.updateLegend(); }

MapObject.prototype.setPointerMark = function(aPOI) { if (aPOI.eDot && aPOI.eDot.onclick) aPOI.eDot.onclick(); }
MapObject.prototype.setPointerMarkPOI = MapObject_SetPointerMarkPOI;
MapObject.prototype.setPointerMarkPIN = MapObject_SetPointerMarkPIN;
MapObject.prototype.getPointerMarkLat = function() { return this.oPointerMark.nLat; }
MapObject.prototype.getPointerMarkLon = function() { return this.oPointerMark.nLon; }

MapObject.prototype.prepareArrow = MapObject_PrepareArrow;
MapObject.prototype.displayArrow = MapObject_DisplayArrow;
MapObject.prototype.loseArrow = function() { this.nArrLon=0; this.displayArrow(); }
MapObject.prototype.setArrow = MapObject_SetArrow;
MapObject.prototype.turnArrow = function(nDir) { this.nArrDir = nDir; this.displayArrow(); }

MapObject.prototype.prepareLegend = MapObject_PrepareLegend;
MapObject.prototype.updateLegend = MapObject_UpdateLegend;

MapObject.prototype.prepareMapMenu = MapObject_PrepareMapMenu;
MapObject.prototype.updateMapMenu = function(arrLayers) { this.oMapMenu.update(arrLayers); }

MapObject.prototype.getLat = function() { return (this.oMapInfo)?this.oMapInfo.nLat:null; }
MapObject.prototype.getLon = function() { return (this.oMapInfo)?this.oMapInfo.nLon:null; }
MapObject.prototype.LatToY = function(nLat) { var arrMI=this.oMapInfo; return Math.floor((nLat - arrMI.nLatNorth) / arrMI.nLatPixSize); }
MapObject.prototype.LonToX = function(nLon) { var arrMI=this.oMapInfo; return Math.floor((nLon - arrMI.nLonWest) / arrMI.nLonPixSize); }
MapObject.prototype.XToLon = function(X)	{ var arrMI=this.oMapInfo; return arrMI.nLonWest + (X * arrMI.nLonPixSize); }
MapObject.prototype.YToLat = function(Y)	{ var arrMI=this.oMapInfo; return arrMI.nLatNorth + (Y * arrMI.nLatPixSize); }
MapObject.prototype.panMapLatLon = function(nLat, nLon) { this.getByLatLon(nLat, nLon, this.oMapInfo.nRegion); }
MapObject.prototype.panMapXY = function(nX, nY) { this.panMapLatLon(this.YToLat(nY), this.XToLon(nX)); }

MapObject.prototype.setZoom = MapObject_SetZoom;
MapObject.prototype.moveZoom = function(nDir) {	var nNZ=this.nZoom+nDir; this.setZoom(nNZ); }
MapObject.prototype.zoomIn = function() { this.moveZoom(-1); }
MapObject.prototype.zoomOut = function() { this.moveZoom(+1); }

MapObject.prototype.setVisibleLayer = function(sLayer) { if (sLayer && sLayer.count>0) this.sLayer=sLayer; else this.sLayer="aerial"; this.getByLatLon(this.getLat(), this.getLon()); }

MapObject.prototype.show= function() { this.node.style.visibility = "visible"; if (this.oZoomMenu) this.oZoomMenu.show(); }
MapObject.prototype.hide= function() { this.removePOI(); this.node.style.visibility = "hidden"; if (this.oZoomMenu) this.oZoomMenu.hide(); }

MapObject.prototype.setCurLayer =
function (sLayer)
{
	if (this.sLayer!=sLayer)
	{
		this.sLayer=sLayer;
		this.getByLatLon(this.getLat(), this.getLon());
	}
}

MapObject.prototype.setLoggedInMode =
function(bLoggedIn)
{
	this.bLoggedIn = bLoggedIn;
	this.oMapMenu.showIcon(bLoggedIn);
	if (this.bZoomMenu)
	{
		this.nZoomLimit = bLoggedIn?8:4;
		this.oZoomMenu = new ZoomMenu(this, this.nZoomLimit);
		this.oZoomMenu.selectZoom(this.nZoom);
	}
}


function MapObject(vImage, sMapFrame, bZoomMenu)
{
	this.node = (typeof(vImage)=="string") ? document.getElementById(vImage) : vImage;
	this.sFrameName = sMapFrame;
	this.node.object = this;
	this.node.bPreserve = true;

	if (window.event && typeof(window.event.offsetX)=="number")
		this.node.onclick = MOEvent_Click;

	this.oMapInfo = null;
	this.bGeocoding = false;
	this.nZoom = 3;

	this.bZoomMenu = bZoomMenu?true:false;
	this.bLoggedIn = false;

	this.arrCurPOI = null;

	this.nHeight = this.node.parentNode.offsetHeight;

	this.prepareArrow();
	this.prepareMarks();
	this.prepareLegend();
	this.prepareMapMenu();

	this.sLayer = "Aerial";

	this.setLoggedInMode(false);
}

function MapObject_GetMapBase()
{
	var sURL = sURLBase + "Scripts/Server/GetMapData.asp?Zoom=" + String(this.nZoom) + "&Height=" + String(this.nHeight);

	if (this.nRegion)
		sURL += "&Region=" + String(this.nRegion);

	if (!this.bLoggedIn && this.oMapInfo)
		sURL += "&MLat=" + String(this.oMapInfo.nLat) + "&MLon=" + String(this.oMapInfo.nLon);

	return sURL;
}

function MapObject_GetByCountyPin(nCounty, sPin)
{
	if (this.bLoggedIn)
	{
		if (oMOWaiting==null)
		{
			oMOWaiting = this;
			this.removePOI();
			this.node.style.visibility = "hidden";

			frames[this.sFrameName].location.href = this.getMapBase() + "&Pin=" + sPin + "&County=" + String(nCounty);
		}
	}
}

function MapObject_GetByLatLon(nLat, nLon, bZoomOnly)
{
	if (this.bLoggedIn || (arguments.length>2 && bZoomOnly))
	{
		if (oMOWaiting==null)
		{
			oMOWaiting = this;

			this.removePOI();
			this.node.style.visibility = "hidden";

			var sURL = this.getMapBase() + "&Lat=" + String(nLat) + "&Lon=" + String(nLon);

			if (this.sLayer!="Aerial")
			{
				sURL += "&Layer=" + this.sLayer;
				if (this.sLayer=="Tax")
					sURL += "&CurTaxMap=" + this.getTaxMapName();
			}

			if (bZoomOnly)
				sURL += "&ReqZoomOnly=1";

			frames[this.sFrameName].location.href = sURL;
		}
	}
}

function MapObject_GetTaxMapByLatLon(nLat, nLon)
{
	if (oMOWaiting==null && this.sLayer=="Tax")
	{
		oMOWaiting = this;
		var sURL = this.getMapBase() + "&Layer=tax&CurTaxMap=" + this.getTaxMapName() + "&Lat=" + String(nLat) + "&Lon=" + String(nLon);
		frames[this.sFrameName].location.href = sURL;
	}
}

function MapObject_GetByRunPic(sRun, nPic)
{
	if (this.bLoggedIn)
	{
		if (oMOWaiting==null)
		{
			oMOWaiting = this;

			this.removePOI();
			this.node.style.visibility = "hidden";

			frames[this.sFrameName].location.href = this.getMapBase() + "&Run=" + sRun + "&Pic=" + String(nPic);
		}
	}
}

function MapObject_Clear()
{
	this.oMapInfo = null;
	this.nRegion = null;
	if (this.node)
		this.node.src = "";

	this.removePOI();
	this.hide();
}

function MapObject_UpdateInfo(oMapInfo)
{
	this.oMapInfo = oMapInfo;
	this.nRegion = oMapInfo.nRegion;
	if (oMapInfo.nRegion)
		nRegion = oMapInfo.nRegion;

	if (this.node)
		this.node.src = oMapInfo.sMapURL;

	this.updatePOI();
	this.displayArrow();

	if (this.oMapInfo.nArrIndex!=-1)
	{
		var arrPOI = this.oMapInfo.arrPOI[this.oMapInfo.nArrIndex];
		var nLat = arrPOI[oPOI.ParLat];
		var nLon = arrPOI[oPOI.ParLon];
		if (nLat && nLon)
			this.oTargetMark.moveLatLon(nLat, nLon);
	}
	else
		this.oTargetMark.show(false);

	this.oTargetMark.display();
	this.oPointerMark.display();

	this.show();
}

function MapObject_RemovePOI()
{
	this.arrCurPOI = null;

	var eDiv = this.node.parentNode;
	var eDelete = eDiv.lastChild;
	var ePrevNode;
	while ((ePrevNode=eDelete.previousSibling)!=null)
	{
		if (eDelete.eToolTip)
		{
			eDelete.eToolTip.remove();
			eDelete.eToolTip = null;
		}

		if (!eDelete.bPreserve)
			eDiv.removeChild(eDelete);

		eDelete = ePrevNode;
	}
}

function MapObject_UpdatePOI(bShowSelect)
{
	var eDiv = this.node.parentNode;

	this.removePOI();
	var ePOIToClick = null;

	var bDisplayArrIndex = false;
	var nArrIndex = this.oMapInfo.nArrIndex;
	var arrPOI = this.oMapInfo.arrPOI;
	var nCamX, nCamY, bActiveDot;

	var nStop = arrPOI.length;
	for (var i=0; i<nStop; i++)
	{
		var curPOI = arrPOI[i];
		curPOI.eDot = null;

		if (this.bLoggedIn)
			bActiveDot = true;
		else
		{
			var nCamY = this.LatToY(curPOI[oPOI.CamLat]);
			var nCamX = this.LonToX(curPOI[oPOI.CamLon]);
			bActiveDot = this.isPointInMargins(nCamX, nCamY);
		}

		var nY = this.LatToY(curPOI[oPOI.ParLat]);
		var nX = this.LonToX(curPOI[oPOI.ParLon]);

		if (this.isPointInMargins(nX, nY))
		{
			var eImg = document.createElement("IMG");
			eImg.nIndex = i;
			eImg.oMO = this;

			eImg.src = "/gfx/DVIcons/Dot" + (bActiveDot?"Yellow":"Grey") + ".gif";
			eImg.style.position = "absolute";
			eImg.style.visibility = "visible";
			eImg.style.top =  String(nY - 6)+ "px";
			eImg.style.left = String(nX - 6)+ "px";

			if (window.MOEvent_POIClick && bActiveDot)
				eImg.onclick = MOEvent_POIClick;

			eImg.onmouseover = MOEvent_POIMouseOver;
			eImg.onmouseout = MOEvent_POIMouseOut;

			eDiv.appendChild(eImg);

			if (nArrIndex==i)
				ePOIToClick = eImg;

			curPOI.eDot = eImg;
		}
	}

	if ((arguments==0 || bShowSelect) && ePOIToClick)
		ePOIToClick.onclick();
}

function MapObject_OpenPOI(nIndex)
{
	if (nIndex>=0 && nIndex<this.oMapInfo.arrPOI.length)
	{
		var sArgStr = "Region=" + String(this.getRegion());

		var arrPOI = this.oMapInfo.arrPOI[nIndex];
		if (arrPOI.length>=10)
			sArgStr += "&County=" + String(arrPOI[8]) + "&Pin=" + arrPOI[9];
		else
			sArgStr += "&Run=" + arrPOI[2] + "&Pic=" + String(arrPOI[3]);

		NTourOpenViewer(sArgStr);
	}
}

function CompPOI2Array(arrPOI, arrMatch)
{
	var nStop = arrMatch.length / 2;
	for (var i=0; i<nStop; i++)
		if (arrPOI[arrMatch[i*2]] != arrMatch[i*2+1])
			return false;

	return true;
}

function MapObject_FindPOI(nIndex, vValue)
{
	if (arguments.length%2)
		alert("Mismatched search arguments");

	var aaPOI = this.oMapInfo.arrPOI;
	var aPOI;
	var nStop = aaPOI.length;
	for (var i=0; i<nStop; i++)
	{
		aPOI = aaPOI[i];
		if (CompPOI2Array(aPOI, arguments))
			return aPOI;
	}

	return null;
}

function MapObject_PrepareLegend()
{
	var eDiv = document.createElement("DIV");
	eDiv.className = "MapLegend";

	var eImg = document.createElement("IMG");
	eImg.src = this.oPointerMark.getSrc();
	eDiv.appendChild(eImg);

	var eText = document.createTextNode(" ");
	eDiv.appendChild(eText);

	var node = this.node.parentNode;
	var eNext = node.nextSibling;
	if (eNext)
		node.parentNode.insertBefore(eDiv, eNext);
	else
		node.parentNode.appendChild(eDiv);

	this.eLegendDiv = eDiv;
	this.eLegendText = eText;
	this.bHasLegend = true;
}

function MapObject_UpdateLegend(sLegend)
{
	var sVis = "hidden";
	if (sLegend && sLegend.length>0)
	{
		this.eLegendText.data = sLegend;
		sVis = "visible";
	}

	this.eLegendDiv.style.visibility = sVis;
}

function MapObject_PrepareMapMenu()
{
	this.oMapMenu = new MapMenu(this);
	this.oMapMenu.showIcon(this.bLoggedIn);
	this.node.parentNode.appendChild(this.oMapMenu.node);
}

function MapObject_PrepareMarks(sName, nZIndex)
{
	this.oPointerMark = new MarkObject(this, "DotRed", 12, 9);
	this.oTargetMark = new MarkObject(this, "DotBigCyan", 18, 0);

	this.node.parentNode.appendChild(this.oPointerMark.node);
	this.node.parentNode.appendChild(this.oTargetMark.node);
}


function MapObject_SetPointerMarkPOI(nIndex)
{
	if (this.oMapInfo)
		if (nIndex>=0 && nIndex<this.oMapInfo.arrPOI.length)
		{
			var arrPOI = this.oMapInfo.arrPOI[nIndex];
			if (arrPOI[oPOI.ParLat] && arrPOI[oPOI.ParLon])
				this.setPointerMark(arrPOI);
		}
}

function MapObject_SetPointerMarkPIN(sPin)
{
	if (this.oMapInfo)
	{
		var arrPOI = this.oMapInfo.arrPOI;
		var nStop = arrPOI.length;
		for (var i=0; i<nStop; i++)
			if (arrPOI[i][oPOI.Pin] == sPin)
			{
				this.setPointerMark(arrPOI[i]);
				break;
			}
	}
}

function TranslateDir(nDir)
{
	var sPoint = String(Math.round(nDir / 22.5) % 16);
	if (sPoint.length==1)
		return "0" + sPoint;
	else
		return sPoint;
}

function MapObject_PrepareArrow()
{
	var eArrow = document.createElement("IMG");
	eArrow.style.position = "absolute";
	eArrow.style.left = eArrow.style.top = "-25px";
	eArrow.style.height = eArrow.style.width = "25px";
	eArrow.style.visibility = "hidden";
	eArrow.style.zIndex = 0;
	eArrow.bPreserve = true;

	this.eArrow = eArrow;
	this.node.parentNode.appendChild(eArrow);

	this.nArrLat = this.nArrLon = this.nArrDir = null;
}

function MapObject_DisplayArrow(bShiftMap)
{
	if (this.nArrLon)
	{
		var nLeft = this.LonToX(this.nArrLon);
		var nTop = this.LatToY(this.nArrLat);

		if (this.isPointInMargins(nLeft, nTop))
		{
			var sArr = "/gfx/DVIcons/Arr" + TranslateDir(this.nArrDir) + ".gif";
			this.eArrow.src = sArr;
			this.eArrow.style.visibility = "visible";
			this.eArrow.style.left = String(nLeft - 13) + "px";
			this.eArrow.style.top = String(nTop - 13) + "px";
			return;
		}
		else if (arguments.length>0 && bShiftMap)
		{
			this.panMapLatLon(this.nArrLat, this.nArrLon);
		}
	}

	this.eArrow.style.visibility = "hidden";
}

function MapObject_SetArrow(nLat, nLon, nDir)
{
	this.nArrLat=nLat;
	this.nArrLon=nLon;
	this.nArrDir=nDir;
	this.displayArrow(true);
}

function MapObject_SetZoom(nZoom, bUpdateMenu)
{
	if (nZoom>0 && nZoom<=this.nZoomLimit)
	{
		if (this.nZoom != nZoom)
		{
			this.nZoom = nZoom;
			this.getByLatLon(this.getLat(), this.getLon(), true);
			if (arguments.length>1 && bUpdateMenu)
				this.oZoomMenu.selectZoom(nZoom);
		}
	}
}


