bb = {}

bb.sidenav = function() {
	var $view=$(document);
	var $menu = $("#nav");
	
	if(!$menu.size()) {
		return;
	}
	
	var originalMenuTop=$menu.offset().top;
	$view.bind("scroll resize",function(){
		var viewTop=$view.scrollTop();
		if(viewTop>originalMenuTop){
			$("body").addClass("fixed");
		}else if(viewTop<=originalMenuTop){
			$("body").removeClass("fixed");
		}
	});
}

bb.treatments = {
	
	timer: null,
	
	delay: 100,
	
	closeme: function() {
		this.timer = setTimeout('bb.treatments.hidemenu()',this.delay);
	},
	
	mouseover: function() {
		this.amover = true;
		this.showmenu();
	},
	
	mouseout: function() {
		this.amover = false;
		this.closeme();
	},
	
	showmenu: function() {
		clearTimeout(this.delay);
		$("#nav-treatments").addClass("over");
		$("#treatments-nav").show()
	},
	
	hidemenu: function() {

		if(this.amover || $("body").hasClass("treatments")) {
			return;
		}
		
		clearTimeout(this.delay);
		$("#nav-treatments").removeClass("over");
		$("#treatments-nav").hide();
	}
}

$(function(){

	// external links
	$("a[rel='external']").click(function(e){
		e.preventDefault();
		window.open($(this).attr("href"));
	});

	// sitemap
	$("#sitemap-link").click(function(e){
		e.preventDefault();
		$("body").toggleClass("sitemap");
		if($.browser.msie) return;
		$("#sitemap").slideToggle(function(){
			if($("body").hasClass("sitemap")) {
				$('html,body').animate({scrollTop: $("#sitemap").offset().top}, 'fast');
			}
		})
	})
	
	// treatments menu
	$("#nav-treatments, #treatments-nav").hover(
		function(){bb.treatments.mouseover()},
		function(){bb.treatments.mouseout()}
	);
	
	bb.sidenav();
})
