$(document).ready(function(){
	$(document).pngFix();
	
	// All input fields assigned with the class 'default' will
	// get their default values hidden when clicked, and reset again
	// if the user leaves the field empty or hasnt changed anything
	// The searchfields needs an accompanying hidden input with the
	// same id but suffixed "-default" that contains the default value.
	$("input.default, textarea.default").each(function() {
		var defaultVal = $(this).next(".defaultvalue").val();
		if (!$(this).val())
			$(this).val(defaultVal);
		$(this).focus(function() {
			if ($(this).val() == defaultVal)
				$(this).val("");
		});
		$(this).blur(function() {
			if ($(this).val() == "")
				$(this).val(defaultVal);
		});
	});
	
	$(".validate").click(function() {
		var parentObj = $(".form");
		var validated = true;
				
		$(parentObj).find(".required").each(function() {
			
			if($(this).val() == "" || $(this).val() == $(this).next(".defaultvalue").val()) {
				$(this).addClass("has-error");	
				validated = false;
			}
		});

		if (!validated) {
			$("p.error").show();
			return false;
		}
		else if (validated) {
			$("p.error").hide();				
			$("#contactform").submit();	
		}
	});

	// Clear error when correcting it
	$(".required")
	.keydown(function() {
		if($(this).hasClass("has-error"))
			clearError($(this));
	});
	
	// Trigger the search function when using the ENTER key
	$("#site-search-input").keydown(function(e) {
		if (e.keyCode == 13) {
			var valobj = $(this).siblings("#site-search-submit");
			submitsearch($(valobj));
			return false;
		}
	});
	
	// Top search
	$("#site-search-submit")
	.click(function() {
		if($("#site-search").hasClass("expanded")) {
			submitsearch($(this));
		}
		else {
			$("#site-search").animate(
				{ width: 167 },
				{ queue: false, duration: 200,
					complete: function() {
						$("#site-search").addClass("expanded");
					}
				}
			);
			$("#site-search-content").addClass("active");
		}
		return false;
	});
	
	$("#site-search-input").blur(function() {
		if($("#site-search").hasClass("expanded")) {
			$("#site-search").animate(
				{ width: 29 },
				{ queue: false, duration: 200,
					complete: function() {
						$("#site-search").removeClass("expanded");
						$("#site-search-content").removeClass("active");
					}
				}
			);	
		}
	});
	
	$(".toggle").click(function() {
		$(this).closest(".richtext").find(".in-depth").slideToggle(200);
	});
	
	$("a.in-english").click(function() {
		// Create the dialog
		$("#lang-dialog").dialog({
			dialogClass: 'lang-dialog',
			modal: true,
			resizable: false,
			width: 600,
			close:function() {
				// Destroy the dialog-object when it is closed
				$(this).dialog("destroy");
			}
		});
		return false;
	});
});

function clearError(obj) {
	if (obj.val()) {
		obj.removeClass("has-error");
	}
}

function submitsearch(obj) {
	var url = $(obj).attr("href");
	url += url.indexOf("?") > -1 ? "&" : "?";
	url += "query=" + encodeURIComponent($(obj).siblings("input#site-search-input").val());
	document.location = url;
}

// Startpage animation
$(function() {
	$("#site-areas li")
	.mouseover(function() {
		$(this).stop();
		activate($(this).children(".area-content"));
		deactivate($(this).siblings("li").children(".area-content"));
	})
	.click(function() {
		var url = $(this).find("a").attr("href");
		document.location = url;
	});
	
	$("#site-areas ul")
	.mouseleave(function() {
		var obj = $("li .area-content");
		obj.stop();
		obj.children(".detail").fadeOut(500);
		obj.animate(
		{ height: 190, opacity: 1},
			{ queue: false, duration: 300,
				complete: function() {
					obj.removeClass("active");
				}
			}
		);	
	});
});

function activate(obj) {
	obj.children(".detail").fadeIn(500);
	obj.animate(
		{ height: 223, opacity: 1},
		{ queue: false, duration: 300,
			complete: function() {
				obj.addClass("active");
			}
		}
	);	
}

function deactivate(obj) {
	obj.children(".detail").fadeOut(500);
	obj.stop();
	obj.animate( 
		{ height: 190, opacity: 0.5},
		{ queue: false, duration: 300,
			complete: function() {
				obj.removeClass("active");
			}
		}
	);	
}
