/**
 * Defines all necessary JavaScript functions for the search box
 */

/**
 * SearchTab is a JavaScript representation of a search tab. This is
 * used to encapsulate the information required for tab interactions.
 */
function SearchTab(helpText, srchUrl, srchText, show) {
	this.m_helpText = helpText;
	this.m_srchUrl = srchUrl;
	this.m_srchText = srchText;	
	this.m_show = show;
	
}

SearchTab.prototype = {
	getHelpText : function () {
		return this.m_helpText;
	},
	
	getSrchUrl : function () {
		return this.m_srchUrl;
	},
	
	getSrchText : function () {
		return this.m_srchText;
	},
	
	getShow : function () {
		return this.m_show;
	}
};

/***************************************** EVENT HANDLERS *****************************************/

function tabClicked(e) {
	// Get the tab clicked
	var tab = e.findElement('li');
	
	// Get the form
	var form = $('gs-search');	
	var searchTab = searchTabs[tab.id];
	
	if (tab.id == 'more') {
		// Special tab (more) clicked
		$('additional-search-tabs').show();
		$$('ul#search-tabs li#more a')[0].setStyle({backgroundImage:'url(/pix/EnhancedSearchBox/more_mo.gif)'});
	} else if (tab.id == 'close-more') {
		// Close the more tab
		$$('ul#search-tabs li#more a')[0].setStyle({backgroundImage:'url(/pix/EnhancedSearchBox/more_n.gif)'});
		$('additional-search-tabs').hide();
	} else if (!searchTab || searchTab == null) {
		// Invalid selection
		alert ('Invalid search tab ID ' + tab.id + ' specified.');	
	} else if (tab.hasClassName('selected')) {
		// Already selected - do nothing other than making sure the additional options tab is hidden
		$('additional-search-tabs').hide();
	} else {
		// Update the tab selection correctly
		$$('li.tab').each(function(s) { var tab = $(s); tab.removeClassName('selected');});
		tab.addClassName('selected');
		
		fixTabBoarders(tab.id);
		
		if (tab.descendantOf($('additional-search-tabs'))) {
			$('more').setStyle({fontWeight:'bold'});
		} else {
			$('more').setStyle({fontWeight:'normal'});
		}
		
		// Hide the additional search tabs
		$('additional-search-tabs').hide();
		$$('ul#search-tabs li#more a')[0].setStyle({backgroundImage:'url(/pix/EnhancedSearchBox/more_n.gif)'});
		
		// Update the submit button		
		if (searchTab.getSrchText() == '') {
			$('search-button').value = 'Search';
			$('search-button').removeClassName('search-button-wide');
		} else {
			$('search-button').value = 'Search ' + searchTab.getSrchText();
			$('search-button').addClassName('search-button-wide');
		}
		
		
		// The form's action URL needs to be set according to the tab selected. Also
		// if the user has entered text into the search box, the form should be submitted
		form.action = searchTab.getSrchUrl();		
		$('engweb-show').value = searchTab.getShow();
		
		if (searchBoxIsEmpty()) {
			// The news tab gets special handling - clicking that with
			// an empty search box will just bring the user to the news
			// homepage
			
			if (tab.id == 'engnews') {
				location.href = '/engnews';
			} else {
				$('query').value = searchTab.getHelpText();
				$('query').addClassName('empty-search');
			}			
		} else {			
			runGsSearch("SearchSource.PETAL_TAB");
		}	
	}
	
	// Stop the browser from following the link
	e.stop();
	//return false;
}

function searchBoxFocus(e) {	
	// If the box is empty, (no user-entered text), prep it for the user to enter
	// a search term.
	if (searchBoxIsEmpty()) {
		e.element().value = '';
		e.element().removeClassName('empty-search');
	}
}

function searchBoxBlur(e) {	
	// If the search box is empty (no user-entered text), put the appropriate
	// help text back in.
	if (searchBoxIsEmpty()) {
		$('query').addClassName('empty-search');
		
		// Find the appropriate tab
		var selectedTab = $$('li.selected')[0];
		if (selectedTab) {
			$('query').value = searchTabs[selectedTab.id].getHelpText();
		} else {
			// This should never actually happen..
			alert ('NOT GOOD!');
			$('query').value = '';
		}
	}
}

function myGsButtonClick(e) {
	e.element().toggleClassName('my-gs-button-active');
	e.element().toggleClassName('my-gs-button-pushed');
	$('my-gs-menu').toggle(); 
	
	e.stop();
}

function toggleMyGsMenuHover(e) {
	// Using a JS function to do this instead of a css hover pseudo class to preserve
	// IE6 support
	var menuItem = e.findElement('li');
	menuItem.toggleClassName('hover');
}

/***************************************** HELPER FUNCTIONS *****************************************/

function searchBoxIsEmpty() {
	return ($('query').hasClassName('empty-search') || $F('query').blank());
}

function validateGsSearchForm() {
	if (searchBoxIsEmpty()) {
		alert('Please enter a search term.');
		return false;
	}
	
	return true;
}

/**
 * 
 * @param searchSource - searchButton or petalTab
 * @return
 */
function runGsSearch(searchSource) {
	if (validateGsSearchForm()) {
		var selectedTab = $$('li.selected')[0];
		if (selectedTab && selectedTab != null) {
			var srchUrl = searchTabs[selectedTab.id].getSrchUrl();
			
			if (selectedTab.id == 'pas' || selectedTab.id == 'techlib') {
				srchUrl += "?criteria=";
			} else {
				srchUrl += "?query=";
			}
			
			srchUrl += encodeURIComponent($F('query'));
			
			if (searchTabs[selectedTab.id].getShow() != '') {
				srchUrl += "&show=" + searchTabs[selectedTab.id].getShow();
			}
			
			/*location.href = srchUrl;*/
			gotoSearchUrl(srchUrl, searchSource);
		}
	}
}

/**
 * Once we know the URL to hit to run the appropriate search,
 * we post to it by calling this function rather than doing a
 * simple redirect.  This allows us to forward hidden form
 * variables to the target search page with information about
 * what triggered the search, for user behavior tracking.
 * 
 * param searchUrl - The URL to post to, to run the search
 * param searchSource - The name of a member of the Java enum, SearchSource.
 * @return void
 */
function gotoSearchUrl(searchUrl, searchSource) {
	
	// Get rid of any previous instances of this form.
	var form = document.getElementById("searchSubmissionForm");
	if (form != null) {
		document.body.removeChild(form);
	}
				
	// Create a new form
	form = document.createElement("form");
	form.id = "searchSubmissionForm";
	form.method = "POST";
	form.action = searchUrl;

	var input = document.createElement("input");
	input.type = "hidden";
	input.name = "searchSource";
	input.value = searchSource;		
	form.appendChild(input);
	
	document.body.appendChild(form);
	
	// and submit it					
	form.submit();
}

function fixTabBoarders(tabId) {
	// Put all tabs back to a consistent state
	$$('li.tab').each(function (e) {$(e).removeClassName('no-sep');});
	
	if (!$(tabId).descendantOf($('additional-search-tabs'))) {
		// Remove borders from the next tab
		var prevTabs = $(tabId).previousSiblings();
		if (prevTabs.length > 0) {			
			prevTabs[0].addClassName('no-sep');
		}
	}
}
