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

function checkActiveLinks(base) {
	if(typeof(base) == 'undefined' && typeof(document.path) != 'undefined') base = document.path;

	linksArray = $$('a');

	var url = window.location;

	for(i = 0; i < linksArray.length; ++i ){
		var a = linksArray[i];

		url = new String(window.location);

		var link = a.href
		if(link.substring(link.length-4, link.length) == '.ait') link = link.substring(0, link.length-4)

		if(link == url || (link != base && link != (base + '/') && link == url.substring(0, link.length))) {
			if(link == url || (link + '/') == url || link == (url + '/')) {
				a.className += ' active_exact';
			}
			if(url.length <= link.length || (url.length > link.length && (url.substring(link.length, link.length+1) == '/' || url.substring(link.length, link.length+4) == '.ait'))) {
				a.className += ' active';
			}
		}
	}
}

function positionSubMenus() {
	var menu_items = $$('a[id^=menu_]');
	for(var i = 0; i < menu_items.length; ++i) {
		var menu = menu_items[i];
		var submenu = $('submenu_'+ menu_items[i].id.substring(5));

		if(submenu != null && menu != null) {
			submenu.style.top = menu.offsetTop + 'px';
			submenu.style.zIndex = 100;
			
			menu.addEvent('mouseover', showSubMenu);
			submenu.addEvent('mouseover', showSubMenu);
			menu.addEvent('mouseout', hideSubMenu);
			submenu.addEvent('mouseout', hideSubMenu);
		}
	}
}

function showSubMenu(e) {
	var submenu = $('submenu_' + getEventId(e));
	if(submenu != null && typeof(submenu) == 'object') {
		submenu.style.display = 'block';

		// save the menu only for ie.. grmbl
		document.last_submenu = submenu;
	}
}

function hideSubMenu(e) {
	var submenu = $('submenu_' + getEventId(e));

	// load the menu only for ie.. grmbl
	if(submenu == null || typeof(submenu) != 'object') {
		submenu = document.last_submenu;
	}

	if(submenu != null && typeof(submenu) == 'object') {
		submenu.style.display = 'none';
	}
}

function getEventId(e) {
	var obj = e.target;
	while(obj.id.substring(0, 5) != 'menu_' && obj.id.substring(0, 8) != 'submenu_') obj = obj.parentNode;

	var offset = 0;
	while((i = obj.id.indexOf('_', offset)) !== -1) offset = i + 1;

	return parseInt(obj.id.substr(offset));
}

function preLoad(images) {
	document.preLoad = new Array();

	if (document.images) {
		for(i = 0; i < images.length; ++i) {
			document.preLoad[i] = new Image();
			document.preLoad[i].src = images[i];
		}
	}
}

function mouseOver(element, image) {
	if(typeof(element.src_out) == 'undefined' || element.src_out == null) {
		element.src_out = element.src;
		if(typeof(image) != 'undefined') {
			element.src = image;
		} else {
			element.src = element.src.substring(0, element.src.length - 4) + '_up' + element.src.substring(element.src.length - 4, element.src.length);
		}
	}
}

function mouseOut(element) {
	if(typeof(element.src_out) != 'undefined' && element.src_out != null) {
		element.src = element.src_out;
		element.src_out = null;
	}
}

/* POPUP */

function showMessage(title, innerHTML, buttonText, popupFunction) {
	document.getElementById("popup_bg").style.display = "block";
	document.getElementById("popup_bg").style.height = getDocHeight() + "px";

	document.getElementById("popup").style.display = "block";
	if(BrowserDetect.browser == "Explorer") {
		document.getElementById("popup").style.top = (document.body.scrollTop + (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight)) + 'px';
	} else {
		document.getElementById("popup").style.top = (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight) + 'px';
	}

	document.getElementById("popup_title").innerHTML = title;
	document.getElementById("popup_text").innerHTML = innerHTML;

	document.getElementById("popup_b1").innerHTML = buttonText;
	document.popupFunction = popupFunction;
}

function hidePopup() {
	document.getElementById("popup").style.display = "none";
	document.getElementById("popup_bg").style.display = "none";
}

function positionPopup() {
	if(BrowserDetect.browser == "Explorer") {
		document.getElementById("popup").style.top = (document.body.scrollTop + (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight)) + 'px';
	}
}

function popupButton(id) {
	eval(document.popupFunction);
}

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

