//-- Browsercheck
function BrowserCheck()
{
	var v;
	this.opera = navigator.userAgent.indexOf('Opera') > -1;
	this.konqueror = navigator.userAgent.indexOf('Konqueror') > -1;
	this.ie = !this.opera && !!document.all && !!(v = navigator.userAgent.match(/MSIE (\d(\.\d)?)/));
	if (this.ie) this.ieversion = parseFloat(v[1]);
}
var is = new BrowserCheck();


function code_tags()
{
	var el = getElementsByClassName('phphighlight', 'table'), i = el.length, row, div, pre, img;

	while (i--)
	{
		row = first_child(el[i]);
		if (row.tagName.toLowerCase() != 'tr') row = first_child(row);
		div = first_child(row.childNodes[row.childNodes.length-1]);
		if (!div || div.tagName.toLowerCase() != 'div') continue;
		div.style.paddingBottom = '0px';
		pre = first_child(div);
		if (!pre || (pre.tagName.toLowerCase() != 'pre' && pre.tagName.toLowerCase() != 'code')) continue;

		if (div.scrollWidth > div.clientWidth)
		{
			if (is.ie)
			{
				div.orgPadding = (div.scrollHeight - div.clientHeight) + 'px';
				div.style.paddingBottom = div.orgPadding;
			}
			pre.style.width = div.scrollWidth + 'px';
			img = document.createElement('img');
			img.src = aopen.src;
			div.orgWidth = div.clientWidth;
			img.className = 'klipklap hand';
			img.onclick = klipklap_code;
			el[i].parentNode.insertBefore(img, node_before(node_before(el[i])));
		}
	}

	// pre tags as well
	el = getElementsByClassName('(pre|cmd)', 'div'), i = el.length;

	while (i--)
	{
		div = el[i];
		div.style.paddingBottom = '0px';
		pre = first_child(div);
		if (!pre || pre.tagName.toLowerCase() != 'pre') continue;

		if (div.scrollWidth > div.clientWidth)
		{
			if (is.ie)
			{
				div.orgPadding = (div.scrollHeight - div.clientHeight) + 'px';
				div.style.paddingBottom = div.orgPadding;
			}
			pre.style.width = div.scrollWidth + 'px';
			div.orgWidth = div.clientWidth;
			img = document.createElement('img');
			img.src = aopen.src;
			img.className = 'klipklap hand';
			img.onclick = klipklap_pre;
			div.parentNode.insertBefore(img, div);
		}
	}
}

function klipklap_code()
{
	var table = node_after(node_after(node_after(this)));
	if (!table || table.tagName.toLowerCase() != 'table') return;
	var row = first_child(table);
	if (row.tagName.toLowerCase() != 'tr') row = first_child(row);
	var div = first_child(row.childNodes[row.childNodes.length-1]);
	if (!div || div.tagName.toLowerCase() != 'div') return;
	var pre = first_child(div);
	if (!pre || (pre.tagName.toLowerCase() != 'pre' && pre.tagName.toLowerCase() != 'code')) return;

	if (this.src == aopen.src)
	{
		this.src = aclosed.src;
		table.style.width = 'auto';
		div.style.overflow = 'visible';
		if (is.ie) div.style.paddingBottom = '0px';
		pre.style.borderRight = '1px solid black';
		div.style.width = pre.scrollWidth + 'px';
	}
	else
	{
		this.src = aopen.src;
		table.style.width = '100%';
		div.style.overflow = '';
		if (is.ie) div.style.paddingBottom = div.orgPadding;
		pre.style.borderRight = 'none';
		div.style.width = div.orgWidth + 'px';
	}
}

function klipklap_pre()
{
	var div = node_after(this);
	if (!div || div.tagName.toLowerCase() != 'div') return;
	var pre = first_child(div);
	if (!pre || pre.tagName.toLowerCase() != 'pre') return;

	if (this.src == aopen.src)
	{
		this.src = aclosed.src;
		div.style.overflow = 'visible';
		if (is.ie) div.style.paddingBottom = '0px';
		div.style.width = pre.scrollWidth + 'px';
	}
	else
	{
		this.src = aopen.src;
		div.style.overflow = '';
		if (is.ie) div.style.paddingBottom = div.orgPadding;
		div.style.width = div.orgWidth + 'px';
	}
}


/* DOM functions */

function is_ignorable( nod )
{
	return (nod.nodeType == 8 || (nod.nodeType == 3 && !/[^\t\n\r ]/.test(nod.data)));
}

