var oDOWaiting = null;

function AlertNewDriveData(win)
{
	var oDO = oDOWaiting;
	var oMO = oDO.oMapObj;

	if (win.oDriveInfo)
	{
		var bContinue = bLoggedIn;
		if (!bContinue)
		{
			var nCLat = win.oDriveInfo.nCamLat;
			var nCLon = win.oDriveInfo.nCamLon;
			bContinue = (oMO.hasNoInfo() || oMO.isPointInMarginsLatLon(nCLat, nCLon));
		}

		if (bContinue)
		{
			if (oMO)
			{
				if (win.oMapInfo)
					oMO.updateInfo(win.oMapInfo);
				if (win.arrMapLayers)
					oMO.updateMapMenu(win.arrMapLayers);
			}

			if (win.oPicInfo && oDO.oPicObj)
			{
				var oPI = win.oPicInfo;
				oDO.oPicObj.updateInfo(oPI);
				var aPOI = oMO.findPOI(oPOI.Run, oPI.sRun, oPOI.Pic, oPI.nPic);
				if (aPOI)
					oMO.setPointerMark(aPOI);
			}

			oDO.updateInfo(win.oDriveInfo);
		}
	}
	else if (win.oMapInfo && oMO)
	{
		oMO.updateInfo(win.oMapInfo);
		this.oPicObj.showNoImageMessage();
	}

	if (win.sError)
		alert(win.sError);

	oDOWaiting = null;
}

DriveObject.prototype.baseGetDriveData = DriveObject_BaseGetDriveData;
DriveObject.prototype.getByRunPic = DriveObject_GetByRunPic;
DriveObject.prototype.getByCountyPin = DriveObject_GetByCountyPin;
DriveObject.prototype.getByPOI = DriveObject_GetByPOI;

DriveObject.prototype.getCamFromDir = function(d) { var i=Math.floor(d); var j=Math.round(10*(d-i)); return this.arrCurCamDir[i][j][0]; }
DriveObject.prototype.findDir = DriveObject_FindDir;
DriveObject.prototype.procCams = DriveObject_ProcCams;
DriveObject.prototype.setMoveMenu = DriveObject_SetMoveMenu;
DriveObject.prototype.updateInfo = DriveObject_UpdateInfo;
DriveObject.prototype.alertNewPic = DriveObject_AlertNewPic;
DriveObject.prototype.updateDir = DriveObject_UpdateDir;

DriveObject.prototype.makeInputSpan = DriveObject_MakeInputSpan;
DriveObject.prototype.updateAltDir = DriveObject_UpdateAltDir;

DriveObject.prototype.turnLeft = DriveObject_TurnLeft;
DriveObject.prototype.turnRight = DriveObject_TurnRight;
DriveObject.prototype.moveLeft = DriveObject_MoveLeft;
DriveObject.prototype.moveRight = DriveObject_MoveRight;
DriveObject.prototype.moveUp = DriveObject_MoveUp;
DriveObject.prototype.moveDown = DriveObject_MoveDown;


function DriveObject(sDriveFrameName, oPicObj, sDriveMenu, oMapObj)
{
	switch(arguments.length)
	{
		case 4:
			this.oMapObj = oMapObj;
		case 3:
			this.oDriveMenu = new DriveMenu(sDriveMenu, this);
		case 2:
			this.oPicObj = oPicObj;
			oPicObj.attachNewPicUser(this);
	}

	this.sFrameName = sDriveFrameName;
	this.oDriveInfo = null;

	this.eAltDiv = document.getElementById("AltDirections");
}

function DriveObject_BaseGetDriveData(bKeepMap)
{
	var sURL = sURLBase + "Scripts/Server/GetDriveData.asp?Height=" + String(oPicObj.nHeight);
	var oMO = this.oMapObj;

	var nReg;
	if (top.nRegion && !isNaN(top.nRegion))
		nReg = top.nRegion;
	else if (oMO)
		nReg = oMO.getRegion();

	if (nReg)
		sURL += "&Region=" + String(nReg);

	if (oMO)
	{
		sURL += "&Zoom=" + String(oMO.nZoom);
		if (oMO.sLayer && oMO.sLayer.length>0)
		{
			sURL += "&Layer=" + oMO.sLayer;
			if (oMO.sLayer=="Tax")
				sURL += "&CurTaxMap=" + oMO.getTaxMapName();
		}
	}

	if (bKeepMap || !oMO)
		sURL += "&NoMap=1";

	return sURL;
}

function DriveObject_GetByRunPic(sRun, nPic, nJump, bKeepMap)
{
	if (oDOWaiting==null)
	{
		oDOWaiting = this;
		var bKM = (arguments.length>3 && bKeepMap);
		var sURL = this.baseGetDriveData(bKM) + "&Run=" + sRun + "&Pic=" + String(nPic);

		if (arguments.length>2 && nJump!=0)
			sURL += "&Jump=" + String(nJump);

		frames[this.sFrameName].location = sURL;
	}
}

