var arrMenuParts2Align = new Array();
var arrMenuParts2AlignDepth = new Array();

var currentQuestionDiv = null;
var currentAnswerDiv = null;

function showDiv(key) {
	var divQuestion = document.getElementById('qst'+key);
	var divAnswer = document.getElementById('ans'+key);
	if(divQuestion) {
		divQuestion.style.display = "none";
		divAnswer.style.display = "block";
		if(currentQuestionDiv) {
			currentQuestionDiv.style.display = "block";
			currentAnswerDiv.style.display = "none";
		}
		currentQuestionDiv = divQuestion;
		currentAnswerDiv = divAnswer;
	}
}

function switchImage(imageId, oldImage, newImage) {
	var imageElement = imageId;
	if(imageElement.src.lastIndexOf(oldImage) > -1) {
			imageElement.src = newImage;
	} 
	else {
			imageElement.src = oldImage;
	}
}


function chooseStyle(newstyle) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + (1000*3600*24*365));
	document.cookie = 'style=' + newstyle + '; expires=' + expdate.toGMTString() + '; path=/';
	window.location.reload(true);
}

sfHover = function() {
	if (document.getElementById("menu")) {
		var sfEls = document.getElementById("menu").getElementsByTagName("LI");
		
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
				var temp = this.childNodes.item(0);
				temp.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
				var temp = this.childNodes.item(0);
				temp.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function getPosition(objReference) {
  var arrPosition = new Array()
  arrPosition.left = 0
  arrPosition.right = objReference.offsetWidth
  arrPosition.top = 0
  arrPosition.bottom = objReference.offsetHeight
  do {
    arrPosition.left += objReference.offsetLeft
    arrPosition.top += objReference.offsetTop
  	objReference = objReference.offsetParent
  } while (objReference.tagName != 'BODY' && (objReference.style.position == '' || objReference.style.position == 'relative'))
  arrPosition.right += arrPosition.left
  arrPosition.bottom += arrPosition.top
  return arrPosition
}


function popupWindow(url, windowsettings) {
	window.open(url, "popup", windowsettings); 
}

function openWindowAndPrint () {
  var browserWidth = screen.availWidth-20;
	var positionRight = location.href.length;
	if (location.href.indexOf("#") != -1) {
		positionRight = location.href.indexOf("#") - 1;
	}
	var printUrl = location.href.substr(0,positionRight);
	printUrl = printUrl + ((printUrl.indexOf("?") != -1) ? "&" : "?") + "popup=true&PrintPreview=1";	

	if (browserWidth<780)
	{
		var left = (screen.availWidth - 550) / 2;
		var top = (screen.availHeight - 350) / 2;
		var printwindow = window.open(printUrl, "newwindow", "left="+left+", top="+top+", width=550, height=350, location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
	}
	else 
	{
		var left = (screen.availWidth - 550) / 2;
		var top = (screen.availHeight - 600) / 2;
		var printwindow = window.open(printUrl, "newwindow","left="+left+", top="+top+", width=550, height=600, location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
	} 
  printwindow.print();
}


function addMenuPart2Alignment(obj_id,depth) {
	var newItemIndex = arrMenuParts2Align.length;
	arrMenuParts2Align[newItemIndex] = obj_id;
	arrMenuParts2AlignDepth[newItemIndex] = depth;
}

function AlignMenuParts() {
	var i;
  var MenuWidth = 206;
  //debugger;
  if (0 < arrMenuParts2Align.length) {
		for(i=0; i < arrMenuParts2Align.length; i++) {
			var htmlObject = document.getElementById(arrMenuParts2Align[i]);
			var objPos = getPosition(htmlObject.parentNode);
			//if menuparts parent is not out of the screen then it is the actual menu button
			// thus we can measure its left position and calculate everything from there
			var actualMenuPos;
			if (arrMenuParts2AlignDepth[i] == 0) {actualMenuPos = objPos.left;}
			if (actualMenuPos) { //without an actual menuposition we do nothing
				if (arrMenuParts2AlignDepth[i] > 0) { // if it is an actual submenu
					if (actualMenuPos + ((arrMenuParts2AlignDepth[i]+1) * MenuWidth) > windowWidth()) 
					{	
						htmlObject.style.marginLeft = "-435px"; 
					}
					else //default situation
					{	
						htmlObject.style.marginLeft = "1px"; // Mildly odd
					}
				}
			}
		} //for
	} //if
} //function

function windowWidth(){
   if (document.getElementById){

       if (window.innerWidth)
         return window.innerWidth;
       if (document.documentElement&&document.documentElement.clientWidth)
         return document.documentElement.clientWidth;
       if (document.body.clientWidth)
         return document.body.clientWidth;
   }
}

var arrFunctions = new Array();

function addFunction(fReference){
    /* Deze functie voegt een functie-pointer toe aan de array van functies die uitgevoerd wordt in de body onload. */
    arrFunctions[arrFunctions.length] = fReference;
}

function callAllFunctions(){
    var i;
    if (0 < arrFunctions.length){
        for(i=0; i < arrFunctions.length; i++){
            arrFunctions[i]();
        }
    }
}

//The try catch is for errors in the mijnpagina module, where the general.js is included for some reason?
//Can be removed when the mijnpagina modules isn't used anymore, or if general.js is removed form the mijnpagina module.
try{
    $(document).ready(callAllFunctions);
}catch(e) {}
