﻿
// All manespaces have to be declared in the first file that gets loaded into the browser
// Therefore, it's important to have this Core.js file to be included first in the page
try
{
	KSLaw.Internet.Core = {};
}
catch (ex)
{
	KSLaw = { Internet: { Core: {}} };
}

KSLaw.Internet.Core.Point = function(a, b) { this.x = a; this.y = b; };
// as of July 2010, the Chrome and Safari browsers are both based on WebKit
// so, the Chrome object represents both Safari and Chrome browsers
KSLaw.Internet.Core.Browser = { IE: {}, Firefox: {}, Chrome: {}, Opera: {} };
KSLaw.Internet.Core.Browser.agent = null;
KSLaw.Internet.Core.Browser.version = parseFloat(navigator.appVersion);

if (navigator.userAgent.indexOf(" MSIE ") > -1)
{
	KSLaw.Internet.Core.Browser.agent = KSLaw.Internet.Core.Browser.IE;
	KSLaw.Internet.Core.Browser.version = parseFloat(navigator.userAgent.match(/MSIE (\d+\.\d+)/)[1]);
}
else if (navigator.userAgent.indexOf(" Firefox/") > -1)
{
	KSLaw.Internet.Core.Browser.agent = KSLaw.Internet.Core.Browser.Firefox;
	KSLaw.Internet.Core.Browser.version = parseFloat(navigator.userAgent.match(/Firefox\/(\d+\.\d+)/)[1]);
}
else if (navigator.userAgent.indexOf(" AppleWebKit/") > -1)
{
	KSLaw.Internet.Core.Browser.agent = KSLaw.Internet.Core.Browser.Chrome;
	KSLaw.Internet.Core.Browser.version = parseFloat(navigator.userAgent.match(/AppleWebKit\/(\d+(\.\d+)?)/)[1]);
}
else if (navigator.userAgent.indexOf("Opera/") > -1) KSLaw.Internet.Core.Browser.agent = KSLaw.Internet.Core.Browser.Opera;

