/* 
	TEKSTGROOTTE
	Knoppen en functionaliteit worden automatisch gegenereerd in
	het element met id="tekstgrootte"
*/

var paginaTaal; // welke taal gebruikt de pagina? Gezet in initializeAll

var setFocus = {
    init: function()
    {
     var adresboekZoekVeld = document.getElementById("addressBook_freeSearch");
     if (adresboekZoekVeld)
     {
      adresboekZoekVeld.focus();
     }
    }
}



var printLink = {
	tekst: {
		nl: "Print",
		en: "Print",
                                fr: "Imprimer"
	},
	init: function () {
		var writeroot = document.getElementById('printlink');
		if (!writeroot) return;
		var link = document.createElement('a');
		link.href = '#';
		link.appendChild(document.createTextNode(this.tekst[paginaTaal]));
		link.onclick = function () {
			window.print();
			return false;
		}
		writeroot.appendChild(link);
	}
}

var popups = {
	standaardBreedte: 300,
	standaardHoogte: 150,
	waarschuwingTekst: {
		nl: '(opent in popup)',
		en: '(opens in popup)'
	},
	init: function () {
		this.initPopups(document);
	},
	initPopups: function (obj) {
		var x = obj.getElementsByTagName('a');
		for (var i=0;i<x.length;i++) {
			if (x[i].getAttribute('rel') && x[i].getAttribute('rel').indexOf('popup') != -1) {
				x[i].firstChild.nodeValue += ' ' + this.waarschuwingTekst[paginaTaal];
				x[i].onclick = function(){popups.openPopup(this); return false};
			}
		}
	},
	openPopup: function (link) {
		var breedte = this.standaardBreedte;
		var hoogte = this.standaardHoogte;
		var data = link.getAttribute('rel').split(';');
		if (data.length == 3) {
			breedte = data[1];
			hoogte = data[2];
		}
		window.open(link.href,'popup','width='+breedte+',height='+hoogte+',resizable=yes');
	}
}

var pollresultaten = {
	factor: undefined,
	init: function () {
		var writeroot = document.getElementById('peilingResultaat');
		if (!writeroot) return;
		var items = writeroot.getElementsByTagName('li');
		for (var i=0;i<items.length;i++) {
			var spans = items[i].getElementsByTagName('span');
			if (!spans.length) continue;
			var staaf = spans[0];
			if (!this.factor)
				this.factor = staaf.offsetWidth/100;
			var percentage = parseInt(spans[1].firstChild.nodeValue);
			spans[1].style.display = 'none';
			var innerSpan = document.createElement('span');
			innerSpan.style.width = parseInt(percentage * this.factor) + 'px';
			staaf.appendChild(innerSpan);
		}
	}
}

var inhoudRecht = {

	/* Patch voor footer-door-content-probleem , NFI Internet specifiek*/

	init: function () {
		var subnav = document.getElementById('subnavigatie');
		var nav = 0;
		if (subnav) {
			nav = subnav.offsetHeight;	/* hoogte subnavigatie meten */
		}
		var inhoud = document.getElementById('inhoud');
		if (!inhoud) {
			inhoud = document.getElementById('standaardpagina_schil_contentPlaceholder_inhoudPlaceholder_inhoud');
		}
		var content = 0;
		if (inhoud) {
			inhoud.style.paddingTop = 0; 		/* padding-top content verwijderen; die compliceert de zaak */
			var content = inhoud.offsetHeight; 	/* nu hoogte content meten */
			inhoud.style.paddingTop = ''; 		/* padding-top herstellen */
		}
		var newHoogte = content;
		var seccontent = 0;
		if (document.getElementById('secundaireinhoud')) {
			seccontent = document.getElementById('secundaireinhoud').offsetHeight;
		}
		if (seccontent>content) {
			newHoogte=seccontent;
			document.getElementById('container').style.height = seccontent + 'px';
		}
		if (nav > newHoogte) {
			newHoogte = nav;
		}
		if (newHoogte > 0) {
			document.getElementById('container').style.height = newHoogte+55 + 'px';
		}
	}
}

/* INITIALISATIE 
         aangeroepen door DOMContentReady
         behalve inhoudRecht; deze moet wachten totdat de plaatjes 
zijn geladen. Derhalve
         wordt hij onload aangeroepen (addEventSimple onderaan)
*/

/* Initialiseer de functie door van var inits te maken: */
var inits = [
	'printLink',
	'popups',
	'pollresultaten',
                'setFocus'
];




function initializeAll() {
	paginaTaal = document.documentElement.lang || 'nl';
	for (var i=0;i<inits.length;i++) {
		if (window[inits[i]])
			window[inits[i]].init();
	}
}

/* UTILITIES */

function listify(dataArray) {
	var x = document.createElement('ul');
	for (var i=0;i<dataArray.length;i++) {
		var y = document.createElement('li');
		y.appendChild(dataArray[i]);
		x.appendChild(y);
	}
	return x;
}

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			// alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")},
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++)
	{
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

// push and shift for IE5

function Array_push() {
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++) {
		this[this.length] = arguments[A_p]
	}
	return this.length
}

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = Array_push
}

function Array_shift() {
	var A_s = 0
	var response = this[0]
	for (A_s = 0; A_s < this.length-1; A_s++) {
		this[A_s] = this[A_s + 1]
	}
	this.length--
	return response
}

if (typeof Array.prototype.shift == "undefined") {
	Array.prototype.shift = Array_shift
}


/* COOKIES */

var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	},
	eraseAll: function () {
		for (var i in this) {
			if (typeof this[i] == 'function') continue;
			this.erase(i);
		}
	}
};
Cookies.init();

// inhoudRecht moet onload worden aangeroepen, en niet ondomcontentready

function addEventSimple(obj,evt,fn) {
         if (obj.addEventListener)
                 obj.addEventListener(evt,fn,false);
         else if (obj.attachEvent)
                 obj.attachEvent('on'+evt,fn);
}
addEventSimple(window,'load',inhoudRecht.init);

/* DOMCONTENTREADY */

function ContentReady() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer) clearInterval(_timer);

	// do stuff
	initializeAll();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", ContentReady, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	var src='javascript:void(0)';
	if (window.location.protocol == "https:") {
		src='';
	}
	document.write("<script id=__ie_onload defer src='"+src+"'><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			ContentReady(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			ContentReady(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = ContentReady;