
function init_page()
{
	var side_box = $('side_link');
	var headers = $$('#dezon_content h2, #dezon_content h3');
		
	elektropost();
	
	if(headers.size() == 0) {
		$('side_content').hide();
	}
	else {		
		side_box.update('');
		headers.each(function(h, index) {
			
			if (index > 0) {
				h.insert ({'before': '<div class="top_link"><a href="#container">top ▲</a></div>'});
			}
			
			var header_id = null;
			
			if (h.id) {
				header_id = h.id;
			}
			else {
				header_id = "side_link_" + index;
				h.id = header_id;
			}			
			side_box.insert('<li><a href="#'+header_id+'">' + h.innerHTML + '</a></li>');			
		});
	}
	
	$('print_button').observe('click', do_print);	
	$('logo_box').observe('click', function() { location.href = "/" });	
	
	format_anchors();
	
	
	init_enquete();
	
	if(typeof meerkeuzeopenantwoordforum != 'undefined') {
		init_meerkeuzeopen();				
	}
	
	activate_facebook();
}

function format_anchors()
{	
	var anchors = $$('#dezon_content a');

	anchors.each(function(a) {
		var attr = Element.readAttribute(a, 'href');		
		if(attr != null) {
			attr = attr.toLowerCase()
			if(attr.endsWith(".pdf")) {
				a.addClassName('file');
				a.addClassName('pdf_file');
			}
			else if (attr.endsWith(".xls")){
				a.addClassName('file');
				a.addClassName('xls_file');
			}
			else if (attr.endsWith(".doc")){
				a.addClassName('file');
				a.addClassName('doc_file');
			}
		}
	});	
}

function elektropost()
{
	var a = new Array('&#x65;&#x6e;&#x64;&#x65;&#x7a;&#x6f;','&#x76;&#x65;&#x72;&#x6c;&#x6f;&#x73;','&#109;&#97;&#105;&#108;&#116;&#111;','&#x3a;&#x69;&#x6e;&#x66;&#x6f;&#x40;','&#x6b;&#x75;&#x6e;&#x64;&#x69;&#x67;','&#110;&#46;&#110;&#108;');
	var elektropost = $$('.elektropost');
	
	elektropost.each(function(p) {
		p.update("<a href='"+a[2]+a[3]+a[1]+a[4]+a[0]+a[5]+"'>"+"&#105;&#110;&#102;&#111;&#64;&#118;&#101;&#114;&#108;&#111;&#115;&#107;&#117;&#110;&#100;&#105;&#103;&#101;&#110;&#100;&#101;&#122;&#111;&#110;&#46;&#110;&#108;"+"<"+"/a>");
	});
}

function init_meerkeuzeopen()
{
	var form = $('enquete');
	var choices = form['keuze'];
	
	for(var i=0; i < choices.length-1; i++) {
		choices[i].observe('click', function() { text_disable() });			
	}
	
	choices[i].observe('click', function() { text_enable() });
	if(choices[i].checked){
		text_enable();
	} 
	else {
		text_disable();
	}
}

function text_disable() {	
	var text = $('id_tekst');
	text.value = "";
	text.disable();
	text.setStyle({
		  backgroundColor: '#EEE'
	});	
}

function text_enable() {
	var text = $('id_tekst');
	text.enable();
	text.setStyle({
		  backgroundColor: ''
	});
	text.focus();
}

function init_enquete() {
	if($('enquete')) {
		$('submit_button').observe('click', post_form);
		if ($('prev_button')) {
			$('prev_button').observe('click', prev_question);
		}
		if ($('unanswered_button')) {
			$('unanswered_button').observe('click', unanswered_question);
		}
		$('enquete').reset();
	}
}

function unanswered_question() {
	$('unanswered_button').update("Laden...")
	$('id_jumplast').value = 'True'
	$('enquete').action = $('unanswered_value').value;
	$('enquete').request({
	  onComplete: function(transport){ 
		  $('enquete_div').update(transport.responseText);
		  init_enquete();
	  }
	})
}

function prev_question() {
	var url = $('prev_value').value;
	$('prev_button').update("Laden...")
	new Ajax.Request(url, {
		  method: 'get',
		  onComplete: function(transport) {
			  $('enquete_div').update(transport.responseText);
			  init_enquete();
		  }
		});
}

function post_form() {
	$('submit_button').value = "Laden...";
	$('enquete').request({
	  onComplete: function(transport){ 
		  $('enquete_div').update(transport.responseText);
		  init_enquete();
	  }
	})
}

function go_home() {
	location.href = "/";	
}

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

function do_print() {
	
	host = getBaseURL()
	
	if($('dezon_content'))
	{
		text = $('dezon_content').innerHTML;
	}
	else
	{
		alert("Container niet gevonden");
	}
	text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head><title>'+window.document.title+"</title><base href='"+host+"' /><link href='/static/layout/print.css' rel='stylesheet' /></head><body>"+text+"<br/><br/><center><a href='javascript: window.print()'>Pagina printen</a> &nbsp;&nbsp; <a href='javascript: window.close()'>Venster sluiten</a></center></body></html>";
	print_win = window.open('','','width=700,height=600,scrollbars=yes,resizable=yes,titlebar=0');
	print_win.document.write(text);
	print_win.document.close();
	print_win.print();
}

function activate_facebook()
{	
	if($('fb-proto-like')) {
		$('fb-proto-like').observe('click', function() { 
			$('fb-proto-like').up().update("");
			update_facebook();
		});
	}
}

function update_facebook(e){
	(function(d, s, id) {
		  var js, fjs = d.getElementsByTagName(s)[0];
		  if (d.getElementById(id)) {return;}
		  js = d.createElement(s); js.id = id;
		  js.src = "//connect.facebook.net/nl_NL/all.js#xfbml=1";		  
		  fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));	
}

document.observe("dom:loaded", init_page);

