$(function(){
  
  // For each .bbq widget, keep a data object containing a mapping of
  // url-to-container for caching purposes.
  /*$('.nav').each(function(){
						  
	$('#content-loading').show();
	
    $(this).data( 'content', { 
      cache: {
        // If url is '' (no fragment), display this div's content.
        '': $('#content-default')
      }
    });
  });*/
  
  // Keep a mapping of url-to-container for caching purposes.
  var cache = {
    // If url is '' (no fragment), display this div's content.
    '': $('#content-default')
  };
  
  // For all links inside a .nav widget, push the appropriate state onto the
  // history when clicked.
  /*$('.nav a[href^=#]').live( 'click', function(e){
    var state = {},
      
      // Get the id of this .nav widget.
      id = $(this).closest( '.nav' ).attr( 'id' ),
      
      // Get the url from the link's href attribute, stripping any leading #.
      url = $(this).attr( 'href' ).replace( /^#/, '' );
    
    // Set the state!
    state[ id ] = url;
    $.bbq.pushState( state );
    
    // And finally, prevent the default link click behavior by returning false.
    return false;
  });*/
  
  // Bind an event to window.onhashchange that, when the history state changes,
  // iterates over all .nav widgets, getting their appropriate url from the
  // current state. If that .nav widget's url has changed, display either our
  // cached content or fetch new content to be displayed.
  $(window).bind( 'hashchange', function(e) {
    
    // Iterate over all .nav widgets.
    //$('.nav').each(function(){
      //var that = $(this),
        
        // Get the stored data for this .nav widget.
        //data = that.data( 'content' ),
        
        // Get the url for this .nav widget from the hash, based on the
        // appropriate id property. In jQuery 1.4, you should use e.getState()
        // instead of $.bbq.getState().
        //url = e.getState( that.attr( 'id' ) ) || '';
    
      // If the url hasn't changed, do nothing and skip to the next .nav widget.
      //if ( data.url === url ) { return; }
      
      // Store the url for the next time around.
      //data.url = url;
	  
	  
	  
	  
	  
// Get the hash (fragment) as a string, with any leading # removed. Note that
    // in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
    var url = e.fragment;
	
      // Remove .nav-current class from any previously "current" link(s).
      $( 'a.nav-current' ).removeClass( 'nav-current' );
      
      // Hide any visible ajax content.
	  $('#content').children( ':visible' ).hide();
      
      // Add .nav-current class to "current" nav link(s), only if url isn't empty.
      url && $( 'a[href="#' + url + '"]' ).addClass( 'nav-current' );
	  
	  //alert(url.substring(0,url.indexOf('.')));
	  
	  var img_array = Array();
	  
	  switch(url.substring(0,url.indexOf('.'))){
		  case 'about':	img_array = ['about_on','menu_on_off','dot','staff_off','dot','moments_off','dot','location_off'];	
		  				url = '';
		  				break;
		  case 'menu':	img_array = ['about_off_on','menu_on','staff_on_off','dot','moments_off','dot','location_off'];
		  				break;
		  case 'staff':	img_array = ['about_off','dot','menu_off_on','staff_on','moments_on_off','dot','location_off'];
		  				break;
		  case 'moments':	img_array = ['about_off','dot','menu_off','dot','staff_off_on','moments_on','location_on_off'];
		  					break;
		  case 'location':	img_array = ['about_off','dot','menu_off','dot','staff_off','dot','moments_off_on','location_on']; 
		  					break;
		  default:	img_array = ['about_on','menu_on_off','dot','staff_off','dot','moments_off','dot','location_off']; 
		  			break;
	  }
	  	  
 
      if ( cache[ url ] ) {
        // Since the widget is already in the cache, it doesn't need to be
        // created, so instead of creating it again, let's just show it!
        cache[ url ].show();
			  menuChange(url, img_array);

        
      } else {
        // Show "loading" content while AJAX content loads.
		$('#content-loading').show();
  
        // Create container for this url's content and store a reference to it in
        // the cache.
        cache[ url ] =  $('<div class="content-page"/>')
		
          
          // Append the content container to the parent container.
          .appendTo( $('#content') )
          
          // Load external content via AJAX. Note that in order to keep this
          // example streamlined, only the content in .infobox is shown. You'll
          // want to change this based on your needs.
          .load( url, function(){
			 // alert("hello");
		  	  menuChange(url, img_array);

            // Content loaded, hide "loading" content.
			//setTimeout('$("#content-loading").hide()',2000);
			$('#content-loading').hide();
          });
      }
	  
	  function menuChange(url, img_array){
		  var length = img_array.length;
	
		  var newHTML = "";
           
		  for (i = 0; i < length; i++){
			  if (img_array[i] == "dot"){
			      newHTML = newHTML.concat('<span><img src="images/dot.png" class="navbar_dot" /></span>');
			  }
			  else{
		          newHTML = newHTML.concat('<a href="#'+img_array[i].substring(0,img_array[i].indexOf("_"))+'.php"><img src="images/'+img_array[i]+'.png" class="navbar_img" /></a>');
			  }
		  }
		  if (url == ''){
	  	      $( 'a[href="#about.php"]' ).parent().html(newHTML);
		  }
		  else {
              $( 'a[href="#' + url + '"]' ).parent().html(newHTML);
		  }
		  
		  setTimeout('setNavBarWidth()',5);
	  }
    //});
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );
  
});
