jQuery.extend({
  /* Compares two arrays
   * @param [Array] arrayA the first array to compare
   * @param [Array] arrayB the second array to compare
   * @example Comparing two identical arrays
   *   array1 = ["hello", "world"]
   *   array2 = array1
   *   $.compare(array1, array2); // == true
   * @example Comparing two different arrays
   *   $.compare(["hello"], ["world"]); // == false
   * @api private
   */
    compare: function (arrayA, arrayB) {
        if (arrayA.length != arrayB.length) { return false; }
        var a = jQuery.extend(true, [], arrayA);
        var b = jQuery.extend(true, [], arrayB);
        for (var i = 0, l = a.length; i < l; i++) {
            if (a[i] !== b[i]) { 
                return false;
            }
        }
        return true;
    }
});

$(document).ready(function(){
  // set up the breadcrumb menus, if they are present
  if ( $("#xbreadcrumbs").length > 0 )
    $("#xbreadcrumbs").xBreadcrumbs({hideSpeed: 'normal', showSpeed: 'normal'});
  // first check if we even have any crumbs on this page
  if ( $("#crumbs").length > 0 ) {
    // Get the Yahoo! Store paths, history, and current page
    var crust = store.get('crumbs') || null,
        bread = store.get('bread') || [],
        crumb = location.pathname,
        theWayHome = null;
    console.log('before any parsing');
    console.log('crust: ' + crust);
    console.log('bread: ' + bread);
    console.log('crumb: ' + crumb);
    // check that there is a trail of crumbs to follow
    if ( crust != null ) {
      // get the dirt out the crumb
      crumb = crumb.split('/');
      crumb = crumb[crumb.length-1];
      // if the current crumb is in the bread chop the bread down to the crumb
      if ( $.inArray(crumb, bread) ) bread = bread.slice(0,$.inArray(bread,crumb));
      console.log('after slicing the bread');
      console.log('bread: ' + bread)
      // find out which trail of crumbs takes us home the way we came
      console.log('looking for the way home:')
      for (y in crust) {
        path = []
        for (seed in crust[y]) path.push(seed);
        // compare the path to the last chunk of bread of equal length to the path
        b = bread.slice(bread.length-path.length, bread.length);
        console.log('path: ' + path);
        console.log('b: ' + b);
        if ($.compare(path, b)) {
          theWayHome = crust[y];
          console.log('found the way home: ' + theWayHome);
          // store this chunk of bread, and throw away the rest
          store.set('bread', b);
          break;
        }
      }
      
      // if we arrived using another route, walk home on the first trail
      if ( theWayHome == null ) {
        console.log('did not find a way home');
        theWayHome = crust[0];
      }
      // create a breadcrumb trail for theWayHome
      trail = "";
      for ( crumb in theWayHome )
        trail += '<li><a href="' + crumb + '">' + theWayHome[crumb] + '</a></li>';
      $("#crumbs").html(trail);
      
      // push this crumb onto the bread if it isn't there already
      if ( bread[bread.length-1] != crumb ) bread.push(crumb);
      console.log('storing bread: ' + bread);
      // store the bread for the next session
      store.set('bread', bread);
    }
  }
})

