// siteMap.js
var com = com || {};
com.YourFriendPaul = com.YourFriendPaul || {};
com.YourFriendPaul.siteMeter = com.YourFriendPaul.siteMeter || {}; 
(function( ns ){
   
   // flag to reload page if location has hash and is false
   var isAjax = false;
   
   
   // post is not used for siteMap
   function ajaxPostUpdate( params ){
   
         //alert( 'params: ' + params );
         $.post( "php/siteMapAjax.php", params,
            function( data ) {
               //alert( data );
               var obj = JSON.parse( data );
               $( "#pageNavContent" ).html( obj.nav );
               
               $( "#pageMainContent" ).html( obj.main );
               captureClicks();
            } 
         );
   }
   
   // parmas has as a minimum a valid psm1 value.
   // params is something like: stb=psm4&submitName=SiteMap
   // The submitName is added by the click handler. The php can use it to determine
   //  that this is an ajax request

   ns.ajaxGetUpdate = function( params ){
   
      // ( url, [ data ], [ callback( data, textStatus, XMLHttpRequest ) ], [ dataType ] )
      $.get( "php/siteMapAjax.php", params,
            // only use the data portion in the callback
            function( data ) {
               //alert( "callback data is: " + data );
               var obj = JSON.parse( data );
               
               // get the page name
               var newPage = obj.page;
               
               $( "#pageNavContent" ).html( obj.nav );
               
               $( "#pageMainContent" ).html( obj.main );
               $( "body" ).attr("id", obj.page );
               ns.modifyLocBar( obj.isAjax, obj.page );
               ns.captureClicks();
            } 
         );
   };
   

   function paramStringToObj( str ){
   
      //var ret = { pg: "", id: "", submitName: "" };
      var itm;
      var ret = {}, p2;
      var parts = str.split( "&" );

      for( itm=0; itm < parts.length; itm++ ){
      
         p2 = parts[ itm ].split( '=' );
         ret[ p2[ 0 ] ] = ( p2[ 1 ] ) ? p2[ 1 ] : "";
      }
      
      return ret;
   }
   
   
   // all should be stb=psm
   // params is something like: stb=psm4&submitName=SiteMap
   // check the the stb value is valid, if so start the ajax get()
   function handleClicks( params ){
      var parObj = paramStringToObj( params );
      
      // alert( "stb type in handle clicks: " + parObj.stb );
      switch( parObj.stb ){
         case "psm1":
         case "psm2":
         case "psm3":
         case "psm4":
         case "psm5":
            ns.ajaxGetUpdate( params );
            return false;
         
         // do nothing for illegal value
         default:
            //alert( "bad ajax type in handle clicks: " + params );
            break;
      }
      
      return true;
   }
   
   // look for any nav button click
   ns.captureClicks = function(){
   
      $( '#smNavBar a' ).click( function( event ){
      
            var hr, hr1, hr2, hr3, params;
            
            hr = $(this).attr( 'href' );
            // replace before the '?' with empty
            hr1 = hr.replace( /(\.+(php|html)+\?).*$/, "" );
            
            // just show the stuff after the '?'
            hr2 = hr.replace( /^.*\?/, "" );
            hr3 = hr2 + "&submitName=" + hr1;
            params = hr3;
            
            // hr3 ends up something like: stb=psm4&submitName=SiteMap
            //alert( "captureClicks link to: " + hr3 );
            
            // don't allow the link to be followed
            event.preventDefault();
            return handleClicks( params );
         }
      );
      
   };

   ns.modifyLocBar = function( isAjax, page ){
   
      //alert(  isAjax + " and page is " + page );
      var loc = window.location;
      
      if( isAjax ){
      
         // can't change search, it causes a reload
         //window.location.search = "";
         if( window.location.hash != page ){
            window.location.hash = page;
         }
      }
   };
   
   // see if we need to change the content because we got here
   // by a back button or bookmark
   // Doesn't work with back buttons, but does with bookmark links
   ns.checkLocation = function(){
   
      var hr, hr1, hr2, hr3, params;
      var hash = window.location.hash;
      //alert( "checkLocation shows window hash: " + hash );
      hash = hash.slice( 1 ); // drop the #

      var psmPart = hash.slice( 0, 3 ); // first 3 chars
      var psmNum = parseInt( hash.slice( 3 ), 10 ); // next should be a digit

      // if it parsed into a digit
      if( psmNum > 0 ){
      
            // determine the domain name without prefix or suffix for: submitName=
            hr = window.location.pathname;
            // remove from .php or .html to end of path
            hr1 = hr.replace( /(\.(php|html)).*$/, "" );
            // remove any leading /
            hr1 = hr1.replace( /^(.*)\//, "" );
            // remove everything before the last ., incl. the .
            hr1 = hr1.replace( /^(.*)\./, "" );
            
            hr2 = psmPart + psmNum;
            hr3 = 'stb=' + hr2 + "&submitName=" + hr1;
            params = hr3;
            
            // hr3 ends up something like: stb=psm4&submitName=SiteMap
            //alert( "link to: " + hr3 );
            // pretend this was generated by a click to start processing
            return handleClicks( params );
      }
      
      //alert( "hash: " + hash + ", " + psmPart + psmNum );
      // just so the function always returns a value
      return null;
   };
   

})( com.YourFriendPaul.siteMeter );

// start captureClicks() again on successful ajax return
$(document).ready( function() {

   com.YourFriendPaul.siteMeter.captureClicks();
   com.YourFriendPaul.siteMeter.checkLocation();
});

