var oPicObj;
var oMapObj;
var oDriveObj;

var winLM = null;

var eUGBox, eUGText, tID, nCycle;
var nUGBoxMax = 255;
var nUGMaxMin = 153;	// 99
nUGMaxMin = 204;		// CC

var wManager = null;
var bLoggedIn = false;

var ePicLegend = null;

var eFeatureBox = null;
var eFeatureInfo = null;
var eFeaturePrompt = null;
var eFeatureName = null;
var eFeatureDesc = null;
var bHasFeatureInfo = false;

var oFOWaiting = null;

var sUrlLogin;

var arrRegions = [];

function CycleUpgrade()
{
	if (eUGBox.style.visibility == "hidden")
	{
		if (tID)
		{
			window.clearTimeout(tID);
			tID = null;
		}
	}
	else if (tID)
	{
		var sCol;
		if (nCycle>0)
		{
			nCycle++;
			if (nCycle<=nUGBoxMax)
				sCol = nCycle.toString(16);
			else
				nCycle = -nUGBoxMax;
		}
		else
		{
			nCycle++;
			if (nCycle<=-nUGMaxMin)
				sCol = (-nCycle).toString(16);
			else
			{
				window.clearTimeout(tID);
				tID = null;
				window.setTimeout("CycleUpgrade();", 4000);
			}
		}

		if (sCol)
			eUGBox.style.backgroundColor = "#" + "CC" + sCol + "CC";
	}
	else
	{
		tID = window.setInterval("CycleUpgrade()", 20);
		nCycle = nUGMaxMin;
	}
}

function AlertNewPic(oPicObj)
{
	if ((top.sFeatureRun==oPicObj.sRun) && (top.nFeaturePic==oPicObj.nPic))
		SetFeatureBoxMode(1);
	else
		SetFeatureBoxMode(2);
}

function SetupPage()
{
	var bShowLogin = false;
	var arrLogin = /[?&]login(=([^&]+))?(&|$)/.exec(window.location.href);
	if (arrLogin)
	{
		bShowLogin = true;
		sUrlLogin = arrLogin[2];
	}

	window.name = sViewerWinName;
	ePicLegend = document.getElementById("PicLegend");
	eFeatureBox = document.getElementById("FeatureBox");
	eFeatureInfo = document.getElementById("FeatureInfo");
	eFeatureName = document.getElementById("FeatureName");
	eFeatureDesc = document.getElementById("FeatureDesc");
	eFeaturePrompt = document.getElementById("FeaturePrompt");

	SetupButtons();

	oPicObj = new PicObject("PicImage", "PicData");
	oMapObj = new MapObject("MapImage", "MapData", true);
	oDriveObj = new DriveObject("DriveData", oPicObj, "DriveMenu", oMapObj);

	oMapObj.setLoggedInMode(bLoggedIn);

	if (top.LoadPage)
		LoadPage();

	if (top.sError)
		alert(top.sError);

	SetFeatureInfo(top.sFeatureName, top.sFeatureDesc);

	if (top.nLat && top.nLon)
	{
		// Make sure there's a way to place the red-dot!
		oMapObj.setMark(top.nLat, top.nLon);
	}

	if (bLoggedIn)
		ShowProView();
	else
	{
		eUGBox = document.getElementById("Upgrade");
		eUGText = document.getElementById("UGText");
		window.setTimeout("CycleUpgrade();", 2000);

		if (bShowLogin)
			OpenLoginForm();
	}
}

function getLocInfo()
{
	var oLIA = null;
	var nReg = oMapObj.getRegion();
	var sRun = oPicObj.sRun;
	var nPic = oPicObj.nPic;

	if (nReg && sRun && nPic)
	{
		oLIA = new LocInfoAccess();
		oLIA.putRegion(nReg);
		oLIA.putRun(sRun);
		oLIA.putPic(nPic);

		oLIA.putLat(oMapObj.getPointerMarkLat());
		oLIA.putLon(oMapObj.getPointerMarkLon());

		if (oMapObj.arrCurPOI)
		{
			arrPOI = oMapObj.arrCurPOI;
			oLIA.putName(arrPOI[oPOI.Legend]);
			oLIA.putPIN(arrPOI[oPOI.Pin]);
			oLIA.putCounty(arrPOI[oPOI.County]);
		}
	}

	return oLIA;
}

function showLocInfo(oLI)
{
	if (oLI.sPIN && oLI.nCounty)
		oDriveObj.getByCountyPin(oLI.nCounty, oLI.sPIN);
	else if (oLI.sRun && oLI.nPic)
	{
		oDriveObj.getByRunPic(oLI.sRun, oLI.nPic);
		if (oLI.nLat && oLI.nLon)
			oMapObj.setMark(oLI.nLat, oLI.nLon);
	}
}

function ShowProView(arrMapLayers)
{
	top.bLoggedIn = true;

	if (eUGBox)
		eUGBox.style.display = "none";

	var eLogin = document.getElementById("bcLogin");
	var eListM = document.getElementById("bcListM");
	var eSearch = document.getElementById("bcSearch");

	GetRegions();

	eLogin.style.display = "none";
	eListM.style.display = "inline";
	eSearch.style.display = "inline";

	if (arrMapLayers)
		oMapObj.updateMapMenu(arrMapLayers);

	oMapObj.bGeocoding = true;

	oMapObj.setLoggedInMode(true);
	if (oMapObj.oMapInfo)
		oMapObj.updatePOI(false);
}

