﻿$(document).ready(function(){	
	
	// check for what is/isn't already checked and match it on the fake ones
	$("input:checkbox").each( function() {
		(this.checked) ? $("#fake"+this.id).addClass('fakechecked') : $("#fake"+this.id).removeClass('fakechecked');
	});
	// function to 'check' the fake ones and their matching checkboxes
	$(".fakecheck").click(function(){
		($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
		$(this.hash).trigger("click");
		return false;
	});

	// 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);
		});
	});
	
	// Subscription form
	$("#subscribe .button").click(function() {
		if (validate($("#subscribe"))) {
			// Don't send default values
			$("#subscribe").find("input.default, textarea.default").each(function() {
				if ($(this).val() == $(this).next(".defaultvalue").val()) {
					$(this).val("");
				}
			});
			// Send the form
			$.ajax({
				type: "POST",
				url: $(this).attr("href"),
				data: $("#subscribe :input").serialize(),
				success: function(msg) {
					$("#subscribe .dialog-content").html(msg);
				}
			});
		}
		return false;
	});
	
	// Clear graphical error on required fields when correcting it
	$("input.required, textarea.required")
	.keydown(function() {
		if ($(this).val())
			$(this).removeClass("has-error");			
	});
	$("a.fakecheck.required")
	.click(function() {
		if ($(this).hasClass("fakechecked"))
			$(this).removeClass("has-error");
		else
			$(this).addClass("has-error");
	});
	
	// Hovermenu for country
	$("li.hover-link").hover(
		function() {
			var offset = 8;
			var hovermenu = $(this).children(".hovermenu");
			var posx = hovermenu.width() - $(this).width() - offset;
			hovermenu.css("left", -posx);
			hovermenu.show();
		},
		function() {
			$(this).children(".hovermenu").hide();
		}
	);
	
	// Toggle archive
	$("p.toggle").click(function() { 
		$(this).next(".expandable").toggle();
		$(this).toggleClass("expanded");
		return false;
	});

	// Open corresponding image in lightbox when clicking the paginator
	$(".gallery-nav li").click(function() {
		var index = $(this).prevAll().length;
		$(".lightbox:eq(" + index + ")").click();
	});

	// Print button
	$("a.printit").click(function() { window.print(); return false; });
	
	/*
	 * All search-boxes requires three elements:
	 *  div.search-box (container)
	 *  input.search-query
	 *  a.search-button
	 */
	// Submit searches on mouse-clicks
	$("a.search-button").click(function() {
		submitSearch($(this).closest("div.search-box"));
		return false;
	});
	// Submit searches on ENTER-key
	$(".search-query").keydown(function(e) {
		if (e.keyCode == 13) {
			submitSearch($(this).closest("div.search-box"));
			return false;
		}
	});

	// SIS start top-image
	$('.shiny-sisstart #top-banner').cycle({ fx: 'fade' });

	// SIS start dropdowns
	var wizardEntriesVisible = false;
	function position(using) {
		$(".wizardentry").each(function() {
			$(this).find(".wizardentry-dropdown").position({
				of: $(this),
				my: "left top",
				at: "left bottom",
				offset: "0 -1px",
				using: using,
				collision: "flip"
			});
		});
	}
	$(".wizardentry-selector").click(function(event) {
		// Dont send the click-event to the body-binding
		event.stopPropagation();
		// Hide all other dropdowns
		$(".wizardentry-dropdown").not($(this).next()).hide();
		// Toggle the current dropdown
		$(this).next().toggle();
		// Update the flag that avoids repositioning of ddls if none are visible when scrolling
		wizardEntriesVisible = $(".wizardentry-dropdown:visible").length > 0;
		// Reposition ddl
		position();
	});
	$(window).bind("scroll resize", function() {
		if (wizardEntriesVisible)
			position();
	});
	$("body").click(function() {
		$(".wizardentry-dropdown").hide();
	});
});

//
// Validates all input and textarea children of the passed object.
// Adds a red border on required input/textareas that are not correctly filled.
//
function validate(obj) {
	var validated = true;
	
	// Dont send anything if one of the fields are equal to the default vaule
	$(obj).find("input.default.required, textarea.default.required").each(function() {
		if($(this).val() == $(this).next(".defaultvalue").val()) {			
			validated = false;
		}
	});
	
	// Add a class if the field is required as not correctly filled.
	$(obj).find("input.required, textarea.required").each(function() {
		if($(this).val() == "" || $(this).val() == $(this).next(".defaultvalue").val()) {
			$(this).addClass("has-error");	
			validated = false;
		}
	});
	$(obj).find(".fakecheck.required").each(function() {
		var checked = $(this).next("input.fake").get(0).checked;
		$(this).removeClass("has-error");	
		if (!checked) {
			$(this).addClass("has-error");	
			validated = false;
		}
	});
	
	return validated;
}

//
// Submits a search query
//
function submitSearch(searchBox) {
	if (!validate(searchBox))
		return;
	var query = searchBox.find("input.search-query").val();
	var url = searchBox.find("a.search-button").attr("href");
	url += url.indexOf("?") > -1 ? "&" : "?";
	url += "query=" + encodeURIComponent(query);
	document.location = url;
}

