/*
 * jQuery disappearing-labelled box (la-box)
 * http://www.redberrydigital.com
 *
 */
(function($)
{
	$.fn.labox = function( options )
	{		
		options = $.extend( {}, $.fn.labox.defaults, options );
		return this.each(function() {
		  init( $(this), options );
		});
	};

	$.fn.labox.defaults =
	{
	    activeClass:   	"labox-active",
	    inactiveClass: 	"labox-inactive",
	    errorClass: 	"labox-error"
	};

	function init( labox, options )
	{		
	    // On gaining focus...
	    labox.focus( function() {
	    	if( labox.attr("value") == labox.attr("title") )
	    	{
	    		labox.attr( "value", "" );
	    	}
	    	labox.removeClass(options.errorClass).removeClass(options.inactiveClass).addClass(options.activeClass);
	    });
	
	    // On losing focus...
	    labox.blur( function() {
	    	if( ! labox.attr("value").replace(/\s+/gi,"").length )
	    	{
	    		labox.removeClass(options.activeClass).addClass(options.inactiveClass).attr( "value", labox.attr("title") );
	    	}
	    });
	    
	    labox.parents('form').submit( function () {
	    	if( labox.attr("value") == labox.attr("title"))
	    	{
	    		labox.attr( "value", "" );
	    	}	    	
	    })

	    // Init labox if required.
	    if( labox.attr("value") == labox.attr("title") || labox.attr("value") == "" )
	    {
	    	labox.attr("value", labox.attr("title"));
	    	labox.addClass(options.inactiveClass);
	    }
	    else
	    {
	    	labox.addClass(options.activeClass);
	    }
	}
})(jQuery);

