$(document).ready(
  function () {

		// Assign click events to the tab buttons
		$("#search-box .tab a").click(change_tab);
		
		$("#askalib_drop").hide();		
		$("#askalib").click(askalib_toggle);

		// hide the labels on the search form
		$("#search-box label.access").hide();


		// Set up blur and focus rules (as well as set the initial state
		$(".layered-form input[type=text]").focus(search_focus).blur(search_blur).blur();
		
		// Set the sidebar heights to fill the page
		var max_height = 0;
		var normalize_targets = ["#main-wrapper", "#left-sidebar-wrapper", "#right-sidebar-wrapper"];
		for (var i = 0; i < normalize_targets.length; i++) {
			max_height = ($(normalize_targets[i]).height() > max_height) ? $(normalize_targets[i]).height() : max_height;
		}
		normalize_targets.shift();
		for (var j = 0; j < normalize_targets.length; j++) {
			$(normalize_targets[j]).height(max_height + "px");
		}
	}
	
);
// Toggles databases A-Z box
function askalib_toggle(e) {
	$("#askalib_drop").slideToggle(500);
		return false;
}

// Switches out the tabs on the search box
function tab_switch(e) {
	var target = $(this).parent().attr("id").substr(4);
	
	// Hide the controls until we switch
	$("div.control").hide();
	
	// Hide the current tab and content
	$(".layered_search.active").removeClass("active");
	$("#search-headers .active").removeClass("active");
	
	// Display the new tab and content
	$("div#search-" + target).addClass("active");
	$(this).parent().addClass("active");
	
	// Adjust the background image
	target = target.substr(0, 3) == "sit" ? "site" : target.substr(0, 3);

	$("#" + target + "_search").blur();
	
	
	// Return false to keep the user from leaving the page
	return false;
}

function change_tab(e) {
  // Clear currently active tabs  
  $('#search-box .tab.active').removeClass("active");
	$('#search-box .layered-form.active .control').hide();
  $('#search-box .layered-form.active').removeClass("active");
  
  // Make the new tab / form active
  var tab = $(this).parents('.tab').attr('id').match(/tab-(catalog|articles|journals|site)/).pop();
  $('#tab-' + tab).addClass("active");
  $('#' + tab + '-form').addClass("active");
  
  // Cancel the event
  return false;
}

// Removes the search box background when there is focus
function search_focus(e) {
	// Controls the search options for each search box 
	if ($(this).val().length == 0 ) {
	  var tab = $(this).parents('.layered-form').attr('id').match(/(catalog|articles|journals|site)-form/).pop();

  	$(this).css("backgroundPosition", "0px 21px");
	}
	
	// Drop down the controls
	if ($(this).parents('#search-box').hasClass('site')) {
		$('.control', $(this).parents('.layered-form')).slideDown(500);
	}
}

// Puts the background back if the user does not enter anything
function search_blur(e) {
	// Determine which tab we are on and adjust the tartget
	var tab = $(this).parents('.layered-form').attr('id').match(/(catalog|articles|journals|site)-form/).pop();
	var offset = 21;
	
  if ($(this).val().length == 0)
    switch (tab) {
      case 'catalog':
        offset = 0;
        break;
      
      case 'articles':
        offset = -21;
        break;
      
      case 'journals':
        offset = -43;
        break;
      
      case 'site':
        offset = -65;
        break;
    }
	
	if ($(this).parents('#search-box').hasClass('site')) {
		// On a second level page
		offset = offset - 4;
	}
	
	$(this).css("backgroundPosition", "8px " + offset + "px");
}

// Function: getSlashes
// Input: The URL to be parsed
// Output: An array containing the position of all slashes in the URL in order of appearence.
function getSlashes(URL){
	var slashes = 0;
	var loc = new Array();
	
	for(var i=0;URL.indexOf("/", i+1)!=-1; i = URL.indexOf("/", i+1)){
		loc[slashes] = URL.indexOf("/", i+1);
		slashes++;
	}	
	return 	loc;
}				

// Function: getPipes
// Input: The title to be parsed
// Output: An array containing the position of all pipes in the title in order of appearence.
function getPipes(title){
	var pipes = 0;
	var loc = new Array();
	
	for(var i=0;title.indexOf("|", i+1)!=-1; i = title.indexOf("|", i+1)){
		loc[pipes] = title.indexOf("|", i+1);
		pipes++;
	}	
	return loc;
}

// Function: getURLSections
// Input: The URL to be parsed
// Output: An array containing the sections of the URL.
function getURLSections(URL){
	var loc = new Array();
	var sections = new Array();
	
	loc = getSlashes(URL);
	
	for(i=1;i<loc.length-1;i++){
		sections[i-1] = URL.substr(loc[i]+1, loc[i+1]-loc[i]);
	}
	
	if(URL.substr(loc[loc.length-1]+1) == ""){
		sections[i-1] = "index.html";
	} else {
		sections[i-1] = URL.substr(loc[loc.length-1]+1);
	}
	
	return sections;
}

// Function: getTitleSections
// Input: The title to be parsed
// Output: An array containing the sections of the title.
function getTitleSections(title){
	var loc = new Array();
	var sections = new Array();
	
	loc = getPipes(title);
	
	sections[0] = title.substr(0, loc[0]-1);
	for(i=1;i<loc.length;i++){
		sections[i] = title.substr(loc[i-1]+2, loc[i]-loc[i-1]-2);
	}
	sections[i] = title.substr(loc[loc.length-1]+2);

	return sections;
}

// Function: getBreadCrumbs
// Input: The URL and title to be parsed
// Output: A string containing the bread crumbs.
function getBreadCrumbs(URL, title) {
	var URLsections = new Array();
	var titleSections = new Array();	
	var breadcrumb = "";
	var target = "http://";
	
	URLsections = getURLSections(URL);
	titleSections = getTitleSections(title);
	
	if(URLsections[URLsections.length-1] == 'index.html')
		count = URLsections.length-1;
	else
		count = URLsections.length;
	
	if(count > titleSections.length)
		count = titleSections.length;
	
	target = "/";
	breadcrumb = "";
	breadcrumb = '<a href="' + target + '">VCU Libraries</a> &raquo; ';
	
	for(i=1;i<count;i++){
		target = target + URLsections[i];
		breadcrumb += '<a href="'+target+'">'+titleSections[i]+'</a> &raquo; ';
	}
	
	return breadcrumb;
}

