/*******************************************************************************************
 * cssjs
 * Written by Christian Heilmann (http://icant.co.uk)
 * Eases the dynamic application of CSS classes via DOM
 * Parameters: action a, object o and class names c1 and c2 (c2 optional)
 * Actions: swap exchanges c1 and c2 in object o
 *			add adds class c1 to object o
 *			remove removes class c1 from object o
 *			check tests if class c1 is applied to object o
 * Example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
 *******************************************************************************************/

function cssjs (a, o, c1, c2) {
	switch (a) {
		case 'swap':
			o.className = !cssjs('check', o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
			break;
		case 'add':
			if (!o.className)
				o.className = '';
			if (!cssjs('check', o, c1)) {
				o.className += o.className ? ' '+c1 : c1;
			}
			break;
		case 'remove':
			var rep = o.className.match(' '+c1) ? ' '+c1 : c1;
			o.className = o.className.replace(rep, '');
			break;
		case 'check':
			if (!o.className)
				return(false);
			return(new RegExp('\\b'+c1+'\\b').test(o.className));
			break;
	}
	return(true);
}


/*******************************************************************************************
 * addEvent / removeEvent
 *******************************************************************************************/

function addEvent (obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		return(true);
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return(r);
	} else {
		return(false);
	}
}

function removeEvent (obj, evType, fn) {
	if (obj.removeEventListener) {
		obj.removeEventListener(evType, fn, false);
		return(true);
	} else if (obj.detachEvent) {
		var r = obj.detachEvent("on"+evType, fn);
		return(r);
	} else {
		return(false);
	}
}


/*******************************************************************************************
 * addLoadEvent
 * Originally written by Simon Willison (http://simonwillison.net/2004/May/26/addLoadEvent/)
 * Incorporated with code by Dean Edwards (http://dean.edwards.name/weblog/2006/06/again)
 * Takes a function as an argument which should be executed once the DOM has loaded
 * Parameters: function func
 * Example:
 *		addLoadEvent(myFunction);
 *		addLoadEvent(function() {
 *			alert ('a');
 *		});
 *******************************************************************************************/

	//--------------------------------------------------
	// Loading setup

		var addLoadEventStack = []; // Array
		var addLoadEventCount = 0;

		function addLoadEvent(func) {
			addLoadEventStack[addLoadEventCount++] = func;
		}

	//--------------------------------------------------
	// Execute the functions when the page has loaded

		var addLoadEventDone = false;
		var addLoadEventSafariTimer;

		function addLoadEventInit() {

			//--------------------------------------------------
			// Do not run the functions twice

				if (addLoadEventDone) {
					return;
				} else {
					addLoadEventDone = true;
				}

			//--------------------------------------------------
			// Remove the loading timer for Safari

				if (addLoadEventSafariTimer) {
					clearInterval(addLoadEventSafariTimer);
				}

			//--------------------------------------------------
			// Execute the functions - cannot use variable 'i'
			// as the executed functions scope could change it

				var addLoadEventProgress;
				for (addLoadEventProgress = 0; addLoadEventProgress < addLoadEventCount; addLoadEventProgress++) {
					addLoadEventStack[addLoadEventProgress]();
				}

		}

	//--------------------------------------------------
	// Triggers for the different browsers

		//--------------------------------------------------
		// For DOM compatible browsers - Firefox and Opera

		 	if (document.addEventListener) {
		 		document.addEventListener('DOMContentLoaded', addLoadEventInit, false);
		 	}

		//--------------------------------------------------
		// For Safari

			if (/WebKit/i.test(navigator.userAgent)) { // sniff
				addLoadEventSafariTimer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) {
						addLoadEventInit(); // call the onload handler
					}
				}, 10);
			}

		//--------------------------------------------------
		// For Internet Explorer

			/*@cc_on @*/
			/*@if (@_win32)

				document.write('<script id="addLoadEventIeOnload" defer="defer" src=//:><\/script>');
				var script = document.getElementById("addLoadEventIeOnload");
				script.onreadystatechange = function() {
					if (this.readyState === "complete") {
						addLoadEventInit(); // call the onload handler
					}
				};

			@end @*/

		//--------------------------------------------------
		// Fall back for other browsers

			window.onload = addLoadEventInit;

/*******************************************************************************************
 * Uncomment if you need to stop JS Flicker
 *******************************************************************************************/
/*
if (document.getElementsByTagName) {
	addLoadEvent (function() {
		document.body.style.visibility = 'visible';
	});
	document.write ('<style type="text/css"> body { visibility: hidden; } \</style>');
}
*/

