
/**
 * Search input jquery plugin
 *
 * @author HAAN <haam@fynskemdier.dk>
 */
(function($) {
	var options = {
		preventSubmit : true
	}

	function getForm(obj) {
		while(!$(obj).is('form')) {
			if($(obj).is('body')) {
				return false;
			}

			obj = $(obj).parent();
		}

		return obj;
	}

	function setBlur() {
		$(this).removeClass('focus');

		if($(this).val().length == 0) {
			$(this).val($(this).data('search_comment'));
		}

		if($(this).val() == $(this).data('search_comment')) {
			$(this).addClass('comment');
		}
	}

	function setFocus() {
		$(this).addClass('focus');
		$(this).removeClass('comment');

		if($(this).val() == $(this).data('search_comment')) {
			$(this).val('');
		}
	}

	$.fn.searchComment = function(opt) {
		options = $.extend(options, opt);

		$(this).each(function() {
			if($(this).is('input[type=text]')) {
				$(this).data('search_comment', $(this).val()); // Store default value
				
				// Set events
				$(this)
					.each(setBlur)
					.blur(setBlur)
					.focus(setFocus);
			}

			if((form = getForm(this)) && options.preventSubmit) {
				form.data('search_comment_input', $(this));

				form.submit(function() {
					var input = $(this).data('search_comment_input');

					if($(input).val() == $(input).data('search_comment')) {
						return false;
					}
				});
			}
		});
	}
})(jQuery);