function HideProView()
{
	top.bLoggedIn = false;

	oMapObj.updateMapMenu([]);

	var eLogin = document.getElementById("bcLogin");
	var eListM = document.getElementById("bcListM");
	var eSearch = document.getElementById("bcSearch");

	eLogin.style.display = "inline";
	eListM.style.display = eSearch.style.display = "none";
	oMapObj.bGeocoding = false;
	oMapObj.setLoggedInMode(false);
}

function SubmitLogin()
{
	var eForm = document.liForm;
	if (eForm.uname.value.length > 0)
		eForm.submit();
}

function AlertLoginResults(win)
{
	LoginForm_CloseForm();

	if (win.sError)
		alert(win.sError);
	else if (win.sSubmitType=="email")
		alert("Password sent to registered email.");
	else if (win.nFieldError==0)
	{
		ShowProView(win.arrMapLayers);
		if (win.bscreen)
			oPicObj.loadBackground(win.bscreen);
	}
}

function AlertNewSearchData(win)
{
  	RemoveSearchForm();
	if (!win.bSessionAlive)
	{
		win.Announce();
		HideProView();
	}
	else if (win.sError)
		alert(win.sError);
	else
	{
		SetFeatureInfo();
		oMapObj.clearMarks();

		var oGCI = win.oGeoCodeInfo;
		switch(win.nAccuracy)
		{
			case 100:
				var oPH = win.oParcelHit;
				ShowSearchResult(oPH.sAddress + "\n" + oPH.sCity + " " + oPH.sZip + "\nPIN:" + oPH.sPin + "\nLat:" + String(oGCI.nLat) + "\nLon:" + String(oGCI.nLon));
				oDriveObj.getByCountyPin(oPH.nCounty, oPH.sPin);
				break;

			case 9:
				oPicObj.hide();
				if (win.oMapInfo)
					oMapObj.updateInfo(win.oMapInfo);
				else
					oMapObj.getByLatLon(oGCI.nLat, oGCI.nLon);

				ShowSearchResult("Address not found:\nmap centered on centroid of zip+4 area.");
				break;

			case 5:
			case 0:
			default:
				ShowSearchResult("Unable to find address or zip+4 area.");
				break;
		}
	}
}

function JumpToUpgrade()
{
	window.location = "http://www.geovista.com/Content/neighborhoodtourpro.asp";
}

function JumpToFeature()
{
	oDriveObj.getByRunPic(top.sFeatureRun, top.nFeaturePic,0,true);
}

function OpenListManager()
{
	if (wManager && !wManager.closed && wManager.ShowLocation)
		wManager.ShowLocation(getLocInfo());
	else
		wManager = window.open("ListManager.asp", sManagerWinName, "width=600,height=600,resizable,toolbar");

	wManager.focus();
}

function LoginForm_CloseForm()
{
	var oFO = oFOWaiting;
	if (oFO.eOpenForm)
	{
		RemoveFormContainer(oFO.eOpenForm);
		oFO.eOpenForm = null;
	}
	oFOWaiting = null;
}

function OpenLoginForm()
{
	if (oFOWaiting==null)
	{
		oFOWaiting = 1;

		var arrLogin = [new FieldObject("hidden", "SubmitType", "Login"),
						new FieldObject("text", "uname", sUrlLogin?sUrlLogin:null, "User ID", 10, 10),
						new FieldObject("password", "password", null, "Password", 10, 10),
						new FieldObject("button", "email", "Email password", "Forgot password?")
						];

		var sFirstField = sUrlLogin ? "password" : "uname";

		var oForm = new FormObject(arrLogin, "Scripts/Server/GetLoginData.asp", "post", "LoginData", LoginForm_CloseForm);
		oForm.setSubmit("Login");

		oFOWaiting = oForm;
		oForm.eOpenForm = MakeFormContainer("Member Login", oForm.makeForm(sFirstField));

		var eLogin = oForm.findField("uname").eInput;
		var eButton = oForm.findField("email").eInput;
		SetupEmailButton(eButton, eLogin, "LoginData");
	}
}

function SetFeatureInfo(sName, sDesc)
{
	RemoveChildren(eFeatureName);
	RemoveChildren(eFeatureDesc);

	bHasFeatureInfo = (sName || sDesc);

	if (sName)
		eFeatureName.appendChild(document.createTextNode(sName));
	if (sDesc)
		eFeatureDesc.appendChild(document.createTextNode(sDesc));

	var nHeight = eFeatureName.offsetHeight + eFeatureDesc.offsetHeight;
	eFeatureBox.style.height = nHeight;
}

function SetFeatureBoxMode(sMode)
{
	if (sMode==0 || !bHasFeatureInfo)
	{
		eFeatureInfo.style.visibility = 
		eFeaturePrompt.style.visibility = 
		eFeatureBox.style.visibility = "hidden";
	}
	else
	{
		eFeatureBox.style.visibility = "visible";
		if (sMode==1)
		{
			eFeatureInfo.style.visibility = "visible";
			eFeaturePrompt.style.visibility = "hidden";
		}
		else
		{
			eFeatureInfo.style.visibility = "hidden";
			eFeaturePrompt.style.visibility = "visible";
		}
	}
}

function ShowSearchResult(sResults)
{
	AnnounceDlg("Search Results", sResults);
}

function SetLegend(sLegend)
{
	var sVis = "hidden";
	if (sLegend && sLegend.length>0)
	{
		ePicLegend.firstChild.data = sLegend;
		sVis = "visible";
	}
	ePicLegend.style.visibility = sVis;
}
