var navigation, Navigation = Class.create();
Navigation.prototype = {
	initialize: function(container, options){		
		this.container = $(container);
		
		this.timeout = false;
		
		this.options = new Hash();
		this.options.set('effect', 'blind');
		if(options)	this.options = this.options.merge(options);
		
		this.items = [];
		elements = this.container.select('li.section');
		for(var i=0; i<elements.length; i++){
			this.items[i] = new NavigationItem(this, elements[i]);
		}
	},
	
	find_active: function(){
		for(var i=0; i<this.items.length; i++){
			if(this.items[i].is_active()) return this.items[i];
		}
		
		return false;
	}
}

NavigationItem = Class.create();
NavigationItem.prototype = {
	initialize: function(instance, element){
		this.instance = instance;
		this.locked = false;
		
		this.element = element;
		this.pages = $(this.element.down('.pages'));
		
		if(link_element = this.element.down('a.link')){
			this.element.down('a.link').observe('mouseover', function(){
				if(this.pages.visible()) return false;
				
				if(instance.timeout){
					window.clearInterval(instance.timeout);
				}
			
				instance.timeout = window.setTimeout(function(){
					this.toggle();
				}.bind(this), 350);
			}.bind(this));
			
			this.element.down('a.link').observe('click', this.toggle.bind(this));
		
			if(section && this.element.down('a.link').innerHTML == section.toUpperCase()) this.open(false);
		}
	},
	
	set_locked: function(){
		this.locked = true;

		window.setTimeout(function(){
			this.locked = false;
		}.bind(this), 1000);
	},
	
	toggle: function(){
		if(this.locked) return false;
	
		if(this.is_active()){	
			if(sub_menu = this.element.down("ul.sub_menu")){
				sub_menu.hide();
			}
			
			this.close();
		} else {
			this.open(true);
		}
		
		this.set_locked();
	},
	
	open: function(animate){
		if(active_element = this.instance.find_active()){
			if(sub_menu = active_element.element.down("ul.sub_menu")){
				sub_menu.hide();
			}
		
			active_element.close();
		}
	
		this.element.addClassName('active');
	
		if(animate){
			switch(this.instance.options.get('effect')){
				case "blind":
					new Effect.BlindDown(this.pages, {duration: 0.5});
				break;
				case "appear":
					this.pages.appear({duration: 0.5, to: 0.9});
				break;
			}
		}	else this.pages.show();
	},
	
	close: function(){
		this.element.removeClassName('active');
	

		switch(this.instance.options.get('effect')){
			case "blind":
				new Effect.BlindUp(this.pages, {duration: 0.3});
			break;
			case "appear":
				this.pages.fade({duration: 0.3});
			break;
		}
	},
	
	is_active: function(){
		return this.element.hasClassName('active');
	}
}

/* FUNCTIONS */
function submitform(form, validation){
	if(validation){
		prefix = form.replace('form_', '');
		errors = new Array();
	
		for(i=0; i<validation.length; i+=2){		
			field = $(prefix + '_' + validation[i]);
			
			if(field){
				if(field.value == ''){
					errors[errors.length] = validation[i+1] + ' is niet ingevuld.';
				} else if(validation[i] == 'email'){				
					if(!((field.value.indexOf(".") > 2) && (field.value.indexOf("@") > 0))){
						errors[errors.length] = validation[i+1] + ' is geen geldig e-mail adres.';
					}
				}
			}
		}
		
		if(errors.length > 0){
			str = "Corrigeer de volgende fouten en probeer het nog eens.\n";
			
			for(i=0; i<errors.length; i++){
				str += "\n" + errors[i];
			}
			
			alert(str);
			return false;
		}
	}
	
	$(form).submit();
}

function tickcheckbox(object, value){
	if(object.src.indexOf('checked') != -1){
		object.src = object.src.replace('_checked', '');
		value = '';
	} else {
		object.src = object.src.replace('.gif', '_checked.gif');
	}

	parts = object.id.split('_');
	classname = 'checkbox';
	for(i=1; i<parts.length; i++){
		if(i < parts.length-1){
			classname += '_' + parts[i];
		}
	}

	$(classname.replace('checkbox_', '')).value = value;
	
	checkboxes = document.getElementsByClassName(classname);
	
	for(i=0; i<checkboxes.length; i++){
		if(checkboxes[i] != object){
			checkboxes[i].src = checkboxes[i].src.replace('_checked', '');
		}
	}
}

function nextpage(page){
	$('nextpage_to').value = page;
	$('form').submit();
}