/*
 * jQuery defaultValue plugin
 *
 * Copyright (c) 2011 Tommy Ullberg (www.imcode.com)
 * Licensed under the GPL license and MIT:
 *   http://www.opensource.org/licenses/GPL-license.php
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Version: 1.3 (2011-09-20)
 * 
 * Usage :
 *  jQuery(function($) {
 *    $('form').defaultValue('lightgrey') ;
 *    OR:
 *    $('.defaultValue').defaultValue('dimText') ;
 *  }) ;
 *  form fields of type text|password or textareas that has class='defaultValue'.
 *  Eg:
 *  <input type="text" name="myInput" value="" class="formField defaultValue" title="My default value" />
 *  ...are fixed so that they get another specified CSS class while not changed, and a default value from the title.
 *  The title is then removed:
 *  <input type="text" name="myInput" value="My default value" class="formField defaultValue lightgrey" title="" />
 *  To temporarily remove the default values, eg. for validation - use:
 *  $.defaultValueRemove() ;
 *  To restore them again - use:
 *  $.defaultValueRestore() ;
 *  Note: Is has to be initiated before these are used. (line #13)
 */

var $defaultValueSelector = null ;

jQuery.fn.defaultValue = function(classWhileDefaultValue) {
	$defaultValueSelector = this
					.filter('input[type=text],input[type=password],textarea')
					.filter('.defaultValue') ;
	return $defaultValueSelector.each(function() {
		var $ = jQuery ;
		var $this = $(this) ;
		var thisType = $this.attr('type') ;
		if (!$this.hasClass('defaultValue') || (!/^(text|password)$/i.test(thisType) && !$this.is('textarea'))) {
			return ;
		}
		var hasClass = (null != classWhileDefaultValue && '' != classWhileDefaultValue) ;
		var theValue = $this.attr('title').replace(/\r\n/g, '\n') ;
		$this.data('defaultValue', theValue) ;
		$this.removeAttr('title') ;
		$this.focus(function() {
			if (theValue == $this.val() || '' == $this.val()) {
				$this.val('') ;
				if (hasClass) {
					$this.removeClass(classWhileDefaultValue) ;
				}
			}
		}) ;
		$this.blur(function() {
			if (theValue == $this.val() || '' == $this.val()) {
				$this.val(theValue) ;
				if (hasClass) {
					$this.addClass(classWhileDefaultValue) ;
				}
			}
		}) ;
		$this.parents('form').submit(function() {
			if (theValue == $this.val()) {
				$this.val('') ;
				if (hasClass) {
					$this.removeClass(classWhileDefaultValue) ;
				}
			}
		}) ;
		if ('' == $this.val()) {
			$this.val(theValue) ;
			if (hasClass) {
				$this.addClass(classWhileDefaultValue) ;
			}
		}
	}) ;
} ;


jQuery.defaultValueRemove = function() {
	$defaultValueSelector.each(function() {
		var $this = $(this) ;
		var thisType = $this.attr('type') ;
		if (!$this.hasClass('defaultValue') || (!/^(text|password)$/i.test(thisType) && !$this.is('textarea'))) {
			return ;
		}
		var theValue = $this.data('defaultValue') ;
		if (theValue == $this.val()) {
			$this.val('') ;
		}
	}) ;
} ;

jQuery.defaultValueRestore = function() {
	$defaultValueSelector.each(function() {
		var $this = $(this) ;
		var thisType = $this.attr('type') ;
		if (!$this.hasClass('defaultValue') || (!/^(text|password)$/i.test(thisType) && !$this.is('textarea'))) {
			return ;
		}
		var theValue = $this.data('defaultValue') ;
		if ('' == $this.val()) {
			$this.val(theValue) ;
		}
	}) ;
} ;


