//------ Keep track of whether the navigation is fixed in place.
var SCROLLELEMENT = 'html, body';
var SCROLLPADDING = 0;
function scrollToElement(el) {
	$(SCROLLELEMENT).stop().animate(
		{ scrollTop: el.offset().top - SCROLLPADDING }, 
		600,function(){
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				$("#nav").css("visibility","visible");
			}	
		}
	);	
}

//------ Navigation event handling.
$(document).ready(function() {			
	
	//------ Add a class to the body for JS enabled pages
	$("body").addClass("js");
	
	//------ Cufon text replacement
	Cufon.replace('h2,h3');
	
	//------ Setup the scrollable items
	$(".scrollable").scrollable({circular: true}).navigator();
	
	//------ Setup the overlay items
	$("a[rel='#overlay']").overlay({
		top:100,
		mask:"#fff",
		onBeforeLoad: function() {
			var wrap = this.getOverlay().find("#overlayContentWrap");
			wrap.html("");
			wrap.load(this.getTrigger().attr("href"));
		}/** /,
		onLoad: function() {
			$("html").animate({
				opacity:1
			},500,
			function(){
				Cufon.replace('h2,h3');	
			});
		}/**/
	});
	
	//------ Setup in-page scrolling for .internal links.
	$('.internal').click(function(e) {
		e.preventDefault();
		var target = $(this.hash);
		if(target.length && this.hash) {
			scrollToElement(target);
		}
	});
	
	//------ Set the highlighted link
	$('#nav a').click(function(e) {
		e.preventDefault();
		var tabSet = $(this).parent().parent();
		tabSet.find('li').removeClass('active');
		$(this).parent().addClass('active');
		$(this).blur();
	});
	
	//------ Listen on scroll event to modify the navigation's position as needed.
	var nav = $('#nav');
	var the_window = $(window);
	var topmost_point = nav.offset().top;
	
	//------ Capture the height of the navigation bar.			
	SCROLLPADDING = nav.height();
	
	//------ Sniff for whether to use html or body element for scrolling.
	$('html, body').each(function () {
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') == initScrollTop + 1) {
			SCROLLELEMENT = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}
	});
	
	//------ Listen to window scroll event for IE6
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		the_window.scroll(function() {
			// IE6 Specifics
			nav.css({
				position: 'absolute',
				top:document.documentElement.scrollTop,
				left:0
			});
		});
	}
	
});
