//standaard functie om document.getElementById te doen, bijvoorbeeld $('test').style.color = '';
function $(id) {
	return document.getElementById(id);
}

//onthoud vorige geselecteerde hoofdmenu
var vorige_basismenu
//onthoud vorige geselecteerde menu
var vorige
//timeout variabele
var t


var browser

//functie om te detecteren of het IE 6 is of niet
function is_ie6() {
	if (typeof document.body.style.maxHeight != "undefined") {
		return false;
	}
	else {
		return true;
	}
}

//wordt aangeroepen wanneer iemand met de muis op een hoofdmenu gaat staan
function menu_uitklap(id) {

	//verwijder automatisch inklappen timeout
	clearTimeout(this.t)
	
	
	try {
		//klap vorige menu in
		vorige_basismenu.className = 'submenu'
		
		//klap alle kinderen ook in
		this.close_all_childnodes(vorige_basismenu,id)
		vorige.className = 'submenu'
	}
	catch (e) {}
	try {
		//onthoud geselecteerde object in 'vorige'
		vorige = $("child_"+id)
		//onthoud geselecteerde object ook als vorige_basismenu
		vorige_basismenu = vorige
		
		//bereken afstand vanaf links
		vorige.style.left = $(id).offsetLeft + "px"
		//klap geselecteerde menu open
		vorige.className = 'submenu_over'
	}
	catch (e) {}
}

//functie die wordt aangeroepen wanneer de muis op een submenu staat
function submenu_uitklap(id) {

	
	
	//verwijder automatisch inklappen timeout
	clearTimeout(this.t)
	try {
		//sluit alle childnodes van andere elements in dit subelement
		this.close_all_childnodes($(id).parentNode,id);

		//als het geselecteerde niet gelijk is aan de parent van de geopende, gooi die dicht
		if ($(id).parentNode.parentNode.id != vorige.id && "child_"+id != vorige.id) {
			try {
				vorige.className = 'submenu'
			}
			catch (e) { }
		}
	}
	catch (e) { }
	
	try {
		//onthoud geselecteerde object in 'vorige'
		this.vorige = $("child_"+id)
		
		//afstand vanaf links en boven
          pluslinks = 145
          minboven = 26
 
		//bereken afstand vanaf links
		vorige.style.left = ($("child_"+id).parentNode.offsetLeft+pluslinks) + "px"
		vorige.style.top = ($(id).offsetTop-minboven)+"px"
		//klap menu uit
		vorige.className = 'submenu_over'
	}
	catch (e) {}
}

//sluit alle submenu's, id mag leeg zijn
function close_all_childnodes(element,id) {
	
	for (x in element.childNodes) {
		if (element.childNodes[x].className == "submenu_over") {
			//als id gevuld is en de id is gelijk aan de child van het geselecteerde id, doe niets
			if (!(id != null && 'child_'+id == element.childNodes[x].id)) {
				element.childNodes[x].className = "submenu"
			}
		}
		//recursief, roept zichzelf aan bij subsub
		this.close_all_childnodes(element.childNodes[x],id)
	}
}

//wordt aangeroepen als de muis uit een menuitem gaat
function menu_mouseout(id) {
	clearTimeout(t)
	t = setTimeout("menu_uitklap(0)",800)
}


//BIJBELTEKST FUNCTIE
function bible_over(obj,nr) {
	var biblediv = $('hidden_div_'+nr);
	
	//dit moet erbij
	add_left = 40;
	if (this.is_ie6()) {
		if ($('content_padding_donker') != null) {
			add_left = -24
		}
		biblediv.style.marginTop= '15px';
	}
	//stel pixels vanaf links in
	biblediv.style.left = (obj.offsetLeft+add_left)+'px';
	
	//maak bijbeldiv zichtbaar
	biblediv.style.display = '';
}

//voor de zoekbox
function zoek(uri) {
	//doorsturen naar uri
	window.location = '/'+uri+'/?s='+encodeURIComponent($('zoekbalk').value)
}
function zoek_keyup(e,uri) {
  var characterCode
  if(e && e.which){           // NN4 specific code
    e = e
    characterCode = e.which
  }
  else {
    e = event
    characterCode = e.keyCode // IE specific code
  }
  
  //als er op 'enter' is gedrukt, stuur door naar zoekpagina
  if (characterCode == 13) {    // Enter key is 13
    zoek(uri)
  }
}


//voorpagina functies
/*var vorige_voorpagina
function open_voorpagina(nr) {
	try {
		$(vorige_voorpagina+'_link').className = 'voorpagina_knop_rechts';
		$(vorige_voorpagina+'_uitklap').style.display = 'none';
	} catch (e) {}
	$(nr+'_link').className = 'voorpagina_knop_rechts_hover';
	//$(nr+'_uitklap').style.top = ($(nr+'_link').offsetTop +21) + 'px';
	$(nr+'_uitklap').style.display = 'block';
	
	vorige_voorpagina = nr;
}*/


//automatisch focus op het eerste invoerveld
function putFocus(formInst, elementInst) {
	try {
		if (document.forms.length > 0) {
			document.forms[formInst].elements[elementInst].focus();
		}
	} catch (e) {}
}
function onloadbody() {
	putFocus(0,0)
}
window.onload = onloadbody;




// AJAX CHAT
var xmlHttp
var a_refresh
var vorige_url
var refresh_rate = 1000
var vorige_reactie = ""

