/*
 * defaultValue - jQuery plugin to display and remove
 * a default value in a searchvalue on blur/focus
 *
 * Copyright (c) 2009 Raido Orumets
 * 
 * $Id: jquery.defaultValue.js 6233 2009-06-11 03:16:34Z raido.orumets $
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Clear the help text in a search field (either in the value or title attribute)
 * when focused, and restore it on blur if nothing was entered. If the value is
 * blank but there is a title attribute, the title will be moved to the initial value.
 *
 * @example $('#quicksearch').defaultValue();
 * @before <input id="quicksearch" title="Enter search here" name="quicksearch" />
 * @result <input id="quicksearch" value="Enter search here" name="quicksearch" />
 *
 * @name defaultValue
 * @type jQuery
 * @cat Plugins/DefaultValue
 */
 
/*

$(window).load(function(){
	$('.default-value').each(function() {
		var fld_current = this;
		var default_value = this.value;
		$(this).css('color', '#666'); // this could be in the style sheet instead
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
				$(this).css('color', '#333');
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				$(this).css('color', '#666');
				this.value = default_value;
			}
		});
		$(this).parents("form").each(function() {
			//Bind parent form submit
			$(this).submit(function() {
				if(fld_current.value==default_value) {
					fld_current.value='';
				}
			});
		});
	});
});

<input name="username" type="text" class="default-value" value="Username:" />

*/
 
jQuery.fn.defaultValue = function(mark, style_class){
	return this.each(function() {
		var mark = mark || this.title;
		var style_class = (style_class)?style_class:"field_default_value";
		
		if (!mark)
			return;
			
		var target = this;
		var original = $(this);
		if (this.type == "password") {
			target = $("<input />")
				.insertBefore(this)
				.css("display", $(this).css("display"))
				.attr("size", this.size)
				.attr("title", this.title)
				.attr("class", this.className)
				.attr("autocomplete", "off")
				.addClass(style_class)[0];
			if (!this.value) {
				$(this).hide();
			} else {
				$(target).hide();
			}
		}
		
		if(!target.value || mark == this.value) {
			$(target).addClass(style_class);
		}
		
		// setup initial value
		if (!this.value || target != this) {
			target.value = mark;
		}
		
		$(target).attr("autocomplete", "off");
		
		$(target).focus(function() {
				if (target != original[0]) {
				$(this).hide();
				original.show().focus();
			} else if (this.value == mark) {
				this.value = '';
				$(this).removeClass(style_class);
			}
		});
		$(this).blur(function() {
			if (!this.value.length) {
				if (target != original[0]) {
					$(target).show();
					original.hide();
				} else {
					this.value = mark;
					$(this).addClass(style_class)
				} 
			}
		});
		
		// make sure that when the form is submitted, the field_default_value is
		// replaced with the empty string, which is usually the expected behavior.
       $(this).parents("form:first").submit(function(){
           if ($(target).hasClass(style_class)) {
               $(target).attr("value", "");
               $(target).removeClass(style_class);
           }
       });
	});
};
