/** Handles the default text for a text input
 * expects (jQuery obj) elem | The text input to fade / unfade / clear
 *
 * TODO: Make this a jQuery UI widget
 */
function initInputWithDefaultText( options )
{
	var o = {
		elem: '',
		colorTextOff: '#999',
		colorTextOn: '#000',
		defaultText: ''
	};

	$.extend(o, options);

	var searchBox = o.elem;

	if( o.defaultText == '' )
	{
		o.defaultText = searchBox[0].defaultValue
	}

	if( ! searchBox )
	{
		return;
	}

	searchBox.focus(function(s){
		if( $(this).val() == o.defaultText )
		{
			$(this).val('');
		}

		if( $(this).val() == o.defaultText )
		{
			$(this).css('color', o.colorTextOn);
		}
	});

	searchBox.blur(function(s){
		if( $(this).val() == '' )
		{
			$(this).val(this.defaultValue);
		}

		if( $(this).val() == o.defaultText )
		{
			$(this).css('color', o.colorTextOff);
		}
	});

	searchBox.blur();
}

function initSearchEndecaSubmit( options )
{
	var o = {
		elem: ''
	};

	$.extend(o, options);

	var searchForm = o.elem;
	if( ! searchForm )
	{
		return;
	}

	// The Endeca search form needs a little processing when it gets submitted
	searchForm.submit(function(s){
		// Focus the search box to clear default text if nothing else was entered
		var searchBox = $(this).find('input.keyword');

		searchBox.focus();

		// NVals select sets multiple parameters, so we add it to the URL separately.
		// TODO: Make the form work properly w/o Javascript.
		var $url = $(this).attr('action') + '?';

		var $searchText = $.trim(searchBox.val());

		// Only include search text if the User provided any.
		var $inputs = $(this).find('input');
		if( $searchText == '' )
		{
			$inputs = $inputs.not('form.search input.keyword').not('#center-col-searchform-ntk');
		}

		$url += $inputs.serialize() + '&' + $(this).find('select[name=NVals]').val();

		// Add boolean search capability.
		if( /\b(and|or|not)\b/i.test($searchText) )
		{
			$url += '&Ntx=mode+matchboolean&D=' + encodeURIComponent($searchText);
		}

		location.href = $url;
		return false;
	 });
}

function initSearchNormalSubmit( options )
{
	var o = {
		elem: ''
	};

	$.extend(o, options);

	var searchForm = o.elem;

	if( ! searchForm )
	{
		return;
	}

	searchForm.submit(function(s){
		// Focus the search box to clear default text if nothing else was entered
		$(this).find('input.keyword').focus();
	});
}
