/*	jquery.emptyfield.js
	********************
	
	Run this plugin on inputs and textarea and it will use
	the rel attribute as its value.
	
	Usage:
	$('input.empty').emptyfields();
	
*/

(function($) {
	$.fn.emptyfields = function() {
		return this.each(function() {
			$this = $(this);
			
			// First remove the .empty class
			$this.removeClass('empty');
			
			// Check if it's empty the first time
			checkInput($this)
			
			// Bind events
			$this.bind('focus blur', function(event) {
				checkInput($(this), event);
			})
		})
	};
	
	// Private functions
	function checkInput($elm, event) {
		if (event) {
			if(event.type == 'focus') {
				// A cursor is blinking inside
				if ($elm.val() == '') $elm.removeClass('empty').val('');
				if ($elm.val() == $elm.attr('rel')) $elm.removeClass('empty').val('');
			}
			
			if(event.type == 'blur') {
				// No cursor inside
				if ($elm.val() == '' || $elm.val() == $elm.attr('rel')) $elm.addClass('empty').val($elm.attr('rel'))
			}
		} else {
			// No event, so this probably is on initial page load
			if ($elm.val() == '' || $elm.val() == $elm.attr('rel')) $elm.addClass('empty').val($elm.attr('rel'))
		}
	}
	
})(jQuery)
