var menu = {
	containingLI: null,
	theLink: null,
	opened: false,
	closeTimer: null,
	init: function(){
		 this.containingLI = $('nav-museum');
		 this.containingLI.style.overflow='hidden';
		 this.containingLI.addEvent('mouseout',function(){
		 	menu.doTimedCloseDown();
		 });
		 this.containingLI.addEvent('mouseover',function(){
		 	menu.cancelTimedCloseDown();
		 });
		 var a = $(this.containingLI.$$('a')[0]);
		 this.theLink = a;
		 this.theLink.addEvent ('click',menu.doCloseDown);
		 this.initialHide();
	},
	initialHide: function(){
		var uls = this.containingLI.$$('ul');
		for (var i=0,j; j = uls[i] ; i++){
			j.theHEIGHT = j.offsetHeight;
			j.style.height = j.theHEIGHT + 'px';
			j.style.overflow = 'hidden';
			//j.style.display = 'none';
		}
		//this.containingLI.removeClass('open');
		this.closeDown();
	},
	doOpenUp: function(e){
		menu.openUp();
		What.Event.cancelBubble(e);
		What.Event.preventDefault(e);
		return false;
	},
	openUp: function(){
		var uls = this.containingLI.$$('ul');
		var ul1 = uls[0];
		var ul2 = uls[1];
		var targetHeight = Math.max(ul1.theHEIGHT,ul2.theHEIGHT);
		ul1.style.display = ul2.style.display = 'block';
		this.containingLI.addClass('open');
		var ani1 = new What.Animation.Move (ul1,targetHeight,10,'height','in','px');
		var ani2 = new What.Animation.Move (ul2,targetHeight,10,'height','in','px');
		ani1.onAnimationEnd = function(){
			menu.containingLI.style.zIndex = 10;
			menu.containingLI.delEvent ('mouseout',setDefaultZindex);
		};
		ani1.init();
		ani2.init();
		menu.opened = true;
		this.theLink.delEvent('click',menu.doOpenUp);
		this.theLink.addEvent('click',menu.doCloseDown);
	},
	doCloseDown: function(e){
		menu.closeDown();
		What.Event.cancelBubble(e);
		What.Event.preventDefault(e);
		return false;
	},
	closeDown: function(){
		var uls = this.containingLI.$$('ul');
		var ul1 = uls[0];
		var ul2 = uls[1];
		var targetHeight = 0;
		var ani1 = new What.Animation.Move (ul1,targetHeight,10,'height','out','px');
		var ani2 = new What.Animation.Move (ul2,targetHeight,10,'height','out','px');
		ani1.onAnimationEnd = function (){
			ul1.style.display = 'none';
		}
		ani2.onAnimationEnd = function (){
			ul2.style.display = 'none';
			menu.containingLI.removeClass('open');
			menu.theLink.delEvent('click',menu.doCloseDown);
			menu.theLink.addEvent('click',menu.doOpenUp);
			menu.opened = false;
			menu.containingLI.style.zIndex = 2;
			menu.containingLI.addEvent ('mouseout',setDefaultZindex);
		};
		ani1.init();
		ani2.init();
	},
	doTimedCloseDown: function(){
		if (!menu.timer && menu.opened){
			menu.timer = setTimeout (function(){
				menu.closeDown();
				clearTimeout(menu.timer);
				menu.timer = null;
			},1000);
		}
	},
	cancelTimedCloseDown: function(){
		if (menu.timer !== null){
			clearTimeout(menu.timer);
			menu.timer = null;
		}
	}
};

