﻿$(document).ready(function(){
	
	// Clear default value for the inputfields
	$("input.default").each(function() {
		var defaultVal = $(this).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);
		});
	});
	
	// Change the color of the input and the textfield for the comment form
	$("input.validate").click(function() {
		var parentObj = $(this).closest(".validate-form");

		$(parentObj).find(".required").each(function() {
			if($(this).val() == "") {
				$(this).addClass("has-error");	
			}							   
		});
	});

	// Clear error when correcting it
	$(".required").focus(function() {
		clearError($(this));
	}).keydown(function() {
		clearError($(this));			
	});
	
	// Show UI dialog when reporting a comment
	$(".report").click(function() {
		$("#dialog").dialog({
			bgiframe: true,
			modal: true,
			closeText: 'stäng',
			close: function() {
				$(this).dialog("destroy");
			}
		});
	});

	// Customer-club form
	var custClubWrapped = false;
	$("#customerclub input.button").click(function() {
		// First create an html-form-element
		if (!custClubWrapped) {
			$("#customerclub").wrapInner($("<form />").attr( {
				action: "http://onserver.se/collectUser.aspx",
				method: "post",
				enctype: "multipart/form-data"
			}));
			$("#customerclub form").validate();
			custClubWrapped = true;
		}

		// Post the form
		$("#customerclub form").submit();

		return false;
	});
	
	// Hide comments, if not the url tells otherwise.
	$(".comments-list").each(function(){
		var id = $(this).closest(".blog-item").attr("id");
		if (id != null && "#" + id != location.hash) {
			$(this).hide();	
		}	
	});
	
	// Collapse/expand blog comments.
	$(".blog-controls a.comments").click(function(){
		var content = $(this).closest(".blog-controls").children(".comments-list");
		content.slideToggle(500);
		if(!$(content).hasClass("sifrRendered")){
			sifr_replace();
			$(content).find("h2").each(function() {
				$(this).css("height", $(this).height());
			});
			$(content).addClass("sifrRendered")
		}
	});
});

function clearError(obj) {
	if (obj.val())
		obj.removeClass("has-error");
}

