// Timothy Alford
// 16/9/2010
// default text for values, works by using a fake textbox
(function ($) {
	$.fn.defaultText = function () {
		return this.each(function () {
			var self = $(this);

			var defaultCssClassName = "default";
			var fakeExtension = "-fake";

			// HTML template
			var template;
			if ($(this).is("input")) {
				template = "<input type=\"text\" id=\"{ID}\" value=\"{VALUE}\" class=\"{CLASS}\" />";
			}
			else {
				template = "<textarea id=\"{ID}\" class=\"{CLASS}\">{VALUE}</textarea>";
			}

			//alert("current classes = " + $(this).attr("class"));
			defaultCssClassName = $(this).attr("class") + " " + "hidden";

			var fakeId = self.attr("id") + fakeExtension;
			var newText = template.replace("{ID}", fakeId).replace("{VALUE}", self.attr("title")).replace("{CLASS}", defaultCssClassName);

			self.after(newText);

			if ($(this).val() != "") {
				$("#" + fakeId).hide();
			}
			else {
				$(this).hide();
			}

			$(this).blur(function () {
				var thisObj = $(this);
				if (thisObj.val() == "") {
					thisObj.hide();
					$("#" + thisObj.attr("id") + fakeExtension).show();
				}
			});

			$("#" + fakeId).focus(function () {
				$(this).hide();
				var realText = $("#" + fakeId.replace(fakeExtension, ""));
				realText.show();
				realText.focus();
			});
		});
	}
})(jQuery);