//wordt aangeroepen door het input-field op de chatpagina
function chat(url,e) {

	var characterCode
	//er is op de button gedrukt
	if (e == 'click') {
		var characterCode = 13
	}
	//er is op een toets gedrukt
	else {
		try {
			if(e && e.which){           // NN4 specific code
				e = e
				characterCode = e.which
			}
			else {
				e = window.event
				characterCode = e.keyCode // IE specific code
			}
		}
		catch (ef) {}
	}

	//enter is ingedrukt	
	if (characterCode == 13) {    // Enter key is 13
		var str = $('chatinput').value
		
		//leeg inputfield
		$('chatinput').value = '';
		
		//als er iets is ingevuld, verstuur het naar 'url'
		if (str.length != 0) {
			request(url,str)
		}
	}
}

//functie waarmee er automatisch gerefreshed wordt, aan de hand van refresh_rate
function auto_refresh(url) {
	//roep url aan
	request(url,"")
	vorige_url = url
	a_refresh = setTimeout("auto_refresh('"+url+"')",refresh_rate)
}

//functie waarmee url voor een ajax-request aangeroepen wordt
function request(url,str) {
	xmlHttp=GetXmlHttpObject();
	
	//als er een xmlHttp-object is
	if (xmlHttp != null) {
		url=url+"?q="+encodeURI(str);
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//functie die aangeroepen wordt de ajax-request is afgerond
function stateChanged() { 
	try {
		if (xmlHttp.readyState==4) { 
			//bericht dat terugkomt van de ajax-request
			text = xmlHttp.responseText;
			
			if (text != '') {
			
				//splits bericht op door # in drieen
				gesplit = text.split("#",3)
				
				//refreshrate instellen
				try {
					refr = parseInt(gesplit[0]);
					if (refr > 500) {
						if (refr != refresh_rate) {
							refresh_rate = refr;
							if (vorige_url != null) {
								clearTimeout(a_refresh)
								auto_refresh(vorige_url)
							}     
						}
						
					}
				}
				catch (e) {}
				
				//text instellen
				if (vorige_reactie != gesplit[1]) {
					//
					$('nieuw_bericht').src = '/images/chat/chat_load_active.gif'
					var loadrefresh = setTimeout("$('nieuw_bericht').src = '/images/chat/chat_load.gif'",800)
					
				
					$("chatdiv").innerHTML=gesplit[2]
					$("chatdiv").scrollTop = $("chatdiv").scrollHeight;
					vorige_reactie = gesplit[1]
				}
				//status instellen
				$("chatstatusdiv").innerHTML=gesplit[1]
			}
		}
	} catch (e) {}
}

//functie om xmlHttp te vullen
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	if (xmlHttp==null) {
		alert ("Uw browser ondersteunt geen AJAX, daarom werkt de chatfunctie niet.");
	}
	
	return xmlHttp;
}

/* minichatvenster sluiten */
function close_minichat(url) {
	request(url,'minichat_close')

	//verberg minichat
	$('minichat').style.display='none';
	//stop ajax refresh
	clearTimeout(a_refresh);
}


/* minichatvenster minimaliseren */
function minimize_minichat(minimizeknop,url) {
	
	if ($('minimize_chatdiv').style.display != 'none') {
		$('minimize_chatdiv').style.display='none';
		$('chatblok').style.height = '20px'
		$('schaduwlinks').style.height = '20px'
		$('schaduwrechts').style.height = '20px'
		minimizeknop.src = '/images/chat/chat_maximize.gif'
		request(url,'minimize_minichat')
	}
	else {
		$('minimize_chatdiv').style.display='';
		$('chatblok').style.height = '300px'
		$('schaduwlinks').style.height = '300px'
		$('schaduwrechts').style.height = '300px'
		minimizeknop.src = '/images/chat/chat_minimize.gif'
		$("chatdiv").scrollTop = $("chatdiv").scrollHeight;
		request(url,'maximize_minichat')
	}
}

function rollover_icon(action,object) {
	if (action == 'over') {
		object.src = object.src.replace(/.gif/,'_over.gif')
	}
	if (action == 'out') {
		object.src = object.src.replace(/_over.gif/,'.gif')
	}
}



/* minichatvenster verslepen */
var drag_allowed = false
var start_x = 0
var start_y = 0

function startdrag(e) {
	drag_allowed = true
	e = getEvent(e)


	start_x = e.clientX;
	start_y = e.clientY;
}

function drag(e) {
	if (drag_allowed) {
		e = getEvent(e)
		
		//bereken verschil
		var nieuw_x = e.clientX;
		var nieuw_y = e.clientY;
		
		var verschil_x = nieuw_x - start_x
		var verschil_y = nieuw_y - start_y
		
		start_x = nieuw_x;
		start_y = nieuw_y;
		
		
		//console.log(verschil_x + " . " + verschil_y)
		
		var minichat = $('minichat')
		var right = parseInt(minichat.style.right.replace(/px/,""))
		var top = parseInt(minichat.style.top.replace(/px/,""))
		
		minichat.style.right = ((right - verschil_x) + "px")
		minichat.style.top = ((top + verschil_y) + "px")
	}
}

var vorige_xy = ""
function stopdrag(url) {
	var str = 'rememberxy_ right:'+ $('minichat').style.right + "; top:"+$('minichat').style.top+";"
	if (str != vorige_xy) {
		request(url,str)
		vorige_xy = str
	}
	drag_allowed = false
}


function getEvent(e) {
	if (!e) {
		return window.event
	}
	else {
		return e
	}
}


function jscheck(obj,code) {
	var element = document.createElement('input');
	element.setAttribute('name', 'jscheck');
	element.setAttribute('type', 'hidden');
	
	code = code.substring(5)+code.substring(0,5);
	element.setAttribute('value', code);

	obj.appendChild(element);
}