var nieuwsroll = {
	containingDIV: null,
	nodes: null,
	currentIndex: 1,
	numberOfPages: 0,
	currentList: null,
	nextList: null,
	prevList: null,
	itemsDisplayed: 2,
	init: function(){
		nieuwsroll.containingDIV = $('nieuwsroll');
		nieuwsroll.containingDIV.style.overflow = 'hidden';
		nieuwsroll.prev = $(nieuwsroll.containingDIV.getElementsByClassName('next','li')[0].getElementsByTagName('a')[0]);
		nieuwsroll.next = $(nieuwsroll.containingDIV.getElementsByClassName('prev','li')[0].getElementsByTagName('a')[0]);
		if (!What.AJAX.isSupported()){
			nieuwsroll.next.style.display = 'none';
			nieuwsroll.prev.style.display = 'none';
			return false;
		}
		if (typeof nieuwsURL != 'undefined') {
			var a = new What.AJAX.request(nieuwsURL,'GET');
		} else {
			return false;
		}
		a.onReady = function(){
			nieuwsroll.nodes = this.response.xml.getElementsByTagName('item');
			var l = nieuwsroll.nodes.length;
			nieuwsroll.numberOfPages = Math.ceil(l/nieuwsroll.itemsDisplayed);
			if (nieuwsroll.numberOfPages <= 1){
				nieuwsroll.next.style.display = 'none';
				nieuwsroll.prev.style.display = 'none';
				return false;
			}
			
			nieuwsroll.currentList = $(nieuwsroll.containingDIV.$$('ul')[0]);
			nieuwsroll.containingDIV.style.height = nieuwsroll.containingDIV.offsetHeight + 'px';
			nieuwsroll.currentList.style.position = 'absolute';
			nieuwsroll.currentList.style.left = '0';
			nieuwsroll.currentList.style.top = '12px';
			
			nieuwsroll.toggleButtons();
			nieuwsroll.prev.addEvent('click',nieuwsroll.showPrevious);
			nieuwsroll.next.addEvent('click',nieuwsroll.showNext);
		};
		a.onFailure = function (){
			nieuwsroll.next.style.display = 'none';
			nieuwsroll.prev.style.display = 'none';
			return false;
		};
		a.load();
	},
	createList: function(){
		var index1 = (nieuwsroll.currentIndex-1)*nieuwsroll.itemsDisplayed;
		var index2 = ((nieuwsroll.currentIndex-1)*nieuwsroll.itemsDisplayed)+1;
		var node1 = nieuwsroll.nodes[index1];
		var item1 = {
			header: node1.getElementsByTagName('naam')[0].firstChild.nodeValue,
			tekst: node1.getElementsByTagName('beschrijving')[0].firstChild.nodeValue
		};
		
		if (node1.getElementsByTagName('link_naam')[0].firstChild && node1.getElementsByTagName('link_naam')[0].firstChild.nodeValue != '' &&
			node1.getElementsByTagName('link_url')[0].firstChild && node1.getElementsByTagName('link_url')[0].firstChild.nodeValue != ''){
			item1.link_naam = node1.getElementsByTagName('link_naam')[0].firstChild.nodeValue;
			item1.link_url = node1.getElementsByTagName('link_url')[0].firstChild.nodeValue;
		}
		
		if (nieuwsroll.nodes[index2]){
			var node2 = nieuwsroll.nodes[index2];
			var item2 = {
				header: node2.getElementsByTagName('naam')[0].firstChild.nodeValue,
				tekst: node2.getElementsByTagName('beschrijving')[0].firstChild.nodeValue
			};
			if (node2.getElementsByTagName('link_naam')[0].firstChild && node2.getElementsByTagName('link_naam')[0].firstChild.nodeValue != '' &&
				node2.getElementsByTagName('link_url')[0].firstChild && node2.getElementsByTagName('link_url')[0].firstChild.nodeValue != ''){
				item2.link_naam = node2.getElementsByTagName('link_naam')[0].firstChild.nodeValue;
				item2.link_url = node2.getElementsByTagName('link_url')[0].firstChild.nodeValue;
			}
		}
		
		var theList = What.Element.create('ul',{},[
			What.Element.create('li',{},[
				What.Element.create('h3',{},[item1.header]),
				What.Element.create('p',{},[item1.tekst])
			])
		]);
		
		if (item1.link_naam) {
			theList.$$('li')[0].appendChild (What.Element.create('p',{},[
				What.Element.create('a',{href:item1.link_url},[item1.link_naam])
			]));
		}
		
		if (node2) {
			theList.appendChild (What.Element.create('li',{},[
				What.Element.create('h3',{},[item2.header]),
				What.Element.create('p',{},[item2.tekst])
			]));
			if (item2.link_naam) {
				theList.$$('li')[1].appendChild (What.Element.create('p',{},[
					What.Element.create('a',{href:item2.link_url},[item2.link_naam])
				]));
			}
		}
		
		return theList;
	},
	showNext: function(e){
		if (((nieuwsroll.currentIndex-1) >= 0) && !this.DISABLED){
			if (!nieuwsroll.nextList) {
				nieuwsroll.currentIndex--;
				
				nieuwsroll.nextList = nieuwsroll.createList();
				
				nieuwsroll.nextList.style.position = 'absolute';
				nieuwsroll.nextList.style.top = '12px';
				nieuwsroll.nextList.style.left = '-230px';
				
				nieuwsroll.nextList.style.width = nieuwsroll.currentList.style.width = '230px';
				
				nieuwsroll.containingDIV.insertAfter(nieuwsroll.nextList,nieuwsroll.currentList);
				
				nieuwsroll.checkHeight (nieuwsroll.nextList);
				
				var ani1 = new What.Animation.Move (nieuwsroll.nextList,0,10,'left','in');
				var ani2 = new What.Animation.Move (nieuwsroll.currentList,230,10,'left','in');
				ani1.onAnimationEnd = function (){
					nieuwsroll.currentList.remove();
					nieuwsroll.currentList = nieuwsroll.nextList;
					nieuwsroll.nextList = null;
					nieuwsroll.toggleButtons ();
				}
				ani1.init();
				ani2.init();
			}
		}
		What.Event.preventDefault(e);
		return false;
	},
	showPrevious: function(e){
		if (((nieuwsroll.currentIndex+1) <= nieuwsroll.numberOfPages) && !this.DISABLED){
			if (!nieuwsroll.prevList) {
				nieuwsroll.currentIndex++;
				
				nieuwsroll.prevList = nieuwsroll.createList();
				nieuwsroll.prevList.style.position = 'absolute';
				nieuwsroll.prevList.style.top = '12px';
				nieuwsroll.prevList.style.left = '230px';
				
				nieuwsroll.prevList.style.width = nieuwsroll.currentList.style.width = '230px';
				
				nieuwsroll.containingDIV.insertAfter(nieuwsroll.prevList,nieuwsroll.currentList);
				
				nieuwsroll.checkHeight (nieuwsroll.prevList);
				
				var ani1 = new What.Animation.Move (nieuwsroll.prevList,0,10,'left','in');
				var ani2 = new What.Animation.Move (nieuwsroll.currentList,-230,10,'left','in');
				ani1.onAnimationEnd = function (){
					nieuwsroll.currentList.remove();
					nieuwsroll.currentList = nieuwsroll.prevList;
					nieuwsroll.prevList = null;
					nieuwsroll.toggleButtons ();
				}
				ani1.init();
				ani2.init();
			}
		}
		What.Event.preventDefault(e);
		return false;
	},
	checkHeight: function (newList){
		// als de nieuwe lijst groter is dan de oude lijst, pas de hoogte van de containingDIV aan
		var h = parseInt(nieuwsroll.containingDIV.style.height);
		var oldListH = nieuwsroll.currentList.offsetHeight;
		var newListH = newList.offsetHeight;
		
		nieuwsroll.containingDIV.style.height = (Math.ceil(Math.max(oldListH,newListH)) + 26) + 'px';
	},
	toggleButtons: function(){
		if (nieuwsroll.currentIndex >= nieuwsroll.numberOfPages){
			nieuwsroll.prev.DISABLED = true;
			var fade = new What.Animation.Fade(nieuwsroll.prev,null,0,2);
			fade.onAnimationEnd = function (){
				nieuwsroll.prev.addClass('disabled');
			}
			fade.init();
		} else {
			nieuwsroll.prev.DISABLED = false;
			var fade = new What.Animation.Fade(nieuwsroll.prev,null,100,2);
			fade.onAnimationEnd = function (){
				nieuwsroll.prev.removeClass('disabled');
			}
			fade.init();
		}
		if (nieuwsroll.currentIndex <= 1) {
			nieuwsroll.next.DISABLED = true;
			var fade = new What.Animation.Fade(nieuwsroll.next,null,0,2);
			fade.onAnimationEnd = function (){
				nieuwsroll.next.addClass('disabled');
			}
			fade.init();
		} else {
			nieuwsroll.next.DISABLED = false;
			var fade = new What.Animation.Fade(nieuwsroll.next,null,100,2);
			fade.onAnimationEnd = function (){
				nieuwsroll.next.removeClass('disabled');
			}
			fade.init();
		}
	}
};

