/*******************************************************************************
*                                SPLat Controls                                *
*                          Product Development Group                           *
*                            Melbourne,  AUSTRALIA                             *
*                            http://www.splatco.com                            *
********************************************************************************
   DESCRIPTION:
   This is the anchor navigation module the allows back/forward navigation
   through the tabs and topics.

   DEPENDANCIES:
   - jquery
   - jquery.ba-hashchange.js

********************************************************************************
*           Copyright (c) 2010 SPLat Controls. All rights reserved.            *
*******************************************************************************/

//Call the WhenReady function once the page has loaded
$( WhenReady );


/*------------------------------------------------------------------------------
DESCRIPTION:
 This function is called when the page has loaded.  It binds the DoAnchorNav()
 function to the hashchange event that comes courtesy of the
 jquery.ba-hashchange.js script.  This allows navigation when the user clicks
 forward/back navigation.  Finally it triggers the hashchange event to navigate
 to the specified page, or the default page if none was specified.
PARAMETERS:
  -> nil
RETURNS:
 <-  nil
------------------------------------------------------------------------------*/
function WhenReady()
{
   //bind the DoAnchorNav() function to the window.onhashchange function that, when the hash changes, shows the matching container.
   $( window ).bind( 'hashchange', function()
   {
      DoAnchorNav( location.hash );
   });

   //Now the page has loaded and since the event is only triggered when the hash changes, we need to trigger the event to handle the hash the page may have loaded with.
   //$( window ).trigger( 'hashchange' );
   //the search script will do this for us
}


/*------------------------------------------------------------------------------
DESCRIPTION:
 This function is called when either a tab or content link is clicked or when
 the user navigates backward or forward.  It shows the corresponding content and
 highlights the corresponding tab.
PARAMETERS:
  -> id of the container content to show.  For example "#href-tab2-content3", where:
   - "tab2-content3" is the id of the container to show
   - "tab2" is the corresponding tab to highlight
RETURNS:
 <-  nil
------------------------------------------------------------------------------*/
function DoAnchorNav( hash )
{
   var deflt = 'tab1-content1';
   var tab;
   var container;
   var i;
   var parent;

   //no link clicked? (occurs when navigating directly to the page without an anchor)
   if( hash.length < 7 )
      //none, so use the default
      hash = '#href-' + deflt;

   //find a '&' char in the hash (note, this is non-standard)
   i = hash.indexOf( '&' );
   //was a '?' found?
   if( i != -1 )
      //yes, so remove the '&' & trailing text
      hash = hash.slice( 0, i );

   for(;;)
   {
      //figure the container id by removing the 'href-' prefix
      container = '#' + hash.slice( 6 );

      //is the container empty?
      if( $( container ).is( ':empty' ) )
         //yes, so use the default
         hash = '#href-' + deflt;
      else
         break;
   }

   //figure the tab that corresponds to this link (a container id of "#aaa-bbb-ccc" is tab id "#href-aaa")
   i = container.indexOf( "-" ); //find the '-' pos
   i = i == -1 ? container.length : i; //if there's no '-', use the whole string
   tab = '#href-' + container.substring( 1, i ); //prepend '#href-' to the 'aaa'
   parent = '#' + container.substring( 1, i );//get the parent container id 'aaa'

   //hide all table decendents of the content container
   $( '[id^="tab"]' ).hide();

   //show the parent container
   $( parent ).show();

   //fade in the chosen content container
   $( container ).fadeIn( 500 );

   //unhighlight the content links
   $( '#content a' ).removeClass( 'sidelinksBold' );

   //highlight the content link
   $( '#content a[href="' + hash + '"]' ).addClass('sidelinksBold');

   //unhighlight the tabs
   $( '.tabs li' ).removeClass( 'current' );

   //change the tab href to remember the chosen topic
   //highlight the tab (add "current" to the <li>) that corresponds to the chosen container and ensure it's visible)
   $( '.tabs li a[href|="' + tab + '"]' ).attr( 'href', hash ).parent().addClass('current').show();

   //contruct page title from tab + sideline text
   //get the sidelink text (do this first so we don't add a separator if there's no sideline text (eg "Get Started" has no sideline))
   i = $( parent + ' a[href="' + hash + '"]' ).text();

   //any sideline text?
   if( i.length )
      //yes, prepend a separator
      i = ' - ' + i;

   //prepend the tab text
   i = $( '.tabs li a[href="' + hash + '"]' ).text() + i;

   //remove newline & tab chars OR multiple spaces with a single space
   i = i.replace( /[\n\r\t ]+/g, ' ' );

/*
   //does the container have a title?
   if( $( container ).attr( 'title' ).length )
      i = $( container ).attr( 'title' )
*/

   //set the document title
   document.title = '[SPLat Controls] ' + i;

   //===============
   //== Analytics ==
   //===============
   //is analytics not disabled?
   if( $( container ).hasClass( 'noanalytics' ) == false )
   {
      //not disabled, so try sending
      DoAnalytics( 'Homepage - ' + i );
   }
}




