// Namespace our global variables and functions
var dartlib = {
  simpSrchTips: {
	  	'bookSearchForm': "Find books and more (keyword)",
		'AVSearchForm': "Find Dartmouth owned A/V materials (keyword)",
		'reserveSearchForm': "Search by instructor's last name",
		'catauthorSearchForm' : "Find by Author",
		'cattitleSearchForm' : "Find by Title",
		'catsubjectSearchForm' : "Find by Subject",
		'cataudioSearchForm' : "Find Audio Material",
		'catvideoSearchForm' : "Find Video Material",
		'catReserveSearchForm' : "Find by Course Name",
		'cse-search-box': "Search the Dartmouth College Library website",
		'summonSearchForm': "Summon library content here and beyond"

	  },
  balanceSrchInputs: function(e) { // function to keep search value the same between tabbing
			var inputs = e.data.inputs;
		if(typeof e != 'undefined' && e.type == 'load') {
			dartlib.resetSrchInputs(inputs);
		} else {
			for (var i=0; i<inputs.length; i++) {
				inputs[i].value = this.value;
				$(inputs[i]).removeClass('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];
			$(inputs[i]).addClass('dimmed');
		}
  },
  clearSrchInputs: function(e) {
  			var inputs = e.data.inputs;
		for (var i=0; i<inputs.length; i++) {
			if(inputs[i].value == dartlib.simpSrchTips[inputs[i].form.id]) inputs[i].value = '';
			$(inputs[i]).removeClass('dimmed');
		}
  }
  // Make sure there's no comma after last property, as that breaks js in Safari
}
		$(document).ready( function() {
		
		// Set listeners for the simple search text inputs within the tabs		
		var searchBoxes = $('.searchIn');
		searchBoxes.bind('blur', {inputs : searchBoxes}, dartlib.balanceSrchInputs);
		searchBoxes.bind('focus', {inputs : searchBoxes}, dartlib.clearSrchInputs);
		dartlib.resetSrchInputs(searchBoxes);
	   
		//Set Listeners for Tab clicks
		var searchBoxTabs = $('.yui-nav li a');
		searchBoxTabs.bind('click', function(){
	   
			//calculate the relevant content ids
			var selectedContentID = "#"+$(this).attr("id")+"Content"; //the associated content tab
			var contentParentID = "#"+$(this).parent().parent().attr("id")+"Content"; // the content parent div
		
			//do the class switching to show/hide relevant parts
			$(this).parent().siblings().removeClass("selected");
			$(contentParentID).children().removeClass("content-selected");
			$(this).parent().addClass("selected");
			$(selectedContentID).addClass("content-selected");
		
			//don't follow the hyperlink in the href in the tab
			return false;
			}
		);
		
		//Validate form submission
		// 1. check to see if the search text is the tip text or if it's empty - if so, go to the default search page
		// 2. if there is valid search text, perform the search in that engine
		
		//Set listeners for click or submit.  Submit for visible forms and click for more options section
		var searchText = $(".searchFormSubmit");
		searchText.bind('click submit', function(){
		
			var inputValue = $(this).siblings(".searchIn").val(); //get the value of the form input text
			var parentFormID = $(this).parent().attr("id"); //get the id of the parent  form
			var tipTextValue = dartlib.simpSrchTips[parentFormID]; //get the associated tip text for that form
		
			//if the text in the input is the same as the tip text, empty, or undefined, go to the default search page
			if (inputValue == tipTextValue || inputValue == "" || inputValue == 'undefined')
				{
					switch (inputValue){
				
					case dartlib.simpSrchTips["summonSearchForm"]: //Summon has a special landing page different from the form action
					location.href="http://dartmouth.summon.serialssolutions.com/advanced/";
					return false;
					break;
				
					case dartlib.simpSrchTips["AVSearchForm"]: //A/V search has a special landing page different from the form action
					location.href="http://libcat.dartmouth.edu/search~S1/X?NOSRCH=&searchscope=1&SORT=D&m=j&m=i&m=g&Da=&Db=&p=&SUBKEY=";
					return false;
					break;
				
					default:
					location.href = $(this).parent().attr("action");
					return false;
					break;
				
				}
			}
			
		//otherwise, submit the form
			else 	{
			
				if ($(this).attr('type') == "submit")
					{
					$(this).parent().submit();
					}
					
				else{
					$(this).parent().append().submit();
					return false;
					}
			}
		
	   }
	);
	
	//Emergency Banner
	var banner = $("#emergency-banner");
	if (banner.text().length > 0) {
		$("#emergency-banner-placeholder").replaceWith(banner);
	}

  	//listen for click on the print friendly button
	var printFriendly = $("#printFriendly");
	printFriendly.bind('click', function ()
		{

		//write in the new  style link
		$("head").append('<link href="/~library/_permacode/common/library/styles/packages/printfriendly.css" rel="stylesheet" media="screen, print" type="text/css" />');
	
		//write out the back and print buttons
		var printMarkUp = "<div id=\"print-button\"><div  class=\"PrintBtn\"><a id=\"printReturn\" href=\"#\">Back<\/a> &nbsp; <a href=\"#\" id=\"printBtn\">Print Page<\/a><\/div><\/div>";
		$("#print-css-div").replaceWith(printMarkUp); 
		
		//Jump to the top of the page - IE fix
		window.scrollTo(0,0); 
		
		//Listen for click on Print Button
		var printButton = $("#printBtn");
		printButton.bind('click', function(){window.print(); return false;});

		//Listen for click on back button
		var backButton = $("#printReturn");
                backButton.click(function()
		  {
		    $("link[href$='printfriendly.css']").remove(); 
		    $("#print-button").remove();
 
		    //IE 6 doesn't like to redraw the page correctly and/or remove the link
		    //Fix is to redraw the page
		    if ($.browser.msie && $.browser.version < 7){window.location.reload();}
 
		    return false;
		  }
		);

		return false;
		});
		
	//Change tabs for sitemap
	if (location.href.indexOf("/sitemap/",0) != -1) {$("#dclWebsite").click();}
	
	//Summon Button behaves like a button
	$("#horiz-summon").bind(
		{click:function(){
			location.href="http://www.dartmouth.edu/~library/home/find/summon/";
			return false;
		},
		mouseenter:function(){
			$("#horiz-summon-link").css({'color':'#90a860','text-decoration':'underline'});
		},
		mouseleave:function(){
			$("#horiz-summon-link").css({'color':'','text-decoration':''});
		}
	}); 
        //Fancybox image overlay, replaces lightbox v.1
	$("a[rel|=thumbnail]").fancybox();
});

//ask us rotation
	function askUsRotation(location,numChatImages,newChatImages){
	
    var randomnumber = Math.floor(Math.random()*numChatImages + 1);

   	if (randomnumber < 10)
       	{imgnum = "0" + randomnumber.toString();}
    else
       	{imgnum = randomnumber.toString();}

	if (location == "tools" || location =="www")
	{
		if (location == "tools"){
			location = "http://libanswers.dartmouth.edu/";	
			}
		else if (location == "www"){
			location = "http://libanswers.dartmouth.edu/";	
		}	
	}
	//if the location is not tools or generic
	else {	imageLocation = location; //set the base href for images
			location = location+".html"; //and add the html to the location
		}
		
	if (newChatImages == "yes") //where do the chat images come from - the global location or a local location.  if local, the the images *must* be in a subfolder of the chat directory called "chatImages"
		{var askUsDiv = "<a href=\""+location+"\"><img src=\""+imageLocation+"images/" + imgnum + ".gif\"></a>";}
		
	else{
		var askUsDiv = "<a href=\""+location+"\"><img src=\"/~library/_permacode/common/library/styles/images/askus/" + imgnum + ".gif\"></a>";}
        
	$("#horiz-askus").children().replaceWith(askUsDiv);
	}

