var oPOWaiting = null;

function AlertNewPicData(win)
{
	var oPO = oPOWaiting;

	if (win.bSessionAlive==false)
	{
		win.Announce();
		top.location.reload(true);
	}
	else if (win.sError)
		alert(win.sError)
	else
		oPO.updateInfo(win.oPicInfo);

	oPOWaiting = null;
}
PicObject.prototype.valid = function() { return (this.sRun!=null && this.nPic!=null); }
PicObject.prototype.getByRunPic = PicObject_GetByRunPic;
PicObject.prototype.updateInfo = PicObject_UpdateInfo;
PicObject.prototype.clear = PicObject_Clear;
PicObject.prototype.loadBackground = PicObject_LoadBackground;
PicObject.prototype.setBackgroundMessage = PicObject_SetBackgroundMessage;
PicObject.prototype.showNoImageMessage = PicObject_ShowNoImageMessage;

PicObject.prototype.attachNewPicUser = function(oUser) { this.oPicUser = oUser; }
PicObject.prototype.hide = function()
{
	if (this.eBackground!=null)
		this.eBackground.style.display = "";
	this.node.style.visibility = "hidden";
}

PicObject.prototype.show = function()
{
	if (this.eBackground!=null)
		this.eBackground.style.display = "none";

	this.node.style.visibility = "visible";
}



function PicObject(vImage, sPicFrame, bConfirmSession)
{
	this.node = (typeof(vImage)=="string") ? document.getElementById(vImage) : vImage;
	this.sFrameName = sPicFrame;
	this.bConfirmSession = bConfirmSession?true:false;

	this.nHeight = this.node.parentNode.offsetHeight;

	this.nRegion = null;
	this.sRun = null;
	this.nPic = null;

	this.eBackground = null;
}

function PicObject_GetByRunPic(sRun, nPic)
{
	if (oPOWaiting == null)
	{
		oPOWaiting = this;

		this.showNoImageMessage();

		var sURL = sURLBase + "Scripts/Server/GetPicData.asp?Run=" + sRun + "&Pic=" + String(nPic);
		if (this.nHeight)
			sURL += "&Height=" + String(this.nHeight);
		if (top.nRegion)
			sURL += "&Region=" + String(nRegion);
		if (this.bConfirmSession)
			sURL += "&ConfirmSession=1";

		frames[this.sFrameName].location = sURL;
	}
}

PicObject.prototype.getByLocInfoAccess = function PicObject_GetByLocInfoAccess(oLIA)
{
	var sURL, sData = "";
	function addData(n, v) {sData += (sData.length?"&":"?") + n + "=" + String(v);}

	if (oPOWaiting==null)
	{
		oPOWaiting = this;
		this.showNoImageMessage();

		var nRegion = oLIA.getRegion();
		if (nRegion)
			addData("Region", nRegion);
		if (this.nHeight)
			addData("Height", this.nHeight);
		if (oLIA.getRun() && oLIA.getPic())
		{
			addData("Run", oLIA.getRun());
			addData("Pic", oLIA.getPic());
		}
		else if (oLIA.getCounty() && oLIA.getPIN())
		{
			addData("County", oLIA.getCounty());
			addData("Pin", oLIA.getPIN());
		}
		sURL = sURLBase + "Scripts/Server/GetPicData.asp" + sData;

		frames[this.sFrameName].location = sURL;
	}
}

function PicObject_UpdateInfo(oPicInfo)
{
	this.nRegion = oPicInfo.nRegion;
	this.sRun = oPicInfo.sRun;
	this.nPic = oPicInfo.nPic;
	this.node.src = oPicInfo.sURL;
	this.show();

	if (this.oPicUser && this.oPicUser.alertNewPic)
		this.oPicUser.alertNewPic(this);
}

function PicObject_Clear()
{
	this.nRegion = null;
	this.sRun = null;
	this.nPic = null;
	this.node.src = "";
	this.hide();
}

function PicObject_LoadBackground(eEl)
{
	var ePar = this.node.parentNode;
	if (this.eBackground)
	{
		ePar.removeChild(this.eBackground);
		this.eBackground = null;
	}

	var eClone = eEl.cloneNode(true);
	if (eClone)
	{
		try
		{
			ePar.appendChild(eClone);
			this.eBackground = eClone;
		}
		catch(e)
		{
			this.eBackground = document.createElement("DIV");
			this.eBackground.innerHTML = eClone.innerHTML;
			ePar.appendChild(this.eBackground);
		}
	}

}

function PicObject_SetBackgroundMessage(sMsg)
{
	var eDiv = document.createElement("DIV");
	eDiv.style.margin = String(Math.floor(this.nHeight/3)) + "px 10px";
    eDiv.appendChild(document.createTextNode(sMsg));
	this.loadBackground(eDiv);
}

function PicObject_ShowNoImageMessage()
{
	this.setBackgroundMessage("No image available");
	this.hide();
}


