﻿$(document).ready(function(){
	
	// Fixes PNG transparency on the video thumbnails in IE
	$("a.video-thumb").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 inputs in #related. Used for störningsinfo sms and email subscription.
	$("#related .validate").click(function() {
		if(validate($(this))) {
			setStatus($(this).parent(), true);
		}
		else {
			setStatus($(this).parent(), false);
			return false;
		}
	});
	
	// Validate entire forms
	$(".validate-form .validate").click(function() {
		
		if(validate($(this).parent())) {
			setStatus($(this).parent().parent(), true);
		}
		else {
			setStatus($(this).parent().parent(), false);
			return false;
		}
		
	});
	
	// Hook up the searchbox
	$(".search-box .validate").click(function() {
		if(validate($(this))) {
			submitsearch($(this));
		}
		return false;
	});
	
	// Trigger the search function when using the ENTER key
	$(".search-box input").keydown(function(e) {
		if (e.keyCode == 13) {
			var valobj = $(this).siblings(".validate");
			
			if(validate($(valobj))) {
				submitsearch($(valobj));
			}
			
			return false;
		}
	});
	
	// Expand the next content, used for contact and QA
	$(".expandable").click(function() {
		$(this).next(".expand-content").toggle();
		$(this).toggleClass("expanded");
		return false;
	});
	
	// Clear graphical error on required fields when correcting it
	$("input.required, textarea.required")
	.keydown(function() {
		clearError($(this));			
	});
	
	// Show UI dialog when reporting a comment
	$(".report a").click(function() {
		// Create the dialog
		$("#report-dialog").dialog({
			title: $(this).html(),
			dialogClass: 'report-dialog',
			modal: true,
			resizable: false,
			width: 370,
			closeText: 'stäng',
			close:function() {
				// Destroy the dialog-object when it is closed
				$(this).dialog("destroy");
			}
		});

		// When a dialog is opened it is moved outside the ASP.NET-form. Workaround that.
		$(".ui-dialog").appendTo("form");

		// Add the comment-id to the form as a backreference
		$(".reportedreference").val($(this).attr("href").substring(1));

		return false;
	});
});

//
// Show and hide the status text for a form
//
function setStatus(obj, val) {
	obj.find(".status p").hide();
	if(val) {
		obj.find(".status .success").show();
	}
	else {
		obj.find(".status .error").show();
	}
}

// 
// Clear error on required fields
//
function clearError(obj) {
	if (obj.val())
		obj.removeClass("has-error");
}

//
// Validates all input and textarea children of the passed object.
// Adds a yellow background on required input/textareas that are not correctly filled.
//
function validate(obj) {
	
	var validated = true;
	var parentObj = $(obj).parent();
	
	// Dont send anything if one of the fields are equal to the default vaule
	$(parentObj).find("input, textarea").each(function() {
		if($(this).val() == $(this).next(".defaultvalue").val()) {
			validated = false;
		}
	});
	
	$(parentObj).find(".required").each(function() {
		if($(this).val() == "" || $(this).val() == $(this).next(".defaultvalue").val()) {
			$(this).addClass("has-error");	
			validated = false;
		}
	});
	
	$(parentObj).find(".required.email").each(function() {
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		if(!emailPattern.test($(this).val())) {
			$(this).addClass("has-error");	
			validated = false;
		}
	});

	if (!validated) {
		return false;
	}
	else {
		return true;
	}
}

//
// Submits a search query
//
function submitsearch(obj) {
	var url = $(obj).children().attr("href");
	url += url.indexOf("?") > -1 ? "&" : "?";
	url += "query=" + encodeURIComponent($(obj).siblings("input.search-input").val());
	document.location = url;
}

// 
// Create a dialog and put a flash video player in it.
//
function showVideoThickbox(root, isYouTube, url, info) {
	// Create a new dialog-object each time, workaroung a bug where it prevents clicks in the entire window
	$("#video-thickbox").dialog({
	    modal: true,
		resizable: false,
		width: isYouTube ? 580 : 550,
	    overlay: {
	        opacity: 0.5,
	        background: "black"
	    },
		close:function() {
			// Destroy the dialog-object when it is closed
			$(this).dialog("destroy");
		}
	});

	// Set fallback contents
	$("#movie-container").html('<a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.');
	$("#movie-info").html(info);

	// Display video-player
	if (isYouTube) {
		// YouTube
		$("#movie-container").html('<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/' + url + '&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + url + '&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>');
	} else {
		// FLV Player
		$("#movie-container").html($("<div/>").attr("id", "flv-placeholder"));
		var flashvars = {
			flashvars: "file=../" + url
		};
		var params = {
			menu: "false",
			scale: "noScale",
			wmode: "transparent"
		};
		swfobject.embedSWF(
			root + "static/player.swf",
			"flv-placeholder",
			"531",
			"300",
			"9.0.0",
			"#ffffff",
			root + "static/expressInstall.swf",
			flashvars,
			params);
	}

	return false;
}

