if(typeof($)=='function') {
  $(document).ready(function(){
    // Removes non-canonical parts from the pathname of a location
    function filterPath(string) {
      return string
        .replace(/^\//, '') 
        .replace(/(index|default).[a-zA-Z]{3,4}$/, '') 
        .replace(/\/$/, '');
    }
    
    // Works out which frame we have selected
    function getSelectedFrame() {
      var selected = location.hash;
      if(selected=='') {
        // default to 'start'
        selected = '#start';
      }
      return selected;
    }

    // Displays the sepecified frame
    function showFrame(selected) {
      $('#start, #broadband, #smartbox>li>ol>li')
       .not('#smartbox>li>ol>li li').not(selected).hide();
      $(selected).show();
    }



    // Hide all of the frames except the selected one
    showFrame(getSelectedFrame());
    
    // Handle link clicks
    // - based on code from http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
    $('#smartbox a[href*=#]').each(function() {
      if(filterPath(location.pathname)==filterPath(this.pathname)
         && location.hostname == this.hostname
         && this.hash.replace(/#/,'')) {
        var $targetId = $(this.hash);
        var $targetAnchor = $('[name=' + this.hash.slice(1) +']');
        var $target = $targetId.length
                      ? $targetId
                      : $targetAnchor.length
                        ? $targetAnchor
                        : false;
        if($target) {
          var targetOffset = $target.offset().top;
          $(this).click(function() {
            showFrame(this.hash);
            return false;
          });
        }
      }
    });
  });
}

