//  Site search javascript functions:
var tsr;
var showCountSelectorId;


function initSiteSearch(tabUrls, currentTab, countSelectorId, onComplete) 
{
	tsr = new TabbedSearchResults(onComplete);
	// update the array of tab urls
	tsr.replaceUrls(tabUrls);
	// set the current tab to the tab value handed to the function
	tsr.setCurrentTab(currentTab);
	// checking the tab and offset values in the url
	tsr.setCurrentUrlTabValue();	
	// get offset is in the url and update the urls
	tsr.setParams('offset',tsr.getCurrentUrlOffset());
	// making a Ajax call and loading the data into the search div
	tsr.loadSearchResults();	
}


function initViewAll()
{
    initCollapsibleNavigatorsOpen();
	initCollapsibleNavigatorsClose();
	initCollapsibleContainers();
}
	
function sort(dir)
{
 tsr.setParams('sort',dir);
 tsr.loadSearchResults();
} 

function searchInter(url,offset,el)
{   
	var sel = document.getElementById('searchcat');
	var searchVal = document.getElementById('quer').value;
	var curTab = 1;
	if(searchVal != null || searchVal != "")
	{
		curTab = (sel.selectedIndex - 1);
		tsr.setParams('q',searchVal)
		tsr.setCurrentTab(curTab);
		tsr.loadSearchResults();
	}
	else
	{
	 alert("Please enter a search keyword.");
	}
}

function TabbedSearchResults(onComplete)
{
	this.divId = "searchContent";
	this.tabId = "contentTabs";
	this.currentTab = 0;
	this.tabUrls = new Array();
	this.tabValues = new Array();
	this.onComplete = onComplete;

	// get the div the ajax data will be swapped into
	this.getTabId = function() 
	{
		return this.tabId;
	}
	
	// get the div the ajax data will be swapped into
	this.getDivId = function() 
	{
		return this.divId;
	}

	// add one url to tabUrl array
	this.addUrl = function (strUrl)
	{
		this.tabUrls [this.tabUrls.length] = strUrl;
	}
	
	// add all urls to the tabUrl array 
	this.replaceUrls = function (tabUrls)
	{
		this.tabUrls = tabUrls;
		for (var i = 0; i < tabUrls.length; i++)
		{
			this.tabValues[i] = tabUrls[i].match("tabValue=[^&#]*");
		}
	}
	
	// get the current tab
	this.getCurrentTab = function()
	{
		return this.getCurrentUrlTabValue();
	}
	
	// set the current tab
	this.setCurrentTab = function(tabNumber)
	{
		if (!tabNumber) 
		{
			tabNumber = 0;
		}
		this.currentTab = tabNumber;
		this.setCurrentUrlTabValue();
	}
	
	// get the current tab url
	this.getCurrentUrl = function(){
		return this.tabUrls[this.currentTab]; 
	}
	
	// get the url of the given tabIndex
	this.getUrl = function(tabIndex)	
	{
		return this.tabUrls[tabIndex]; 
	}
	
	// update params
	this.setParams = function(param,value)
	{
		 if (!isArray(param))
		 {
		 	param = [param];
		 	value = [value];
		 }
		 var urls = this.tabUrls;
		 var newUrls = new Array();
		 for(var i=0;i<urls.length;i++)
		 {
		  newUrls[i] = setParams(param,value,urls[i]);
		 }
		 this.replaceUrls(newUrls);
	}
	
	// get the current tab value from the "window.localtion.href" url
	this.getCurrentUrlTabValue = function()
	{
		var tabNo = 0;
		var stateArgs = getCachedStateArgs();  		// pulles the tab number from the url and returns an array
		if (stateArgs.length > 0) {  				// gets the first number form the array and returns an intager
		    var tabState = parseInt(stateArgs[0]);
		    if (!isNaN(tabState)) {
			    tabNo = tabState;
		    }
		}
		return tabNo;
	}
	
	// get the current tab value from the "window.localtion.href" url
	this.setCurrentUrlTabValue = function(tab)
	{
		if (!tab)
		{
			tab = this.currentTab
		}
		window.location.href = getBaseURL() + "#" + tab + ":" + this.getCurrentUrlOffset();
	}
	
	// get the offset value for the "window.localtion.href" url
	this.getCurrentUrlOffset = function()
	{
		var offset = 0;
		var stateArgs = getCachedStateArgs();  			// pulles the tab number from the url and returns an array
		if (stateArgs && stateArgs.length > 1) {  		// gets the first number form the array and returns an intager
		    var offsetState = parseInt(stateArgs[1]);
		    if (!isNaN(offsetState)) {
			    offset = offsetState;
		    }
		}
	    return offset;
	}
	
	// set the offset value in the "window.localtion.href" url from a value handed to it or with what is in the url
	this.setCurrentUrlOffset = function(offset)
	{
		if (!offset)
		{
			offset = this.getCurrentUrlOffset();
		}
		window.location.href = getBaseURL() + "#" + this.currentTab + ":" + offset;
	}

	
		
	// loads data ajax call
	this.loadSearchResults = function(params)
	{		
	    if (!params) 
		{
			params = "";
		}
		showLoadingMessage(this.divId);
		
		new Ajax.Request(this.getUrl(this.getCurrentUrlTabValue()),
			{  
				onSuccess  :  this.processSearchResultsSuccess,
				onFailure  :  this.processSearchResultsFailure,
				parameters :  params
			});
	
	}
	
	// put my data in my div
	this.processSearchResultsSuccess = function(httpResponse) 
	{
		$("searchContent").innerHTML = httpResponse.responseText;
		var innerHTMLJS = $$('.javascript');
		for (var i = 0; i < innerHTMLJS.length; i++)
		{
			eval(innerHTMLJS[i].innerHTML);
		}
		registerSearchTabs();
		
		if (tsr.onComplete)
		{
			tsr.onComplete();
		}
	}
	
	// error 
	this.processSearchResultsFailure = function(httpResponse)
	{
		showErrorMessage(this.divId);
	}

	
}	

