/*
±íµ¥Ô¤Ìî³ä²å¼þ
 * e.g.
 *  $('input#name').example('Bob Smith');
 *  $('textarea#message').example('Type your message here', {
 *		class_name: 'example_text',
 *		hide_label: true
 *	});
  */

(function($) {	
	$.fn.example = function(text, args) {
		/* Load the default options. */
		var options = $.extend({}, $.fn.example.defaults, args);
		var bound_class_names = $.data(document.body, 'example') || [];		
		if ($.inArray(options.class_name, bound_class_names) == -1) {
			$(window).unload(function() {
				$('.' + options.class_name).val('');
			});
			$(this).parents('form:first').submit(function() {
				if($('.' + options.class_name).val()==text){
					$('.' + options.class_name).val('');
				}
			});		
			/* Add the class name to the array. */
			bound_class_names.push(options.class_name);
			$.data(document.body, 'example', bound_class_names);
		}		
		return this.each(function() {
			var $this = $(this);
			if ($this.val() == '') {
				$this.addClass(options.class_name);
				$this.val(text);
			}		
			if (options.hide_label) {
				$('label[@for=' + $this.attr('id') + ']').next('br').andSelf().hide();
			}
			$this.focus(function() {
				if ($(this).hasClass(options.class_name)) {
					$(this).val('');
					$(this).removeClass(options.class_name);
				}
			});
			$this.blur(function() {
				if ($(this).val() == '') {
					$(this).addClass(options.class_name);
					$(this).val(text);
				}
			});
			$this.change(function() {
				if ($(this).val() != '') {
					$this.removeClass(options.class_name);
					//$(this).val(text);
				}
			});
		});
	};
	$.fn.example.defaults = {
		class_name: 'example',
		hide_label: false
	};	
})(jQuery);
