var gOriginalSize = 0;
var gPrevSize = 0;
var gMenu = 0;
var gAnimating = false;

function menu_toggle_submenu(item)
{
	if(gAnimating)
		return;
		
	// Grab the menu that's a child of item
	var menu = item.getElementsByTagName("ul")[0];

	// Store the default values
	gMenu = menu;
	
	// Check if we have to expand or contract
	if(menu.style.display == "block")
		menu_start_shrink_animation();
	else menu_start_expand_animation();
}

function menu_start_expand_animation()
{
	// Make sure the menu is visible
	gMenu.style.visibility = "hidden";
	gMenu.style.position = "absolute";
	gMenu.style.display = "block";
	
	// Grab the full height first
	gOriginalSize = gMenu.clientHeight;

	// Start with 0px height
	gMenu.style.height = "0px";
	gPrevSize = 0;
		
	// Reset position and visibility
	gMenu.style.position = "";
	gMenu.style.visibility = "";
	
	// Toggle animating flag
	gAnimating = true;
	
	// Start the animation
	menu_tick_expand_animation();
}

function menu_tick_expand_animation()
{
	// Check if we are done
	if(gPrevSize >= gOriginalSize) {
		gAnimating = false;
		return;
	}
		
	// Increment the prev size and set the style
	gPrevSize += 6;
	gMenu.style.height = (gPrevSize) + "px";
	
	// Keep ticking
	setTimeout(menu_tick_expand_animation, 5); 
}




function menu_start_shrink_animation()
{
	// Start backwards
	gPrevSize = gMenu.clientHeight;
	
	// Toggle animating flag
	gAnimating = true;
	
	// Start the animation
	menu_tick_shrink_animation();
}

function menu_tick_shrink_animation()
{
	// Check if we are done
	if(!gPrevSize)
	{
		gMenu.style.height = "";
		gMenu.style.display = "none";
		
		gAnimating = false;
		
		return;
	}	
	
	// Increment the prev size and set the style
	gPrevSize -= 6;
	gMenu.style.height = (gPrevSize) + "px";
	
	// Keep ticking
	setTimeout(menu_tick_shrink_animation, 5); 
}

