// Namespace our global variables and functions
var dartlib = {
  searchEl: "search", // ID of search widget
  searchInCleared: false,
  simpSrchTip: "search for keywords...",
  simpSrchTips: {
	  	'bookSearchForm': "Find books and more (keyword)",
		'AVSearchForm': "Find Dartmouth owned A/V materials (keyword)",
		'reserveSearchForm': "Search by instructor's last name",
		'seek': "Search the Dartmouth College Library website (keyword)",
		'journalSearchForm': "Find articles with Search 360 (phrase)"
	  },
  balanceSrchInputs: function(e,inputs) { // function to keep search value the same between tabbing
		if(typeof e != 'undefined' && e.type == 'load') {
			dartlib.resetSrchInputs(inputs);
		} else {
			for (var i=0; i<inputs.length; i++) {
				inputs[i].value = this.value;
				YAHOO.util.Dom.removeClass(inputs[i],"dimmed");
			}
		}
  },
  resetSrchInputs: function(inputs) {
		for (var i=0; i<inputs.length; i++) {
			// Add a dimmed hint message to the search input
			inputs[i].value = dartlib.simpSrchTips[inputs[i].form.id];
			YAHOO.util.Dom.addClass(inputs[i],"dimmed");
		}
  },
  clearSrchInputs: function(e,inputs) {
		for (var i=0; i<inputs.length; i++) {
			if(inputs[i].value == dartlib.simpSrchTips[inputs[i].form.id]) inputs[i].value = '';
			YAHOO.util.Dom.removeClass(inputs[i],"dimmed");
		}
  }
  // Make sure there's no comma after last property, as that breaks js in Safari
}

if (typeof(YAHOO) == "object") { // Don't run any of the YUI stuff unless the YAHOO object exists

var $E = YAHOO.util.Event;
var $D = YAHOO.util.Dom;
var $El = YAHOO.util.Element;

// Make header search tab widget
YAHOO.util.Event.onAvailable(dartlib.searchEl, function() {
		// Build tabview widget from existing markup
		var tabView = new YAHOO.widget.TabView(dartlib.searchEl);
		var srchBtns = $D.getElementsByClassName('searchBtn','input','search');
		var simpSrchInputs = $D.getElementsByClassName('searchIn','input','search');
		// Set listeners for the simple search text inputs within the tabs
        $E.on(simpSrchInputs, 'blur', dartlib.balanceSrchInputs, simpSrchInputs);
		$E.on(simpSrchInputs, 'focus', dartlib.clearSrchInputs, simpSrchInputs);
		dartlib.resetSrchInputs(simpSrchInputs);
});

}

//This is a bit brute force at the moment.  We want this next function to take the input of the search box and
// 1. check to see if the search text is the tip text - if so, discard and go to the default search page for that search engine
// 2. check to see if there is no search text - if so, go to the defauelt search page for that engine
// 3. if there is valid search text, perform the search in that engine

function submitDCLCatForm(formName,inputID)
{
  var formInput = document.getElementById(inputID);
  
   if (formInput.value != 'undefined'){
 
  if (formInput.value == "Find books and more (keyword)")
    {location.href="http://libcat.dartmouth.edu/search~S1/X";}

  else if(formInput.value == "Find articles with Search 360 (phrase)")
    {location.href="http://wfxsearch.webfeat.org/clients/wfxdartmouth/advSearch.asp?cid=10340";}

   else if(formInput.value == "Find Dartmouth owned A/V materials (keyword)")
    {location.href="http://libcat.dartmouth.edu/search~S1/X?NOSRCH=&searchscope=1&SORT=D&m=j&m=i&m=g&Da=&Db=&p=&SUBKEY=";}
    
   else if(formInput.value == "Search by instructor's last name")
    {location.href="http://libcat.dartmouth.edu/search~S1/r";}
    
   else if(formInput.value == "Search the Dartmouth College Library website (keyword)") 
  {location.href="http://search.dartmouth.edu/query.html?col=dartmain&col=dartothr&col=dms&col=math&col=research&col=sudikoff&col=thayer&col=tuck&col=vansites&col=xpubs&qt=&charset=iso-8859-1&nh=20&rf=0&qp=url%3Awww.library.dartmouth.edu%2C+url%3A%2F~libtest";}
  
 	else { document.getElementById(formName).submit();}
  }
  else {location.href=document.getElementById(formName).action;}	
}