(function() {
	/**
	* Create YATIMA namespace
	*/
	if(!window.YATIMA) {window['YATIMA'] = {} }
	
	/**
	 * @requires jQuery
	 * Validates fields in form
	 */
	 function validate_fields(form_id) {
	 	
	 	// Diffrent validators for diffrent types of field to validate.
 	 	function validator(fn, value) {
 	 		var email_pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
 	 		if(fn == "email") {
 	 			return email_pattern.test(value);
 	 		}
 	 		else if(fn == "text") {
 	 			if(value !== "") {
 	 				return true;
 	 			}
 	 			return false;
 	 		}
 	 	}
 	 	
		var form = $(form_id);
		var valid_form = true;
		var error = {
			class : "message-error",
			text : "Obligatoriskt fält",
			text_class : "message"
		};
		
		function validate(field_jquery, display_valid) {
			var type = field_jquery.attr("class");
			var value = field_jquery.val();
			if(!validator(type, value)) {
				valid_form = false;
				field_jquery.parent().addClass(error.class);
				if(field_jquery.next("." + error.text_class).length == 0)
					field_jquery.after("<span class=\""+ error.text_class +"\">" + error.text +"</span>");
			}
			else {
				if(display_valid) {
					field_jquery.next("." + error.text_class).remove();
					field_jquery.parent().removeClass(error.class).css("backgroundColor", "rgba(140, 177, 85, 0.2)").animate({
					    "backgroundColor": "rgba(140, 177, 85, 0)",
					 }, 1500, function() {
					 	field_jquery.parent().removeAttr("style");
					 });
				}
			}
		};
		
		form.find(".field input, .field textarea").each(function() {
			$(this).blur(function() {
				validate($(this), true);
			});
		});
		form.submit(function() {
			form.find(".field input, .field textarea").each(function() {
				validate($(this), false);
			});
			var temp_valid = valid_form;
			valid_form = true;
			return temp_valid;
		});
		

	 };
	 window['YATIMA']['validate_fields'] = validate_fields;
	 
	 /**
	  * @requires jQuery
	  * Changes classes on label to move and style inside the correct textfield
	  */
	 
	 function populate_with_label(label_class) {
	 	var classes = {
	 		inside : "inside",
	 		hidden : "hidden",
	 		focused : "focused"
	 	};
		$(label_class).each(function() {
			var label = $(this).children("label");
			var textfield = $(this).children("input")
			label.addClass(classes.inside);
			if(textfield.val() != "") {
				label.addClass(classes.hidden);
			}
			textfield.focus(function() {
					label.addClass(classes.focused);
			});
			textfield.blur(function() {
					label.removeClass(classes.focused);
					if($(this).val() == "")
						label.removeClass(classes.hidden);
			});
			textfield.keydown(function() {
				if($(this).val() != "") {
					label.removeClass(classes.focused).addClass(classes.hidden);
				}
			});
		});
	 };
	 window['YATIMA']['populate_with_label'] = populate_with_label;
	 
	/**
	* @requires jQuery
	* Makes a div floating with at the top while user scroll down
	*/
	  
	function make_div_float(div) {
		var div_jquery = $(div);
		if(div_jquery.length > 0) {
			var top_height = 260;
			var max_padding = parseInt($("#content-primary").height()) - parseInt(div_jquery.height());
			var offset = (($(document).scrollTop() >= top_height) ? $(document).scrollTop() - top_height : 0) + "px";
			
			if(parseInt(offset) < max_padding) {
				div_jquery.animate({paddingTop:offset},{duration:600,queue:false});
			}
			
			$(window).scroll(function () { 
				var offset = (($(document).scrollTop() >= top_height) ? $(document).scrollTop() - top_height : 0) + "px";
				if(parseInt(offset) < max_padding) {
					div_jquery.animate({paddingTop:offset},{duration:600,queue:false});
				}
			});
		}
	};
	window['YATIMA']['make_div_float'] = make_div_float;
	
	/**
	 * @requires jQuery
	 * Makes a div floating with at the top while user scroll down
	 */
	
	function highlight_comment() {
		var hash_jquery = $(window.location.hash);
		if(hash_jquery.length == 1 && window.location.hash.indexOf("comment-") != -1) {
			hash_jquery.css("backgroundColor", "rgba(255, 236, 99, 0.3)").animate({
			    "backgroundColor": "rgba(255, 236, 99, 0)",
			 }, 1500, function() {
			 	hash_jquery.removeAttr("style");
			 });
		}
	};
	window['YATIMA']['highlight_comment'] = highlight_comment;
	
	/**
	 * @requires jQuery
	 * Makes the primary content layer equal height as the secondary content layer, if to short.
	 */
	
	function set_layerheight() {
		var secondary_height = $("#content-secondary").height(),
		primary_jquery = $("#content-primary");
		if(secondary_height > primary_jquery.height()) {
			primary_jquery.css("min-height", $("#content-secondary").height());
		}
	};
	window['YATIMA']['set_layerheight'] = set_layerheight;
	
	/**
	 * @requires jQuery
	 * Sets up the gallery
	 */
	 
	function gallery() {
		var site_imagefolder = "http://www.yatima.se/system/img/uploads/";
		if($(".gallery-item").length > 0) {
			$("#content-secondary").html($("#gallery").html()).css("paddingTop", "2.9em");
			$("#content-secondary .gallery-item dt").each(function() {
				$(this).next("dd").hide();
			});
			if(window.location.hash != "") {
				var hash_img = window.location.hash.split("#");
				var img_src = site_imagefolder + hash_img[1];
			}
			else {
				var img_src = $("#content-secondary .gallery-icon a:first").attr("href");
			}
			$("#gallery").html("<img src=\"" + img_src + "\" width=\"509\" height=\"309\" />");
			$(".gallery-icon a").live("click", function(event) {
				var img_src = $(this).attr("href");
				var id = img_src.split("uploads/");
				var caption = ($(this).parent().next("dd").length != 0 ? "<p class=\"caption\">" + $(this).parent().next("dd").html() + "</p>" : "");
				$("#gallery").html("<img src=\"" + img_src + "\" width=\"509\" height=\"309\" />" + caption);
	
				window.location.hash = "#" + id[1];
				event.preventDefault();
				 
			});
		}
	};
	window['YATIMA']['gallery'] = gallery;
	
	/**
	 * @requires jQuery
	 * Sets aria role-attributes on elements
	 */
	 
	function set_roles(roles) {
		for(var role in roles) {
			$(roles[role]).each(function() {
				$(this).attr("role", role);
			});
		}
		
	};
	window['YATIMA']['set_roles'] = set_roles;

})();
