;(function($){
  $.fn.secondaryNav = function( navTitles, options ) {
    navTitles = $.extend( [], navTitles );
    options = $.extend( {
      'pagerSelector': '.secondNav',
      'pagerStartIndex': 0
    }, options );
    
    var title = document.title;
    var convertedNavTitles = [];
    $.each( navTitles,function( i, val ) {
      convertedNavTitles[ i ] = ( function( val ) {
        return ( encodeURIComponent( ( val + '' ).toString() ).replace( /!/g, '%21' )
              .replace( /'/g, '%27' ).replace( /\(/g, '%28' )
              .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' )
              .replace( /%20/g, '-' ).replace( /%26/g, 'and') ).toLowerCase();
      } )( val );
    } );

    var targetSlide = ( function( name ) {
      var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec( window.location.href );
      if( results ) {
        return results[ 1 ];
      } else {
        return 0;
      }
    } )( 's' );
    
    if( targetSlide ) {
      target = $.inArray( targetSlide, convertedNavTitles );
      if( target == -1 ) { target = "0"; }
    } else {
      target = "0";
    }
    
    $( this ).cycle( { 
      fx: 'scrollHorz', 
      speed: 600, 
      pager: options.pagerSelector,
      timeout: 0,
      startingSlide: target,
      pagerAnchorBuilder: function( idx, slide ) {
        if( idx < options.pagerStartIndex ) { return; }
        return '<li>' + navTitles[ idx ] + '</li>'; 
      },
      before: function( currSlide, slide ) {
        var slide = $( slide );
        slide.parent().animate( {
          'height': slide.height()
        } );
      },
      after: function( cSlide, nSlide, opt ) {
        window.location.hash = '#?s=' + convertedNavTitles[ opt.currSlide ];
        if( document.title != title ) {
          document.title = title;
        }
      }
    } );
    
    $( options.pagerSelector + ' li' ).hover( function() {
      $( this ).addClass( 'hover' );
    }, function() {
      $( this ).removeClass( 'hover' );
    } );
    
    ( function() {
      /**
       *  Keep hash pointing at current slide when quick links are used 
       *  within a page. Uses onHashChange where available, otherwise falls back 
       *  to polling window.location.hash for changes.
       */
      var hash = '';
      var onhashchange = function() {
        var cHash = window.location.hash, tmp ='';
        /* Fix IE6 Bug */
        if( cHash == '#' ) {
          tmp = window.location.href.split( '#' );
          cHash = '#' + tmp[ 1 ];
        }
        if( ( cHash.substring( 0, 4 ) == '#?s=' ) && ( hash != cHash ) ) {
          hash = cHash;
        } else {
          if( cHash != hash ) {
            window.location.hash = hash;
          }
          if( document.title != title ) {
            document.title = title;
          }
        }
      };

      if( 'onhashchange' in window ) {
        hash = window.location.hash;
        window.onhashchange = onhashchange;
      } else {
        setInterval( onhashchange, 500 );
      }
    } )();
    
    return this;
  };
}(jQuery));