/*==[ CODE SHORTCUTS ]=============================================*/
if(typeof($d)       != "object")   { var $d = document; }
if(typeof($w)       != "object")   { var $w = window; }
if(typeof($root)    != "object")   { var $root = $d.documentElement; }
if(typeof($getID)   != "function") { function $getID(e)   { return $d.getElementById(e); }}
if(typeof($getTags) != "function") { function $getTags(t) { return $d.getElementsByTagName(t); }}
if(typeof($addTag)  != "function") { function $addTag(t)  { return $d.createElement(t); }}
/*=================================================================*/


/*==[ UTILITIES OBJECT ]===========================================*/
function WebUtilities()
{
	this.LOADED  = false;
	this.ENV     = this.getEnv();
	this.BROWSER = this.ENV.BROWSER + "_" + this.ENV.VERSION;
	this.OS      = this.ENV.OS;

	this.DEBUG  = this.getUrlParam("debug");

	this.DEBUG_MSG  = "JAVASCRIPT: DEBUG MODE ACTIVE\n";
	this.DEBUG_MSG += "-------------------------------------------\n";
	this.DEBUG_MSG += "Debug mode is active, scripts may not work \n";
	this.DEBUG_MSG += "as anticipated. To deactivate debug mode,  \n";
	this.DEBUG_MSG += "please remove 'debug=<value>' from the URL.\n";

	if(this.DEBUG) { alert(this.DEBUG_MSG); }
}

WebUtilities.prototype.getUrlParam = function(param)
{
	param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]" + param + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null) { return false; }
	else { return results[1]; }
}

WebUtilities.prototype.addLoadEvent = function(func)
{
	var curOnload = $w.onload;
	if(typeof($w.onload) != "function") { $w.onload = func; }
	else
	{
		$w.onload = function()
		{
			if (curOnload) { curOnload(); }
			func();
		}
	}
}


/*==[ FUNCTION: getObjXY() ]=======================================*/
/*  Returns the actual (calculated) X (top)/Y (left) of an object  */
WebUtilities.prototype.getObjXY = function(o)
{
	var x=0, y=0;

	if($root.getBoundingClientRect)
	{
		var box = o.getBoundingClientRect();
		var scrollXY = this.getScrollXY();
		clientTop  = $root.clientTop  || $d.body.clientTop  || 0;
		clientLeft = $root.clientLeft || $d.body.clientLeft || 0,

		x = box.left + scrollXY[0] - clientLeft;
		y = box.top  + scrollXY[1] - clientTop;
	}
	else
	{
		while (o.offsetParent) {
			x += o.offsetLeft;
			y += o.offsetTop;
			o  = o.offsetParent;
		}
	}
	return [x,y];
}


/*==[ FUNCTION: getDocHeight() ]===================================*/
/*  Returns the actual height of the document (not the browser)    */
WebUtilities.prototype.getDocHeight = function()
{
	return Math.max(
		Math.max($d.body.scrollHeight, $root.scrollHeight),
		Math.max($d.body.offsetHeight, $root.offsetHeight),
		Math.max($d.body.clientHeight, $root.clientHeight)
	);
}


/*==[ FUNCTION: getScrollXY() ]====================================*/
/*  Returns the current X (vertical)/Y (horizontal) scroll value   */
WebUtilities.prototype.getScrollXY = function()
{
	var x = 0, y = 0;
	if(typeof($w.pageYOffset) == 'number') { x = $w.pageXOffset;     y = $w.pageYOffset;    }
	else if($d.body && $d.body.scrollTop)  { x = $d.body.scrollLeft; y = $d.body.scrollTop; }
	else if($root && $root.scrollTop)      { x = $root.scrollLeft;   y = $root.scrollTop;   }
	return [x,y];
}


/*==[ FUNCTION: getSize() ]========================================*/
/*  Returns the actual (calculated) X (top)/Y (left) of the window */
WebUtilities.prototype.getSize = function()
{
	var w = 0, h = 0;
	if(typeof($w.innerWidth) == 'number')   { w = $w.innerWidth;       h = $w.innerHeight;       }
	else if($root && $root.clientWidth)     { w = $root.clientWidth;   h = $root.clientHeight;   }
	else if($d.body && $d.body.clientWidth) { w = $d.body.clientWidth; h = $d.body.clientHeight; }
	return [w,h];
}


