/*==[ REQUIRES UTILITIES SCRIPT (utilities.js) ]===================*/
if(typeof(UTILITIES) == "object" && UTILITIES.LOADED) {

/*==[ DEFAULT EVENT HANDLERS ]=====================================*/
function LinkUtility ()
{
	this.template_path  = "/_assets/templates/misc/";
	this.mask           = null;
	this.box            = null;
	this.tip            = null;
	this.links          = [];
	this.type           = { "TOOLTIP" : 0, "LIGHTBOX" : 1 };
	this.isIE6          = /MSIE 6/i.test(navigator.userAgent);
}

var LU = new LinkUtility();

UTILITIES.addLoadEvent(function() { LU.init(); } );

if(LU.isIE6) { $w.onscroll = function(e) { if(!e) var e=$w.event; LU.setPosition("scroll", e); } }


LinkUtility.prototype.init = function()
{
	var pgLinks = $getTags("a");

	for(var i=0; i < pgLinks.length; i++)
	{
		/* get configurable link elements */
		if(pgLinks[i].rel != null)
		{
			var rel = pgLinks[i].rel;
			if(rel == "external" || rel == "glossary")
			{
				this.links.push(pgLinks[i]);
			}
		}
	}

	for(var i=0; i < this.links.length; i++)
	{
		this.links[i].linkUtility = this;
		this.links[i].onclick = function(evnt)
		{
			if(!evnt) var evnt=window.event;
			this.linkUtility.activateLink(this.rel, this.href, evnt);
			return false;
		}
	}

	this.createDivs();
}

LinkUtility.prototype.createDivs = function()
{
	/* create background div */
	this.mask               = $addTag('div');
	this.mask.id            = "lightbox_mask";
	this.mask.style.display = "none";
	this.mask.style.height  = UTILITIES.getDocHeight() + "px";
	this.mask.innerHTML     = "&nbsp;";

	/* create lightbox div */
	this.box                = $addTag('div');
	this.box.id             = "lightbox";
	this.box.style.display  = "none";
	this.box.style.position = (this.isIE6) ? "absolute" : "fixed";

	/* create tooltip div */
	this.tip                = $addTag('div');
	this.tip.id             = "tooltip";
	this.tip.style.display  = "none";
	this.tip.style.position = "absolute";

	$d.body.appendChild(this.mask);
	$d.body.appendChild(this.box);
	$d.body.appendChild(this.tip);
}

LinkUtility.prototype.activateLink = function(linkREL, linkHREF, evnt)
{
	switch(linkREL)
	{
		case "external":
			var template = UTILITIES.loadFile(this.template_path + "external_link.html");
			template     = template.replace(/###_LINK_###/, linkHREF);
			this.box.innerHTML = template;
			this.show(this.type.LIGHTBOX, evnt);
			break;

		case "glossary":
			var term = linkHREF.substr(linkHREF.indexOf("#")+1);
			linkHREF = linkHREF.substr(0,linkHREF.indexOf("#"));
			var def  = UTILITIES.loadFile(linkHREF + "?term=" + term);
			this.tip.innerHTML  = def;
			this.tip.innerHTML += "<img id='tooltipClose' src='/_assets/images/ui/tooltip_close.gif' onclick='LU.hide()' />";
			this.show(this.type.TOOLTIP, evnt);
			break;
	}
}

LinkUtility.prototype.show = function(type, evnt)
{
	switch(type)
	{
		case this.type.LIGHTBOX:
			this.setPosition("scroll", evnt);
			this.mask.style.display = "block";
			this.box.style.display = "block";
			break;

		case this.type.TOOLTIP:
			this.setPosition("object", evnt);
			this.tip.style.display = "block";
			break;
	}
}

LinkUtility.prototype.hide = function()
{
	this.mask.style.display = "none";
	this.box.style.display  = "none";
	this.tip.style.display  = "none";
}

LinkUtility.prototype.setPosition = function(ref, e)
{
	var obj = (e.target) ? e.target : (e.srcElement) ? e.srcElement : false;

	var viewport = UTILITIES.getSize();
	var mouseXY  = UTILITIES.getMouseXY(e);
	var scrollXY = UTILITIES.getScrollXY();
	var objectXY = (obj) ? UTILITIES.getObjXY(obj) : mouseXY;

	if(this.isIE6) { ref += "_ie6"; }

	switch(ref)
	{
		case "scroll":
			this.box.style.left = viewport[0] / 2 - 250 + "px";
			this.box.style.top  = viewport[1] / 2 - 100 + "px";
			break;
		case "scroll_ie6":
			this.box.style.left = viewport[0] / 2 - 250 + "px";
			this.box.style.top  = viewport[1] / 2 + scrollXY[1] - 100 + "px";
			break;
		case "mouse":
		case "mouse_ie6":
			this.tip.style.left = (mouseXY[0] - 150) + "px";
			this.tip.style.top  = (mouseXY[1] + 20)  + "px";
			break;
		case "object":
		case "object_ie6":
			this.tip.style.left = (objectXY[0]) + "px";
			this.tip.style.top  = (objectXY[1] + 20)  + "px";
			break;
	}
}


} // End of conditional inclusion block [requires utilities.js]
