$(function(){
	if(window.location.hash) {
		$('#main-nav .current').removeClass('current');
		$('#main-nav a[href=' + window.location.hash + ']').parent().addClass('current');
	}
	
	setHeights();
	
	$(window).resize(function() {
		setHeights();
	});
	
	$(window).scroll(function(e) {
		$('section:not(#privacy-policy)').each(function() {
			var offset = $(this).offset();
			var height = $(this).outerHeight();
			
			if($(window).scrollTop() >= offset.top && $(window).scrollTop() <= offset.top + height) {
				$('#main-nav li').removeClass('current');
				$('#main-nav a[href=#' + $(this).attr('id') + ']').parent().addClass('current');
				
				var elementClicked = '#' + $(this).attr('id');
				var node = $(elementClicked);
				node.removeAttr('id');
				window.location.hash = elementClicked;
				node.attr('id', elementClicked.replace(/^#/, ''));
			}
		});
	});
	
	$('#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;
		
		if(elementClicked == '#home') {
			destination = 0;
		}
		
		$("html:not(:animated),body:not(:animated)").animate({scrollTop: destination}, 750, 'easeInOutExpo');
		
		// Update hash - The ID is removed 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;
	});
	
	$('#logo-slider').easySlider({ auto: true, continuous: true, pause: 5000, controlsShow: false });
	
	$('#pp-link').click(function() {
		$('#privacy-policy').show();
		
		return false;
	});
	
	$('#close-link').click(function() {
		$('#privacy-policy').hide();
		
		return false;
	});
});

function setHeights() {
	$('section:not(#privacy-policy) .wrapper').each(function() {
		$(this).css('min-height', $(window).height());
	});
}
