/* 
 * Copyright (c) 2006 Halmat Ferello (http://halmat.greydust.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * Build: v0.1.1 (adapted for jquery 1.0.1)
 * Date: October 2006
 *
 * Hot fix: display problem caused <div id="search-results"> not to slide down properly
 * created another <div id="new_results"> inside <div id="search-results"> (adaptation of jsearch v1.0)
 */
var jsearch_text = 'Search for music...'; 				// Default text in the search box
var jsearch_searchform = 'search_products_form'; 		// id of form
var jsearch_searchbox = 's';							// id of serach input
var jserach_radio = 'search_type';
var jsearch_searchbutton = 'searchbutton';				// id of submit button
var jsearch_searchurl = 'searchURL';				// id of ajaxSearchURL
var jsearch_results_div = 'search-results'				// id of div container for search results

var jSearch = {

	activateSearch: function() {
		if ($('form#'+jsearch_searchform)) {
			$('input#'+jsearch_searchbox).val(jsearch_text); 
			$('div#'+jsearch_results_div).css('display', 'none'); // hot fix
			$("form#"+jsearch_searchform).submit(function(e){ e.preventDefault(); jSearch.doSearch(); return false; } );
		}
	},

	doSearch: function() {
		var s = $('input#'+jsearch_searchbox).val();
		var searchURLString = $(	'input#'	+	jsearch_searchurl	).val();

		var radio_value = '';
		// scans through all inputs
		// checks to see if its a radio button and is checked
		$('input').each(function(){

			if (this.type != 'radio') return;
			if (!this.checked) return;

		  	radio_value = this.value;
		});

		if (s == '' || s == jsearch_text) return false;
		
		$('div#'+jsearch_results_div).html('<div id="new_results"></div>'); // hot fix

		$('input#'+jsearch_searchbutton).removeClass('submit');
		$('input#'+jsearch_searchbutton).addClass('searching');
		/* $('div#new_results').load( searchURLString 'index.cfm?fuseaction=USmusicSalescart.SearchProductCatalogAjax&SearchString=' + escape(s) + '&SearchType=' + radio_value, function(){ jSearch.doComplete(); }); */
		$('div#new_results').load(	searchURLString	+	'&SearchString='	+	escape(	s	)	+	'&SearchType='	+	radio_value	,	function(){ jSearch.doComplete()	;	}	)	;
	},

	doComplete: function()
	{
		$('div#'+jsearch_results_div).css('display', 'block');  // hot fix
		$('div#new_results').slideDown('slow', function() { TB_init(); jSearch.resetForm() });
	},

	resetForm: function() {
		$('input#'+jsearch_searchbutton).removeClass('searching');
		$('input#'+jsearch_searchbutton).addClass('submit');
	},

	setClick: function(element, jsearch_text) {
		element.value = jsearch_text;
	},

	setBlur: function(element, jsearch_text) {
		if(element.value == ''){
			element.value = jsearch_text;
		}
	}
};