var musearoll = {
	bigFatContainer: null,
	numberOfPages: 3,
	currentPage: 1,
	busy: false,
	init: function(){
		this.bigFatContainer = $('musea');
		this.bigFatContainer.style.overflow = 'hidden';
		var sectie1 = this.bigFatContainer.getElementsByClassName('sectie','div')[0];
		this.bigFatContainer.style.height = this.bigFatContainer.offsetHeight + 'px';
		var ani = new What.Animation.Move(this.bigFatContainer,sectie1.offsetHeight,5,'height','in');
		ani.onAnimationEnd = function (){
			musearoll.orderLists();
			musearoll.prev = $(musearoll.bigFatContainer.getElementsByClassName('prev','li')[0].getElementsByTagName('a')[0]);
			musearoll.next = $(musearoll.bigFatContainer.getElementsByClassName('next','li')[0].getElementsByTagName('a')[0]);
			musearoll.prev.addEvent('click',musearoll.showPrevious);
			musearoll.next.addEvent('click',musearoll.showNext);
			musearoll.toggleButtons();
		};
		ani.init();
	},
	showPrevious: function(e){
		if (!this.DISABLED && !menu.busy){
			menu.busy = true;
			var secties = musearoll.bigFatContainer.getElementsByClassName('sectie','div');
			for (var i=0,sectie; sectie = secties[i]; i++){
				sectie = $(sectie);
				var currleft = parseInt(sectie.style.left);
				var targetleft = currleft+657;
				var ani = new What.Animation.Move (sectie,targetleft,10,'left','in');
				ani.onAnimationEnd = function(){
					menu.busy = false;
				};
				ani.init();
			}
			musearoll.currentPage--;
			musearoll.toggleButtons();
		}
		What.Event.preventDefault(e);
		return false;
	},
	showNext: function(e){
		if (!this.DISABLED && !menu.busy){
			menu.busy = true;
			var secties = musearoll.bigFatContainer.getElementsByClassName('sectie','div');
			for (var i=0,sectie; sectie = secties[i]; i++){
				sectie = $(sectie);
				var currleft = parseInt(sectie.style.left);
				var targetleft = currleft-657;
				var ani = new What.Animation.Move (sectie,targetleft,10,'left','in');
				ani.onAnimationEnd = function(){
					menu.busy = false;
				};
				ani.init();
			}
			musearoll.currentPage++;
			musearoll.toggleButtons();
		}
		What.Event.preventDefault(e);
		return false;
	},
	orderLists: function(){
		var secties = musearoll.bigFatContainer.getElementsByClassName('sectie','div');
		for (var i=0,sectie; sectie = secties[i]; i++){
			sectie = $(sectie);
			sectie.style.position = 'absolute';
			sectie.style.top = '5px';
			sectie.style.left = ((657 * i) + 25)+'px';
			sectie.style.overflow = 'hidden';
			sectie.style.width = '630px';
		}
		musearoll.bigFatContainer.style.width = '632px';
	},
	toggleButtons: function(){
		if (musearoll.currentPage <= 1){
			musearoll.prev.DISABLED = true;
			var fade = new What.Animation.Fade(musearoll.prev,null,0,2);
			fade.onAnimationEnd = function (){
				musearoll.prev.addClass('disabled');
			}
			fade.init();
		} else {
			musearoll.prev.DISABLED = false;
			var fade = new What.Animation.Fade(musearoll.prev,null,100,2);
			fade.onAnimationEnd = function (){
				musearoll.prev.removeClass('disabled');
			}
			fade.init();
		}
		if (musearoll.currentPage >= musearoll.numberOfPages) {
			musearoll.next.DISABLED = true;
			var fade = new What.Animation.Fade(musearoll.next,null,0,2);
			fade.onAnimationEnd = function (){
				musearoll.next.addClass('disabled');
			}
			fade.init();
		} else {
			musearoll.next.DISABLED = false;
			var fade = new What.Animation.Fade(musearoll.next,null,100,2);
			fade.onAnimationEnd = function (){
				musearoll.next.removeClass('disabled');
			}
			fade.init();
		}
	}
};

