$(document).ready(function(){
	// hide all non current categories
	$("#categories > ul > li:not(.current) > ul").hide();

	// on click of category link
	$("#categories > ul > li > a").click(function(e){

		//prevents default event
		e.preventDefault();

		// animation variables
		var animationTime = 600;
		var animationEasing = 'linear'

		// If parent of li is not current
		if ($(this).parent().not(".current").length != 0) {
			
			$("#categories > ul > li").removeClass("current"); // remove current class
			$(this).parent().addClass("current"); // add current class to clicked link
	
			// if there are any ul elements not current but still visible
			if ($("#categories > ul > li:not(.current) > ul:visible").length != 0) {
				
				// hide non current ul
				$("#categories > ul > li:not(.current) > ul:visible").animate({ height: "toggle" }, $(this).children().length * animationTime, animationEasing);

				//show child ul of current li
				$("#categories > ul > li.current > ul:hidden").animate({ height: "toggle" }, $(this).children().length * animationTime, animationEasing);
			}
			else
			{
				// show child ul of current li
				$("#categories > ul > li.current > ul:hidden").animate({ height: "toggle" }, $(this).children().length * animationTime, animationEasing)
			}
		}
		// parent of li is current
		else {
			// if child ul of li is current
			if ($("#categories > ul > li.current > ul:visible").length != 0) {
				$("#categories > ul > li.current > ul").animate({ height: "toggle" }, $(this).children().length * animationTime, animationEasing);
				$("#categories > ul > li").removeClass("current");
			}
			else {
				$("#categories > ul > li.current > ul").animate({ height: "toggle" }, $(this).children().length * animationTime, animationEasing)
				$(this).parent().addClass("current");
			}
		}
	});
});
