deriveRoot = function ()
{
  var root = $('link[rel="shortcut icon"]:first').attr('href'); // Will find something like https://secure.example.com/favicon.ico

  return root.replace( 'favicon.ico','' );
}

function activateSearchBox ()
{
  if ( $( 'form#search input#keywords' ).val() == '' )
  {
    $('form#search input#keywords').attr('value','Search...');
  }

  $('form#search input#keywords').bind('focus',function()
  {
    if ( $(this).val() == 'Search...' )
    {
      $(this).attr('value','');
    }
  }).bind('blur',function()
  {
    if ( $(this).val() == '' )
    {
      $(this).attr('value','Search...');
    }
  });

  // Stop open searches
  $('form#search').bind('submit',function()
  {
    var q = $('input#keywords').val();

    if ( q == '' || ( ( q.split('%').length - 1 ) == q.length ) )
    {
      return false;
    }
  });
}

function captureExternalLinks ()
{
  $('a[rel*="external"]').live( 'click',function()
  {
    window.open( $(this).attr('href') );
    return false;
  } );
}

function captureFilterForms ()
{
  $('form#filter button').hide();
  $('form#filter ol').css( 'float','right' );
  $('form#filter select').live( 'change',function()
  {
    $('form#filter').submit();
  } );
}

function captureBackToTop ()
{
  $('a[href="#top"]').live( 'click',function()
  {
    $.scrollTo( 0,{duration:800} );
    return false;
  } );
}

function enableLightboxes ()
{
  $('a[rel*=lightbox]').lightBox();
}

function enableCarousels ()
{
  $('ul.carousel').cycle();
}

function enableTabs ()
{
  $('div#tabset ul.tabs').show();
  $('ul.tabs').tabs('div#tabset > div.tab',{ history: true });
}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  },
  getUrlFragments: function(){
    var frags = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      frags.push(hash[0]);
      frags[hash[0]] = hash[1];
    }
    return frags;
  },
  getUrlFragment: function(name){
    return $.getUrlFragments()[name];
  }
});

restripeTable = function( table )
{
  $(table).find('tbody tr').removeClass('alt');
  $(table).find('tbody tr:odd').addClass('alt');
}

function removeFBHash () {
  if ( 'replaceState' in history ) {
    history.replaceState( '',document.title,window.location.pathname );
  } else { // older browsers...
    window.location.hash = '';
  }
}

$(document).ready( function ()
{
  var root = deriveRoot();
  
  if ( window.location.hash == '#_=_' ) { // Extraneous crud from FB login redirect
    removeFBHash();
  }
  
  activateSearchBox();
  captureExternalLinks();
  captureFilterForms();
  captureBackToTop();
  enableLightboxes();
  enableCarousels();
  enableTabs();
});
