function LightboxHistoryCloseLightbox() 
{
	// close lightbox
		if (myLightbox) myLightbox.end();
}

function LightboxHistoryLoadLightbox(id) 
{
	if (!id) return false;

	// get anchor the id relates to
		var anchor = document.getElementById(id);

	// if it exists, then try to load its lightbox
		if (anchor)
		{
			// see if the lightbox is already open, if it is, don't open it
				var lightbox = document.getElementById("lightbox");
				if (lightbox)
				{
					var display = String(lightbox.style.display);

					// if the the lightbox isn't display: none then it must already be open so quit
						if (!display.match(/none/i)) return false;
				}

alert ("myLightbox = " + myLightbox);
			myLightbox.start(anchor);
		}

	return true;
}

function AddLightBoxHistoryEvent(anchor, i) 
{
	if (anchor)
	{
		// make sure anchor has an id
			if (!anchor.id) anchor.id = "lightbox_history" + i;

		// overwrite anchor click event
			anchor.onclick = function () 
			{
				myLightbox.start(this);
				if (parent.frames && parent.frames["lightbox_history"]) parent.frames["lightbox_history"].location.href = "lightbox_history.htm?" + this.id;
				return false;
			} 
	}

	return true;
}

function GetLightboxAnchors() 
{
	var anchors = document.getElementsByTagName('a');

	// loop through all anchor tags
		for (var i = 0; i < anchors.length; i++)
		{
			var anchor = anchors[i];

			var relAttribute = String(anchor.getAttribute('rel'));

		// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox')))
			{
				var onclick = String(anchor.onclick);

				// if there's a lightbox click event, overwrite it
					if (onclick.match(/myLightbox\.start/))
					{
						if (!AddLightBoxHistoryEvent(anchor, i)) return false;
					}


			}
		}

	return true;
}

function CreateHistoryContainer() 
{
	if (document.createElement && document.getElementsByTagName)
	{
		// create iframe
			var iframe = document.createElement("iframe");
			iframe.setAttribute("name", "lightbox_history");
			iframe.setAttribute("id", "lightbox_history");
			iframe.setAttribute("src", "lightbox_history.htm");

		// make it invisible
			iframe.style.display = "none";
			AttachStyle("#lightbox_history","display:none");
		
		// get body
			var body = document.getElementsByTagName("body");

			if (body.length)
			{
				// assume first body tag
					body = body[0];

				// add iframe to body
					if (body && body.appendChild) return (body.appendChild(iframe)) ? true : false;
			}
	}

	return false;
}

function LoadLightBoxHistory(i) 
{
	// only try 3 times
		if (i > 3) return false;
	
	// increment tries
		i = !i ? 1 : (i + 1);

	// make sure we've got the lightbox
		if (typeof(myLightbox)!="undefined")
		{
			setTimeout("LoadLightBoxHistory(" + i + ")",1000);
			return false;
		}

	if (!CreateHistoryContainer()) return false;
	if (!GetLightboxAnchors()) return false;
}

AttachOnload(LoadLightBoxHistory);