
$(document).ready(function(){
	// navigation handler
	$('#navigation').sDrop({animate:true, duration:150});
	
	// link handlers
	$("a").click(function(event){
		type = $(this).attr('rel');
		if (type == 'messagebox'){
			// messagebox openen
			//event.preventDefault();
		}
		else if (type == 'popup'){ event.preventDefault(); }						// show inline element
		else if (type == 'pano'){
			event.preventDefault();
			width = Math.round($(window).width() * 0.6);
			height = Math.round($(window).height() * 0.8);
			$.fn.colorbox({
				href: 'ajax.php?p=flash&swf='+$(this).attr('href')+'&w='+width+'+&h='+height,
				title: $(this).attr('name'),
				transition:'slide', 
				speed:500
			});
		}
	});
	
	// escape key handler
	$(document).keypress(function(e){ if (e.keyCode == 27) messagebox.sluit(); });
	
	// slider handler
	$("#slider").easySlider({
		auto: false,
		continuous: true,
		numeric: true,
		speed: 400,
		pause: 3000,
		controlsShow: true
	});
	
	
	//contact.open('sendmail');
});

function center(e, width, height){
	width = ((width == 'undefined') ? 300 : width);
	height = ((height == 'undefined') ? 300 : height);
	$(e).width(width);
	$(e).css('margin-left', '-'+(width/2)+'px');
	//$(e).height(height);
	$(e).css('margin-top', '-'+(height/2)+'px');
}

function paginaLaden(container, url, data, fade){
	$.ajax({
		url: url,
		async: false,
		cache: false,
		type: 'GET',
		dataType: 'html',
		data: (data),
		success: function(response){
			if (container.length > 0){
				if (fade == true){
					$(container).hide();
					$(container).html(response);
					$(container).fadeIn(400);
				}
				else{
					$(container).html(response);
				}
			}
			return response;
		}
	});
}

function messagebox(){
	this.tonen = tonen;
	this.inhoud = inhoud;
	this.laden = laden;
	this.sluit = sluit;

	function tonen(width, height){
		$("#blockUI").fadeIn(250, function(){
			center('#messagebox', width, height);
			$("#messagebox").fadeIn(250, function(){$("#loader").fadeOut(250);});
		});
	}
	
	function inhoud(e){
		$("#messagebox").html('<div class="right"><img src="images/cross.png" onClick="messagebox.sluit();" /></div>'+e);
	}
	
	function laden(url, data, width, height){
		
		$("#blockUI").fadeIn(250);
		$("#loader").fadeIn(250, function(){
			center('#messagebox', width, height);
			$.ajax({
				url: url,
				async: false,
				cache: false,
				type: 'GET',
				dataType: 'html',
				data: (data),
				success: function(response){
					$("#loader").fadeOut(250);
					$("#messagebox").empty().append('<div class="right"><img src="images/cross.png" onClick="messagebox.sluit();" /></div>'+response).fadeIn(250)
					return 1;
				}
			});

			
		});
	}
	
	function sluit(actie){
		$("#messagebox").fadeOut(250);
		$("#blockUI:visible").fadeOut(250, function(){(actie == 'herladen' ? window.location.reload() : '')});
	}
}
messagebox = new messagebox;

function contact(){
	this.open = open;
	this.send = send;
	
	function open(t){
		if (t == 'callback'){
			messagebox.laden('ajax.php?p=callback', '', 200, 300);
		}
		else{
			messagebox.laden('ajax.php?p=sendmail', '', 690, 450);
		}
	}
	
	function send(type){
		voornaam = $("#txtVoornaam").val();
		naam = $("#txtNaam").val();
		email = $("#txtEmail").val();
		telefoon = $("#txtTelefoon").val();
		bericht = $("#txtBericht").val();
		
		error = '';
		error += mark_field('#txtVoornaam', voornaam == '', 'field_error');
		error += mark_field('#txtNaam', naam == '', 'field_error');
	
		if (type == 'sendmail'){
			error += mark_field('#txtEmail', correctEmail(email) != true, 'field_error');
			error += mark_field('#txtBericht', bericht == '', 'field_error');
		}
		else if(type == 'callback'){
			error += mark_field('#txtTelefoon', (telefoon.length < 9), 'field_error');
		}
		
		if (error.length <= 0){
			$("#loader").fadeIn(250, function(){
				messagebox.laden('ajax.php', { 
					p: type, a: 'submit', 
					voornaam: voornaam, 
					naam: naam, 
					email: email, 
					telefoon: telefoon,
					bericht: bericht
				});
			});
		}
	}
}
contact = new contact;

function customer(){
	this.login = login;
	this.logout = logout;
	
	function login(){
		messagebox.laden('ajax.php?p=customer-login&login='+$("#txtLogin").val()+'&wachtwoord='+$("#txtWachtwoord").val(), '', 340, 200);
	}
	function logout(){
		messagebox.laden('ajax.php?p=logout', '', 340, 200);
	}
}
customer = new customer;

function tree_toggle_fold(e){
	$(e).parent().find("ul").slideToggle("fast");
}

function correctEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(str)){
		return (true);
	}
	else
		return (false);
}
function mark_field(field, condition, classname){
	if (condition == false){
		$(field+"_label").removeClass(classname);
		return '';
	}
	else{
		$(field+"_label").addClass(classname);
		return 'error';
	}
}