// topmenu

var IE = (document.all)?true:false;
var TOPTIMER = false; // tx ie...

function topMenuShow(elem) {
	if(IE) { // stupid
		TOPTIMER = false;
	}
	var dd = document.getElementById(elem.id+"_dd");
	dd.style.width = (elem.offsetWidth -4) + "px";
	dd.style.display = "block";
}

function topMenuHide(elem) {
	if(IE) {
		TOPTIMER = true;
		setTimeout("topMenuIeHide('"+elem.id+"')", 500)
	} else {
		document.getElementById(elem.id+"_dd").style.display = "none";
	}
}

function topMenuIeHide(elemid) {
	if(TOPTIMER) {
		TOPTIMER = false;
		document.getElementById(elemid+"_dd").style.display = "none";
	}
}

function getCssClassByName(clname) { // FF only!
	var i;
	for(i = 0; i < document.styleSheets.length; i++) {
		var j;
		for(j = 0; j < document.styleSheets[i].cssRules.length; j++) {
			if(document.styleSheets[i].cssRules[j].selectorText == clname) {
				return document.styleSheets[i].cssRules[j];
			}
		}
	}
	return false;
}

// LEFT positie van een opject uitrekenen
function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

// TOP positie van een object uitrekenen
function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function getWindowWidth() {
	var myWidth = 0;
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
	} else if(document.documentElement && document.documentElement.clientWidth) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if(document.body && document.body.clientWidth) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}

function getWindowHeight() {
	var myHeight = 0;
	if(typeof(window.innerHeigth) == 'number') {
		//Non-IE
		myHeight = window.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if(document.body && document.body.clientHeight) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}

function getScrollX() {
	if (navigator.appName == "Microsoft Internet Explorer") {
		return document.documentElement.scrollLeft;
	} else {
		return window.scrollX;
	}	
}
function getScrollY() {
	if (navigator.appName == "Microsoft Internet Explorer") {
		return document.documentElement.scrollTop;
	} else {
		return window.scrollY;
	}
}

function getMouseX(e) {
	var IE = document.all?true:false;
	if(IE) {
		tempX = event.clientX + document.body.scrollLeft;
	} else {
		tempX = e.pageX;
	}
	if (tempX < 0) {
		tempX = 0;
	}
	return tempX;
}

function getMouseY(e) {
	var IE = document.all?true:false;
	if(IE) {
		tempY = event.clientY + document.body.scrollTop;
	} else {
		tempY = e.pageY;
	}
	if(tempY < 0) {
		tempY = 0;
	}  
	return tempY;
}

//selectie binnen document uitzetten

function selectStop() {
	document.onmousedown = function() {
		return false;
	}
	document.onselectstart = function() {
		return false;
	}
	document.onmousemove = function() {
		return false;
	}
}

//selectie binnen document terug aanzetten

function selectStart() {
	document.onmousedown = function() {
		//nothing
	}
	document.onselectstart = function() {
		//nothing
	}
	document.onmousemove = function() {
		//nothing
	}
}

//arrays

function findKeyPlusOne(needle, haystack) {
	var i;
	for(i = 1; i <= haystack.length; i++) {
		if(haystack[i -1] == needle) {
			return i;
		}
	}
	return false;
}

function in_array(needle, haystack) {
	var i;
	for(i = 0; i < haystack.length; i++) {
		if(haystack[i] == needle) {
			return true;
		}
	}
	return false;
}

function clearField(script, handlerfunction, args) {
	document.getElementById(elem).value = "";
}

// feyenoord

function ajaxSendPost(script, handlerfunction, args, content) {
	
	if (!args)
		var args = new Array();

	var xmlHttp;
	try {
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			args["response"] = xmlHttp.responseText;
			if (handlerfunction)
				handlerfunction.call(this, args, false);
		}
	}

	xmlHttp.open("POST", script, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//xmlHttp.setRequestHeader("Content-length", content.length);
	//xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(content);
}

function ajaxSendGet(script, handlerfunction, args) {

	if (!args)
		var args = new Array();

	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Kut browser
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			args["response"] = xmlHttp.responseText;
			if (handlerfunction)
				handlerfunction.call(this, args, false);
		}
	}

	xmlHttp.open("GET", script, true);
	xmlHttp.send(null);
}

