$(document).ready(function () {

	$("#box-content input[type='submit']").live('click',function () {
		switch($(this).attr('id')) {
			case 'subscribe':
			case 'unsubscribe':
				var errors = false;
				if ($("#subscribenaam").val()=='') {
					$("#subscribenaam").addClass("error");
					errors = true;
				} else {
					$("#subscribenaam").removeClass("error");
				}
				var pattern= /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+$/;
				if ($("#subscribeemail").val()==''||!pattern.test($("#subscribeemail").val())) {
					$("#subscribeemail").addClass("error");
					errors = true;
				} else {
					$("#subscribeemail").removeClass("error");
				}
				if (!errors) {
					$.post("/bulletin.php",{subscribenaam:$("#subscribenaam").val(),subscribeemail:$("#subscribeemail").val(),action:$(this).attr('id')},function (data) {
						$("#box-content").html(data);
					});
				}
			break;
			case 'login':
			break;
		}
		return false;
	});
	
	var headerboxstate = false;
	var headerboxcurrent = false;
	
	$.loadContent = function (current) {
		$("#box-content").load('/templates/'+current+'.html',function () {
			if (!headerboxstate) {
				setTimeout(function () {
					$("#box-content").fadeIn("slow");
					headerboxstate = true;
				},400);
			} else {
				$("#box-content").fadeIn("slow");
				headerboxstate = true;
			}
			$("#subscribe,#unsubscribe,#login").hover(function() { 
				$(this).addClass("hover"); 
			}, function() { 
				$(this).removeClass("hover"); 
			});
		});
	}
	
	$.closeHeaderBox = function () {
		$("a.tab").removeClass('current');
		$("div#box-outercontainer").animate( { top:"-107px" }, 400 );
		headerboxstate = false;
		headerboxcurrent = false;
		$("#box-content").empty();
	}
	
	$("ul#nav li a").each(function () {
		var link = $(this).attr('href');
		var path = location.pathname.substr(1).split('/');
		var currentmodule = path[0];
		if (link=="/"+currentmodule) {
			$(this).addClass('current');
		}
	});

	$("#bulletin").click(function () {
		if (!headerboxstate) {
			$("div#box-outercontainer").animate( { top:"0" }, 400 );
			headerboxcurrent = $(this).attr('id');
			$.loadContent(headerboxcurrent);
		} else {
			if ($(this).attr('id')!=headerboxcurrent) {
				headerboxcurrent = $(this).attr('id');
				$("#box-content").fadeOut("slow",function () {
					$.loadContent(headerboxcurrent)
				});
			}
		}
	});
	
	$("#box-close").click(function () {
		if (headerboxstate) {
			$("#box-content").fadeOut("slow",function () {			
				$.closeHeaderBox();
			});
		}
	});
	
	$("a.tab").click(function () {
		$("a.tab").removeClass('current');
		$(this).addClass('current');
	});
});