/*
Generic functions
Copyright (c) 2003-2009 Ylab, www.ylab.nl
*/

//generic event handling
var debugging = true;
var loadFunctions = new Array();
function addLoadFunction(f){loadFunctions[loadFunctions.length] = new Function(f);}
window.onload   = function(){for(var i=0; i<loadFunctions.length; i++){loadFunctions[i]();}}

var isOpera = (navigator.userAgent.indexOf("Opera") > -1);
var isNav = ((!isOpera) && (navigator.appName == "Netscape"));
var isIE  = ((!isOpera) && (navigator.appName.indexOf("Explorer") > -1 ));
var versionMajor = parseInt(navigator.appVersion);
if(isOpera){ var versionMinor = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("Opera")+6));}
else if(isIE){ var versionMinor = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5));}
else{ var versionMinor = parseFloat(navigator.appVersion);}
var hasFirebug = (window.console && console.firebug);

//display mailto link
function printMail(username, linktext){
	var hostLevels = location.host.split('.');
	var len = hostLevels.length;

	username = username.toLowerCase() + '@' + hostLevels[len-2] + '.' + hostLevels[len-1];
	if(!linktext){
		linktext = username;
	}
	document.write(linktext.link("mailto:" + username));
}

//Convert id into object
function id2object(el){
	if(typeof(el)=="string"){el = document.getElementById(el);}
	return el;
}


function openWindow(url, name, width, height, resizable, toolbar, menubar){
	var args = "";
	if(width){
		width = Math.min(width, screen.availWidth);
		args += "width=" + width + ",left=" + (screen.width - width)/2 + ",";
	}
	if(height){
		height = Math.min(height, screen.availHeight);
		args += "height=" + height + ",top=" + (screen.height - height)/2 + ",";
	}
	if(resizable){args += "resizable,scrollbars,";}
	if(toolbar){args += "toolbar,";}
	if(menubar){args += "menubar,";}
	var newwin = window.open(url, name, args);
	if(!newwin){
		alert("Er kan geen nieuw venster geopend worden.\nEr is waarschijnlijk een 'popup-killer' actief.");
		return false;
	}
	if(newwin.focus){newwin.focus();}
	return newwin;
}

function openModalWindow(url, width, height, resizable, toolbar, menubar){
	window.modalwindow = openWindow(url, "modalwindow", width, height, resizable, toolbar, menubar);
	if(window.modalwindow){
		try{window.modalwindow.focus();}
		catch(ex){window.modalwindow = false; window.onfocus = null;}
	}
}

function closeModalWindow(refresh){
	if(window.opener){
		window.opener.modalwindow = false;
		window.onfocus = null;
		if(refresh){window.opener.location.reload();}
		window.opener.focus();
	}
	window.close();
}

String.prototype.searchToArray = function(){
	var i, pair, params = {}, q = this.substring(1).split('&');

	for(i = 0; i < q.length; i++){
		pair = q[i].split('=');
		params[pair[0]] = pair[1];
	}
	return params;
}

//DEBUGGING
function devAlert(){
	if(!debugging){return;}
	var code = "Deze functie is nog niet geïmplementeerd.\n";
	for(var i=0; i < arguments.length; i++){
		code += arguments[i] + "\n";
	}
	alert(code);
}

function debugAlert(){
	if(!debugging){return;}
	var code = "";
	for(var i=0; i < arguments.length; i++){
		code += arguments[i] + "\n";
	}
	code += '\nKlik op Annuleren om verdere meldingen te onderdrukken.';
	debugging = confirm(code);
}

function debugConfirm(){
	if(!debugging){return;}
	var code = "";
	for(var i=0; i < arguments.length; i++){
		code += arguments[i] + "\n";
	}
	return confirm(code);
}

try{
	jQuery(document).ready(function($){
		$('.imgHover').each(function(){
			//add -hover by default
			var extpos = this.src.lastIndexOf('.');
			this.srcHover = this.src.substring(0,extpos) + '-hover' + this.src.substring(extpos);
			this.srcNormal = this.src;
		}).mouseover(function(){
			this.src = this.srcHover;
		}).mouseout(function(){
			this.src = this.srcNormal;
		});
		$('.channel-home #mainmenu a').click(function(event){
			event.preventDefault();
			var $a = $(this);
			//keep hover state
			$('img', $a)[0].srcNormal = $('img', $a)[0].srcHover;
			//hide photo
			$('#slideshow').css({border:0}).animate({height:'0px'}, 'normal', function(){
				location.href = $a[0].href;
			});
		});
	});
}catch(exception){}