function showMyPic(pic, elem, iframe, admin, fixonmouse) {
	
	// LET OP. Geef bij fixonmouse geen "true" door, maar "event" als hij relatief aan de muis moet verschijnen!

	var picwin = document.createElement("div");
	picwin.id = "picdiv";
	if(admin) {
		picwin.className = "admin_picdiv";
	} else {
		picwin.className = "picdiv";
	}
	document.body.appendChild(picwin);

	if(elem) {

		if (!fixonmouse) {	// Niet op de muispos. Dus reletief aan het plaatje
			var elemtop = findPosY(elem);
			var elemleft = findPosX(elem);
		} else {			// Relatief aan de positie van de muis!!
			var elemtop = getMouseY(fixonmouse);
			var elemleft = getMouseX(fixonmouse)-15;
		}

		var midtop = elemtop + (elem.offsetHeight / 2);
		var centleft = elemleft + (elem.offsetWidth / 2);
		if(midtop < (getWindowHeight() / 2)) {
			picwin.basetop = elemtop + 144;
		} else {
			picwin.basetop = (elemtop + elem.offsetHeight) - 160;
		}
		if(centleft < (getWindowWidth() / 2)) {
			picwin.baseleft = elemleft + elem.offsetWidth + 194; 
		} else {
			picwin.baseleft = elemleft - 214;
		}
	} else if(iframe) {
		picwin.basetop = getScrollY() + 170;
		picwin.style.top = picwin.basetop + "px";
		picwin.baseleft = (getWindowWidth() - 280);
	} else {
		picwin.basetop = 380;
		picwin.style.top = picwin.basetop + "px";
		picwin.baseleft = (getWindowWidth() / 2);
	}
	picwin.style.left = picwin.baseleft + "px";
	picwin.style.top = picwin.basetop + "px";
	picwin.style.width = "20px";
	picwin.style.height = "20px";
	picwin.style.backgroundImage = "url('"+pic+"')";

	var pw = 20;
	var ph = 15;
	function hereGoes() {
		if(pw < 400) {
			pw = pw + 20;
			picwin.baseleft = (picwin.baseleft - 10);
			picwin.style.width = pw + "px";
			picwin.style.left = picwin.baseleft + "px";
			setTimeout(hereGoes, 10);
		} else if (ph < 300) {
			ph = ph + 15;
			picwin.basetop = picwin.basetop - 7.5;
			picwin.style.top = picwin.basetop + "px";
			picwin.style.height = ph + "px";
			setTimeout(hereGoes, 10);
		}
	}
	hereGoes();
}

function hideMyPic() {
	var picwin;
	if(picwin = document.getElementById("picdiv")) {
		picwin.parentNode.removeChild(picwin);
	}
}

function swapMyPic(elem, psrc) {
	elem.src = psrc;
}

function moveThisObject(handleid, payloadid, event) {
	var payload = document.getElementById(payloadid);
	var mainCont = document.getElementById("main_container");

	selectStop();

	var objOrX = findPosX(payload);
	var objOrY = findPosY(payload);

	var corrX = getMouseX(event) - objOrX;
	var corrY = getMouseY(event) - objOrY;

	var minusX = findPosX(mainCont);
	var minusY = findPosY(mainCont);

	document.onmouseup = function() {
		selectStart();
		document.onmouseup = function() {
			// pfew
		}
	}
	moveObject();
		
	function moveObject() {
		var tempX = 0;
		var tempY = 0;
		var ie = document.all?true:false;
		document.onmousemove = moveIt;
		function moveIt(e) {
			if (ie) {
				tempX = event.clientX + document.body.scrollLeft;
				tempY = event.clientY + document.body.scrollTop;
			} else {
				tempX = e.pageX;
				tempY = e.pageY;
			}  
			tempX = (tempX < 0)?0:tempX;
			tempY = (tempY < 0)?0:tempY;
			var newX = ((tempX - corrX) - minusX);
			var newY = ((tempY - corrY) - minusY);
			payload.style.left = newX + 'px';
			payload.style.top = newY + 'px';
		}			
	}
}

function showMyPop(elemid, type, ewidth, eheight) {
	var elem = document.getElementById(elemid);
	if(elem.style.display == "none") {
		elem.style.display = "block";
	} else {
		elem.style.display = "none";
	}
}



