// Utility functions

// IE Anti-background flicker
/*@cc_on
try { document.execCommand('BackgroundImageCache', false, true); } catch (e) {}
@*/

browser = {
	msie:	 /msie/i.test(navigator.userAgent),
	opera:	 /opera/i.test(navigator.userAgent),
	safari:  /webkit/i.test(navigator.userAgent),
	mozilla: /mozilla/i.test(navigator.userAgent)
}

// get elements of a certain kind
getElementsByAttributeValue = function(attribute, value, root) {
	var output = [];
	var attributeName = (browser.msie && attribute.toLowerCase() == "class") ? "className" : attribute;
	var rootElement = root ? root : document;
	var els = (rootElement.all ? rootElement.all : rootElement.getElementsByTagName("*"));
	
	var attributeReg = new RegExp(value);
	var attributeValue;

	if(els.length == 0) return output;

	for(var i = els.length-1; i >= 0; i--) {
		try {
			attributeValue = els[i].getAttribute(attributeName);
			if(attributeValue  && (!value || attributeReg.test(attributeValue))) output[output.length] = els[i];	
		} catch (e) {
		//	alert('ERROR MET '+attributeName+'\n\n'+e.message)
		}
	}
	return output.reverse();
}

// manipulate class names
addClass = function(obj, cName) { 
	ClassName.add(obj, cName);
}

removeClass = function(obj, cName) {
	ClassName.remove(obj, cName);
}

var ClassName = {
	add:function (node, name) {
		if(!this.contains(node, name)) node.className += ' ' + name;
	},

	remove:function (node, name) {
		if(node.className)
			node.className = node.className.replace(new RegExp('(^|\\s)'+name+'(\\s|$)','g'), ' ');
	},

	contains:function (node, name) {
		return new RegExp('(^|\\s)'+name+'(\\s|$)').test(node.className);
	},

	swap:function (node, old, name) {
		node.className = this.contains(node, old)?
			node.className.replace(new RegExp('(^|\\s)'+old+'(\\s|$)','g'), '$1'+name+'$2') : 
			node.className.replace(new RegExp('(^|\\s)'+name+'(\\s|$)','g'), '$1'+old+'$2');
	},

	toggle:function(node, name) {
		if(!this.contains(node, name)) {
			this.add(node, name);
		} else {
			this.remove(node, name);
		}
	}
}

// get parent element
getParentByTagName = function(startNode, parentType) {
	if(!startNode) return null;
	var node = startNode.parentNode;
	var nodeReg = new RegExp('^'+parentType+'$', 'i');
	while(node) {
		if(nodeReg.test(node.tagName)) return node;
		node = node.parentNode;
	}

	return null;
}

// good old jquery browser sniffing ;-)
var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used
Browser = {
	version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
	safari: /webkit/.test(userAgent),
	opera: /opera/.test(userAgent),
	msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
	mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
};

function getLanguage() {
	var metas = document.getElementsByTagName("meta");
	var language = /^content-language$/i;
	//alert(Browser.msie);
	for (var i=0; i<metas.length; i++) {
		// get attribute and watchout for the ie bug
		var attribute;
		if (Browser.msie)
			attribute = metas[i].getAttribute('httpequiv');
		else
		attribute = metas[i].getAttribute('http-equiv');
		if(language.test(attribute)) {
			return metas[i].getAttribute('content');
		}
	}
	return 'nl';
}

// method for context
Object.prototype.method = function(method) {
	var context = this;
	return function(){
		return method.apply(context, arguments);
	}
}

// get coordinates
calculateLeft = function(object) {
	if (object) return object.offsetLeft + calculateLeft(object.offsetParent); 
	else return 0;
}

calculateTop = function(object) {
	if (object) return object.offsetTop + calculateTop(object.offsetParent); 
	else return 0;
}

// cookie handler
Cookie = function (n, v, e) {
	this.name = n;
	this.value = v;
	this.expires = e;
}

Cookie.prototype.set = function () {
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + (this.expires * 24 * 3600 * 1000));
	document.cookie = this.name + "=" + escape(this.value) + ((this.expires) ? "; expires=" + expDate.toUTCString() : "") + "; path=/";
} 

Cookie.prototype.get = function () {
	try {
		var reg = new RegExp(this.name+'=([^;$]+)','i');
		var cookie = reg.exec(document.cookie)
		if(cookie && cookie[1])	return decodeURI(cookie[1]);
	} catch (e) {
		return null;
	}
}

// Not the first clue what this is about.
var w=null;function log(m){if(!w)w=window.open();w.document.write(m+'<br />');}

function checkCSSSUpport() {
	try {
		// create a 0 px element and request its height, not 0? then no css
		var p = document.createElement('p'), c = p.style; p.innerHTML = 'check';
		c.height = 0; c.padding = 0; c.margin = 0; c.overflow = 'hidden';
		document.body.appendChild(p);
		var h = p.offsetHeight;
		document.body.removeChild(p);
		return (h > 1)? false : true;
	} catch (e) {
		// on error, assume css is enabled (majority)
		return true;
	}
}

getVals = function(el) {
	var r = "";
	for(var i in el)	{
		r += i + " = " + el[i] + "<br>";
	}
	var w = window.open();
	w.document.write(r);
}