<!-- Begin JavaScript
/*
Global JavaScript variable that determines whether the log() functions output is 
enabled (true) or disabled (false).
*/
var __debug = true;

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Creates a new window to which JavaScript messages may be logged.

Example:
log("This is a log message.");
*/
function log(message) {
    if(!__debug) {
		return;
	}
	if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
}

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Disables the context menu on target element. Note: This 
function is not guaranteed to work on all browsers. Some
browsers such as FireFox allows the user to overide the
hiding of context menus through the browser options.

Example:
disableContextMenu(document.getElementById("photo"));
*/
function disableContextMenu(element) {
    element.oncontextmenu = function() {
        return false;
    }
}

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Disables text selection on target element. Useful for 
preventing text selection on elements during drag/drop 
applications.

Example:
disableSelection(document.getElementById("text"));
*/
function disableSelection(element) {
    element.onselectstart = function() {
        return false;
    };
    element.unselectable = "on";
    element.style.MozUserSelect = "none";
    element.style.cursor = "default";
}

/*
Attribution:
Original Code By Peter-Paul Koch -- http://www.quirksmode.org/js/opacity.html
License Info: http://www.quirksmode.org/about/copyright.html

Description:
Cross browser function to set opacity on the target element.

Example:
setOpacity(document.getElementById("text"), 5);;
*/
function setOpacity(element, value) {
	element.style.opacity = value/10;
	element.style.filter = 'alpha(opacity=' + value*10 + ')';
}

/*
Attribution:
Original Code By Douglas Crockford -- http://javascript.crockford.com/
License Info: unknown

Description:
Extends prototype of String to include trim function for JavaScript.

Example:
var str = "     this is my string    ";
alert("!" + str.trim() + "!");
*/
String.prototype.trim = function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

/*
Attribution:
Original Code By Peter-Paul Koch -- http://www.quirksmode.org/dom/domparse.html
License Info: http://www.quirksmode.org/about/copyright.html

Description:
Removes all child nodes from a parent node.

Example:
clearChildren(document.getElementById("myorderedlist"));
*/
function clearChildren(parentn) {
	while(parentn.hasChildNodes()) {
		parentn.removeChild(parentn.childNodes[0]);
	}
}

