$(window).load(function() {
	$("#content .galleryList a.galleryImg[rel=gallery]").each(function () {
		var imgHeight = $(this).find("img").height();
		if (imgHeight < 120) $(this).height(imgHeight);
	});

	$("#rightColumn .galleryList a.galleryImg[rel=gallery]").each(function () {
		var imgHeight = $(this).find("img").height();
		if (imgHeight < 150) $(this).height(imgHeight);
	});
});

$(function(){
	log("js start");

	$("header form input.text").focus(function () {
		if ($(this).val() == 'Søg...') $(this).val("").removeClass("inactive");
	}).blur(function() {
		if ($(this).val() == '') $(this).val("Søg...").addClass("inactive");
	}).val("Søg...").addClass("inactive");

	$("ul.sf-menu").superfish({
		animation: {height: 'show'},
		autoArrows: false,
		dropShadows: false,
		speed: 'fast'
	});

	$("form.contact").validate();
	$("form.contact a.button").click().click(function () {
		mailForm(this);
		return false;
	});
	
	$("ul li.right form a.button").click(function () {
		$(this).closest("form").submit();
	});

	$(".spotNavigation a").click(function () {
		$(".spotNavigation a").removeClass("selected");

		var index = $(this).addClass("selected").text() - 1;

		$(".spotGalleryLarge .spot").addClass("hidden");

		$(".spotGalleryLarge .spot:eq(" + index + ")").removeClass("hidden");
	});
	
	$(".galleryList a.galleryImg[rel=gallery]").colorbox( { photo: true });

	log("js end");
});

function mailForm(e) {
	var $form = $(e).closest("form");
	$form.validate();
	if ($form.valid()) {
		var url = "/umbraco/plugins/contact/contact.ashx";
		$.post(url, $form.serialize(), function (data) {
			var dataArr = data.split("|");
			if (dataArr.length > 1) {
				$("#" + dataArr[0] + " div.formFeedback").html(dataArr[1]).removeClass("hidden");
			}
			else {
				$("div.formFeedback").html(data).removeClass("hidden");
			}
		}, "html");
	}
}
