﻿
// These namespaces are declared in Core.js
if (!KSLaw || !KSLaw.Internet || !KSLaw.Internet.Core)
	throw new Error("Some of the expected KSLaw namespaces were not registered");

KSLaw.Internet.PopUp = { Control: {}, Settings: {} };

KSLaw.Internet.PopUp.Settings = function(c, s, p, x, u, y, z)
{
	this.contentOnRight = c;
	this.linkCssClass = s;
	this.contentCssClass = p;
	this.emptyCssClass = x;
	this.arrowImageUrl = u;
	this.arrowImageCssClass = y;
	this.noMouseOut = KSLaw.Internet.Core.defined(z) ? z : false;
	this.onClick = false;
};
KSLaw.Internet.PopUp.Control = function(settings)
{
	var elLink, tblPop, tdTransparent, tdContent, contentId, built;
	var worker = null, waiter = null, opened = false, method, linkImageUrl, linkText, url, popId = null;
	var ATTR = "out";

	this.init = function(linkId, id, callback, i, t, u, tblId)
	{
		linkImageUrl = i;
		linkText = t;
		url = u;
		method = callback;
		contentId = id;
		popId = KSLaw.Internet.Core.defined(tblId) ? tblId : linkId + "popup";
		elLink = document.getElementById(linkId);
		if(!KSLaw.Internet.Core.defined(elLink))return;
		if(settings.onClick){elLink.onclick = function(e){open();};}
		else
		{
			elLink.onmouseover = function(e) { getReady(); };
			elLink.onmouseout = function(e) { giveUp(); };
			if(!settings.noMouseOut)elLink.onmouseout = function(e) { giveUp(); };
		}
	};
	this.close = function(){opened = false;out();};
	this.onBuilt = function(delegate){built = delegate;};

	var getReady = function()
	{
		if(settings.noMouseOut)
		{
			opened = true;
			over();
		}
		else
		{
			try { waiter = window.setInterval(open, 160); }
			catch (e) { };
		}
	};
	var open = function()
	{
		giveUp();
		opened = true;
		over();
	};
	var giveUp = function()
	{
		try { window.clearInterval(waiter); }
		catch (e) { };
		waiter = null;
	};
	var over = function()
	{
		if(!settings.onClick)elLink.onclick = null;
		else elLink.onmouseover = null;
		build();
		var b = KSLaw.Internet.Core.getBounds(elLink);
		var s = KSLaw.Internet.Core.getBounds(tblPop);
		if (settings.contentOnRight)
		{
			tblPop.style.left = b.x + "px";
			tblPop.style.top = b.y + b.height - s.height + "px";
		}
		else
		{
			tblPop.style.left = b.x + "px";
			tblPop.style.top = b.y + "px";
		}
		if(!settings.onClick)
		{
			var kids = KSLaw.Internet.Core.getChildrenByAttribute(tblPop, ATTR);
			for (var i in kids)
			{
				if(!settings.noMouseOut)
				{
					kids[i].onmouseover = function(e) { opened = true; };
					kids[i].onmouseout = function(e) { opened = false; };
				}
			}
		}
		if(KSLaw.Internet.Core.defined(built))built();
		if(!settings.noMouseOut && !settings.onClick)start();
	};
	var start = function()
	{
		try { worker = window.setInterval(out, 80); }
		catch (e) { };
	};
	var stop = function()
	{
		try { window.clearInterval(worker); }
		catch (e) { };
		worker = null;
	};
	var out = function()
	{
		stop();
		if (opened && !settings.onClick) start();
		else
		{
			try
			{
				document.body.removeChild(tblPop);
				if(settings.onClick){elLink.onclick = function(e){open();};}
				else
				{
					elLink.onmouseover = function(e) { opened = true; getReady(); };
					elLink.onmouseout = function(e) { opened = false; giveUp(); };
				}
			}
			catch(e){ };
		}
	};
	var build = function()
	{
		tblPop = document.createElement("TABLE");
		tblPop.id = popId;
		tblPop.cellPadding = tblPop.cellSpacing = 0;
		tblPop.style.position = "absolute";
		document.body.appendChild(tblPop);
		tblPop.style.left = tblPop.style.top = "0px";
		var row, cell, div, cont;
		if (settings.contentOnRight)
		{
			row = tblPop.insertRow(-1);
			cell = row.insertCell(-1);
			if(KSLaw.Internet.Core.defined(settings.emptyCssClass))cell.className = settings.emptyCssClass;
			div = document.createElement("DIV");
			div.innerHTML = "&nbsp;";
			cell.appendChild(div);
			cell = row.insertCell(-1);
			cell.setAttribute(ATTR, "true");
			cell.rowSpan = 2;
			cell.style.verticalAlign = "bottom";
			if(KSLaw.Internet.Core.defined(settings.contentCssClass))cell.className = settings.contentCssClass;
			cont = document.createElement("DIV");
			cont.innerHTML = method(contentId);
			cell.appendChild(cont);
			appendLink(tblPop.insertRow(-1));
			div.style.height = KSLaw.Internet.Core.getBounds(cont).height - KSLaw.Internet.Core.getBounds(elLink).height + "px";
		}
		else
		{
			var ie = KSLaw.Internet.Core.Browser.agent == KSLaw.Internet.Core.Browser.IE;
			row = tblPop.insertRow(-1);
			appendLink(row, ie ? null : 10);
			var empty = row.insertCell(-1);
			div = document.createElement("DIV");
			div.innerHTML = "&nbsp;";
			empty.appendChild(div);
			row = tblPop.insertRow(-1);
			cell = row.insertCell(-1);
			cell.setAttribute(ATTR, "true");
			if(KSLaw.Internet.Core.defined(settings.contentCssClass))cell.className = settings.contentCssClass;
			cont = document.createElement("DIV");
			cont.innerHTML = method(contentId);
			cell.appendChild(cont);
			var dd = KSLaw.Internet.Core.getBounds(tblPop).width - KSLaw.Internet.Core.getBounds(elLink).width;
			if (dd < 6)
			{
				dd = 0;
				cell = row.insertCell(-1);
				if(KSLaw.Internet.Core.defined(settings.emptyCssClass))cell.className = settings.emptyCssClass;
				div = document.createElement("DIV");
				div.innerHTML = "&nbsp;";
				cell.appendChild(div);
			}
			else
			{
				cell.colSpan = 2;
				if(KSLaw.Internet.Core.defined(settings.emptyCssClass))empty.className = settings.emptyCssClass;
			}
			div.style.width = dd + "px";
		}
	};
	var appendLink = function(row, width)
	{
		var cell = row.insertCell(-1),img;
		if(KSLaw.Internet.Core.defined(settings.linkCssClass)) cell.className = settings.linkCssClass;
		cell.setAttribute(ATTR, "true");
		if (KSLaw.Internet.Core.defined(width)) cell.style.width = width + "px";
		var a;
		if(KSLaw.Internet.Core.defined(url))
		{
			a = document.createElement("A");
			a.href = url;
		}
		else a = document.createElement("SPAN");
		cell.appendChild(a);
		if (KSLaw.Internet.Core.defined(linkImageUrl))
		{
			img = document.createElement("IMG");
			img.src = linkImageUrl;
			img.alt = "";
			a.appendChild(img);
		}
		else if(KSLaw.Internet.Core.defined(linkText)) a.innerHTML = linkText;
		if (KSLaw.Internet.Core.defined(settings.arrowImageUrl))
		{
			img = document.createElement("IMG");
			img.src = settings.arrowImageUrl;
			img.alt = "";
			if (KSLaw.Internet.Core.defined(settings.arrowImageCssClass))
				img.className = settings.arrowImageCssClass;
			cell.appendChild(img);
		}
	};
};
