/* Init the script */

window.onload = function() {
 //extLinks();
 setHuidige();
 if (document.getElementById && document.documentElement) {
  fixBreadcrumb();
  zebraTables();
  initToggle();
  pollRows();
  fixFunctions();
  //createTopButton();
  //setInterval("adjustTopButton()", 100);
 }
}


function extLinks() { // tweaked by Low
 if (!document.getElementsByTagName) return;

 // get current domain
 var tmp = location.href.replace('http://','');
 var tmpar = tmp.split('/');
 var domain = tmpar[0];

 // get all links on page
 var anchors = document.getElementsByTagName("a");

 // loop thru links
 for (var i=0; i<anchors.length; i++) {

  // get single link
  var anchor = anchors[i];

  // if the link is # or a mailto link, skip it
  if (anchor.href == '#' || anchor.href.indexOf('mailto:') != -1) {continue;}

  // if the link does not contain our domain, set the rel to external
  if (anchor.href.indexOf(domain) == -1 && anchor.href.indexOf('stijlgids.overheid.nl') == -1 && anchor.href.indexOf('internetstijlgids.nl') == -1) {
    anchor.setAttribute('rel', 'external');
  }

  // for external links, add exit marker
  if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
   if (anchor.title.length) {
    anchor.title = "Externe site: " + anchor.title;
   }
   mark = document.createElement("img");
   mark.src = "/images/exit.png";
   mark.alt = " (externe site)";
   mark.width = "11";
   mark.height = "10";
   mark.className = "exit";

   anchor.appendChild(mark);
  }
 }
}



// Hilite links function by Low
function setHuidige() {
  if (!document.getElementsByTagName || !document.getElementById) return;
  if (!document.getElementById('subinhoud')) return;

  // get menu div
  var menus = document.getElementById('subinhoud');

  // get links in menu div
  var A = menus.getElementsByTagName('a');

  // loop thru links, compare href to location.href
  for (var i = 0; i < A.length; i++) {
    if (location.href.indexOf(A[i].href) > -1)
      A[i].className = A[i].className + " huidige";
  }
}

function createTopButton () {

	t = document.getElementById("blok");
	div = document.createElement("div");
	div.id = "naar-boven";
	t.appendChild(div);

	for(u = 0; u < 2; u++) {
		a = document.createElement("a");
		a.className = "off";
		a.href = "#top";
		a.title = "Naar boven";
		//a.onclick = "return scrollToTop();"
		if(u == 0) a.id = "top-links";
		else a.id = "top-rechts";
		div.appendChild(a);

		img = document.createElement("img");
		img.alt = "Naar boven";
		img.src = "/images/top.png";
		img.width = "17";
		img.height = "17";
		a.appendChild(img);
	}

}

function adjustTopButton() {
						
	tl = document.getElementById("top-links");
	tr = document.getElementById("top-rechts");
	s = document.body.scrollTop || (document.documentElement && document.documentElement.scrollTop);

	if(s > 0) {

		tl.className = tr.className = "on"; 
		// determine support for position: fixed and apply hack if necessary
		if(document.getElementById("naar-boven").currentStyle) 
			pos = document.getElementById("naar-boven").getElementsByTagName("a")[1].currentStyle.position;
		else pos = 0;
		if(pos == "absolute") {

			docHeight = document.getElementById("top").clientHeight;
			winHeight = document.documentElement.clientHeight;

			p = s + winHeight - 113 + "px";
			tl.style.top = tr.style.top = p;

		}
	} else tl.className = tr.className = "off";

}

// adds class="eerste" to first breadcrumb item
function fixBreadcrumb() {
 if (!(p = document.getElementById('pad'))) return;
 var li = p.getElementsByTagName('li');
 try { li[0].className = 'eerste'; } catch(x) {}}

function zebraTables() {
 if (!(p = document.getElementById('blok'))) return;
 var tbody = p.getElementsByTagName('tbody');
 for (var i = 0; i < tbody.length; i++) {
  var trs = tbody[i].getElementsByTagName('tr');
  for (var j = 0; j < trs.length; j++) {
   trs[j].className = (j % 2) ? 'even' : '';
  }
 }
}

function showImage(img,w,h) {
 window.open(img);
 return false;
}

// fix functies
function fixFunctions() {
 if (!(f = document.getElementById('functies'))) return;
 var LI = f.getElementsByTagName('li');
 var last = LI[LI.length-1];
 last.className = 'laatste';
}

// toggle links
function initToggle() {
 var A = document.getElementsByTagName('a');
 for (var i = 0; i < A.length; i++) {
  if (!(A[i].className.match(/toggle/g))) continue;
  A[i].onclick = function() {
   var hash = this.href.split('#');
   return toggle(hash[1]);
  }
 }
 var C = document.getElementsByTagName('caption');
 for (var i = 0; i < C.length; i++) {
  if (!(C[i].className.match(/toggle/g))) continue;
  C[i].onclick = function() {
   var p = this.parentNode;
   p.className = (p.className.match(/open/g)) ? p.className.replace(/open/g,'dicht') : p.className.replace(/dicht/g,'open');
  }
 }
}

function toggle(id) {
 if (!(el = document.getElementById(id))) return;
 el.style.display = '';
 if (el.className.match(/hidden/g)) {
  el.className = el.className.replace(/hidden/g, '');
 } else {
  el.className += ' hidden';
 }
 return false;
}

function pollRows() {
 if (!(but = document.getElementById('addrow'))) return;
 
 but.onclick = function() {
  if (!(ol = document.getElementById('poll_answers'))) return;
  var items = ol.getElementsByTagName('li');
  var num    = items.length;
  var newrow = items[(num - 1)].cloneNode(true);
  newrow.firstChild.name = 'option['+ num +']';
  newrow.firstChild.id = 'answer'+ (num + 1);
  ol.appendChild(newrow);
 }
}


// Toegevoegd door Raph de Rooij: anti-spambot maatregel

document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
	}
return retnode;
};

function createEmailAddress() {
	var a = ' [apenstaartje] ';	// put the @-replacement here
	var monkeytail = document.getElementsByClassName('monkeytail');
	for (var i = 0; i < monkeytail.length; i++) {
		var b = monkeytail[i];
			function parseText(b) {
				var theText = '';
				if (b.normalize) b.normalize();
				for (var c, j = 0; c = b.childNodes[j]; j++)
				if (c.nodeType == 3) theText += c.nodeValue;
				return theText;
				};
		var c = parseText(b);
		var d = new Array();
			d = c.split(a);
		var e = (d[0] + '@' + d[1]);
		var f = b.parentNode;
		var g = document.createElement('a');
		if (d.length > 1) {
			f.appendChild(g);
			g.setAttribute('href','mailto:' + e);
			g.setAttribute('title','Stuur een bericht naar ' + e);
			g.appendChild(document.createTextNode(e));
			f.replaceChild(g,b);
			}
		}
	var explanation = document.getElementsByClassName('explanation');
	for (var k = 0; k < explanation.length; k++) {
		var h = explanation[k];
			monkeytail.removeChild(h);
		}
	};

// Piggy-back functions onto onLoad event...

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(createEmailAddress);