function DriveObject_GetByCountyPin(nCounty, sPin, bKeepMap)
{
	if (oDOWaiting==null)
	{
		oDOWaiting = this;
		var bKM = (arguments.length>3 && bKeepMap);
		var sURL = this.baseGetDriveData(bKM) + "&County=" + String(nCounty) + "&Pin=" + sPin;
		frames[this.sFrameName].location = sURL;
	}
}

function DriveObject_GetByPOI(arrPOI, oMO)
{
	var sRun = arrPOI[oPOI.Run];
	var nPic = arrPOI[oPOI.Pic];

	if (sRun && nPic)
	{
		var bKeepMap = oMO.keepMap(arrPOI[oPOI.ParLat], arrPOI[oPOI.ParLon]);
		this.getByRunPic(sRun, nPic, 0, bKeepMap);
	}
	else
	{
		this.oPicObj.showNoImageMessage();
		alert("No image available for " + arrPOI[oPOI.Legend]);
	}
	
}

function DriveObject_SetMoveMenu()
{
	var arr = this.getCamFromDir(this.nCurDir);
	if (arr)
	{
		var nDegFromLeft = arr[5];
		var nRelCamDir = arr[6];

		if (nDegFromLeft>=85 && nDegFromLeft<=95)
		{
			this.oDriveMenu.setMoveMenuForward(true);
			this.nJumpDir = (nRelCamDir>90 && nRelCamDir<270) ? -1 : 1;
		}
		else
		{
			this.oDriveMenu.setMoveMenuForward(false);
			this.nJumpDir = (nDegFromLeft < 90) ? 1 : -1;
		}
	}
}

function DriveObject_FindDir(nCam)
{
	var aDir, i, j;
	var nDir = null;
	for (i=0; nDir==null && i<this.nDirCount; i++)
	{
		aDir = this.arrCurCamDir[i];
		for (var j=0; j<aDir.length; j++)
		{
			if (aDir[j][0][0]==nCam)
			{
				nDir = i + (j/10);
				break;
			}
		}
	}
	return nDir;
}

var regCamAlts = /((zoom)|(mid\w+)|(lo\w+)|(hi\w+))/;
var regCamAlt = new RegExp("(\\w+)\\s" + regCamAlts.source, "gi");
function DriveObject_ProcCams(oDI)
{
	var nCamNum, sCamName, aResult, aDir;
	var arrCamDirs, nCamDirs;
	var nCams = oDI.nCamCount;

	var sCamDir = "arrCamDir" + String(oDI.nCamConfig);
	var i, j;

	if (!this[sCamDir])
	{
		arrCamDirs = new Array();
		nCamDirs = 0;

		for (i=0; i<nCams; i++)
		{
			nCamNum = oDI.arrCams[i][0];
			sCamName = oDI.arrCams[i][1];

			regCamAlt.lastIndex = 0;
			aResult = regCamAlt.exec(sCamName);

			if (aResult==null)
			{
				aDir = new Array(1);
				aDir.sName = sCamName;
				aDir[0] = [oDI.arrCams[i], null];
				arrCamDirs.push(aDir);

				nCamDirs++;
			}
		}

		for (i=0; i<nCamDirs; i++)
		{
			aDir = arrCamDirs[i];
			var re = new RegExp("(" + aDir.sName + ")\\s" + regCamAlts.source, "i");
			for (j=0; j<nCams; j++)
			{
				nCamNum = oDI.arrCams[j][0];
				sCamName = oDI.arrCams[j][1];

				re.lastIndex = 0;
				aResult = re.exec(sCamName);
				if (aResult)
					aDir.push([oDI.arrCams[j], aResult[2]]);
			}
		}

		this[sCamDir] = arrCamDirs;
		this[sCamDir].nDirs = nCamDirs;
	}
	else
	{
		arrCamDirs = this[sCamDir];
		nCamDirs = arrCamDirs.nDirs;
		var nLen, nCamNum;
		for (var i=0; i<nCamDirs; i++)
		{
			aDir = arrCamDirs[i];
			nLen = aDir.length;
			for (j=0; j<nLen; j++)
				for (k=0; k<nCams; k++)
					if (oDI.arrCams[k][0]==aDir[j][0][0])
					{
						aDir[j][0] = oDI.arrCams[k];
						break;
					}
		}
	}

/*
	var sReport = "";
	for (i=0; i<nCamDirs; i++)
	{
		aDir = arrCamDirs[i];
		sReport += (i?"\n":"") + String(i) + ". " + aDir.sName + " camera: " + aDir[0][0][0] + ", Pic: " + aDir[0][0][2];
		if (aDir.length>1)
		{
			for (j=1; j<aDir.length; j++)
				sReport += "\n\t" + String(i) + "." + String(j) + " " + aDir.sName + "-" + aDir[j][1] + " camera: " + aDir[j][0][0] + ", Pic: " + aDir[j][0][2];
		}
	}
	alert(sReport);
*/

	this.arrCurCamDir = this[sCamDir];
	this.nDirCount = this[sCamDir].nDirs;
}

