(function ($) {
    $(document).ready(function () {
        $("input:checkbox").ludercheck();

        $('.int').each(function (index, i) {
			$(i).mask({
				type: 'numeric'
			});
		});

        $('[data-watermark]').each(function (index, i) {
			$(i).watermark($(i).attr("data-watermark"));
		});

        $.each($("table.edit tbody tr, ul.zebra li"), function (i, el) {
			if (i % 2 == 0)
				$(el).addClass("even");
		});		
    });

    $.flash = function (text, options) {
		var defaults = {
			header: null
		};
		var opts = $.extend(defaults, options);

		var iw = $(window).width();
		var h = $("<div class='humanized'></div>");
		h.append(text);

		var t = null;
		$("body").prepend(h);
		var w = h.width();
		h.css("left", (iw / 2) - (w / 2) + "px");
		h.fadeIn(300, function () {
			$("body").mousemove(function () {
				t = window.setTimeout(function () {
					h.fadeOut(500);
				}, 2000);
			});
		});
	};

    $.fn.ludercheck = function (options) {
		var defaults = {
			containercss: 'check'
		};
		var opts = $.extend(defaults, options);
		return this.each(function () {
			var chk = "checked";
			var e = $(this).hide();
			var c = $("<div></div>").addClass(opts.containercss);
			e.hide().after(c);
			if (e.attr(chk))
				c.addClass(chk);

            c.mouseover(function(){
                e.trigger("focus");
            })

			c.click(function () {
				e.trigger("click");
			});
			e.change(function () {
				c.toggleClass(chk);
			});
		});
	};

    $.ludererrors = function (errors) {
		$(errors).each(function (i, err) {
			var ee = $("<span>Fejl</span>").hide();
			var ie = $("[name='" + err.p.toLowerCase() + "']");
			ie.parents("tr")
				.find("td:last")
				.append(ee);
			ee.fadeIn(500);

			var de = $("<div class='expanderflag'></div>").hide();

            if(ie.attr("type") == "checkbox"){
                ie.click(function(){
                    ie.trigger("keypress");
                })
            }


			ie.keypress(function () {
				de.slideUp(200, function () {
					ee.fadeOut(500);
				});
			});

			ie.focus(function () {
				de.empty();
				ee.children().remove();

				ee.append(de);
                de.append(err.l);
				de.slideDown(300);
			});

			ie.blur(function () {
				de.fadeOut(100);
			});
		});
	};

    $.fn.mask = function (options) {
		var defaults = {
			type: 'numeric'
		};
		var opts = $.extend(defaults, options);
		return this.each(function () {
			switch (opts.type) {
				case 'numeric':
					$(this).keypress(function (e) {
						if (e.which != 13 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
							e.preventDefault();
						}
					});
					break;
			}
		});
	};

    $.fn.watermark = function (text) {
		return this.each(function () {
			var el = $(this);
			var wm = $("<div class='watermark'>" + text + "</div>");
			el.parent().append(wm).mouseleave(add);
			var p = el.position();

			function add() {
				if (el.is(":focus"))
					return;
				if ($.trim(el.val()).length == 0) {
					wm
						.css("left", p.left + 5)
						.css("top", p.top + 5)
						.css("width", $.getwidth(el) - 10)
						.css("height", $.getheight(el) - 10)
						.mouseenter(remove);
					el.focus(function () {
						removenamed(wm);
					});
					wm.stop(true, true).show();
				}
			}
			add();

			el.blur(function () {
				if (text != null && $.trim(el.val()).length == 0)
					add();
			});

			function remove() {
				removenamed($(this));
			}

			function removenamed(item) {
				item.stop(true, false).hide();
			}
		});
	};

    $.getwidth = function(el) {
		return sumcss(el, el.width(), ["padding-left", "padding-right", "border-left-width", "border-right-width"]);
	};

	$.getheight = function (el) {
		return sumcss(el, el.height(), ["padding-top", "padding-bottom", "border-bottom-width", "border-top-width"]);
	};

	function sumcss(el, initial, props) {
		$(props).each(function (i, item) {
			initial += parseInt(el.css(item).replace("px", ""));
		});
		return initial;
	}
})(jQuery);
