/*! Copr. (c) 2010, 4delite */

$(document).ready(function()
{
	$('input[title][type="text"]').defaultText();
	$('input[title][type="password"]').defaultPassword();
	if (isOldFirefox())
		$("legend").addClass("fixLegend");
});

function openAuthor()
{
	return window.open("/Author.aspx", "author", "left=0,top=0,width=2000,height=2000,resizable=1"), false;
}

function rot13(s)
{
	return s.replace(
		/[a-zA-Z]/g,
		function(c)
		{
			return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
		}
	);
}

function preLoad(strAnalyticsProfile)
{
	$("a.rot13").each(
		function() {
			this.innerHTML = rot13(this.innerHTML);
			this.href = rot13(this.href);
			//alert(this.innerHTML);
		}
	);
	document.write(unescape("%3Cscript src='" + (("https:" == document.location.protocol) ? "https://ssl." : "http://www.") + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	setTimeout(
		function()
		{
			try
			{
				_gat._getTracker(strAnalyticsProfile || "UA-8647357-1")._trackPageview();
			}
			catch (err)
			{
			}
		}, 100
	);
}

$.fn.fontfit = function(max) {
	var max_size = 18;
	if (typeof (max) == "undefined")
		max = max_size;
	var dheight = $(this).height();
	$(this).wrapInner('<div id="fontfit"></div>');
	var ff = $("#fontfit");
	var cheight = ff.outerHeight();
	var fsize = (($(this).css("font-size")).slice(0, -2)) * 1;
	while (cheight < dheight && fsize < max) {
		fsize += 1;
		$(this).css("font-size", fsize + "px");
		cheight = ff.outerHeight();
	}
	while (cheight > dheight || fsize > max) {
		fsize -= 1;
		$(this).css("font-size", fsize + "px");
		cheight = ff.outerHeight();
	}
	ff.replaceWith(ff.html());
	return this;
}

$.fn.defaultText = function()
{
	var self = this;

	function clear()
	{
		$(this).filter(".default").val('').removeClass("default");
	}

	$(window).unload(function()
	{
		clear.apply($(self));
	});

	$("form").submit(function()
	{
		clear.apply($(self));
	});

	return $(this).blur(function()
	{
		var t = $(this);
		if (t.val() == '')
		{
			t.val('');
			t.addClass('default');
			t.val(t.attr('title'));
		}
	}).focus(clear).blur();
};

$.fn.defaultPassword = function()
{
	var self = this;

	function replace(t, type)
	{
		var nt;
		if ($.browser.msie)
		{
			var html = t[0].outerHTML;
			var newAttr = "type=" + type;
			var re = (type == 'password') ? /\btype=text\b/ : /\btype=password\b/;
			if (html.search(re) < 0)
				html = html.replace ("id=", newAttr + " id=");
			else
				html = html.replace (re, newAttr);
			nt = $(document.createElement(html));
		}
		else
		{
			nt = t.clone();
			nt.attr('type', type);
		}

		nt.attr('id', '__tmp__');
		var id = t.attr('id');
		nt.insertBefore(t);
		t.remove();
		nt.attr('id', id);

		return nt.blur(onBlur).focus(onFocus);
	}

	function onFocus()
	{
		var t = $(this);
		if (t.hasClass('default'))
		{
			var nt = replace(t, 'password');
			nt.val('');
			nt.removeClass('default');
			nt.focus();
		}
	}

	function onBlur()
	{
		var t = $(this);
		if (t.val() == '')
		{
			var nt = replace(t, 'text');
			nt.addClass('default');
			nt.val(nt.attr('title'));
		}
	}

	$("form").submit(function ()
	{
		$(self).filter(".default").val('').removeClass("default");
	});

	$(window).unload(function()
	{
		$(self).filter(".default").val('').removeClass("default");
	});

	return $(this).blur(onBlur).focus(onFocus).blur();
};

function onClickForgot()
{
	var elt = $("#" + _tbUserName);
	if (!elt.hasClass("default"))
	{
		var user = elt.val();
		if (user)
			$("#lnkForgot")[0].href += "?u=" + encodeURIComponent(user);
	}
	return true;
}


function isOldFirefox()
{
	if ($.browser.mozilla)
	{
		var ver = $.browser.version;
		var re = /(\d+)(\.(\d+)(\.(\d+)(\.(\d+))?)?)?/;
		var rg = ver.match(re);
		if (rg[1] != 1) return rg[1] < 1;
		if (rg[3] != 9) return rg[3] < 9;
		if (rg[5] != 2) return rg[5] < 2;
	}
	return false;
}
