/*!
 * jQuery SearchBox Plugin (20111115)
 *
 * Copyright (C) 2011, crooce.com - the internet company, s.r.o.
 */

(function($) {

	function handleFocusInEvent(event) {

		if ($(this).val().replace(' _', '') == $(this).data('value')) {

			$(this).val('');
		} // if
		return true;
	}

	function handleFocusOutEvent(event) {

		if ($(this).val() == '') {

			$(this).val($(this).data('value'));
		} // if
		return true;
	}

	$.fn.attachSearchBox = function() {

		// for each element
		return this.each(function() {

			var $this = $(this);

			// get default value
			$('<div/>').append($this.clone()).html().match(/value=\"([^\"]*)\"/);
			var foo = RegExp.$1;

			// save default value & bind event handlers
			$this
				.data('value', foo)
				.focusin(handleFocusInEvent)
				.focusout(handleFocusOutEvent);

			// set interval
			setInterval(function() {

				if ($this.is(':focus')) return;

				if ($this.val() == $this.data('value')) {

					$this.val($this.data('value') + ' _');
				} else if ($this.val() == $this.data('value') + ' _') {

					$this.val($this.data('value'));
				} // if .. else if ..
			}, 500);
		});
	};
})(jQuery);



jQuery(function($) {

	$('input[type="text"].searchBox').attachSearchBox();
});