function DriveObject_UpdateInfo(oDriveInfo)
{
	this.dRunDate = Date.parse(oDriveInfo.dRunDate);

	// Setup map object:
	this.nLat = oDriveInfo.nCamLat;
	this.nLon = oDriveInfo.nCamLon;

	this.procCams(oDriveInfo);

	this.sRun = oDriveInfo.sRun;
	this.nCamConfig = oDriveInfo.nCamConfig;

	var nNewDir = this.findDir(oDriveInfo.nCamNum);

	if (this.oMapObj)
	{
		var arr = this.getCamFromDir(nNewDir);
		this.oMapObj.setArrow(this.nLat, this.nLon, arr[3]);
		if (oDriveInfo.sReqPin)
			this.oMapObj.setPointerMarkPIN(oDriveInfo.sReqPin);
	}

	this.updateAltDir(nNewDir);
	this.setMoveMenu();
}

function DriveObject_AlertNewPic(oPicObj)
{
	var oMO = this.oMapObj;
	if (oMO)
	{
		var sLegend;

		var sRun = oPicObj.sRun;
		var nPic = oPicObj.nPic;
		if (sRun && nPic)
		{
			var arrPOI = oMO.findPOI(oPOI.Run, sRun, oPOI.Pic, nPic);
			if (arrPOI)
				sLegend = arrPOI[oPOI.Legend];
		}

		if (top.SetLegend)
			top.SetLegend(sLegend);

		if (top.AlertNewPic)
			top.AlertNewPic(oPicObj);
	}
}

function DriveObject_ChangeAltDir()
{
	var oDO = this.oDO;
	var nDir = Math.floor(oDO.nCurDir);
	switch(this.type)
	{
		case "checkbox":
			if (this.checked==true)
				nDir += this.nDirVal/10;
			break;

		case "radio":
			break;
	}

	oDO.updateDir(nDir);
	return true;
}

function DriveObject_MakeInputSpan(sType, sLabel, nDirVal)
{
	var eInput = document.createElement("INPUT");
	eInput.type = sType;
	eInput.oDO = this;
	eInput.nDirVal = nDirVal;
	eInput.onclick = DriveObject_ChangeAltDir;

	var eSpan = document.createElement("SPAN");
	eSpan.appendChild(eInput);
	eSpan.appendChild(document.createTextNode(sLabel));

	eSpan.eInput = eInput;

	return eSpan;
}


function DriveObject_UpdateAltDir(nNewDir)
{
	var nOldDir = Math.floor(this.nCurDir);

	var nDir = Math.floor(nNewDir);
	var nAltDir = Math.round((nNewDir - nDir)*10);
	var aDir = this.arrCurCamDir[nDir];

	this.nCurDir = nNewDir;

	if (nOldDir != nDir)
	{
		RemoveChildren(this.eAltDiv);

		var eIS;

		switch(aDir.length)
		{
			case 1:
				break;
			case 2:
			{
				eIS = this.makeInputSpan("checkbox", aDir[1][1], 1);
				this.eAltDiv.appendChild(eIS);
				eIS.eInput.checked = (nAltDir==1);
				break;
			}
			default:
			{
			}

		}
	}
}

function DriveObject_UpdateDir(nNewDir)
{
	var arr = this.getCamFromDir(nNewDir);
	if (this.oPicObj)
		this.oPicObj.getByRunPic(this.sRun, arr[2]);

	if (this.oMapObj)
		this.oMapObj.turnArrow(arr[3]);

	this.updateAltDir(nNewDir);
	this.setMoveMenu();
}

function DriveObject_TurnLeft()
{
	if (this.oPicObj.valid())
	{
		var nCurDir = Math.floor(this.nCurDir+this.nDirCount-1) % this.nDirCount;
		this.updateDir(nCurDir);
	}
}

function DriveObject_TurnRight()
{
	if (this.oPicObj.valid())
	{
		var nCurDir = Math.floor(this.nCurDir+1) % this.nDirCount;
		this.updateDir(nCurDir);
	}
}

function DriveObject_MoveLeft()
{
	if (this.oPicObj.valid())
	{
		var arr = this.getCamFromDir(this.nCurDir);
		this.getByRunPic(this.sRun, arr[2], -this.nJumpDir, true);
	}
}

function DriveObject_MoveRight()
{
	if (this.oPicObj.valid())
	{
		var arr = this.getCamFromDir(this.nCurDir);
		this.getByRunPic(this.sRun, arr[2], this.nJumpDir, true);
	}
}

function DriveObject_MoveUp()
{
	if (this.oPicObj.valid())
	{
		var arr = this.getCamFromDir(this.nCurDir);
		this.getByRunPic(this.sRun, arr[2], this.nJumpDir, true);
	}
}

function DriveObject_MoveDown()
{
	if (this.oPicObj.valid())
	{
		var arr = this.getCamFromDir(this.nCurDir);
		this.getByRunPic(this.sRun, arr[2], -this.nJumpDir, true);
	}
}

