Cufon.replace('h2');

$(function()
{
	$('#menu li').hover(function()
	{
		var menu_id = $(this).attr('id');
		if(menu_id)
			menu_over(menu_id);
		else
			$(this).addClass('current');
	},function()
	{
		var menu_id = $(this).attr('id');
		if(menu_id)
			setTimeout("menu_out('" + menu_id + "')", 20);	// Delay to make sure we don't hide menu before mouse completely out of both parts of the menu
		else
			$(this).removeClass('current');
	});

	$('#dropdown_menus .dropdown').hover(function()
	{
		var menu_id = $(this).attr('id').replace('_dropdown', '');
		menu_over(menu_id);
	},function()
	{
		var menu_id = $(this).attr('id').replace('_dropdown', '');
		setTimeout("menu_out('" + menu_id + "')", 20);
	});
});

var menu_over_counter = {};
function menu_over(menu_id)
{
	if(menu_over_counter[menu_id])
		++menu_over_counter[menu_id];
	else
	{
		menu_over_counter[menu_id] = 1;

		var primary_menu = $('#' + menu_id);
		var sub_menu = $('#' + menu_id + '_dropdown');

		primary_menu.addClass('current');

		var left = parseInt(primary_menu.offset().left) - 19;
		sub_menu.css('left',left + 'px').fadeIn(250);
	}
}
function menu_out(menu_id)
{
	// Do it before if() because we need to delay this action a little bit to make sure that 
	// The mouse is out of both parts of the menu.
	var primary_menu = $('#' + menu_id);
	var sub_menu = $('#' + menu_id + '_dropdown');
	if(--menu_over_counter[menu_id] == 0)
	{
		sub_menu.fadeOut(250);
		primary_menu.removeClass('current');
	}
}


