var searchDefaultText = "books, journals, scores, DVDs and more";

$(document).ready(function() {

	//set style of submit buttons
	$('input[type=submit]').css('padding','0 2px');
	
	//set selected tab & defaults
	//valid values
	var rexp = /^quicksearch$|^catalog$|^eresources$|^ejournals$|^images$|sitesearch$/;
	//get selected tab - first from URL hash, else from cookie, else default is 'catalog'
	var selectedTab = rexp.test(window.location.hash.substr(1)) ? window.location.hash.substr(1) : rexp.test(getCookie('tabCookie')) ? getCookie('tabCookie') : 'quicksearch';
	if (selectedTab != 'quicksearch') {
		$('#tab-quicksearch').addClass('search-tab').removeClass('search-tab-sel');
		$('#quicksearch').hide();
	}
	selectTab(selectedTab);
	//jQuery URL utils
	//enable fragmentChange
	$.fragmentChange( true );
	//set tab clicks
	$(document).bind('fragmentChange', function(e) { 
		selectedTab = rexp.test(e.fragment) ? e.fragment : selectedTab;
		selectTab(selectedTab);
	});
	
	
	$('#searchdata1').attr("value",searchDefaultText);
	
	$('#searchdata1').focus( function() {
		if(this.value == searchDefaultText) {
			this.value = "";
		} 

		$(this).select();//selecting the text
		$(this).css('color','#000');
	});
	
	$('#searchdata1').blur( function() {
		if(this.value == "") {
			this.value = searchDefaultText;
			$(this).css('color','#b6b6b6');
		}
	});
	
//cluetip
/*
	$('#tab-catalog, #tab-articles').cluetip({
		splitTitle: '|',
	    showTitle: false,
		arrows: false
	});
*/

});

var selectedTabName = null;
function selectTab(tabName) {
	
	if (selectedTabName != null) {
		$('#tab-'+selectedTabName).addClass('search-tab').removeClass('search-tab-sel');
		$('#'+selectedTabName).hide();
	}
	
	selectedTabName = tabName;
	$('#tab-'+selectedTabName).addClass('search-tab-sel').removeClass('search-tab');
	$('#'+selectedTabName).show();
	setCookie('tabCookie', selectedTabName, 365, '/libraries/');
}

//Quick Search formats
$(document).ready( function() {
	var subtabArray = { 
		"everything":"", 
		"archival":"ContentType,Archival Material,false",
		"audio":"ContentType,Audio Recording,false",
		"book":"ContentType,Book / eBook,false",
		"bookchapter":"ContentType,Book Chapter,false",
		"bookreview":"ContentType,Book Review,false",
		"computerfile":"ContentType,Computer File,false",
		"conference":"ContentType,Conference Proceeding,false",
		"dissertation":"ContentType,Dissertation,false",
		"ebook":"ContentType,Book / eBook,false",
		"governmentdoc":"ContentType,Government Document,false",
		"image":"ContentType,Image,false",
		"journal":"ContentType,Journal / eJournal,false",
		"journalarticle":"ContentType,Journal Article,false",
		"manuscript":"ContentType,Manuscript,false",
		"map":"ContentType,Map,false",
		"microfilm":"ContentType,Microfilm,false",
		"musicrecording":"ContentType,Music Recording,false",
		"musicscore":"ContentType,Music Score,false",
		"newspaper":"ContentType,Newspaper,false",
		"newspaperarticle":"ContentType,Newspaper Article,false",
		"patent":"ContentType,Patent,false",
		"photograph":"ContentType,Photograph,false",
		"reference":"ContentType,Reference,false",
		"report":"ContentType,Report,false",
		"specialcollection":"ContentType,Special Collection,false",
		"standard":"ContentType,Standard,false",
		"videorecording":"ContentType,Video Recording,false",
		"webresource":"ContentType,Web Resource,false"
	};
	function showMoreMenu() {
		$("#more-tab").addClass("more-tab-select");
		$("ul#more-search").css({'visibility':'visible'});
	}
	function hideMoreMenu() {
		$("#more-tab").removeClass("more-tab-select");
		$("ul#more-search").css({'visibility':'hidden'});
	}
	function clickSubTab() {
		var which = $(this).attr("data-id");
		//console.log(which);
		//unselect/reset tabs
		$("#subtabs li a").each( function() {
			$(this).removeClass("selected");
			$("input#summon-option").val("");
		});
		$('#add_facets').val("");
		hideMoreMenu();
		//select clicked tab
		$(this).addClass("selected");
		$("input#summon-option").val(subtabArray[which]);
		switch (which) {
			//if ebook, check full text option
			case "ebook" : 
				$('#add_facets').val('addFacetValueFilters(IsFullText,true)');
				break;
			//if archival, also search Special Collections category
			case "archival" : 
				$('#add_facets').val('addFacetValueFilters(ContentType,Special Collection)');
				break;
			default :
				$('#add_facets').val("");
		}
		//console.log($("input#summon-option").val());
		return false;
	}
	//click top subtab except More
	$("#subtabs li a").not('[data-id="more"]').bind("click", clickSubTab);
	//click More subtab - show menu
	$("#subtabs li a[data-id='more']").click( function() {
		if($("#more-tab").hasClass("more-tab-select")) {
			hideMoreMenu();
		}
		else {
			//position More menu at same x coord as More link
			var morePos = $("#more-tab").offset();
			var menuPos = $("ul#more-search").offset();
			$("ul#more-search").offset({ top:menuPos.top, left:morePos.left - 3 });
			//console.log(morePos.left + ", " + menuPos.left);
			showMoreMenu();
			
		}
		return false;
	});
	//if More menu showing, clicking outside will hide it
	$("html").click( function() {
		if($("ul#more-search").css('visibility') == 'visible') {
			hideMoreMenu();
		}
	});
	$("#more-search").click(function(event){
		event.stopPropagation();
	});
	//click link in More menu
	$("#more-search li a").click( function() {
		var optionId = $(this).parent().attr("data-id");
		$("#swap-tab").html($(this).parent().html());
		hideMoreMenu();
		$("#swap-tab a").bind("click", clickSubTab);
		$("#swap-tab a").click();
		return false;
	});
});
