var close_dropdown_timer;
var open_dropdown_timer;
var $hovered_dropdown=0;

function close_dropdowns(){
	if ($hovered_dropdown!=0){
		var $dropdowns = $("#nav-buttons li:not(#"+$hovered_dropdown.parent().attr('id')+") ul.open_dropdown");
	} else {
		var $dropdowns = $("#nav-buttons li ul.open_dropdown");
	}
	$dropdowns.slideUp(90).removeClass('open_dropdown');
}
function open_dropdown(el){
	$hovered_dropdown=el;
	clearTimeout(close_dropdown_timer);
	close_dropdowns();
	$hovered_dropdown.addClass('open_dropdown').slideDown('fast');
}
function over_dropdown(){
	$(this).children('a').addClass('hovered');
	$current = $(this).find('ul');
	if ($current) {
		if (!$current.hasClass('open_dropdown')){
			open_dropdown_timer = setTimeout(function(){open_dropdown($current)}, 200);
		} else {
			clearTimeout(close_dropdown_timer);
		}
	}
}
function off_dropdown(){
	$(this).children('a').removeClass('hovered');
	$current = $(this).find('ul');
	if ($current) {
		if (!$current.hasClass('open_dropdown')){
			clearTimeout(open_dropdown_timer);
		} else {
			$hovered_dropdown=0;
			close_dropdown_timer = setTimeout(close_dropdowns, 300);
		}
	}
}
function click_dropdown(event){
	$current = $(this).find('ul');
	if ($current){
		clearTimeout(open_dropdown_timer);
		open_dropdown($current);
	}
	return false;
}

$(document).ready(function(){
	var $dropdowns = $("#nav-buttons li:has(ul)");
	$dropdowns.hover(over_dropdown, off_dropdown);
	$dropdowns.click(click_dropdown);
	$dropdowns.find('ul a').click(function(event){event.stopPropagation()});
});