function popMyImage(popid, popname, img) {

	var killpop;
	if(killpop = document.getElementById(popid)) {
		killpop.destroy();
	}
	
	unablePage(popid);

	var name = (popname)?popname:"Afbeelding";
	var simg = '<div style="padding: 5px; margin: auto; text-align: center;"><img id="'+popid+'_img" src="'+img+'" /></div>';
	imgPop = standardPop(popid, "popbase_r", "popbar_r", name, true);
	imgPop.fill(simg);
	setTimeout("delayMyPop('"+popid+"')", 100); // for determening img width, apperently race conditions otherwise
}

function delayMyPop(popid) {
	var imgPop = document.getElementById(popid);
	var mywidth = document.getElementById(popid+'_img').width;
	if(mywidth > 380) {
		imgPop.setWidth((mywidth + 30));
	}
	imgPop.show();
	imgPop.center();
}

// TABLE ROW VERBERGEN/WEERGEVEN
function toggleRow(id) {
	if(document.getElementById(id).style.display == "none") {
		var ie = (document.all)?true:false;
		if(ie) {
			document.getElementById(id).style.display = "inline";
		} else {
			document.getElementById(id).style.display = "table-row";
		}
	} else {
		document.getElementById(id).style.display = "none";
	}
}

// SELECTIES IN TEXTAREAS 


function getSelectionStartPos(element) {
	// precies voor FF, ruw & buggy voor IE....
	var ie = (document.all)?true:false;
	if(!ie) {
		return element.selectionStart;
	} else {
		if( document.selection.type != "None"){
			//elem.focus();
			var range = document.selection.createRange();
			var stored_range = range.duplicate();

			stored_range.moveToElementText( element );
			stored_range.setEndPoint( 'EndToEnd', range );
			return stored_range.text.length - range.text.length;
		} else {
			return 0; // no selection? no position in IE, have fun :(
		}
	}
}

function getSelectionEndPos(element) {
	var ie = (document.all)?true:false;
	if(!ie) {
		return element.selectionEnd;
	} else {
		if( document.selection.type != "None"){
			var range = document.selection.createRange();
			var stored_range = range.duplicate();
			stored_range.moveToElementText( element );
			stored_range.setEndPoint( 'EndToEnd', range );
			var selstart = stored_range.text.length - range.text.length;
			return selstart + range.text.length;
		} else {
			return 0;
		}
	}
}

function trim(str, chars) {
	if (!chars)
		chars = " ";
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function getProperties(obj) {
	var i, v;
	var count = 0;
	var props = [];
	if(typeof(obj) === 'object') {
		for (i in obj) {
			v = obj[i];
			if (v !== undefined && typeof(v) !== 'function') {
				props[count] = i;
				count++;
			}
		}
	}
	return props.join("\n");
}

function unablePage(obj) {

	var div = document.createElement('div');
	div.setAttribute("id", "blackdiv_" + obj);
	div.setAttribute("style", "opacity: .4;");
	div.style.display = "block";
	div.style.position = "absolute";
	div.style.zIndex = 8;
	div.style.top = 0;
	div.style.left = 0;
	div.style.backgroundColor = "#000000";
	div.style.filter = "alpha(opacity=40)";
	div.style.height = Math.max($(document).height(),$(window).height(),document.documentElement.clientHeight) + "px";
	div.style.width  = "100%";
	div.onclick = function() {
		enablePage(obj);
		$('#'+obj).remove();
	}
	document.body.appendChild(div);
}

function enablePage(obj) {

	try {
		var div = document.getElementById("blackdiv_" + obj);
		document.body.removeChild(div);
	} catch (e) {}
}

function getContrastColor(color){
	kleur = new Array();
	if(color.charAt(0) == "#") {
		color = color.substring(1);
	}

	kleur[0] = parseInt(color.substring(0,2),16);
	kleur[1] = parseInt(color.substring(2,4),16);
	kleur[2] = parseInt(color.substring(4,6),16);
	
	eindkleur = kleur[0]+kleur[1]+kleur[2];

	if(eindkleur < (785/2)) {
		return "#FFFFFF";
	} else {
		return "#000000";
	}
}

function getSimpleXml(xml, key) {
	var op = '<'+key+'>';
	var cl = '</'+key+'>';
	var rsa = xml.split(op);
	if(rsa.length < 2) {
		return false;
	}
	var rsb = rsa[1].split(cl);
	return rsb[0];
}

function socialShare(url, title,width,height) {
	window.open(url,title,'toolbar=0,status=0,width='+ width +',height=' + height);
	return false;
}
