/*******
** vp **	
*******/
var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=640,height=500'; 

listen('load', window, function() { 
	mlisten('click', getElementsByClass('custom_popup','a'), event_popup ); 
}); 



function setBlur() {
	entries = document.getElementsByTagName('a');
	for (var i=0; i < entries.length; i++) {
		entries[i].onfocus = function() {this.blur()}
	}
}


function pagepreload() {
	document.getElementById('con').style.visibility = 'visible';
	if (document.getElementById('con_footer')) document.getElementById('con_footer').style.visibility = 'visible';
	if (document.getElementById('con_header')) document.getElementById('con_header').style.visibility = 'visible';
	document.getElementById('preloader').style.display = 'none';

}


var preloaded = new Array();

function setMOhandler() {
	var aEls = ['img','input']
	for(var j=0; j < aEls.length; j++){
		var entries = document.getElementsByTagName(aEls[j]);
		for (var i=0; i < entries.length; i++) {
			if (entries[i].className == 'MO') {
				var onImageSrc = entries[i].src.replace('_n.', '_o.');
				preloaded[preloaded.length] = new Image();
				preloaded[preloaded.length-1].src = onImageSrc;
				entries[i].onmouseover = function () {this.src = this.src.replace('_n.', '_o.')}
				entries[i].onmouseout = function () {this.src = this.src.replace('_o.', '_n.')}
			}
		}
	}
}


function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} 
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} 
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

var DEFAULTCONTENTHEIGHT = 0;

function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var con = document.getElementById('con').offsetHeight;
			var header = document.getElementById('con_header').offsetHeight;
			DEFAULTCONTENTHEIGHT = header + con;
			var footerElement = document.getElementById('con_footer');
			var footerHeight = footerElement.offsetHeight;
			if (windowHeight - (DEFAULTCONTENTHEIGHT + footerHeight) >= 0) {
				footerElement.style.position = 'absolute';
				var footerInnerElement = document.getElementById('footerContent');
				var footer = document.getElementById('footer');
				var test = windowHeight - (DEFAULTCONTENTHEIGHT + footerHeight);
				var hoogte = test + footerHeight;
				footerInnerElement.style.height = hoogte + 'px';
			}
			else {
				//footerElement.style.position = 'static';
				footerElement.style.position = 'absolute';
				footerElement.style.top = (DEFAULTCONTENTHEIGHT + 0) + 'px'; // het getal erbij is de padding van de 'con' container.
			}
		}
	}
}

function raw_popup(url, target, features) {
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function event_popup(e) {
    link_popup(e.currentTarget);
    e.preventDefault();
}

function event_popup_features(features) {
    return function(e) { link_popup(e.currentTarget, features); e.preventDefault() }
}

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

// FUNCTIONAL

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}

function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}


// DOM

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}

function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}


// DOM EVENTS

function listen(event, elem, func) {
    elem = getElem(elem);
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    else throw 'cannot add event listener';
}

function mlisten(event, elem_list, func) {
    map(elem_list, function(elem) { listen(event, elem, func) } );
}

function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    return this;
}


// MISC CLEANING-AFTER-MICROSOFT STUFF

function isUndefined(v) {
    var undef;
    return v===undef;
}