function showMailAFriend() {
	var html = '\
	<table>\
		<tr><td id="mailafriend_to_label" class="label">Mailen aan:</td><td class="input"><input id="mailafriend_to" type="text"/></td></tr>\
		<tr><td id="mailafriend_from_label" class="label">Gestuurd door:</td><td class="input"><input id="mailafriend_from" type="text"/></td></tr>\
		<tr><td id="mailafriend_message_label" class="label">Bericht:</td><td class="input"><textarea id="mailafriend_message"></textarea></td></tr>\
	</table>';

	showMessage('Mail a Friend', html, 'Verzenden', 'sendMailAFriend();');
}

function sendMailAFriend() {
	var errors = false;
	if(trim($('mailafriend_to').value).search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		$('mailafriend_to_label').className = 'label error';
		errors = true;
	}
	if(trim($('mailafriend_from').value).search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		$('mailafriend_from_label').className = 'label error';
		errors = true;
	}

	if(!errors) {
		var maf_to = trim($('mailafriend_to').value);
		var maf_from = trim($('mailafriend_from').value);
		var maf_message = trim($('mailafriend_message').value);
		
		var maf_url = window.location.href;
		var maf_title = $$('title')[0].text;

		var jsonRequest = new Request.JSON({url: document.path + '/mailafriend', onSuccess: function(result){
				if(result.success) {
					hidePopup();
					showMessage('Mail a Friend', 'Bericht succesvol verzonden.', 'Sluiten', 'hidePopup();');
				} else {
					hidePopup();
					showMessage('Mail a Friend', 'Bericht kon niet worden verzonden. Probeer het (later) nog eens.', 'Sluiten', 'hidePopup();');
				}
		}}).post({'maf_to': maf_to, 'maf_from': maf_from, 'maf_message': maf_message, 'maf_url': maf_url, 'maf_title': maf_title});
	}
}

function trim(value) {
	value = value.replace(/^\s+/,'');
	value = value.replace(/\s+$/,'');
	return value;
}


/* BROWSER DETECT */

function checkVersion() {
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6) {
		var old_version = document.createElement('div');
		old_version.setAttribute('id', 'old_version');
		old_version.setAttribute('class', 'old_version');
		old_version.className = 'old_version';

		old_version.innerHTML = '<div class="info"></div><span>U maakt gebruik van een verouderde browser. Voor een optimale weergave raden wij u aan uw browser te updaten. <a class=\"icon_firefox\" target="_blank" href="http://www.mozilla.com/firefox" title="Download Firefox"></a><a class=\"icon_ie\" target="_blank" href="http://www.microsoft.com/netherlands/windows/internet-explorer/" title="Download Internet Explorer"></a></span>';

		document.body.insertBefore(old_version,document.body.firstChild);
	}
}
addLoadEvent(checkVersion);

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
		|| this.searchVersion(navigator.appVersion)
		|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
	{
		string: navigator.userAgent,
		subString: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari"
	},
	{
		prop: window.opera,
		identity: "Opera"
	},
	{
		string: navigator.vendor,
		subString: "iCab",
		identity: "iCab"
	},
	{
		string: navigator.vendor,
		subString: "KDE",
		identity: "Konqueror"
	},
	{
		string: navigator.userAgent,
		subString: "Firefox",
		identity: "Firefox"
	},
	{
		string: navigator.vendor,
		subString: "Camino",
		identity: "Camino"
	},
	{	// for newer Netscapes (6+)
		string: navigator.userAgent,
		subString: "Netscape",
		identity: "Netscape"
	},
	{
		string: navigator.userAgent,
		subString: "MSIE",
		identity: "Explorer",
		versionSearch: "MSIE"
	},
	{
		string: navigator.userAgent,
		subString: "Gecko",
		identity: "Mozilla",
		versionSearch: "rv"
	},
	{ 	// for older Netscapes (4-)
		string: navigator.userAgent,
		subString: "Mozilla",
		identity: "Netscape",
		versionSearch: "Mozilla"
	}
	],
	dataOS : [
	{
		string: navigator.platform,
		subString: "Win",
		identity: "Windows"
	},
	{
		string: navigator.platform,
		subString: "Mac",
		identity: "Mac"
	},
	{
		string: navigator.platform,
		subString: "Linux",
		identity: "Linux"
	}
	]

};
BrowserDetect.init();
