
	
var ScheduleSearchForm = function(path, name, useAjax, imagePath) {
	this.path = path;
	this.name = name;
	this.useAjax = useAjax;	
	ScheduleSearchForm.forms[name] = this;
	this.initAutocomplete();
	this.contentElement = $('#advanced_search_'+ this.name);
	this.toggleElement = $('#advanced_search_toggle_'+this.name);
	this._imagePath = imagePath;
	
	this._makeToggle('more');
}

ScheduleSearchForm.prototype.$ = function (name)
{
	return $('#'+name+'_'+this.name);
}

// submits the form using either ajax or std submit
ScheduleSearchForm.prototype.submit = function(searchObject)
{
	searchObject = (searchObject == null) ? search : searchObject; 
	if (this.useAjax) 
	{
		sortBy = ScheduleSearchForm.getRadioButtonValue('sortBy');
		startDate =  this.$('startDate').val();
		endDate =  this.$('endDate').val();
		searchObject.runtime.populate(
			this.$('searchString').val(), 
			this.$('episodeBroadcastId').val(), 
			startDate, 
			endDate, 
			$(".sortBy_"+this.name + ":checked").val(),
			//$RF(this.name + 'SearchForm', 'sortBy'), 
			1, 
			searchObject.runtime.perPage
		);
		search.display();
	} else 
		form.submit();
}

	
ScheduleSearchForm.prototype.initAutocomplete = function() {
	/*new Ajax.Autocompleter("searchString_" + this.name, "autocomplete_choices_" + this.name, this.path + "/?controller=Show", {
		afterUpdateElement: ScheduleSearchForm.submit
	});*/
	this.$("searchString").autocomplete(this.path + "/?controller=Show&action=jquery", {

	});
}


// class methods

ScheduleSearchForm.submit = function(field)
{
	name = field.id.split('_')[1];
	ScheduleSearchForm.forms[name].submit();
}

ScheduleSearchForm.getRadioButtonValue = function(name)
{
	return $('#'+name).val();
}

ScheduleSearchForm.prototype.show = function() {
	this.contentElement.slideDown('normal');
	this._updateToggle('hide');
}

ScheduleSearchForm.prototype.hide = function() {
	this.contentElement.slideUp('normal');
	this._updateToggle('more');	
}

ScheduleSearchForm.prototype.toggle = function() {
	if (this.contentElement.is(':visible'))
		this.hide();
	else
		this.show();
}

// private

ScheduleSearchForm.prototype._makeToggle = function(name) {
	this.toggleElement.html('<img id="collapsible_toggle_image_'+this.name+'" src="'+ this._getImage(name)+'"><span id="toggle_'+this.name+'">'+name+' search options<span>');
}
	
ScheduleSearchForm.prototype._updateToggle = function(name) {
	$('#collapsible_toggle_image_'+this.name).src=(this._getImage(name));
	$('#toggle_'+this.name).html(name +' search options');
}

ScheduleSearchForm.prototype._getImage = function(name) {
	return this._imagePath+'/button_'+name+'.gif';
} 

ScheduleSearchForm.forms = [];