$(document).ready(function() {
	/*
	 * Drawers code for right navigation
	 */

	// hide portfolio dropdown nav when hovering on other nav items
	$("#headernav > .navtop > li.has_navchild").not(".portfolio").hover(
		function() {
			$("#headernav > .navtop > li.portfolio > ul").addClass("hide");
		},
		function() {
			$("#headernav > .navtop > li.portfolio > ul").removeClass("hide");
		}
	);

	// open selected nav items
	$(".navsub > li.has_navchild").filter(".selected").each(function(){
		$(this).children("ul").removeClass("hide").addClass("show");
	});
	$(".navportfolio > li.has_description, .navsub > li.has_navchild").filter(".selected").each(function(){
		$(this).children(".description").removeClass("hide").addClass("show");
	});
	
	// needed so it doesn't try to animate another before this one is finished.
	var oktoanimate = true;

	// run checks and animate on mouseover
	$(".navsub > li:has(ul) > a").click(function () {
		if(oktoanimate) {
			// only animate if is not already out
			if($(this).siblings(".show").length == 0) {
				oktoanimate = false;
				// animate in all that are already out
				list = $(".navsub .show");
				$(list).removeClass("show");
				$(list).slideUp("slow", function() {
					$(this).addClass("hide");
				});
				// animate this one out
				$(this).siblings(".hide").slideDown("slow", function() {
					$(this).removeClass("hide");
					$(this).addClass("show");
					oktoanimate = true;
				});
			}
		}
		return false;
	});

	// run checks and animate on click
	$(".navportfolio > .has_description > a").click(function () {
		if(oktoanimate) {
			// only animate if is not already out
			if($(this).siblings(".show").length == 0) {
				oktoanimate = false;
				// animate in all that are already out
				list = $(".navportfolio .show");
				$(list).removeClass("show");
				$(list).slideUp("slow", function() {
					$(this).addClass("hide");
				});
				// animate this one out
				$(this).siblings(".hide").slideDown("slow", function() {
					$(this).removeClass("hide");
					$(this).addClass("show");
					oktoanimate = true;
				});
			}
		}
		return false;
	});
});
