var maxtabheight = 0;
$('#leftmenu .subitems').each(function() {
	var list = $(this);
	var tab = list.parent().find(".tab");
	var listH = list.height();
	list.css({maxHeight: listH, height: 1, display: "none"});
	if(listH > maxtabheight)
		maxtabheight = listH;
});

maxtabheight += $('#leftmenu').height();

var pageitem = $('#leftmenu .current').parent();
var pagelist = pageitem.find(".subitems");
pageitem.addClass("active");
pagelist.css({height: pagelist.css("maxHeight"), display: "block"});

$('#leftmenu').find(".tab").hover(function() {
		var target = $(this).parent();
		target.addClass("hovered");
		target.find(".tab").addClass("black");
	},
	function() {
		var target = $(this).parent();
		var anim = target.hasClass("animating");
		var active = target.hasClass("active");
		target.removeClass("hovered");
		
		if(!anim && !active)
			target.find(".tab").removeClass("black");

		if((active && anim) || (!active && !anim))
			target.find(".tab").removeClass("black");
	}
);

var body_height = 1;
var active_height = 1;
var new_height = 1;
$('#leftmenu').find(".subitems").parent().find(".tab").click(function() {
	$('#leftmenu').height(maxtabheight);
	var target = $(this).parent();
	if(target.hasClass("active")) {
		target.find(".arrow").removeClass("down");
		target.addClass("animating");
		//target.find(".tab").removeClass("black");
		target.children(".subitems").animate({height:1}, {
			duration: 300,
			complete: function() {
					target.removeClass("animating");
					target.removeClass("active");
					target.children(".subitems").css({display: "none"});
					$('#leftmenu').height("auto");
			}
		});
	}
	else {
		var oldTarget = target.parent().find(".active");
		
		target.find(".arrow").addClass("down");
		target.find(".tab").addClass("black");
		target.addClass("animating");
		
		oldTarget.find(".arrow").removeClass("down");
		oldTarget.find(".tab").removeClass("black");
		oldTarget.find(".tab").removeClass("current");
		oldTarget.addClass("animating");

		body_height = target.children(".subitems").css("maxHeight");
		active_height = target.parent().find(".active").children(".subitems").css("maxHeight");
	
		body_height = parseInt(body_height.toString().replace(/\D/gi, ""));
		if(typeof active_height != "undefined")
			active_height = parseInt(active_height.toString().replace(/\D/gi, ""));
		else
			active_height = 1;	
		
		target.children(".subitems").css({display: "block"});
		target.children(".subitems").animate({height:body_height}, {
			step: function(now, fx) {
				new_height = active_height * (1-(now/body_height));
				if(new_height < 1)
					new_height = 1;
				oldTarget.children(".subitems").height(new_height);											
			},
			duration: 300,
			complete: function() {
				oldTarget.removeClass("active");
				oldTarget.children(".subitems").css({display: "none"});
				oldTarget.removeClass("animating");
				target.addClass("active");
				target.removeClass("animating");
			}
		});
	}
	return false;
});

/*
$('#leftmenu').find(".subitems").parent().find(".tab").hoverIntent({
	sensitivity: 5,
	interval: 100,
	over: function() {
		var target = $(this).parent();
		var oldTarget = target.parent().find(".active");
		
		if (target.attr("class").indexOf("active") == -1) {
		
			target.find(".arrow").addClass("down");
			target.find(".tab").addClass("black");
			
			oldTarget.find(".arrow").removeClass("down");
			oldTarget.find(".tab").removeClass("black");
			
			body_height = target.children(".subitems").css("maxHeight");
			active_height = target.parent().find(".active").children(".subitems").css("maxHeight");
			
			body_height = parseInt(body_height.toString().replace(/\D/gi, ""));
			if(typeof active_height != "undefined")
				active_height = parseInt(active_height.toString().replace(/\D/gi, ""));
			else
				active_height = 1;
			
			target.children(".subitems").animate({height:body_height}, {
				step: function(now, fx) {
					new_height = active_height * (1-(now/body_height));
					if(new_height < 1)
						new_height = 1;
					oldTarget.children(".subitems").height(new_height);											
				},
				duration: 300,
				complete: function() {
					oldTarget.removeClass("active");
					target.addClass("active");
				}
			});
		}
	},
	timeout: 100,
	out: function() {}
});
*/