KSLaw.Internet.Core.defined = function(el) { return el != null && typeof (el) != "undefined" && el + "" != "NaN"; };
KSLaw.Internet.Core.toggle = function(el) { el.style.display = el.style.display == "none" ? "" : "none"; };
KSLaw.Internet.Core.getKey = function(keyEvent)
{
	var k = null;
	if(KSLaw.Internet.Core.defined(keyEvent))
	{
		if(keyEvent.keyCode)k = keyEvent.keyCode;
		else if(keyEvent.charCode)k = keyEvent.charCode;
		else if(typeof(keyEvent.which)!= 'undefined') k = keyEvent.which;
		return k;
	}
	else return window.event.keyCode;
};
KSLaw.Internet.Core.getTarget = function(event)
{
	event = event || window.event;
	return event.target || event.srcElement;
};
KSLaw.Internet.Core.getElementWidth = function(el)
{
	if (el.offsetWidth && el.offsetWidth > 0) return el.offsetWidth;
	if (el.style.pixelWidth && el.style.pixelWidth > 0) return el.style.pixelWidth;
	if (el.style.posWidth && el.style.posWidth > 0) return el.style.posWidth;
	if (el.style.width && el.style.width.length > 0) return parseInt(el.style.width.substr(0, el.style.width.length - 2));
	return 0;
};
KSLaw.Internet.Core.getElementHeight = function(el)
{
	if (el.offsetHeight && el.offsetHeight > 0) return el.offsetHeight;
	if (el.style.pixelHeight && el.style.pixelHeight > 0) return el.style.pixelHeight;
	if (el.style.posHeight && el.style.posHeight > 0) return el.style.posHeight;
	if (el.style.height && el.style.height.length > 0) return parseInt(el.style.height.substr(0, el.style.height.length - 2));
	return 50;
};
KSLaw.Internet.Core.getParentByTagName = function(el, tagName, val)
{
	if (!KSLaw.Internet.Core.defined(val)) val = 0;
	if (val > 0 && el.tagName && el.tagName == tagName) return el;
	if (el.parentNode) return KSLaw.Internet.Core.getParentByTagName(el.parentNode, tagName, ++val);
	return null;
};
KSLaw.Internet.Core.getParentById = function(el, id, val)
{
	if (!KSLaw.Internet.Core.defined(val)) val = 0;
	if (val > 0 && el.id && el.id == id) return el;
	if (el.parentNode) return KSLaw.Internet.Core.getParentById(el.parentNode, id, ++val);
	return null;
};
KSLaw.Internet.Core.getParentByAttribute = function(el, attribute, value)
{
	if (!KSLaw.Internet.Core.defined(el)) return null;
	if (el.getAttribute && KSLaw.Internet.Core.defined(el.getAttribute(attribute)) && (!KSLaw.Internet.Core.defined(value) || el.getAttribute(attribute) == value)) return el;
	if (KSLaw.Internet.Core.defined(el.parentNode)) return KSLaw.Internet.Core.getParentByAttribute(el.parentNode, attribute, value);
	return null;
};
KSLaw.Internet.Core.getChildrenByAttribute = function(el, attribute, value)
{
	var children = [], node, attr;
	for (var i = 0; i < el.childNodes.length; i++)
	{
		node = el.childNodes[i];
		if (node.tagName && node.getAttribute)
		{
			attr = node.getAttribute(attribute);
			if (attr && (!KSLaw.Internet.Core.defined(value) || attr == value)) children.push(node);
		}
		if (node.childNodes.length > 0)
		{
			var c = KSLaw.Internet.Core.getChildrenByAttribute(node, attribute, value);
			for (var j in c) children.push(c[j]);
		}
	}
	return children;
};
KSLaw.Internet.Core.getChildrenByTagName = function(el, tagName, includeSubChildren)
{
	var children = [], node;
	if (el.childNodes)
	{
		for (var i = 0; i < el.childNodes.length; i++)
		{
			node = el.childNodes[i];
			if (node.tagName && node.tagName == tagName) children.push(node);
			if (includeSubChildren && node.childNodes && node.childNodes.length > 0)
			{
				var c = KSLaw.Internet.Core.getChildrenByTagName(node, tagName, includeSubChildren);
				for (var j in c) children.push(c[j]);
			}
		}
	}
	return children;
};
KSLaw.Internet.Core.getDocument = function(el)
{
	var b = el.ownerDocument || el.document || el;
	return b.defaultView || b.parentWindow;
};
KSLaw.Internet.Core.getStyle = function(el)
{
	if (el.nodeType === 3) return null;
	var c = KSLaw.Internet.Core.getDocument(el);
	if (el.documentElement) el = el.documentElement;
	var b = c && el !== c && c.getComputedStyle ? c.getComputedStyle(el, null) : el.currentStyle || el.style;
	if (!b && KSLaw.Internet.Core.Browser.agent === KSLaw.Internet.Core.Browser.Chrome && el.style)
	{
		var g = el.style.display, f = el.style.position;
		el.style.position = "absolute";
		el.style.display = "block";
		var e = c.getComputedStyle(el, null);
		el.style.display = g;
		el.style.position = f;
		b = {};
		for (var d in e) b[d] = e[d];
		b.display = "none";
	}
	return b
};
switch (KSLaw.Internet.Core.Browser.agent)
{
	case KSLaw.Internet.Core.Browser.IE:
		KSLaw.Internet.Core.getLocation = function(a)
		{
			if (a.self || a.nodeType === 9) return new KSLaw.Internet.Core.Point(0, 0);
			var b = a.getBoundingClientRect();
			if (!b) return new KSLaw.Internet.Core.Point(0, 0);
			var d = a.ownerDocument.documentElement, e = b.left - 2 + d.scrollLeft, f = b.top - 2 + d.scrollTop;
			try
			{
				var c = a.ownerDocument.parentWindow.frameElement || null;
				if (c)
				{
					var g = c.frameBorder === "0" || c.frameBorder === "no" ? 2 : 0;
					e += g;
					f += g;
				}
			}
			catch (h) { };
			return new KSLaw.Internet.Core.Point(e, f);
		};
		break;
	case KSLaw.Internet.Core.Browser.Chrome:
		KSLaw.Internet.Core.getLocation = function(c)
		{
			if (c.window && c.window === c || c.nodeType === 9) return new KSLaw.Internet.Core.Point(0, 0);
			var f = 0, g = 0, j = null, e = null, b;
			for (var a = c; a; j = a, (e = b, a = a.offsetParent))
			{
				b = KSLaw.Internet.Core.getStyle(a);
				var d = a.tagName ? a.tagName.toUpperCase() : null;
				if ((a.offsetLeft || a.offsetTop) && (d !== "BODY" || (!e || e.position !== "absolute")))
				{
					f += a.offsetLeft;
					g += a.offsetTop;
				}
			}
			b = KSLaw.Internet.Core.getStyle(c);
			var h = b ? b.position : null;
			if (!h || h !== "absolute")
			{
				for (var a = c.parentNode; a; a = a.parentNode)
				{
					d = a.tagName ? a.tagName.toUpperCase() : null;
					if (d !== "BODY" && d !== "HTML" && (a.scrollLeft || a.scrollTop))
					{
						f -= a.scrollLeft || 0;
						g -= a.scrollTop || 0;
					}
					b = KSLaw.Internet.Core.getStyle(a);
					var i = b ? b.position : null;
					if (i && i === "absolute")
						break;
				}
			}
			return new KSLaw.Internet.Core.Point(f, g);
		};
		break;
	case KSLaw.Internet.Core.Browser.Opera:
		KSLaw.Internet.Core.getLocation = function(b)
		{
			if (b.window && b.window === b || b.nodeType === 9) return new KSLaw.Internet.Core.Point(0, 0);
			var d = 0, e = 0, i = null;
			for (var a = b; a; i = a, a = a.offsetParent)
			{
				var f = a.tagName;
				d += a.offsetLeft || 0;
				e += a.offsetTop || 0;
			}
			var g = b.style.position, c = g && g !== "static";
			for (var a = b.parentNode; a; a = a.parentNode)
			{
				f = a.tagName ? a.tagName.toUpperCase() : null;
				if (f !== "BODY" && f !== "HTML" && (a.scrollLeft || a.scrollTop) && (c && (a.style.overflow === "scroll" || a.style.overflow === "auto")))
				{
					d -= a.scrollLeft || 0;
					e -= a.scrollTop || 0
				}
				var h = a && a.style ? a.style.position : null;
				c = c || h && h !== "static"
			}
			return new KSLaw.Internet.Core.Point(d, e)
		};
		break;
	default:
		KSLaw.Internet.Core.getLocation = function(d)
		{
			if (d.window && d.window === d || d.nodeType === 9) return new KSLaw.Internet.Core.Point(0, 0);
			var e = 0, f = 0, i = null, g = null, b = null;
			for (var a = d; a; i = a, (g = b, a = a.offsetParent))
			{
				var c = a.tagName ? a.tagName.toUpperCase() : null;
				b = KSLaw.Internet.Core.getStyle(a);
				if ((a.offsetLeft || a.offsetTop) && !(c === "BODY" && (!g || g.position !== "absolute")))
				{
					e += a.offsetLeft;
					f += a.offsetTop
				}
				if (i !== null && b)
				{
					if (c !== "TABLE" && c !== "TD" && c !== "HTML")
					{
						e += parseInt(b.borderLeftWidth) || 0;
						f += parseInt(b.borderTopWidth) || 0
					}
					if (c === "TABLE" && (b.position === "relative" || b.position === "absolute"))
					{
						e += parseInt(b.marginLeft) || 0;
						f += parseInt(b.marginTop) || 0
					}
				}
			}
			b = KSLaw.Internet.Core.getStyle(d);
			var h = b ? b.position : null;
			if (!h || h !== "absolute")
			{
				for (var a = d.parentNode; a; a = a.parentNode)
				{
					c = a.tagName ? a.tagName.toUpperCase() : null;
					if (c !== "BODY" && c !== "HTML" && (a.scrollLeft || a.scrollTop))
					{
						e -= a.scrollLeft || 0;
						f -= a.scrollTop || 0;
						b = KSLaw.Internet.Core.getStyle(a);
						if (b)
						{
							e += parseInt(b.borderLeftWidth) || 0;
							f += parseInt(b.borderTopWidth) || 0
						}
					}
				}
			}
			return new KSLaw.Internet.Core.Point(e, f)
		};
		break;
};
KSLaw.Internet.Core.getBounds = function(el)
{
	var l = KSLaw.Internet.Core.getLocation(el);
	return {
		x: l.x,
		y: l.y,
		width: KSLaw.Internet.Core.getElementWidth(el),
		height: KSLaw.Internet.Core.getElementHeight(el)
	};
};
