if(typeof jQuery !== 'undefined')
{

(function($) {
	$(document).ready(function() {
		// Remove all form error content.
		j('.formerror').html('').hide();
	});

	$.fn.formerror = function(msg, before)
	{
		if(!msg) { msg = 'Missing Information'; }
		var id = j(this).attr('id');
		var errorid = id+"_error";
		if(!j("#"+errorid).size())
		{
			var container = "<div id='"+errorid+"' class='formerror'></div>";
			if(before)
			{
				j('#'+id).before(container);
			} else {
				j('#'+id).after(container);
			}
		}
		//console.log(msg);
		j('#'+errorid).html(msg).show();

		return false;
	};

	$.fn.ghostable = function(text)
	{
		var original = $(this);
		var overlay = original.clone();
		overlay.attr('id', original.attr('id')+"_clone");
		overlay.attr('name', '');//original.attr('name')+"_ghostable");
		overlay.addClass('ghost');
		original.after(overlay);
		overlay.val(text);

		overlay.click(function() { overlay.hide(); original.show(); original.focus(); });
		overlay.select(function() { overlay.hide(); original.show(); original.focus(); });
		original.blur(function() { if(!original.val()) { overlay.show(); original.hide(); } });

		if(original.val())
		{
			overlay.hide();
		} else {
			original.hide();
		}
	};


	$.fn.ghostable2 = function(text) // Disabled element that won't submit or be gathered unless custom text.
	{
		$(this).wrap("<span class='ghostable_wrapper'/>");

		var $original = $(this);
		var $parent = $original.closest('span.ghostable_wrapper');
		var $overlay = $(this).clone();
		$overlay.val(text);
		//$("<div class='ghostable_overlay'>"+text+"</div>");	

		// style the overlay
		$overlay.css({
		      // position the overlay in the same real estate as the original parent element 
		        position: "absolute"
		      , top: $parent.position().top
		      , left: $parent.position().left
		      , width: $parent.outerWidth()
		      , height: $parent.outerHeight()
		      , zIndex: 10000
		      // IE needs a color in order for the layer to respond to mouse events
		      , backgroundColor: "#fff"
		      // set the opacity to 0, so the element is transparent
		      , opacity: 0
		    })
		    // attach the click behavior
		    .click(function (){
		    	$self.show(); // Show
			$(this).hide();
		    	// Hide me, focus on original.
		      // trigger the original event handler
		      //return $self.trigger("click");
		    });
		
		    // add the overlay to the page  
		$parent.append($overlay);
	};


})(jQuery);

}

