var showAdvancedOptions = false;
var disableToggle = false;
var tabLoaded = new Array (false, false);
var expandArrowPath = "/assets/images/global/icon_arrow_expand.gif";
var collapseArrowPath = "/assets/images/global/icon_arrow_collapse.gif";

//
function NavigatorPanel (name, filterJsp, addlFields, callback)
{
	this.name = name;
	this.formId = name + "_form";
	this.divId = name + "_filter";
	this.filterJsp = filterJsp;
	this.toggleImageId = null;
	this.show = false;
	this.loaded = false;
	this.callback = callback;
	this.presetFilters = null;
	this.addlFields = false;
	if (addlFields)
	{
		this.addlFields = addlFields;
	}
	
	// get the name of the navigator panel
	this.getName = function ()
	{
		return this.name;
	}
	
	// get the ID of the form this NavigatorPanel will be adding the 
	// navigator dropdowns to
	this.getFormId = function ()
	{
		return this.formId;
	}
	
	// get the ID of the div where this NavigatorPanel will put the
	// navigator dropdowns
	this.getDivId = function ()
	{
		return this.divId;
	}
	
	// get the JSP that creates the dropdowns
	this.getFilterJsp = function ()
	{
		return this.filterJsp;
	}
	
	// get whether this NavigatorPanel has been loaded (i.e. the
	// populateFilters method has been called at least once)
	this.isLoaded = function ()
	{
		return this.loaded;
	}
	
	// set the ID of the image (twisty) that will toggle
	// the show/hide state
	this.setToggleImageId = function (toggleImageId)
	{
		this.toggleImageId = toggleImageId
	}
	
	// returns true if the panel is currently in the show state;
	// false if it is in the hide state
	this.getShow = function()
	{
		return this.show;
	}
	
	// set the panel state to show if true is specified or to
	// hide if false is specified
	this.setShow = function(show)
	{
		if (this.show != show)
		{
			this.toggleAdvancedOptions();
		}
	}
	
	// preset the selected filters
	this.setSelectedFilters = function(filters)
	{
		this.presetFilters = filters;
	}
	
	// This function populates the dropdown filters 
	this.populateFilters = function(selected)
	{	
		// get the hidden field that preserved the dropdown filters' state
	    var navFiltersField = $(this.formId)['navFilters'];
	    var navFilters = "";
	    
	    if (!this.loaded && selected)
	    {
	    	// if the tab has not been loaded then we want to populate the dropdowns with
	    	// either the preset filters specified for this panel, or the preserved Struts 
	    	// session state to have the back button prepopulate
	    	if (this.presetFilters)
	    	{
	    		navFilters = this.presetFilters;
	    	}
	    	else
	    	{
	    		navFilters = navFiltersField.value;
	    	}
	    	
	    	if (this.addlFields && typeof doOnLoad == 'function') 
	    	{
				doOnLoad(this.formId);
			}
	    }
	    
	    // Swap out the div with the AJAX return data
		new Ajax.Updater(this.divId, this.filterJsp, {parameters:Form.serialize($(this.formId)) + navFilters, asynchronous:true, onComplete:callback});
		
		if (this.loaded)
		{
			// if the tab has been loaded, populate the hidden field that preserves the
			// dropdown filters session state with the currently selected dropdown options 
			var filters = $(this.formId)['filter']; 	
			if (filters)
			{
				for (var i = 0; i < filters.length; i++)
				{
					navFilters += "&filter=" + escape($(filters[i]).value);
				}
			}
			navFiltersField.value = navFilters;
		}
		else
		{
			this.loaded = true;
		}
	}
	
	// this function clears the dropdowns in the form, but does not clear
	// their session state
	this.clearDropdowns = function()
	{
		if (this.loaded)
		{
			var filters = $(this.formId)['filter']; 	
			if (filters)
			{
				for (var i = 0; i < filters.length; i++)
				{
					filters[i].value = "";
				}
			}
			this.populateFilters();
		}
	}
	
	// this function clears the query and dropdowns from the form, but doesn't
	// clear the session state
	this.clearFindForm = function()
	{
		if (this.addlFields && typeof clearForm == 'function') 
	    {
			clearForm($(this.formId));
		}
		this.clearDropdowns();
	}
	
	// this function clears the query and the drompdowns from the form, then
	// does a submit so that the session state will be cleared as well
	this.clearSubmitFindForm = function()
	{
		this.clearFindForm();
		submitForm($(this.formId), "clear_search");
	}
	
	// this function shows/hides the dropdown filters
	this.toggleAdvancedOptions = function()
	{
		if (this.toggleImageId)
		{
			this.show = !this.show;
			if(this.show)
			{
				$(this.toggleImageId).src = collapseArrowPath;
				$(this.divId).style.display = "block";
			}
			else
			{
				$(this.toggleImageId).src = expandArrowPath;
				$(this.divId).style.display = "none";
				this.clearDropdowns();
			}
		}
	}
}

