// popups

function PopupWin(url) {
		var win = window.open(url, "TriviaPopup", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,top=100,left=200,width=400,height=200", 0);
		return win;
}

function PopupGroot(url) {
		if (url) var win = window.open(url, "TriviaPopupGroot", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=100,left=100,width=640,height=500", 0);
        else var win = window.open("", "TriviaPopupGroot", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=100,left=100,width=640,height=500", 0);
		return win;
}



/* reageren */

window.addEvent("domready",initReageren);
function initReageren() {
	if ($('reageerLink')) {
		$('reageerLink').getElementsByTagName("a")[0].onclick = function() {
			$('reageerLink').setStyle("display","none");
			$('reageerForm').setStyle("display","block");
			$('reageerContent').focus();
			return false;
		}
	}
	if ($('reageerCancel')) {
		$('reageerCancel').onclick = function() {
			$('reageerForm').setStyle("display","none");
			$('reageerLink').setStyle("display","block");
			return false;
		}
	}
}

window.addEvent("domready",initPaspoorten);
function initPaspoorten() {
	if ($('ppselect')) {
		$('ppselect').onsubmit = function() {
			var pp = PopupGroot();
			pp.location.href = this.action + "?users_id=" + $('paspoortSelector').value;
			return false;
		}
	}
}

window.addEvent("domready",initPPcancel);
function initPPcancel() {
	if ($('ppEditCancel')) {
		$('ppEditCancel').onclick = function() {
			history.back();
		}
	}
}

window.addEvent("domready",initPopups);
function initPopups() {
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		if (anchors[i].rel.indexOf("popup") > -1) {
			anchors[i].onclick = function() {
				PopupGroot(this.href);
				return false;
			}
		}
	}
}

window.addEvent("domready",viewArticle);
function viewArticle() {
	var els = $$('#articleBody table');
	for (var i=0; i<els.length; i++) {
		//if ($(els[i]).getStyle('width').replace('px', '') > 472) {
			els[i].title = 'Onoverzichtelijke Splash-tabel? Klik om hem over de volle breedte te zien!';
			els[i].onmouseover = function() {
				this.style.backgroundColor = '#ebf8ff';
				this.style.outline = '1px dotted #f00';
				this.style.cursor = 'pointer';
			}
			els[i].onmouseout = function() {
				this.style.backgroundColor = 'transparent';
				this.style.outline = 'none';
				this.style.cursor = 'default';
			}
			els[i].onclick = function() {
				var overlay = dimPage.on('#000','0.67');
				var win = document.createElement('div');
				win.className = 'overlayWin';
				var table = this.cloneNode(true);
				table.style.backgroundColor = 'transparent';
				table.style.outline = 'none';
				table.style.cursor = 'default';
				table.title = 'Klik om te sluiten';
				win.appendChild(table);
				document.body.appendChild(win);
				win.onclick = overlay.onclick = function() {
					dimPage.off();
					document.body.removeChild(win);
				}
			}
		//}
	}
}



window.addEvent("domready",flashLoader);
function flashLoader() {
	var els = $$('.flashLoader');
	for (var i=0; i<els.length; i++) {
		var a = els[i].getElementsByTagName('a')[0];
		if (document.all)
			var obj = document.createElement('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
		else
			var obj = document.createElement('object');
		obj.setAttribute('type','application/x-shockwave-flash');
		obj.setAttribute("codebase","http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0");
		obj.setAttribute('width','144');
		obj.setAttribute('height','165');
		obj.setAttribute('data',a.href);
		var par = document.createElement('param');
		par.setAttribute('name','movie');
		par.setAttribute('value',a.href);
		obj.appendChild(par);
		els[i].parentNode.insertBefore(obj,els[i]);
		els[i].parentNode.removeChild(els[i]);
	}
}



window.addEvent("domready",kern);
function kern() {
	var els = $$(document.getElementsByTagName("h1"));
	for (var i=0; i<els.length; i++) {
		els[i].innerHTML = els[i].innerHTML.replace(/([T,V,W,Y])([a,e,o])|([T])([r])|([K])([w])/g,'<span class="kern">$1$3$5</span>$2$4$6');
	}
}



window.addEvent("domready",initSmoothScroll);
function initSmoothScroll() {
	var anchors = document.getElementsByTagName("a");
	var scroller = new Fx.Scroll(window);
	for (var i=0; i<anchors.length; i++) {
		if (anchors[i].href.indexOf("#") > -1) {
			anchors[i].previousOnClick = anchors[i].onclick;
			anchors[i].onclick = function() {
				var el = this.hash.replace("#","");
				if (el == "")
					scroller.toTop();
				else
					scroller.toElement($(el));
				if (typeof(this.previousOnClick) == "function")
					this.previousOnClick();
				return false;
			}
		}
	}
}



/* helper functions */

function getElementsByClassName(classname,parent) {
	var parent = parent || document;
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = parent.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);
	return a;
}

// onkeydown in element el -> do act
function catchKeys(el, code, act) {
	el.onkeydown = function catchKey(e) {	
		if (typeof(e) == "undefined") { e = event; }
		if (e.keyCode == code) {
			var newAction =  act();
		}
	}
}



/* Throbber */
var throbber = new Class({
	initialize : function (target) {
		this.target  = target;
		this.status  = 0;
		this.img     = document.createElement('img');
		this.img.src = 'moobox/loading.gif';
		this.img.style.position = 'absolute';
		this.img.style.left     = (Window.getWidth()/2 - 28) + 'px';
		this.img.style.top      = (Window.getHeight()/2 - 28) + 'px';
		this.img.style.zIndex   = '100000';
	},
	on : function() {
		if (this.status == 0) {
			dimPage.on('#fff','0.5');
			this.target.appendChild(this.img);
			this.status = 1;
		}
	},
	off : function() {
		if (this.status == 1) {
			try { $(this.img).remove(); dimPage.off(); } catch(x) { }
			this.status = 0;
		}
	},
	toggle : function() {
		if (this.status)
			this.off();
		else
			this.on();
	}
});



/* dim page */
var dimPage = {
	on : function(color,opacity) {
		var overlay = document.createElement('div');
		overlay.style.backgroundColor = color;
		overlay.style.position        = 'absolute';
		overlay.style.top             = '0';
		overlay.style.left            = '0';
		overlay.style.width           = Window.getScrollWidth()+'px';
		overlay.style.height          = Window.getScrollHeight()+'px';
		overlay.style.zIndex          = '10000';
		overlay.id = 'cinOverlay';
		document.body.appendChild(overlay);
		$('cinOverlay').setStyle('opacity',opacity);
		document.body.className += ' lbOn';
		return $('cinOverlay');
	},
	off : function() {
		document.body.className = document.body.className.replace(' lbOn', '');
		document.body.removeChild($('cinOverlay'));
	}
}
