var minHeight = 582;	// Minimale hoogte van de website
var menuItems = 5;		// Het aantal hoofdmenuitems
var submenuItems = 5;	// Het aantal submenuitems

var oldheight = 0;  // Oude hoogte van de viewport, nodig om resize event goed te laten verlopen
var aImg = ['images/main_top.jpg', 'images/main_left.jpg', 'images/main_right.jpg', 'images/main_top_1.gif', 'images/main_top_2.gif'];
var preloader = null;

//window.addEvent('domready', init);
window.onload = init;
window.onresize = resizeScrollpane;
var initAdmin = $lambda(false);
var resizeMore = $lambda(false);

function init() {
	externalLinks();
	resizeScrollpane();
	preloader = new ImagePreloader(aImg, onPreload);
	if(!initAdmin())
		initMenu();
	
	$$('p.__button').each( function(item, index) {
		item.set('class', 'button_normal');
		item.setStyle('cursor', 'pointer');
		item.addEvents({
			'mouseenter': function() {
				item.set('class', 'button_active');
			},
			'mouseleave': function() {
				item.set('class', 'button_normal');
			}
		});
		item.getElement('a').set('class', 'button_link');
	});
}

function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
 var areas = document.getElementsByTagName("area"); 
 for (var i=0; i<areas.length; i++) { 
   var area = areas[i]; 
   if (area.getAttribute("href") && 
       area.getAttribute("rel") == "external") 
     area.target = "_blank"; 
 } 
} 

function onPreload(aImages, nImages)
{
	if ( nImages = aImg.length )
	{
		return;
	}
}
 
// Fade functie
var swapArray = new Array();
var swapFade = function(index) {
	if( $defined(swapArray[index]) ) {
		var element = swapArray[index];
		element.set('tween',{
			duration: 1000,
			property: 'opacity'
		});
		element.tween(1,0);
		swapArray[index] = element.getNext('div');
		if( !$defined(swapArray[index]) )
			swapArray[index] = element.getParent().getFirst('div');
		swapArray[index].set('tween',{
			duration: 1000,
			property: 'opacity'
		});
		swapArray[index].tween(0,1);
	}
}

var viewportwidth;
var viewportheight;
function resizeScrollpane(isResized) {
	var leftDiv = $('div_left');
	var rightDiv = $('div_right');
	var topDiv = $('div_top');
	var middleDiv = $('div_middle');
	var scrollpane = $('div_scrollpane');
	var footer = $('div_footer');

	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}

	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
	&& typeof document.documentElement.clientWidth !=
	'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}

	// older versions of IE
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	if( !resizeMore() ) {
		if( topDiv.offsetHeight + minHeight < viewportheight ) {
			var fullheight = viewportheight - topDiv.offsetHeight;
			scrollpane.style.height = ( fullheight - scrollpane.getStyle('padding-top').toInt()-scrollpane.getStyle('padding-bottom').toInt() ) + "px";
			leftDiv.style.height = fullheight + "px";
			rightDiv.style.height = fullheight + "px";
		}
		else {
			scrollpane.style.height = ( minHeight - scrollpane.getStyle('padding-top').toInt()-scrollpane.getStyle('padding-bottom').toInt() ) + "px";
			leftDiv.style.height = minHeight + "px";
			rightDiv.style.height = minHeight + "px";
		}
		if( middleDiv.offsetHeight < scrollpane.offsetHeight )
			footer.setStyles({
				'position': 'absolute',
				'bottom': '4px'
			});
		else {
			middleDiv.adopt(footer);
		}
		
		oldheight = fullheight;
	}
}

/* Functie IsValidEmail
 *
 * Params:
 * -email	String representatie van het e-mailadres
 *
 * Returns:
 * -bool	True wanneer het e-mailadres geldig is, anders false
 */
function IsValidEmail(email) {
	var emailExp = new RegExp("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}");
	return emailExp.test(email);
}

function muteVideo(vidid, linkid) {
	ytplayers[vidid].changeMute();
	if( ytplayers[vidid].player.isMuted() )
		$(linkid).getElement('img').set('src', 'images/icons/soundoff.png');
	else
		$(linkid).getElement('img').set('src', 'images/icons/sound.png');
}

