$.fn.dropdown = function() {
	var self = this;
	return this.each(function(){
		var thisId = "#" + this.id
		var slideOutPanelId = "#" + this.id.split("-")[0] + "-" + this.id.split("-")[1] + "-content"
		var slideOutPanel = $(slideOutPanelId);
		$(this).hover(function(){
			$(".slide-out").slideUp(50);
			self.removeClass("active");
			slideOutPanel.slideDown(50);
	    $(this).addClass("active");
		});
		
		$("#full-menu").hover(function(){},
		function() {
			self.removeClass("active")
			slideOutPanel.slideUp(50);
		})
	});
};

$(document).ready(function(){
	$("#menu .container tr td a").dropdown();
	
	var toc = $("ul#toc").html();
	var linkNum = 0;
	$("#content :header:not(h1), #comments h2").each(function(){
		linkName = "section-" + linkNum;
		toc += "<li class=\""+ $(this)[0].tagName +"\"><a href=\"#" + linkName + "\">" + $(this).text() + "</a></li>";
		this.id = linkName
		linkNum++;
	})
	
	if (linkNum == 0) {
		$("ul#toc").hide();
	}
	
	$("ul#toc").html(toc);
	
	$("ul#toc").css("top", 176)
	if ($(window).scrollTop() < 176) {
		$("ul#toc").css("position", "static")
	}
	else {
		$("ul#toc").css("top", 10)
		$("ul#toc").css("position", "fixed")
	}
	$(window).scroll(function(){
		if ($(window).scrollTop() < 176) {
			$("ul#toc").css("position", "static")
		}
		else {
			$("ul#toc").css("top", 10)
			$("ul#toc").css("position", "fixed")
		}
	})
	
	setInterval("switchSlideshowBackground()", 5000);
	
})

function switchSlideshowBackground(){
	$("#slideshow").css("backgroundPosition", "0 " + (Math.floor(Math.random() * 3) * 300).toString() + "px")
}
