﻿/**
 * @author slackday.se
 */
var html = document.getElementsByTagName("html")[0];
html.className += html.className.length === 0 ? "javascript" : " javascript";

var BABYNEST = function() {
	var DOMReady = function() {
		BABYNEST.form.initialize();
		//BABYNEST.menu.initialize();
	};
	return {
		initialize: function() {
			/*@cc_on
			@if (@_jscript_version < 5.7)
				try {
					document.execCommand("BackgroundImageCache", false, true);
				} catch (e) {}
			@end @*/
			$(document).ready(DOMReady); 
		},
		isVisible: function(el) {
			return !(jQuery(el).is(":hidden") || jQuery(el).parents(":hidden").length);;
		}
	};
}();

BABYNEST.form = function() {
	var validateField = function(elm) {
		var field = elm;
		var form = field.form;
		elm = $(elm);
		if (elm.hasClass("required") && (jQuery.trim(field.value) == "")) {
			form.missing.push(field);
			form.hasErrors = true;
		} else if (elm.hasClass("accept-conditions") && !field.checked) {
			form.unaccepted.push(field);
			form.hasErrors = true;
		} else if (elm.hasClass("email") && jQuery.trim(field.value) != "" && field.value.search(/(\w|\.|\-)+\@(\w|\.|\-)+\.[a-z]{2,6}$/)) {
			form.erronous.push(field);
			form.hasErrors = true;
		} else if (elm.hasClass("numeric") && (/\D/).test(O.numericStrip(field.value))) {
			form.erronous.push(field);
			form.hasErrors = true;
		} else {
			elm.removeClass("has-error");
		}
	};
	
	var alertUser = function(form) {
		var hideDuration = 1;
		var alertBox = $(form).find(".alert-box");
		if (alertBox.length == 0) {
			alertBox = $("<div class=\"new-row alert-box\"></div>");
			$(form).find(".submit").before(alertBox);
			alertBox.hide();
		} else {
			hideDuration = 250;
		}
		alertBox.fadeOut(hideDuration, function() {
			var alertText = "<h2>Oj, n&aring;gonting gick visst fel...</h2><ul>";
			for (var e = 0, l = form.erronous.length; e < l; e++) {
				$(form.erronous[e]).addClass("has-error");
				alertText += "<li>Kontrollera att " + form.erronous[e].title + " &auml;r korrekt" + "</li>";
			}
			for (e = 0, l = form.missing.length; e < l; e++) {
				$(form.missing[e]).addClass("has-error");
				alertText += "<li>Du har inte fyllt i " + form.missing[e].title + "</li>";
			}
			for (e = 0, l = form.unaccepted.length; e < l; e++) {
				alertText += "<li>Du m&aring;ste godk&auml;nna " + form.unaccepted[e].title; + "</li>"
			}
			alertText += "</ul>";
			alertBox.html(alertText);
			alertBox.fadeIn(250);
		});
	};
	
	return {
		initialize: function() {
			var form = $("form.validate");
			form.submit(function() {
				return BABYNEST.form.validate(this);
			});
			form.find(".submit INPUT:reset").bind("click", function() {
				BABYNEST.form.reset(form);
			});
			$("form.validate .has-error").live("keyup", function() {
				validateField(this);
				return false;
			}); 
		},
		validate: function(form){
			form.erronous = [];
			form.missing = [];
			form.unaccepted = [];
			form.hasErrors = false;
			
			$(form).find("input, textarea").each(function() {
				if (BABYNEST.isVisible(this)) {
					validateField(this);
				}
			});
			
			if (form.hasErrors) {
				alertUser(form);
				return false;
			} else {
				return true;
			}
		},
		reset: function(form) {
			form.find("input, textarea").removeClass("has-error");
			form.find(".alert-box").hide();
		}, 
	};
}();

BABYNEST.menu = function() {
	return {
		initialize: function() {
			$("#menu").find("a").each(function() {
				$(this).bind("click", function(e) {
					e.preventDefault();
					$.get(this.href, function(data) {
						var html = $(data).find("#content").contents();
						var content = $("#content");
						content.html(html.get());
						content.css("display", "none");
						content.fadeIn("fast");
					});
				});
			});
		}
	};
}();

BABYNEST.initialize();