/**
 * Determines window diameters.
 * 
 * @return Array [width, height]
 */
function getWindowSize()
{
	var myWidth = 0, myHeight = 0;
	if (typeof( window.innerWidth ) == 'number') {
		// Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement
			&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		// IE 4 compatible; 'quirks mode'
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return [myWidth, myHeight];
}

/**
 * Determines height remaining after "fixed" elements heights are subtracted
 * from window height, and splits it up amongst "dynamic" elements. 
 * 
 * To be used on first level elements - wrappers; do not use magnin nor padding
 * in these as it causes different results in different browsers.
 * 
 * @param dynamicEls Array - list of IDs of elements to be adapted
 * @param fixedEls Array - list of IDS of elements to stay fixed 
 * @return void
 */
function adaptClientsHeight(dynamicEls, fixedEls)
{
	var winHeight = getWindowSize()[1];
	var fixedElsHeight = 0;
	for (i in fixedEls) {
		fixedElsHeight += document.getElementById(fixedEls[i]).offsetHeight;
	}
	var dynamicElsHeight = Math.floor((winHeight - fixedElsHeight) / dynamicEls.length);
	for (i in dynamicEls) {
		document.getElementById(dynamicEls[i]).style.height = dynamicElsHeight + 'px';
	}
}

function setCookie(name, value, days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name)
{
	setCookie(name, "", -1);
}

function changeClassName(el, cName)
{
	el.className = cName;
}

var chsTmp;
function changeStyle(el, propName, val)
{
	if (val != undefined && chsTmp != val) {
		chsTmp = getStyle(el, propName);
	}
	if (el.style.setAttribute) { /* Fucking IE style */
		el.style.setAttribute('cssText', propName + ': ' + val || chsTmp + ';');
	}
	else {
		el.style.setProperty(propName, val || chsTmp, null);
	}
}

function getStyle(el, styleProp)
{
	if (el.currentStyle) {
		var y = el.currentStyle[styleProp];
	}
	else if (window.getComputedStyle) {
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	}
	return y;
}

function toCamel(val, delimiter)
{
	var slices = val.split(delimiter);
	var camel = slices.shift(); 
	for (var i in slices)
	{
		camel += slices[i].substr(1,0).toUpperCase() + slices[i].substr(1);
	}
	
	return camel;
}

// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling)
{
	bubbling = bubbling || false;

	if (window.addEventListener)	{ // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	}
	else if (window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	}
	else {
		return false;
	}
}

function addLoadEvent(func)
{
	if (window.addEventListener) {
		window.addEventListener('load', func, false);
	}
	else if (document.addEventListener) {
		document.addEventListener('load', func, false);
	}
	else if (window.attachEvent) {
		window.attachEvent('onload', func);
	}
	else {
		if (typeof window.onload == 'function') {
			var oldonload = window.onload;
			window.onload = function(){
				oldonload();
				func();
			}
		} else {
			window.onload = func;
		}
	}
}