/**
 * jQuery-Plugin "smartinput"
 * 
 * @version: 1.0, 08.25.2009
 * 
 * @author: Dan Lacy
 *			daniell@arc90.com
 *          Tyler Gaw
 *          tylerg@arc90.com
 *
 * 
 * @example: $('selector').smartinput();
 * 
 * @example: $('selector').smartinput({defaultValue:"Removes on focus",focusClass:"active"});
 * 
 */

;(function($) {
	var settings     = {};
	
	jQuery.fn.smartinput = function(options) {
		
		settings = jQuery.extend({}, $.fn.smartinput.defaults, options);
		
		jQuery(this).each(function() {
			var $this = $(this);
			
			var defaultValue = $this.val() || settings.defaultValue;
			var blurAction   = settings.blurAction || null;
			
            /*
                Author: Dan
                Note: I think we should switch this, so that on focus, it removes the focusClass.
                    On blur, add the class back in. That way we can default inputs to a legible 
                    styling instead of hinted text.
            */
			$this.focus(function() 
			{
			    $(this).addClass(settings.focusClass);
			    if ($(this).val() == defaultValue) 
				{
			        $(this).attr('value', '');
			    }
			});


			$this.blur(function() 
			{
			    if ($(this).val() === '')
			    {
			        $(this).removeClass(settings.focusClass).attr('value', defaultValue);
					
					if(blurAction != null)
					{
						blurAction();
					}
			    }
			});

		});
		return jQuery;
	};

	
	
	/**
	 * Create defaults settings as a public object
	 * this allows users to easily overwrite defaults
	 *
	 * (e.g. $.fn.smartinput.defaults.item = 'value')
	 *
	 */
	jQuery.fn.smartinput.defaults = {
		defaultValue: 'Default Value',
		focusClass: 'active',
		focusAction: null,
		blurAction: null
	};

})(jQuery);