WebUtilities.prototype.getMouseXY = function(e)
{
	if(e.pageX || e.pageY) { return [e.pageX,e.pageY]; }
	var x = ($d.body.parentElement) ? $d.body.parentElement.scrollLeft : $d.body.scrollLeft;
	var y = ($d.body.parentElement) ? $d.body.parentElement.scrollTop  : $d.body.scrollTop;
	return [e.clientX + x - $d.body.clientLeft, e.clientY + y - $d.body.clientTop];
}


WebUtilities.prototype.setCookie = function(cName, cValue, cExpireDays)
{
	var expDate = new Date();
	expDate.setDate(expDate.getDate() + cExpireDays);
	var cExpire = (expiredays == null) ? "" : ";expires=" + expDate.toGMTString();
	$d.cookie = cName + "=" + escape(cValue) + cExpire;
}


WebUtilities.prototype.getCookie = function(cName)
{
	if($d.cookie.length > 0) {
		cStart = $d.cookie.indexOf(cName + "=");
		if(cStart != -1) {
			cStart = cStart + cName.length+1;
			cEnd   = $d.cookie.indexOf(";", cStart);
			if(c_end == -1) { cEnd = $d.cookie.length; }
			return unescape($d.cookie.substring(cStart, cEnd));
		}
	}
	return false;
}


WebUtilities.prototype.loadFile = function(file)
{
	if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); }
	else                       { xhttp = new ActiveXObject("Microsoft.XMLHTTP"); }

	xhttp.open("GET", file, false);
	xhttp.send(null);
	return xhttp.responseText;
}


WebUtilities.prototype.getEnv = function()
{
	var obj  = { "BROWSER" : "", "VERSION" : "", "OS" : "",
				 "AGENT"   : navigator.userAgent,
				 "SSL"     : (location.href.toLowerCase().indexOf("https") === 0)
			   };

	if(obj.AGENT)
	{
		obj.OS = ((/windows|win32|winNT/i).test(obj.AGENT)) ? 'WIN' :
				 ((/macintosh/i).test(obj.AGENT))           ? 'MAC' :
				 ((/linux/i).test(obj.AGENT))               ? 'LNX' :
				 ((/freebsd/i).test(obj.AGENT))             ? 'BSD' :
				 ((/SunOS/i).test(obj.AGENT))               ? 'SUN' :
				 false;

		obj.BROWSER = ((/Chrome/i).test(obj.AGENT))    ? "CH" :
					  ((/Firefox/i).test(obj.AGENT))   ? "FF" :
					  ((/Konqueror/i).test(obj.AGENT)) ? "KQ" :
					  ((/MSIE/i).test(obj.AGENT))      ? "IE" :
					  ((/Opera/i).test(obj.AGENT))     ? "OP" :
					  ((/Safari/i).test(obj.AGENT))    ? "SF" :
					  false;

		var m = false;
		switch(obj.BROWSER)
		{
			case "CH": m = obj.AGENT.match(/Chrome\/([^\s]*)/);    break;
			case "FF": m = obj.AGENT.match(/Firefox\/([^\s]*)/);   break;
			case "KQ": m = obj.AGENT.match(/Konqueror\/([^\s]*)/); break;
			case "IE": m = obj.AGENT.match(/MSIE\s([^;]*)/);       break;
			case "OP": m = obj.AGENT.match(/Opera\/([^\s]*)/);     break;
			case "SF": m = obj.AGENT.match(/Version\/([^\s]*)/);   break;
		}

		if(m && m[1])
		{
			obj.VERSION = Math.round(parseFloat(m[1])*10)/10;
		}
    }

    return obj;
}


/*==[ UTILITIES OBJECT ]===========================================*/
var UTILITIES = new WebUtilities();
UTILITIES.LOADED = true;

