var buttonhandler, ie = document.all?true:false;

window.onload = handleonload;
window.onresize = checkresolution;
window.onclick = handleclick;

function handleonload(){
	checkresolution();

	active = document.getElementsByClassName(controller);
	if(active.length == 1){	
		active[0].id = 'active_' + active[0].className;
		active[0].className = active[0].className + ' active';
	}

	buttonhandler = new Buttons();
	bereken = new Bereken();
}

function checkresolution(){
	if(document.body.offsetWidth) width = document.body.offsetWidth;
	else width = window.innerWidth;
	

	if(width > 1000){
		$('page').className = '';
		if(width < 1024){
			$('slogan').style.display = 'none';
		}
	} else {
		$('page').className = 'nocenter';
	}
}

function handleclick(e){
	target = gettarget(e);

	switch(target.tagName){
		case 'INPUT':
			if(target.className != 'cleared'){
				target.value = '';
				target.className = 'cleared';
			}
		break;
	}
}

function gettarget(e){
	if(ie) return event.srcElement;
	else return e.target;
}

/* BUTTONS CLASS */
var Buttons = Class.create();
Buttons.prototype = {
	initialize: function(){
		this.objects = new Array();
		this.add();
	},
	
	add: function(){
		elements = document.getElementsByClassName('button');
		
		for(i=0; i<elements.length; i++){
			this.objects[i] = new Button(elements[i]);
		}
	},
	
	find: function(object){
		for(i=0; i<this.objects.length; i++){
			if(this.objects[i].object == object){
				return this.objects[i];
			}
		}
	},
	
	findactive: function(){
		for(i=0; i<this.objects.length; i++){
			if(this.objects[i].menu){
				if(this.objects[i].menu.style.display == ''){
					return this.objects[i];
				}
			}
		}
		return false;
	}
}

var Button = Class.create();
Button.prototype = {
	initialize: function(object){
		this.object = object;
		
		this.object.onclick = this.open.bindAsEventListener(this);
		
		classes = this.object.className.split(' ');
		this.menu = $(classes[1]);
		this.name = classes[1];

		this.timeout = false;
	},
	
	open: function(){
		active = buttonhandler.findactive();
		if(active){
			active.close();
			
			if(active.name == this.name){
				return false;
			}
		}
		
		new Effect.Appear(this.menu, {duration: 0.5});
	},
	
	close: function(){
		new Effect.Fade(this.menu, {duration: 0.5});
	}
}

/* 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_', 'input_')).each(function(element){
		element.value = value;
	}.bind(value));

	//$(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();
}