/*
Attribution:
Original Code By Unknown
License Info: Unknown

Description:
Cross browser function to retrieve mouse co-ordinates in relation to the document.

Example:
var mousePos = mouseCoords(evt);
alert(mousePos.x);
alert(mousePos.y);
*/
function mouseCoords(e){
	if(e.pageX || e.pageY){
		return {x:e.pageX, y:e.pageY};
	}
	return {
		x:e.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:e.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

/*
Attribution:
Original Code By Unknown
License Info: Unknown

Description:
Cross browser function to retrieve position of an element relative to the document's
top left corner (now the windows).

Example:
var pagePos = getPagePosition(document.getElementById("myorderedlist"));;
alert(pagePos.x);
alert(pagePos.y);
*/
function getPagePosition(elem) {
	var left = 0;
	var top  = 0;

	while (elem.offsetParent){
		left += elem.offsetLeft;
		top  += elem.offsetTop;
		elem  = elem.offsetParent;
	}

	left += elem.offsetLeft;
	top  += elem.offsetTop;

	return {x:left, y:top};
}

/*
Attribution:
Original Code By Unknown
License Info: Unknown

Description:
Cross browser function to retrieve position of an element relative to the elements next 'relatively' 
positioned ancestor.

Example:
var relPos = getRelPosition(document.getElementById("myorderedlist"));;
alert(relPos.x);
alert(relPos.y);
*/
function getRelPosition(elem) {
	var left = 0;
	var top  = 0;

	while (elem.offsetParent && elem.offsetParent.style.position == "absolute"){
		left += elem.offsetLeft;
		top  += elem.offsetTop;
		elem  = elem.offsetParent;
	}

	left += elem.offsetLeft;
	top  += elem.offsetTop;

	return {x:left, y:top};
}

/*
Attribution:
Original Code By Unknown
License Info: Unknown

Description:
Cross browser function to compute relative position of the mouse to a target element. Position is relative to the document's
top left corner (not the windows).

Example (assume e == event passed my a mousemove event):
var mouseOffset = getPagePosition(document.getElementById("myorderedlist"), e);;
alert(mouseOffset.x);
alert(mouseOffset.y);
*/
function getMouseOffset(target, e){
	var docPos    = getRelPosition(target);
	var mousePos  = mouseCoords(e);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}
//Generate `Kinda` unique ID...
function genKUID() {
	return new Date().getTime();
}

/*
Attribution:
Original Code By Michael Eagle -- http://deathofanalog.com/
License Info: creative commons

Description:
Cross browser function to check if an element has a certain attribute set.

Example:
var elem = document.getElementByID("photo");
if(hazAttribute(elem, "title") {
	alert(elem.getAttribute("title"));
}
*/
function hazAttribute(element, attribute) {
	if(element.getAttribute(attribute) !== "" && element.getAttribute(attribute) !== null) {
		return true;
	}
	return false;
}

/*
Attribution:
Original Code By Michael Eagle -- http://deathofanalog.com/
License Info: creative commons

Description:
Cross browser function to retrieve an attribute value from an element.

Example:
var elem = document.getElementByID("photo");
var myattval = getzAttribute(elem, "title") {
alert(myattval);
}
*/
function getzAttribute(element, attribute) {
	for( var x = 0; x < element.attributes.length; x++ ) {
		if( element.attributes[x].nodeName.toLowerCase() == attribute) {
			return element.attributes[x].nodeValue;
		}
	}
	return null;
}

/*
Attribution:
Original Code By Michael Eagle -- http://deathofanalog.com/
License Info: creative commons

Description:
Cross browser function to retrieve an events target.

Example:
var elem = getEvtTarget(event);
alert(elem.getzAttribute('id'));
}
*/
function getEvtTarget(evt) {
	if (window.event && window.event.srcElement) {
		return window.event.srcElement
	} else if (evt && evt.target) {
		return evt.target
	}
	return null;
}

/*
Attribution:
Original Code By Michael Eagle -- http://deathofanalog.com/
License Info: creative commons

Description:
Cross browser function to whic attempts to create a name space safe element. Defaults to standard createElement if createElementNS is unavailable.

Example:
var elem = createElementNS('http://www.w3.org/1999/xhtml', 'div');
}
*/
function createNSElement(namespace, type) {
	if(!document.createElementNS) {
		document.createElementNS = function(namespace,type) {
			return document.createElement(type);
		}
	}
}

/*
Attribution:
Original Code By Michael Eagle -- http://deathofanalog.com/
License Info: creative commons

Description:
Function whic finds closest relative to element of type 'type'. If element is of type 'type', returns passed element.

Example:
var target = getEvtTarget(event);
var divelem = findClosestAncestor(target, 'div');
}
*/
function findClosestAncestor(element, type) {
	var relement = element;
	while(relement != null && relement.nodeName.toUpperCase() != type.toUpperCase()) {
        relement = relement.parentNode;
    }
	return relement;
}

function getStyle(element, style) {
	if (element.currentStyle) {
		return element.currentStyle[style];
	}
	else if (window.getComputedStyle) {
		return document.defaultView.getComputedStyle(element,null).getPropertyValue(style);
	}
	return null;
}

/*
Attribution:
Original Code By Michael Eagle -- http://deathofanalog.com/
License Info: creative commons

Description:
Performs extended HTML encoding of strings. Uses the standard escape function first, then encodes *, @, -, _, +, ., and /

Example:
var mystring = encodex("JS+CSS is super-awesome.");
*/
function encodex(instring) {
	var retstring = escape(instring);
	var fndstring = new Array("*", "@", "-", "_", "+", ".", "/", "&", "'");
	var repstring = new Array("&#42;", "&#64;", "&#45;", "&#95;", "&#43;", "&#46;", "&#47;", "%26", "''");
	for(var x = 0; x < fndstring.length; ++x) {
		while(retstring.indexOf(fndstring[x]) != -1) {
			retstring = retstring.replace(fndstring[x], repstring[x]);
		}
	}
	return retstring;
}

/*
Attribution:
Original Code By Michael Eagle -- http://deathofanalog.com/
License Info: creative commons

Description:
Performs extended HTML decoding of strings. Uses the standard unescape function first, then encodes *, @, -, _, +, ., and /

Example:
var mystring = encodex("JS+CSS is super-awesome.");
*/
function decodex(instring) {
	var retstring = unescape(instring);
	var fndstring = new Array("&#42;", "&#64;", "&#45;", "&#95;", "&#43;", "&#46;", "&#47;", "%26", "''");
	var repstring = new Array("*", "@", "-", "_", "+", ".", "/", "&", "'");
	for(var x = 0; x < fndstring.length; ++x) {
		while(retstring.indexOf(fndstring[x]) != -1) {
			retstring = retstring.replace(fndstring[x], repstring[x]);
		}
	}
	return retstring;
}

/*
Attribution:
Original Code By SlayerOffice.com -- http://slayeroffice.com/articles/DOM/ (Slide 27)
License Info: unknown

Description:
Fast method of clearing contents of an element

Example:
var myelem = document.getElementById("delement");
clearInnerHTML(myelem);
*/
function clearInnerHTML(elem) {
	elem.parentNode.replaceChild(elem.cloneNode(false),elem);
}
// End JavaScript -->