/* HTML elements */
var oLightBoxBlk = null;
var oLightBoxDiv = null;
var oLightBoxCnt = null;
var oCloseButton = null;

/* flags and other stateful stuff */
var bIsIe = (document.all);
var bLightboxShow = false;
var bDuringLoad = false;
var oGlobalAjax;
var sCurrentUri = "";

/* config options */
var sLoadingMsg = "Loading&hellip;";

function lb_show (sUri)
{
	sCurrentUri = sUri;
	
	// 1. show the lightbox with loading screen
	if (bLightboxShow)
	{		
		oLightBoxCnt.innerHTML = "<div class=\"lightBox_loading\">"+sLoadingMsg+"</div>";
		bLightboxShow = false;
		oCloseButton.style.display = "none";
	}
	oLightBoxDiv.style.display = "block";
	oLightBoxBlk.style.display = "block";
	
	// 2. fire off an AJAX call
	oGlobalAjax = _lb_getAjax();
	var sPref = (sUri.indexOf("?") != -1) ? "&" : "?";
	oGlobalAjax.open("GET", sUri+sPref+"ajax", true);
	oGlobalAjax.onreadystatechange = _lb_show;
	oGlobalAjax.send();
	bDuringLoad = true;
	return true;
}

function _lb_show ()
{
	// 1. check the status
	// a. if it's just dropping by to say 'hi', ignore it
	if (oGlobalAjax.readyState != 4)
		return false;
	
	// b. if it's not returned a status we like, dismiss the lightbox
	if (oGlobalAjax.status >= 400)
	{
		alert("That page could not be displayed, sorry.\nError was: "+oGlobalAjax.status+": "+oGlobalAjax.statusText);
		lb_dismiss();
		return false;
	}
	
	// c. it works, so grab the text and sling it in the inner div...
	oLightBoxCnt.innerHTML = oGlobalAjax.responseText;
	oGlobalAjax = null;
	bLightboxShow = true;
	var iHeight = parseInt(oLightBoxDiv.style.height) - 120;
	oLightBoxCnt.getElementsByTagName("div")[1].style.height = iHeight+"px";
	oCloseButton.style.display = "inline";
	bDuringLoad = false;
	
	// d. go through forms, sort them out
	var aoForm = oLightBoxCnt.getElementsByTagName("form");
	var oForm;
	for (i = 0; i < aoForm.length; i++)
	{
		oForm = aoForm[i];
		oForm.onsubmit = function() { return !lb_submitForm(this); }
	}

	return true;
}

function lb_dismiss ()
{
	if (!bLightboxShow)
	{
		return false;
	}
	// if we're in a loading phase...
	if (bDuringLoad)
	{
		oGlobalAjax.abort();
		bDuringLoad = false;
	}
	bLightboxShow = false;
	oLightBoxBlk.style.display = "none";
	oLightBoxDiv.style.display = "none";
	return true;
}

function lb_submitForm(oEl)
{
	// 1. get INPUT elements
	var oIn, aoIn = oEl.getElementsByTagName("input");
	var aValues = new Array();
	for (i = 0; i < aoIn.length; i++)
	{
		oIn = aoIn[i];
		switch (oIn.getAttribute("type").toLowerCase())
		{
			case "checkbox":
				if (oIn.checked)
					aValues[oIn.getAttribute("name")] = oIn.value;
				break;
			case "radio":
				if (oIn.selected)
					aValues[oIn.getAttribute("name")] = oIn.value;
				break;
			default:
				aValues[oIn.getAttribute("name")] = oIn.value;
				break;
		}
	}

	// 2. get selects
	aoIn = oEl.getElementsByTagName("select");
	for (i = 0; i < aoIn.length; i++)
	{
		oIn = aoIn[i];
		aValues[oIn.getAttribute("name")] = oIn.options[oIn.selectedIndex].value;
	}
	
	// 3. textareas
	aoIn = oEl.getElementsByTagName("textarea");
	for (i = 0; i < aoIn.length; i++)
	{
		oIn = aoIn[i];
		aValues[oIn.getAttribute("name")] = oIn.value;
	}
	
	// 4. concat
	var sData = "";
	for (k in aValues)
	{
		sData += escape(k)+"="+escape(aValues[k])+"&";
	}
	sData += "ajax=true";
	
	// 5. send it!
	oGlobalAjax = _lb_getAjax();
	oGlobalAjax.open("POST", oEl.getAttribute("action"), true);
	oGlobalAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oGlobalAjax.setRequestHeader("Content-length", sData.length);
	oGlobalAjax.setRequestHeader("Connection", "close");
	oGlobalAjax.onreadystatechange = _lb_show;
	oGlobalAjax.send(sData);
	bDuringLoad = true;
	return true;
}

function lb_print ()
{
	return false;
}

function _lb_getAjax ()
{
    var oAjax = null;
    if (window.XMLHttpRequest)
    {
        try
        {
            oAjax = new XMLHttpRequest();
        }
        catch (e)
        {}
    }
    else if (window.ActiveXObject)
    {
        try
        {
            oAjax = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                oAjax = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (ex)
            {}
        }
    }
    
    return oAjax;
}

function _lb_init ()
{
	// 0. check
	if (oLightBoxDiv != null)
		return true;
	
	// 1. internals
	var oWrap = document.getElementById("wrapper");
	var aLoc  = _getXY(oWrap);
	var iRh   = (document.getElementsByTagName("body")[0].className == "homePage")
				? 585 : oWrap.scrollHeight; // because flash homepage breaks normal detection
				
	// 2. blackout div...
	oLightBoxBlk = document.createElement("div");
	oLightBoxBlk.className 		= "lightBox_blackout";
	oLightBoxBlk.style.height	= iRh + "px";
	
	// 2. container div, close box, and inner container
	oLightBoxDiv = document.createElement("div");
	oLightBoxDiv.className		= "lightBox_content";
	oLightBoxDiv.style.height	= (iRh - 135 - 56) + "px";
	oLightBoxDiv.style.width	= "614px";
	oLightBoxDiv.style.top		= (aLoc.y + 115) + "px";
	oLightBoxDiv.style.left		= (aLoc.x + 220) + "px";
	
	oCloseButton = document.createElement("a");
	oCloseButton.className		= "lightBox_close";
	oCloseButton.onclick		= lb_dismiss;
	
	oLightBoxCnt = document.createElement("div");
	oLightBoxCnt.className		= "lightBox_innerContent";
	oLightBoxCnt.innerHTML		= "<div class=\"lightBox_loading\">"+sLoadingMsg+"</div>";
	
	oLightBoxDiv.appendChild(oCloseButton);
	oLightBoxDiv.appendChild(oLightBoxCnt);
	
	// 3. append
	oWrap.insertBefore(oLightBoxBlk, oWrap.firstChild);
	oWrap.insertBefore(oLightBoxDiv, oLightBoxBlk);
	
	// 4. now take a shufti around all links...
	var oA, aoAnchor = document.getElementsByTagName("a");
	for (var i = 0; i < aoAnchor.length; i++)
	{
		oA = aoAnchor[i];
		if ( oA.getAttribute("href") &&
			(oA.getAttribute("rel") == "lightbox"))
		{
			oA.onclick = function ()
			{
				return !lb_show(this.getAttribute("href"));
			}
		}
		
	}
}


/* additional onload stuff */
addLoadEvent(_lb_init);