// $Id: functions.js 7549 2010-05-28 09:09:31Z ashley $

//--- Init/Global JS code ----------------------------------

//Fix for IE's image caching issue. See http://www.mister-pixel.com/#Content__state=is_that_simple and http://code.dbuzz.net/issues/show/149
try {
	  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


//--- Utility functions ------------------------------------

Array.prototype.inArray = function(value) {
	for (var i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};



function $() { /* prototype function */
	var elements = new Array();
	for (var i=0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') element = document.getElementById(element);
		if (arguments.length == 1) return element;
		elements.push(element);
	}
	return elements;
}



/*
http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html

a		defines the action you want the function to perform.
o		the object in question.
c1		the name of the first class
c2		the name of the second class

Possible actions are:
swap		replaces class c1 with class c2 in object o.
add			adds class c1 to the object o.
remove		removes class c1 from the object o.
check		test if class c1 is already applied to object o and returns true or false. 
*/

function multipleClasses (a, o, c1, c2) {
	switch (a) {
		case "swap":
			o.className =! multipleClasses("check", o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
			break;

		case "add":
			if (!multipleClasses("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":
			return new RegExp('\\b'+c1+'\\b').test(o.className);
			break;
	}
}


// Implement a JS version of PHP's in_array
function in_array (needle, haystack) {
	if (typeof haystack == 'undefined') {
		return false;
	}
	for (var i=0; i < haystack.length; i++) {
		if (needle == haystack[i]) {
			return true;
		}
	}
	return false;
}

function insertAfter(newNode, existingNode) {
	// if the existing node has a following sibling, insert the current
	// node before it. otherwise appending it to the parent node
	// will correctly place it just after the existing node.

	if (existingNode.nextSibling) {
		// there is a next sibling. insert before it using the mutual
		// parent's insertBefore() method.
		existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling);
	} else {
		// there is no next sibling. append to the end of the parent's
		// node list.
		existingNode.parentNode.appendChild(newNode);
	}
}

// Checking DOM readiness to attempt to run a specified target function when the DOM content has been loaded
var targetEvent;
var eventParams = new Array();
var domContentLoaded = false;

function fireDOMContentLoadedEvent (event, params) {
	targetEvent = event;
	eventParams = params;
	
	if (document.addEventListener) { // Checking DOM readiness in Firefox, Opera 9+ and Chrome
		document.addEventListener('DOMContentLoaded', function () {
			domContentLoaded = true;
			fireLoadEvent();
		}, false);
	} else if (document.all && !window.opera) { // Checking DOM readiness in IE
		document.write('<script type="text/javascript" id="contentLoaded" defer="defer" src="javascript:void(0)"><\/script>');
		var contentLoadedObj = $('contentLoaded');
		contentLoadedObj.onreadystatechange = function () {
			if (this.readyState == 'complete') {
				domContentLoaded = true;
				fireLoadEvent();
			}
		};
	} else if (/Safari/i.test(navigator.userAgent)) { // Checking DOM readiness in Safari
		var timer = setInterval(function () {
			if (/loaded|complete/.test(document.readyState)) {
				clearInterval(timer);
				domContentLoaded = true;
				fireLoadEvent();
			}
		}, 10);
	}
	
	window.onload = function () {
		setTimeout('if (!domContentLoaded) { fireLoadEvent(); }', 0);
	}
}

function fireLoadEvent () {
	eval(targetEvent+'(\''+eventParams.join(',')+'\')');
}

// Returns true if Google Gears is installed, otherwise false
function gearsInstalled () {
	var gearsResult = false;
	if (typeof google != 'undefined' && typeof google.gears != 'undefined') {
		gearsResult = true;
	}
	return gearsResult;
}

function setCookie(cookieName,cookieValue,expiredays,cookiePath,cookieDomain) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = cookieName + "=" + escape(cookieValue) +
	((expiredays == null) ? "" : ";expires="+exdate.toUTCString()) +
	((cookiePath == null) ? "" : ";path="+cookiePath) +
	((cookieDomain == null) ? "" : ";domain="+cookieDomain);
}

function getCookie(cookieName) {
	if (document.cookie.length>0) {
		cookieStart = document.cookie.indexOf(cookieName + "=");
		if (cookieStart != -1) {
			cookieStart = cookieStart + cookieName.length+1;
			cookieEnd = document.cookie.indexOf(";",cookieStart);
			if (cookieEnd == -1) cookieEnd = document.cookie.length;
			return unescape(document.cookie.substring(cookieStart,cookieEnd));
		}
	}
	return "";
}