function registerSearchTabs() 
{
	if (document.getElementById(tsr.getTabId())) 
	{
		this.searchTabs=document.getElementById(tsr.getTabId()); // Store the object that contains the search tabs
		this.searchTabsLIs=this.searchTabs.getElementsByTagName("li"); // Store the links within it.
		
		urlCount = 0;
		for (var i=0; i < this.searchTabsLIs.length; i++) 
		{
			if (i == tsr.getCurrentTab()) 
			{
				this.searchTabsLIs[i].className="current";
			} 
			else 
			{
				this.searchTabsLIs[i].className="";
			}

			test = this.searchTabsLIs[i].id
			if ((test=="skipTab")) {
				skipTabCount = i;
				if (tsr.getCurrentTab() > i)
				{
					document.getElementById('skipTab').className="current";
				}
				//this.searchTabsLIs[i].tsrCount=i;
				this.searchTabsLIs[i].onmouseover = function(i) 
				{
					document.getElementById('skipTab').className="current";
					document.getElementById('tabDropMenu').style.display='block';
					return false;
				}
				this.searchTabsLIs[i].onmouseout = function(i) 
				{
					document.getElementById('tabDropMenu').style.display='none';
					if (tsr.getCurrentTab() < skipTabCount)
					{
						document.getElementById('skipTab').className="";
					}
					return false;
				}
			
			}
			else 
			{
				this.searchTabsLIs[i].tsrCount=urlCount;
				this.searchTabsLIs[i].onclick = function() 
				{
					var tabValueRegex = new RegExp("tabValue=[^&]*");
					window.location.href = getBaseURL().replace(tabValueRegex, tsr.tabValues[this.tsrCount]) + "#" + this.tsrCount + ":" + 0;
					tsr.setParams('offset', 0);
					tsr.loadSearchResults(this.tsrCount);
					return false;
				}
				urlCount = urlCount + 1;
			}



		}
	}
	initViewAll();
}


function sortLang(sort,language)
{
 tsr.setParams('sort',sort);
 tsr.setParams('lang',language);
 tsr.setParams('offset','0');
 tsr.setCurrentTab(getTabIndex());
 tsr.setCurrentUrlOffset(sort);
 tsr.loadSearchResults();
} 

function showPage(offset)
{
  tsr.setParams('offset',offset);
  tsr.setCurrentTab(getTabIndex());
  tsr.setCurrentUrlOffset(offset);
  tsr.loadSearchResults();
}

function sortPage(sortOn, curSortDir, tabValue)
{
	var sortDir = "asc";
	if ("asc" === curSortDir)
	{
		sortDir = "dsc";
	}
	
	var params = ["sortOn","sortDir"];
	var values = [sortOn,sortDir];
	if (tabValue)
	{
		params[2] = "tabValue";
		values[2] = tabValue;
	}

	window.location.href = setParams(params, values);
}

// need to re-code into new way of doing things

function showNewResultCount(show) {
	
	tsr.setParams('show',show);
	tsr.setCurrentUrlOffset(show);
	tsr.loadSearchResults();
}



