$(document).ready(function() {
	// 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);
		});
	});
	
		
	$(".password-text")
	.focus(function() {
		$(this).hide();
		$("input.password-password").show();
		$("input.password-password").focus();
	});
	
	$(".password-password")
	.blur(function() {
		if($(this).val() == "") {
			$(this).hide();
			$(".password-text").val($(".password-text").next(".defaultvalue.pass").val());
			$(".password-text").show();
			
		}
	});
	
	$(".password-password").keydown(function(e) {
		if (e.keyCode == 13) {
			$("#loginSubmit").click();
		}
	});

	
	// Formulärsubmit för inloggning
	$("#loginSubmit").click(function(e) {
		document.forms['aWebForm1'].submit();
	});
});

// Enable browser-history for the image-switcher
$(function() {
	var index = 0;
	var numImages = $(".gallery li").length;
			
	$(".gallery li").not(":first-child").hide();
	$(".gallery li:first-child").addClass("active");
	updateText(index, numImages, $(".gallery li.active img").attr("alt"));
	
	if(numImages > 1) {	
		$("#prev, #next, .gallery li")
		.click(function() { 
			if ($(this).attr("id") == "prev")
				index--;
			else
				index++;
			if (index < 0)
				index = numImages-1;
			else if(index >= numImages)
				index = 0;
				
			switchImage(index);
			updateText(index, numImages, $(".gallery li.active img").attr("alt"));
		});
	}
	else {
		$(".gallery li img").css("cursor", "default");
		$("#next, #prev, .count").hide();
		$(".gallery .bar p").addClass("single");
	}
});

function updateText(index, numImages, descr) {
	
	$(".gallery span.descr").html(descr);
	$(".gallery span.count").html(index+1+" / "+ numImages);
}

function switchImage(index) {
	
	// Find and activate image
	$(".gallery li.active").hide().removeClass("active");
	var imageToView = $(".gallery li:eq(" + index + ")");
	imageToView.fadeIn().addClass("active");
}

