var section;
var escapedHash;

function findHash() {
  if(window.location.hash) {
  	escapedHash = window.location.hash.replace("#!/", "");
  } else {
  	escapedHash = "index";  
  }
  
  var currentURL = escapedHash.split("/");
  section = currentURL[0];
  
  // For "section" content
  $.get("js/call.php?_escaped_fragment_=" + escapedHash + "&section=" + section, function(data) {
  	$('.content.right').html(data);
  	
  	if(section != 'index'){
  		$('body').removeClass('home');
  	} else if(section == 'index'){
  		$('body').addClass('home');
  	}
  	
  	var currentPage = $('.header h1').text();
	  $('li').removeClass('selected');
	  $('span').removeClass('highlight');
	  
	  $("#main #sidenav .sections .subsections li a").each(function(index){
			var currentLink = $(this).html();
			var currentSection = $(this).closest("ul").attr("id");
			if((currentPage == currentLink) && (currentSection == section)){
		  	$("#main #sidenav .sections .subsections li").eq(index).addClass('selected');
				$('.selected span').addClass('highlight');
				return false;
		  }
		});
		
		$("li.selected").each(function(){
			var thing = $(this).parent().attr('id');
			$('ul#' + thing).css('display','block');
			$('li#' + thing + ' div').addClass('active');
			return false;
		});
  });
}

$(document).ready(function() {
  findHash();
});

$(window).bind('hashchange', function() {
  findHash();
});