/*******************************************************************************************
 * Take a link reference and bind a function to the onclick/onkeypress events
 *******************************************************************************************/

	function addLinkEvent(link, func) {

		try {
			link.style.cursor = 'pointer';
		} catch (e) {
			try {
				link.style.cursor = 'hand';
			} catch (e) {
			}
		}

		link.tabIndex = 0;

		link.onclick = func;

		link.onkeypress = function (e) {
				var keyCode = e ? e.which : window.event.keyCode;
				if (keyCode != 13 && keyCode != 32) return true;
				this.onclick();
				return false;
			};

	}

/*******************************************************************************************
 * printMe
 * Try to run the "print" function, otherwise tell the user how to print correctly
 * Example:	printMe();
 *******************************************************************************************/

function printMe () {
	try {
		print();
	} catch(exception) {
		alert("To print this page, click file and\n select 'Print' or 'Print Preview'");
	}
}


/*******************************************************************************************
 * javasciptElement and javascriptAlternativeElement classes
 * Used to better present the information to users bases on the technology they have enabled
 *******************************************************************************************/

document.write('<style type="text/css" media="screen">');
document.write('<!-- ');
document.write('/* \\*/');
document.write('.javascriptElement { display:block; }');
document.write('.javascriptAlternativeElement { display:none; }');
document.write('/* */');
document.write('-->');
document.write('<\/style>');


/*******************************************************************************************
 * createElement
 * Written by Simon Willison (http://simon.incutio.com/)
 * Create an xhtml element with a nameSpace if possible
 * Example:	createElement('a');
 *******************************************************************************************/

function createElement (element) {
	if (typeof document.createElementNS != 'undefined') {
		return(document.createElementNS('http://www.w3.org/1999/xhtml', element));
	}
	if (typeof document.createElement != 'undefined') {
		return(document.createElement(element));
	}
	return(false);
}

/*******************************************************************************************
 * addCssRule
 * Originally written by Craig Francis (http://craigfrancis.co.uk)
 * Add a CSS rule to the document - includes support for xhtml+xml
 * Example:	addCssRule('#itemId { position: absolute; left: -5000px; }');
 *******************************************************************************************/

	function addCssRule(cssRule) {

		var styleElement;
		var headRef;

		this.useXmlMethods = (document.contentType && document.contentType.indexOf('xml') > -1);
		if (this.useXmlMethods) {

			styleElement = createElement('style');
			styleElement.setAttribute('type', 'text/css');
			styleElement.appendChild(document.createTextNode(cssRule));

			headRef = document.getElementsByTagName('head');
			if (headRef[0]) {
				headRef[0].appendChild(styleElement);
			}

		} else {

			document.write ('<style type="text\/css"> ' + cssRule + ' <\/style>');

		}

	}

/*******************************************************************************************
 * findPosX and findPosY
 * Written by Peter-Paul Koch (http://www.quirksmode.org/) and Alex Tingle (http://blog.firetree.net/)
 * Find where an element is on the page.
 * Example:	findPosX(this);
 *******************************************************************************************/

	function findPosX(obj) {
		var curleft = 0;
		if (obj.offsetParent) {
			while(1) {
				curleft += obj.offsetLeft;
				if(!obj.offsetParent) {
					break;
				}
				obj = obj.offsetParent;
			}
		} else if (obj.x) {
			curleft += obj.x;
		}
		return curleft;
	}

	function findPosY(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while(1) {
				curtop += obj.offsetTop;
				if (!obj.offsetParent) {
					break;
				}
				obj = obj.offsetParent;
			}
		} else if (obj.y) {
			curtop += obj.y;
		}
		return curtop;
	}

/*******************************************************************************************
 * doSearchClear
 * Written by Petr Krojzl (krojzl@gmail.com)
 * Clear field if it contains default value (for onfocus event)
 *******************************************************************************************/

function doSearchClear (field) {
	if (field.value == field.defaultValue)
		field.value = "";
}


/*******************************************************************************************
 * doSearchFill
 * Written by Petr Krojzl (krojzl@gmail.com)
 * Fill field with default value if it is empty (for onblur event)
 *******************************************************************************************/

function doSearchFill (field) {
	if (field.value == "")
		field.value = field.defaultValue;
}


/*******************************************************************************************
 * Firebug
 * Written by Joe Hewitt (http://www.getfirebug.com/)
 * Suppress any calls to console.log, etc.
 *******************************************************************************************/

	if (!window.console || !console.firebug) {

		var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];

		window.console = {};
		for (var i = 0; i < names.length; ++i) {
			window.console[names[i]] = function() {};
		}

	}

