/**
 * subcollections.js
 * Nathan Rogers (nrogers@library.wisc.edu)
 * 2006-08-30
 *
* If it is added to a search page it will automatically select the subcollection
 * given within the 'subcollection' query parameter
 */
function preselectSearchLinkWithIdentifier() {
    var subcollectionSelectElement = document.getElementById("subcollectionSearch");
    var targetSubcollectionIdentifier = extractQueryParameter("subcollection");
    
    if (targetSubcollectionIdentifier == "")
       return;
     
    optionsList = subcollectionSelectElement.childNodes;
    for (i=0; i < optionsList.length; i++) {
      currentOption = optionsList[i];
      if ("OPTION" != currentOption.nodeName)
        continue;
      
      currentSubcollectionIdentifier = currentOption.getAttribute("value").toLowerCase();
      
      if (currentSubcollectionIdentifier.length > targetSubcollectionIdentifier.length) {
        currentSubcollectionIdentifier = currentSubcollectionIdentifier.substring(0,  targetSubcollectionIdentifier.length);
      }
      
      if (targetSubcollectionIdentifier == currentSubcollectionIdentifier) {
        currentOption.selected = true;
        break;
      }
    }
}

function preselectSearchLinkWithTitle() {
    var targetSubcollectionTitle = extractQueryParameter("subcollection");
    if (targetSubcollectionTitle == "")
       return;

    var preferredIdentifiers = ['subcollectionSearch', 'term1-is=', 'term2-is=',
      'term3-is=', 'term4-is='];
        
    var subcollectionSelectElement;
    for (i in preferredIdentifiers) {
      subcollectionSelectElement = document.getElementById(preferredIdentifiers[i]);
      if (null != subcollectionSelectElement) {
        break;
      }
    }
    
    /** If that fails, then quit now */
    if (null == subcollectionSelectElement) {
      return;
    }
     
    optionsList = subcollectionSelectElement.getElementsByTagName('option');
    for (i=0; i < optionsList.length; i++) {
      currentOption = optionsList[i];
      
      /**
       * Scrub any nbsp; that may appear in here.  You shouldn't need this but
       * the HTML markup is not clean
       */
      currentSubcollectionTitle = currentOption.text.toLowerCase();
      currentSubcollectionTitle = currentSubcollectionTitle.replace(/^[^a-z0-9]+/, '');
      
      if (currentSubcollectionTitle.length > targetSubcollectionTitle.length) {
        currentSubcollectionTitle = currentSubcollectionTitle.substring(0,  targetSubcollectionTitle.length);
      }
      
      if (targetSubcollectionTitle == currentSubcollectionTitle) {
        currentOption.selected = true;
        break;
      }
    }
}

function extractQueryParameter(parameter) {
  var queryExpressionString = "[\\?&]" + parameter + "=([^&#]*)";
  var queryExpression = new RegExp(queryExpressionString);
  var currentURL = window.location.href;
  
  var results = queryExpression.exec(currentURL);
  
  if (null == results) {
    return "";
  } else {
    /**
      * Original version
      *
      * return results[1].toLowerCase()
      */
      
    /**
      * New version unescapes the query string first
      */
    returnValue = unescape(results[1].replace(/\+/g, " "));
    return returnValue.toLowerCase();
  }
}

var oldEvent = window.onload;
window.onload = function() {
  if (oldEvent) {
    oldEvent();
  }

  preselectSearchLinkWithTitle();
}
