/* 

core.js  


*/

/*
 * Adds handler to window.onload event.
 */

Initializers = {
	callbacks: [],
	windowLoaded: false,

	add: function(func)
	{
		if (typeof func == 'function')
			Initializers.callbacks.push(func);
	},

	run: function()
	{
		for (var i = 0; i < Initializers.callbacks.length; i++) {
			try {
				Initializers.callbacks[i]();
			}
			catch (err) {
				return;
				/*alert('An error occured when calling an initializer. Reason: ' + err +
					'\n\nInitializer code:\n\n' + Initializers.callbacks[i]);
				*/
			}
		}
	},

	Events: {
		onWindowLoad: function() {
			if (Initializers.windowLoaded)
				return;

			Initializers.windowLoaded = true;
			Initializers.run();
		}
	}
};

window.onload = Initializers.Events.onWindowLoad;

if (document.addEventListener)
	document.addEventListener('DOMContentLoaded', Initializers.Events.onWindowLoad, null);


/*
 * Escapes regular expression meta characters from given string.
 */
String.prototype.escapeRxSpecials = function()
{
	return this.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
}


/*
 * Returns true if the node has CSS class with given name; otherwise returns false.
 */
function hasClass(node, className)
{
	return new RegExp('\\b' + className.escapeRxSpecials() + '\\b').exec(node.className);
}

function setClass(node, className)
{
	if (!hasClass(node, className))
		node.className = className + (node.className.length > 0 ? ' ': '') + node.className;

	return node;
}


function removeClass(node, className)
{
	var classNames = node.className.split(/\s+/);

	for (var i = 0; i < classNames.length; i++) {
		if (classNames[i] == className) {
			classNames.splice(i, 1);
			node.className = classNames.join(' ');
			break;
		}
	}

	return node;
}

function cEl(el) {
	return document.createElement(el);
}

/* Check if IE and if it's version is < 7 */
function ieCheck(appVersion) {

	var ua = navigator.userAgent;
	if (ua.indexOf('MSIE') > -1 && ua.indexOf('Opera') == -1) {
		var temp = navigator.appVersion.split('MSIE');
		var version = parseFloat(temp[1]);
		
		if(appVersion != '') {
			if(version <= appVersion) {
				return true; 
			}
			else {
				return false;
			}
		}
		else {
			return true; 
		}

	}
	return false;
}

/* Cookie handling */
function setCookie( name, value, expires, path, domain, secure ) {

	// set time, its in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );

}

/* this function gets the cookie, if it exists */
function getCookie( name ) {

	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );

}

/* this deletes the cookie when called */
function deleteCookie( name, path, domain ) {

	if ( getCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";

}