function mb_clear_menu(){
	$('.topmenu_entry').css(
					{
						backgroundColor: 	'#ffffff',
						color: '#57CBF5'
					});
	$('.topmenu_entry a').css(
			{
				color: '#57CBF5'
			});
			
	$('.topmenu_entry').each(function() {  
			if($(this).hasClass("topmenu_bg_image_hover")){
				$(this).removeClass("topmenu_bg_image_hover");
				$(this).addClass("topmenu_bg_image");
			}
		});
}

function check_submenus(){
	var invisible = true;
	$('.mb_submenu').each(function() {  
			var display = $(this).css("display");
			if(display != 'none'){
				invisible = false;
			}
		});
		
	return invisible;
}

$(document).ready(function(){
	
	var timer = null;
	var mb_out = true;
	
	$('.topmenu_entry').mouseover(function(){
		mb_out = false;
		
		var id = $(this).attr('id');
		
		mb_clear_menu();
		
		$('.mb_submenu').hide();
		if(timer != null) clearTimeout(timer);	
		
		$(this).css(
			{
				backgroundColor: 	'#57CBF5',
				color: '#ffffff'
			});
		
		
		$('#' + id + ' a').css(
			{
				color: '#ffffff'
			});
			
		if($(this).hasClass("topmenu_bg_image")){
			$(this).removeClass("topmenu_bg_image");
			$(this).addClass("topmenu_bg_image_hover");
		}
			
		if($('#' + id + '_sub').attr('id') == id + '_sub'){
			$('#' + id + '_sub').show();
		}
	});
	
	
	$('.topmenu_entry').mouseout(function(){
		
		if(mb_out == true || check_submenus() == true) {
			mb_clear_menu();
		}
		mb_out = true;
			
		var id = $(this).attr('id');
		if($('#' + id + '_sub').attr('id') == id + '_sub'){
			timer = setTimeout(function(){
				mb_clear_menu();
				$('#' + id + '_sub').hide();
			},500);
		}
	});
	
	
	$('.mb_submenu').mouseover(function(){
		mb_out = false;
		if(timer != null) clearTimeout(timer);	
	
	});
	
	
	$('.mb_submenu').mouseout(function(){
		mb_out = true;
		var id = $(this).attr('id');
			
		timer = setTimeout(function(){
			mb_clear_menu();
			$('#'+id).hide();
		},500);
	});
	
	
	$('.mb_submenu td').mouseover(function(){
		mb_out = false;
		var id = $(this).attr('id');
		
		$(this).css(
			{
				backgroundColor: 	'#57CBF5',
				color: '#ffffff'
			});
		
		$('#' + id + ' a').css(
			{
				color: '#ffffff'
			});
			
	});
	
	
	$('.mb_submenu td').mouseout(function(){
		mb_out = true;
		var id = $(this).attr('id');
		
		$(this).css(
			{
				backgroundColor: 	'#ffffff',
				color: '#57CBF5'
			});
			
		$('#' + id + ' a').css(
			{
				color: '#57CBF5'
			});
	});
	
	$('.topmenu_entry').click(function(){
		var id = $(this).attr('id');
		var url = $('#' + id + ' a').attr('href');
		document.location = url;	 		
	});
	
	$('.mb_submenu td').click(function(){
		var id = $(this).attr('id');
		var url = $('#' + id + ' a').attr('href');
		document.location = url;	 		
	});
	
});