// Menu
function initMenu() {
	var hoofdmenuLists = $$('.hoofdmenu_item');
	var hoofdmenuAnchors = new Array();
	var submenus = $$('ul.submenu');
	var submenuLists = new Array();
	var submenuAnchors = new Array();
	var home = $('a_home');
	home.addEvents({
		mouseenter: function(){
			this.morph('a.homeprint_hover');
		},
		mouseleave: function(){
			this.morph('a.homeprint_default');
		}
	});
	var print = $('a_print');
	print.addEvents({
		mouseenter: function(){
			this.morph('a.homeprint_hover');
		},
		mouseleave: function(){
			this.morph('a.homeprint_default');
		}
	});
	
	// List items van het hoofdmenu
	hoofdmenuLists.each( function(li, i) { 
		hoofdmenuAnchors[i] = li.getElement('a');
		li.addEvents({
			mouseleave: function(){
				if( this.getElement('a').get('class').indexOf('menu_active') < 0 && this.getElement('a').get('class').indexOf('hoofdmenu_link') >= 0 ) {
					this.getElement('a').morph('a.menu_default');
					/*submenus[gPage-1].morph({'opacity':1});
					submenus[i].morph({'opacity':0});
					if( $defined($('divsm0')) ) {
						if(gPage-1 == i)
							$('divsm0').morph({'opacity':0});
						else
							$('divsm0').morph({'opacity':1});
					}*/
				}
			}
		});
	});
	hoofdmenuAnchors.each( function(a, i) { 
		a.addEvents({
			mouseenter: function(){
				if( this.get('class').indexOf('menu_active') < 0 && this.get('class').indexOf('hoofdmenu_link') >= 0 ) {
					this.morph('a.menu_hover');
					/*submenus[gPage-1].morph({'opacity':0});
					submenus[i].morph({'opacity':1});
					if( $defined($('divsm0')) ) {
						if(gPage-1 == i)
							$('divsm0').morph({'opacity':1});
						else
							$('divsm0').morph({'opacity':0});
					}*/
				}
			}
		});
	});
	// De unordered lists van de submenus
	submenus.each( function(ul, i) {
		submenuLists[i] = ul.getElements('li');
		submenuAnchors[i] = ul.getElements('a');
		ul.setStyle('display', 'block');
		if( i == gPage-1 )
			ul.set('opacity', 1);
		else
			ul.set('opacity', 0);
		/*ul.addEvents({
			mouseenter: function(){
				if( hoofdmenuAnchors[i].get('class').indexOf('menu_active') < 0 && hoofdmenuAnchors[i].get('class').indexOf('hoofdmenu_link') >= 0 ) {
					hoofdmenuAnchors[i].morph('a.menu_hover');
					submenus[gPage-1].morph({'opacity':0});
					this.morph({'opacity':1});
				}
			},
			mouseleave: function(){
				if( hoofdmenuAnchors[i].get('class').indexOf('menu_active') < 0 && hoofdmenuAnchors[i].get('class').indexOf('hoofdmenu_link') >= 0 ) {
					hoofdmenuAnchors[i].morph('a.menu_default');
					submenus[gPage-1].morph({'opacity':1});
					this.morph({'opacity':0});
				}
			}
		});*/
	});
	submenuLists.each( function(arr) { 
		arr.each( function(li) {
			if($defined(li)) {
				li.addEvents({
					mouseleave: function(){
						if( this.getElement('a').get('class').indexOf('submenu_active') < 0 && this.getElement('a').get('class').indexOf('submenu_link') >= 0 ) {
							this.getElement('a').morph('a.submenu_default');
						}
					}
				});
			}
		});
	});
	submenuAnchors.each( function(arr) {
		arr.each( function(anchor) {
			if($defined(anchor)) {
				if(anchor.getParent().getParent().get('tag') == 'li') {
					anchor.addEvents({
						mouseenter: function(){
							this.morph('a.submenu_hover');
						}
					});
				}
			}
		});
	});
}
