/*
Generic functions
Copyright (c) 2003-2011 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 isIE  = ((!isOpera) && (navigator.appName.indexOf("Explorer") > -1 ));
var hasFirebug = (window.console && console.firebug);

//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;
}

function dec2hex(channel){
	var h = parseInt(channel).toString(16);
	return h.length == 1 ? '0' + h : h;
}
function rgb2hex(clr){
	match = clr.match(/rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)/);
	if(match != null){
		return '#' + dec2hex(match[1]) + dec2hex(match[2]) + dec2hex(match[3]);
	} else {
		return clr;
	}
}

function doIFR(clr){
	var font = { src: '/css/lane-narrow3.swf' };
	var sCSS = '.sIFR-root {color: ' + rgb2hex(clr) + '; }'
	sIFR.activate(font);

	try{;
		sIFR.replace(font, {wmode: 'transparent', selector: 'h1', css: sCSS});
		sIFR.replace(font, {wmode: 'transparent', selector: '.top3 h2', css: sCSS});
		sIFR.replace(font, {wmode: 'transparent', selector: '.themefont', css: sCSS});
	}catch(e){
	}
}


//DEBUGGING
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);
}

if(!!window.jQuery){
	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;
			});
		});
	});
}

