// Utility functions, used by other objects
// by Lost Boys - www.lostboys.nl

// IE Anti-background flicker
/*@cc_on
try { document.execCommand('BackgroundImageCache', false, true); } catch (e) {}
@*/


// get elements of a certain kind
getElementsByAttributeValue = function(attribute, value, root) {
	var output = new Array;
	if (IsIE8()){
		var attributeName = attribute;
	}else{
		var attributeName = (document.all && attribute.toLowerCase() == "class") ? "className" : attribute;
	}
	
	var rootElement = root ? root : window.document;	
	var els = (rootElement.all ? rootElement.all : rootElement.getElementsByTagName("*"));
	
	var attributeReg = new RegExp(value);
	var attributeValue;
	
	for(var i = els.length-1; i >= 0; i--) {
		attributeValue = els[i].getAttribute(attributeName);
		if(attributeValue  && (!value || attributeReg.test(attributeValue))) {			
			output[output.length] = els[i];	
		}
	}
	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;
}

function getLanguage() {
	var metas = document.getElementsByTagName("meta");
	var language = /^content-language$/i;
	for (var i=0; i<metas.length; i++) {
		if(language.test(metas[i].getAttribute('http-equiv'))) {
			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');
		return decodeURI(reg.exec(document.cookie)[1]);
	} catch (e) {
		return null;
	}
}

// It's Log, Log, it's better than bad, it's good!
var w=null;function log(m){if(!w)w=window.open();w.document.write(m+'<br />');}

getVals = function(el) {
	var r = "";
	for(var i in el)	{
		r += i + " = " + el[i] + "<br>";
	}
	var w = window.open();
	w.document.write(r);
}


IsIE8 = function() {
	var ua = navigator.userAgent.toLowerCase();
	if( ua.indexOf("msie 8.")!=-1 && ua.indexOf("opera")==-1 && ua.indexOf("webtv")==-1 ) {
		return true;
	}
	else {
		return false;
	}
}


IsIE = function() {
	var ua = navigator.userAgent.toLowerCase();
	if( ua.indexOf("msie")!=-1 && ua.indexOf("opera")==-1 && ua.indexOf("webtv")==-1 ) {
		return true;
	}
	else {
		return false;
	}
}