var timeout    = 500;
var closetimer = 0;
var menuitem = 0;
var menubg = 0;

function nav_top_open(){
	nav_top_canceltimer();
	nav_top_close();
	menuitem = $(this).find('ul').css('visibility', 'visible');
	menubg = $(this).find('.bg').css('visibility', 'visible');
}

function nav_top_close() {
	if (menuitem) menuitem.css('visibility', 'hidden');
	if (menubg) menubg.css('visibility', 'hidden');
}

function nav_top_timer() {
	closetimer = window.setTimeout(nav_top_close, timeout);
}

function nav_top_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(document).ready(function() {
	$('#nav_top .bg').each(function() {
		$(this).width($(this).next('ul').width()).height($(this).next('ul').height()).css('margin-top', $(this).prev('a').height());
	});
	$('#nav_top > li').bind('mouseover', nav_top_open);
	$('#nav_top > li').bind('mouseout',  nav_top_timer)
    $('#rotator').cycle({
		fx: 'fade',
		speed: 5000,
		timeout: 2000
	});
});

document.onclick = nav_top_close;