var n8van = {
	init: function (){
		var links = document.getElementsByClassName('addton8','a',function(a){
			a.addEvent ('click',n8van.addActivity);
		});
		var removelinks = document.getElementsByClassName('removefromn8','a',function(a){
			a.addEvent ('click',n8van.removeActivity);
		});
	},
	addActivity: function (e){
		// fetch event id uit URL
		var href = this.href;
		href = href.split('#')[1];
		var eventID = href.substring(3);
		var museumName = this.rel;
		var evenementTitle = this.title.substring(5,(this.title.length-16)) || '';
		
		n8van.addToList (eventID,evenementTitle,museumName);
		
		if (typeof profielPagina == 'boolean' && profielPagina){
			// verander linkje zodat er weer verwijderd kan worden
			this.className = 'removefromn8';
			this.title = 'Verwijder ' + evenementTitle + ' uit jouw n8';
			this.innerHTML = 'Verwijder ' + evenementTitle + ' uit jouw n8';
			
			this.delEvent ('click',n8van.addActivity);
			this.addEvent ('click',n8van.removeActivity);
			
		} else {
			// verander linkje in span om dubbele klik te voorkomen
			var span = What.Element.create('span',{className:'addedton8',title:'Deze activiteit is al toegevoegd aan jouw n8'},['Deze activiteit is al toegevoegd aan jouw n8']);
			this.parentNode.insertBefore (span,this);
			this.remove();
		}
		
		What.Event.preventDefault(e);
		return false;
	},
	removeActivity: function (e){
		// fetch event id uit URL
		var href = this.href;
		href = href.split('#')[1];
		var eventID = href.substring(3);
		var museumName = this.rel;
		var evenementTitle = this.title.substring(10,(this.title.length-12)) || '';
		
		n8van.removeFromList (eventID);
		
		// verander linkje in ander linkje zodat item weer toegevoegd kan worden
		this.className = 'addton8';
		this.title = 'Voeg ' + evenementTitle + ' toe aan jouw n8';
		this.innerHTML = 'Voeg toe';
		
		this.delEvent ('click',n8van.removeActivity);
		this.addEvent ('click',n8van.addActivity);
		
		What.Event.preventDefault(e);
		return false;
	},
	addToList: function (eventID, evenementTitle, museumName){
		if (!n8van.isAdded(eventID)){
			// voeg thumbnail van event toe aan n8van lijst
			var img = What.Element.create('img',{src:basePath+'media/images/evenementen/'+eventID+'.jpg',alt:museumName,width:'30',height:'30'});
			var a = What.Element.create('a',{href:basePath+'museum/'+museumName+'#event'+eventID,title:evenementTitle},[img]);
			var li = What.Element.create('li',{},[a]);
			var parent = $('n8van').getElementsByClassName('thumbs')[0];
			parent.appendChild(li);
			
			if ($('noN8')){
				$('noN8').remove();
			}
			
			var options = $('n8van').getElementsByClassName('options')[0];
			var link1 = options.$$('li')[0];
			var link2 = options.$$('li')[1];
			if (!profielSaved){
				link1.innerHTML = '<a href="' + basePath + 'n8van/jou">overzicht - bewaar</a>';
				link2.innerHTML = '<a href="' + basePath + 'profielen/logout/">leeg je selectie</a>';
			} else {
				link1.innerHTML = '<a href="' + basePath + 'n8van/jou">overzicht</a>';
				link2.innerHTML = '<a href="' + basePath + 'profielen/logout/">log uit</a>';
			}
			
			// maak AJAX call
			var ajax = What.AJAX.request(basePath+'evenementen/add/'+eventID,'get');
			ajax.load();
		}
	},
	removeFromList: function (eventID){
		var parent = $('n8van').getElementsByClassName('thumbs')[0];
		var lis = parent.$$('li');
		for (var i=0,j; j=lis[i];i++){
			if (j.$$('a').length > 0){
				var a = j.$$('a')[0];
				var hrefComponents = a.href.split('#event');
				var theID = hrefComponents[hrefComponents.length-1];
				if (eventID == theID){
					$(j).remove();
					break;
				}
			}
		}
			
		if (lis.length==0){
			var li = What.Element.create('li',{id:'noN8'},['Je hebt nog geen eigen programma. Gebruik hiervoor de plusjes naast evenementen.']);
			parent.appendChild(li);
			
			var options = $('n8van').getElementsByClassName('options')[0];
			var link1 = options.$$('li')[0];
			var link2 = options.$$('li')[1];
			link1.innerHTML = link2.innerHTML = '';
		}
			
		// maak AJAX call
		var ajax = What.AJAX.request(basePath+'evenementen/delete/'+eventID,'get');
		ajax.load();
	},
	isAdded: function (id){
		var parent = $('n8van').getElementsByClassName('thumbs')[0];
		var lis = parent.$$('li');
		for (var i=0,j; j=lis[i];i++){
			if (j.$$('a').length > 0){
				var a = j.$$('a')[0];
				var hrefComponents = a.href.split('#event');
				var theID = hrefComponents[hrefComponents.length-1];
				if (id == theID){
					return true;
				}
			}
		}
		return false;
	},
	saver: {
		init: function (){
			var theLink = $('n8van-options').$('bewaar').$$('a')[0];
			theLink.addEvent ('click',function(e){
				var ajax = What.AJAX.request(basePath+'profielen/addForm','get');
				ajax.onReady = function (){
					var response = this.response.text;
					var dialog = new What.Dialog ();
					dialog.create();
					$('dropSheet').style.zIndex = '9998';
					document.getElementsByClassName('dialog','div')[0].style.zIndex = '9999';
					var div = What.Element.create('div');
					div.innerHTML = response;
					dialog.addElement(div);
					n8van.saver.dialog = dialog;	
				};
				ajax.load();
				What.Event.preventDefault(e);
				return false;
			});
		},
		saveData: function (){
			var naam = $('naam').value;
			var email = $('email').value;
			var errors = 0;
			if (/^\s$/.test(naam) || naam == ''){
				n8van.saver.setError ('naam','Een naam is verplicht!');
				errors++;
			} else { n8van.saver.clearError ('naam'); }
			if (!/^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/.test(email)){
				n8van.saver.setError ('email','Dit is geen geldig e-mailadres');
				errors++;
			} else { n8van.saver.clearError ('email'); }
			if (errors > 0) return;
			var nieuwsbrief = !!$('nieuwsbrief').checked ? 1 : 0;
			
			var ajax = What.AJAX.request (basePath+'profielen/add/'+email+'/'+naam+'/'+nieuwsbrief);
			ajax.onFailure = function (){
				$('bewaar-n8').innerHTML = '<strong>Error: je n8 kon niet worden opgeslagen, probeer het later nog eens!</strong>';
				n8van.saver.doTimedClose ();
			};
			ajax.onReady = function (){
				if (this.response.text == 1){
					$('bewaar-n8').innerHTML = '<p>Je n8 is succesvol opgeslagen! Veel plezier tijdens jouw n8.</p>';
					location.replace (location.href);
				} else {
					ajax.onFailure();
				}
			};
			
			ajax.load();
		},
		setError: function (naam,msg){
			if ($('error_for_'+naam)){
				$('error_for_'+naam).innerHTML = msg;
			}
		},
		clearError: function (naam){
			if ($('error_for_'+naam)){
				$('error_for_'+naam).innerHTML = '';
			}
		},
		doTimedClose: function (){
			var to = setTimeout (function(){
				n8van.saver.dialog.close();
			},1000);
		},
		annuleer: function (e){
			n8van.saver.dialog.close();
			return false;
		}
	},
	embedder: {
		init: function (){
			var theLink = $('n8van-options').$('embed').$$('a')[0];
			if (!theLink.hasClass('disabled')){
				theLink.addEvent ('click',function(e){
					var ajax = What.AJAX.request (basePath+'profielen/embed','get');
					ajax.onReady = function (){
						if (this.response.text != '0'){
							var dialog = new What.Dialog ();
							dialog.create();
							$('dropSheet').style.zIndex = '9998';
							document.getElementsByClassName('dialog','div')[0].style.zIndex = '9999';
							var container = What.Element.create('div',{id:'embed-popup'});
							var textarea = What.Element.create('textarea',{},[ajax.response.text]);
							textarea.style.width = '250px';
							textarea.style.height = '250px';
							var p = What.Element.create('p',{},['Plak dit stukje code in je blog of op je website om je eigen n8 te delen met anderen:']);
							var klaar = What.Element.create('button',{},['Sluiten']);
							klaar.onclick = function (){
								dialog.close();
								return false;
							};
							container.addChildren ([p,textarea,klaar]);							
							dialog.addElement(container);
							textarea.select();
						}
					}
					ajax.load();
					
					What.Event.preventDefault(e);
					return false;
				});
			}
		}
	}
};