// Represents a set of NavigatorPanels.  This can be used to
// organize a group of NavigatorPanels within tabs
function NavigatorPanelSet()
{
	this.tabs = new Array();
	this.selectedIndex = 0;
	
	// add a new NavigatorPanel to the set
	this.addNavigatorPanel = function(np)
	{
		this.tabs[this.tabs.length] = np;
	}
	
	// get the NavigatorPanel at the specified index
	this.getNavigatorPanel = function(index)
	{
		return this.tabs[index];
	}
	
	// get the NavigatorPanel by name
	this.getNavigatorPanelByName = function(name)
	{
		return this.getNavigatorPanelByFormId(name + "_form");
	}
	
	// get the NavigatorPanel with the specified form ID
	this.getNavigatorPanelByFormId = function(formId)
	{
		var result = null;
		var index = indexOfNavigatorPanel(this.tabs, formId);
		if (index > -1)
		{
			result = this.tabs[index];
		}
		return result;
	}
	
	// remove all current NavigatorPanels
	this.clearNavigatorPanels = function()
	{
		this.tabs.clear();
	}
	
	this.populateFilters = function()
	{
		for (var i = 0; i < this.tabs.length; i++)
		{
			populateFilter(this.tabs, i, this.selectedIndex);
		}
	}
	
	this.setShowAll = function(show)
	{
		for (var i = 0; i < this.tabs.length; i++)
		{
			this.tabs[i].setShow(show);
		}
	}
	
	this.populateFilter = function(formId)
	{
		var index = indexOfNavigatorPanel(this.tabs, formId);
		populateFilter(this.tabs, index, this.selectedIndex);
	}
	
	this.clearSubmitFilter = function(formId)
	{
		var index = indexOfNavigatorPanel(this.tabs, formId);
		this.tabs[index].clearSubmitFindForm();
	}
	
	this.toggleSelected = function()
	{
		this.tabs[this.selectedIndex].toggleAdvancedOptions();
	}
	
	// get the currently selected NavigatorPanel
	this.getSelectedIndex = function()
	{
		return this.selectedIndex
	}
	
	// sets the currently selected NavigatorPanel and clears the
	// values (though not in the session state) in the other NavigatorPanels
	this.setSelectedIndex = function(index)
	{
		this.selectedIndex = index;
		for (var i = 0; i < this.tabs.length; i++)
		{
			if (i != index)
			{
				this.tabs[i].clearFindForm();
			}
		}
	}
	
	// returns the currently selected NavigatorPanel
	this.getSelectedNavigatorPanel = function()
	{
		return this.tabs[this.selectedIndex];
	}
	
	// returns the number of NavigatorPanels in the set
	this.size = function()
	{
		var size = 0;
		if (this.tabs)
		{
			size = this.tabs.length;
		}
		return size;
	}
	
	// "private" method to populate a specific NavigatorPanel
	var populateFilter = function(tabs, index, selectedIndex)
	{
		var selected = (index == selectedIndex);
		tabs[index].populateFilters(selected);
	}
	
	// "private" method to determine the index of a NavigatorPanel
	var indexOfNavigatorPanel = function(tabs, formId)
	{
		var index = -1;
		for (var i = 0; i < tabs.length; i++)
		{
			if (tabs[i].getFormId() == formId)
			{
				index = i;
				break;
			}
		}
		return index;
	}
}

// This method sets the query text and filter appropriately when 
// the form is loaded
function doOnLoad(formId)
{
	var form = $(formId);
	var usertext = form['userText'];
	var q = form['q'];
	var userfilter = $$('input#filter.usertext')[0];
	var select = form['searchwordcriteria'];
	var text = usertext.value;

	if (text.length > 0)
	{
		// Test if the user text is keyword (value == "") or a filter (value != "")
		if (select.value == "")
		{
			userfilter.value = "";
			q.value = text;
		}
		else
		{
			q.value = "";
			var pattern = select.value;
			userfilter.value = pattern.replace("REPLACE_ME", "\"" + text + "\"");
		}
	}
}

// This function clears the state of the form
function clearForm(form)
{
	if (form['userText'])
	{
		form['userText'].value = "";
	}
	if (form['q'])
	{
		form['q'].value = "";
	}
	if (form['searchwordcriteria'])
	{
		form['searchwordcriteria'][0].selected = true;
	}
}

function updateUserText(usertext, q, userfilter, select, formId)
{
	var text = usertext.value;
	if (text.length > 0)
	{
	
		// Test if the user text is keyword (value == "") or a filter (value != "")
		if (select.value == "")
		{
			q.value = text;
		}
		else
		{
			var pattern = select.value;
			userfilter.value = pattern.replace("REPLACE_ME", "\"" + text + "\"");
		}
		navigatorSet.populateFilter(formId)
	}
}
	
function changeMode(usertext, q, userfilter, select, formId)
{
	var text = usertext.value;
	if (text.length > 0)
	{
		// Test if the user text is keyword (value == "") or a filter (value != "")
		if (select.value == "")
		{
			userfilter.value = "";
			q.value = text;
		}
		else
		{
			q.value = "";
			var pattern = select.value;
			userfilter.value = pattern.replace("REPLACE_ME", "\"" + text + "\"");
		}
		navigatorSet.populateFilter(formId)
	}
}
