/**
 * jQuery focusMagic plugin
 * @name jquery.focusMagic.js
 * @author Aaron Vanderzwan - http://www.aaronvanderzwan.com/
 * @version 0.1
 * @date January 28, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 Aaron Vanderzwan (aaronvanderzwan.com)
 * @example Visit http://www.aaronvanderzwan.com/focusmagic/ for more informations about this jQuery plugin
 */

(function($) {
	$.fn.focusMagic = function(options) {
		var originalFieldContent = [],
			form = $(this);

		// The first thing we do is reset the form in case the user comes to the page through the back button
		form[0].reset();

		// Sets up what the original field content is for later reference
		form.find('input,textarea').each(function(i) {
			originalFieldContent.push($(this).val());
		});

		// The action for focus/blur
		form.find('input,textarea').not(':submit').focus(function() {
			var $this = $(this);
			var content = $this.val();
			if (originalFieldContent.in_array(content, false)) {
				$this.val('');
				$this.blur(function() {
					if ($this.val() == '') {
						$this.val(originalFieldContent[originalFieldContent.in_array(content, true)]);
					}
				});
			}
		});

		// A function for checking if an item is in an array, can return an int, or boolean
		Array.prototype.in_array = function($item, return_int) {
			for (var i = 0, l = this.length; i < l; i++) {
				if (this[i] == $item) {
					if (return_int) {
						return i;
					} else {
						return true;
					}
				}
			}
			return false;
		};
	};
})(jQuery);