var focusEvent = {
	init: function(elm){
		elm.style.color = "#EB008B";
		var as = $$('a');
		for (var i=0,a; a=as[i]; i++){
			a = $(a);
			a.addEvent ('click',function(e){
				var href = this.href;
				if (href.indexOf('#event') != -1){
					var anchor = href.substring(href.indexOf('#')+1);
					if ($(anchor)){
						focusEvent.currentHighlighted.style.color = '#000';
						focusEvent.currentHighlighted = $(anchor);
						focusEvent.currentHighlighted.style.color = '#EB008B';
					}
				}
			});
		}
	}
};

/** Intialisatie functies **/

if (document.location.hash != '' && $(document.location.hash.toString().substring(1))){
	focusEvent.init($(document.location.hash.toString().substring(1)));
	focusEvent.currentHighlighted = $(document.location.hash.toString().substring(1));
}

if ($('nav-museum')){
	menu.init();
}

What.Event.add(window,'load',function(){
	var inputs = $$('input');
	var txtareas = $$('textarea');
	toggleIt(inputs); toggleIt(txtareas);
});

function toggleIt (collection){
	for (var i=0,j; j = collection[i]; i++){
		if (j.nodeName.toLowerCase() == 'textarea' || (j.nodeName.toLowerCase() == 'input' && j.getAttribute ('type')=='text')){
			if (j.value.length != 0){
				j.__DEFAULTVALUE__ = j.value;
				j.onfocus = function (){
					if (this.value === this.__DEFAULTVALUE__){
						this.value = '';
					}
					return false;
				}
				j.onblur = function (){
					if (this.value.length == 0){
						this.value = this.__DEFAULTVALUE__;
					}
					return false;
				}
			} else continue;
		}
	}
}

function setHighZindex(){
	this.style.zIndex = 10;
}

function setDefaultZindex(){
	this.style.zIndex = this.ORIGINALZINDEX;
}

if (document.getElementsByClassName('addton8').length > 0){
	n8van.init();
}

if ($('nav-museum') && $('nav-programma') && $('nav-kaartverkoop') && $('nav-plattegrond')) {
	var buttons = [$('nav-museum'), $('nav-programma'), $('nav-kaartverkoop'), $('nav-plattegrond')];
	buttons.map (function(b){
		b.ORIGINALZINDEX = b.getStyle ('z-index');
		b.addEvent('mouseover',setHighZindex);
		b.addEvent('mouseout',setDefaultZindex)
	});
}

if ($('plattegrond') && $('plattegrond').$('musea')) {
	musearoll.init();
}

if ($('nieuwsroll')){
	nieuwsroll.init();
}

if ($('n8van-options') && $('bewaar')){
	n8van.saver.init();
}

if ($('n8van-options') && $('embed')){
	n8van.embedder.init();
}