function node_before( sib )
{
	if (sib)
	{
		while ((sib = sib.previousSibling))
		{
			if (!is_ignorable(sib)) return sib;
		}
	}

	return null;
}

function node_after( sib )
{
	if (sib)
	{
		while ((sib = sib.nextSibling))
		{
			if (!is_ignorable(sib)) return sib;
		}
	}

	return null;
}

function last_child( par )
{
	if (par && par.nodeType == 1)
	{
		var res=par.lastChild;
		do
		{
			if (!is_ignorable(res)) return res;
		}
		while ((res = res.previousSibling));
	}

	return null;
}

function first_child( par )
{
	if (par && par.nodeType == 1)
	{
		var res=par.firstChild;
		do
		{
			if (!is_ignorable(res)) return res;
		}
		while ((res = res.nextSibling));
	}

	return null;
}

function getElementsByClassName(needle, tagname)
{
	if (!tagname) tagname = '*';
	var s = document.getElementsByTagName(tagname), i = s.length, e, r = [];
	var re = new RegExp('(^|\\s)' + needle + '(\\s|$)');

	while (i--)
	{
		e = s[i];
		if (e.className && re.test(e.className)) r.push(e);
	}

	return r;
}

function getDocumentProperty(prop)
{
	return document.documentElement && document.compatMode != 'BackCompat' ? document.documentElement[prop] : document.body[prop];
}

//-- classdealer functions
function addClass(element, classname)
{
	var classes = getClassList(element);
	if (classes.indexOf(classname) == -1)
	{
		classes[classes.length] = classname;
	}

	setClassList(element, classes);
}

function removeClass(element, classname)
{
	var classes = getClassList(element), index;
	if ((index = classes.indexOf(classname)) > -1)
	{
		delete classes[index];
	}

	setClassList(element, classes);
}

function replaceClass(element, oldclass, newclass)
{
	var classes = getClassList(element), index;
	if ((index = classes.indexOf(oldclass)) > -1 && classes.indexOf(newclass) == -1)
	{
		classes[index] = newclass;
	}

	setClassList(element, classes);
}

function getClassList(element)
{
	if (element.className) return element.className.split(/\s+/);
	return [];
}

function setClassList(element, classes)
{
	element.className = classes.join(' ');
}

function hasClass(element, classname)
{
	var classes = getClassList(element);
	return classes.indexOf(classname) > -1;
}

if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(searchElement, fromIndex)
	{
		var l = this.length, i = 0;
		if (fromIndex)
		{
			i = fromIndex;
			if (i < 0)
			{
				i += l;
				if (i < 0) i = 0;
			}
		}

		while (i < l)
		{
			if (this[i] === searchElement) return i;
			i++;
		}

		return -1;
	}
}

//-- Positie van element verkrijgen
function getOffsetTop(el)
{
	var offsetTop = 0;
	do
	{
		offsetTop += el.offsetTop
	}
	while ((el = el.offsetParent));

	return offsetTop;
}

function getOffsetLeft(el)
{
	var offsetLeft = 0;
	do
	{
		offsetLeft += el.offsetLeft
	}
	while ((el = el.offsetParent));

	return offsetLeft;
}

function php_code (element)
{
	if (document.selection)
	{
		var range = document.body.createTextRange();
		range.moveToElementText(element);
		range.select();
	}
	else if (window.getSelection)
	{
		var range = document.createRange();
		range.selectNodeContents(element);
		var selection = window.getSelection();
		selection.removeAllRanges();
		selection.addRange(range);
	}
}

function php_code_select (element)
{
	var table = node_before(element);
	if (!table || table.tagName.toLowerCase() != 'table') return;
	var row = first_child(table);
	if (row.tagName.toLowerCase() != 'tr') row = first_child(row);
	var div = first_child(row.childNodes[row.childNodes.length-1]);
	if (!div || div.tagName.toLowerCase() != 'div') return;
	var pre = first_child(div);
	if (!pre || (pre.tagName.toLowerCase() != 'pre' && pre.tagName.toLowerCase() != 'code')) return;
    php_code(pre);
}

//-- IE 5.0 specific
if (!Array.push)
{
	Array.prototype.push = function()
	{
		for (var i = 0; i < arguments.length; ++i)
		{
			this[this.length] = arguments[i];
		}

		return this.length;
	}
}

if (!window.encodeURIComponent)
{
	window.encodeURIComponent = function(string) { return escape(string); }
}

//-- Image preload

var aclosed = new Image(); aclosed.src =  'images/icons/admin_closed.gif';
var aopen = new Image(); aopen.src = 'images/icons/admin_open.gif';

