$(function(){
	if(window.location.hash)
	{
		$('#main-nav .current').removeClass('current');
		$(window.location.hash + '-menu').addClass('current');
	}
	
	$('#main-nav li').each(function(i)
	{
		var randomMargin = (Math.floor(Math.random()*11)) + 'px',
			duration = 200;
		
		$(this).css('margin-left', randomMargin)
			   .hover(function(event)
			   {
			       $.oldMargin = $(this).css('margin-left');
			       var newMargin = (parseInt($.oldMargin) + 3) + 'px';
			        
			       $(this).animate({'margin-left': newMargin}, duration);
			   }, 
			   function(event)
			   {
			       $(this).animate({'margin-left': $.oldMargin}, duration);
			   });
	});
	
	$('#main-nav a').bind('click', function() {
		
		// If this is already the current page, do nothing
		if($(this).attr('href') == window.location.hash)
		{
			return false;
		}
		
		// Change the class to move the highlight color
		$('#main-nav .current').removeClass('current');
		$(this).parent().addClass('current');
		
		var elementClicked = $(this).attr("href");
		var destination = $(elementClicked).offset().top;
		
		$("html:not(:animated),body:not(:animated)").animate({scrollTop: destination}, 750, 'easeInOutExpo');
		
		// Update hash - The ID is reomved and readded to keep the page from scrolling twice
		var node = $(elementClicked);
		node.removeAttr('id');
		window.location.hash = elementClicked;
		node.attr('id', elementClicked.replace(/^#/, ''));
		
		return false;
	});
});
