﻿$.fn.expandedSelect = function ()
{
	return this.each(function ()
	{
		var select = $("select", this).css("opacity", 0);
		var div = $("div", this);
		var selectPosition = select.position();

		div.css({ top: selectPosition.top, left: selectPosition.left }).show().html($(":selected", select).text());

		select.change(function ()
		{
			div.html($(":selected", this).text());
		});
	});
};


$(function ()
{
	var expressions = {
		string: /^[A-Za-z0-9æøå]+$/,
		stringwithspaces: /^[A-Za-z0-9_\-æøå ]+$/,
		email: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
		number: /^[0-9]+$/,
		numberwithspaces: /^[0-9 ]+$/,
		password: /^A-Za-z0-9æøå]+$/,
		minimal: /^.*$/
	};


	$(".usercontrol_textbox").each(function ()
	{
		var input = $("input[type='text'], input[type='password']", this);
		var hidden = $("input[type='hidden']", this);
		var data = input.data();
		var ref = input.val();
		var tooltip = $("#tooltip");


		hidden.val(input.val());


		input.keyup(function ()
		{
			hidden.val(this.value);

		}).focus(function ()
		{
			if (this.value == ref)
			{
				this.value = "";
			}
			else
			{
				this.select();
			}

			input.fadeTo(1, 300);

			var position = input.position();

			if (ref.length > 0)
			{
				tooltip.stop().find("span").html(ref).parent().css({ top: position.top - 27, left: position.left + (input.width() / 2) - (tooltip.width() / 2) }).fadeTo(600, 1);
			}
			else if ($(this).is(":password"))
			{
				tooltip.stop().find("span").html("Dit password").parent().css({ top: position.top - 27, left: position.left + (input.width() / 2) - (tooltip.width() / 2) }).fadeTo(600, 1);
			}

		}).blur(function ()
		{
			if (this.value != ref && this.value.length > 0)
			{
				input.addClass("valid");

				if (data.validation && !expressions[data.validation].test(this.value)) input.removeClass("valid");
			}
			else
			{
				this.value = ref;

				input.removeClass("valid");
			}

			$("#tooltip").hide();
		});
	});


	$(".usercontrol_checkbox").each(function ()
	{
		var a = $("a", this);
		var hidden = $("input[type='hidden']", this);

		a.click(function ()
		{
			if (a.hasClass("active"))
			{
				hidden.val("0");

				a.removeClass("active");
			}
			else
			{
				hidden.val("1");

				a.addClass("active");
			}

			return false;
		});
	});
});
