var timeout = 0;
var closetimer = 0;

function closeMenu(sub_menu_id) {
        $(sub_menu_id).stop(true, false).animate({'opacity' : '0'},
		100, 
		function() {
			$(sub_menu_id).css({'display' : 'none'});
		}
	);
}

function startTimer(sub_menu_id) {
        closetimer = window.setTimeout("closeMenu('" + sub_menu_id + "')", timeout);
}

function stopTimer() {
        if(closetimer) {
                window.clearTimeout(closetimer);
                closetimer = null;
        }
}

var button_pos = {
	"m_about" : "0 0",
	"m_gynecology" : "-108px 0px",
	"m_assisted" : "-216px 0",
	"m_pregnancy" : "-324px 0",
	"m_surgery" : "-431px 0",
	"m_support" : "-540px 0"
};

var button_hover_pos = {
	"m_about" : "0 -49px",
	"m_gynecology" : "-108px -49px",
	"m_assisted" : "-216px -49px",
	"m_pregnancy" : "-324px -49px",
	"m_surgery" : "-431px -49px",
	"m_support" : "-540px -49px"
};

function set_active_btn(id) {
	$("#" + id).addClass("active");
}

// Ready

$(document).ready(function($) {
	$.each($(".menu_top"), function(key, value) {
		var obj_id = $(value).attr("id");
		$("#" + obj_id + " > a").css({"background-position" : button_pos[obj_id]});
	});

        $(".sub_menu")
                .css({"opacity": "0", 
			"display" : "none"})
                .mouseenter(function() {
			var obj_id = $(this).attr("id");
			var menu_obj_id = obj_id.substr(4);

                        stopTimer();

			$("#" + menu_obj_id + " > a").css({"background-position" : button_hover_pos[menu_obj_id]});
                })
                .mouseleave(function() {
			var obj_id = $(this).attr("id");
			var menu_obj_id = obj_id.substr(4);

                        if($('#' + obj_id).css("opacity") == 1)
                                startTimer('#' + obj_id);

			$("#" + menu_obj_id + " > a").css({"background-position" : button_pos[menu_obj_id]});
                });

        $('.menu_top')
                .mouseenter(function() {
			var obj_id = $(this).attr("id");
			
                        if($('#sub_' + obj_id).css("opacity") == 1) 
                        	stopTimer();
			else {
                        	$('#sub_' + obj_id).stop(true, false).animate({'opacity': '1'}, 
					100, 
					function() {
						$('#sub_' + obj_id).css({'display' : 'block'});
					}
				);
			}

			$("#" + obj_id + " > a").css({"background-position" : button_hover_pos[obj_id]});
                })
                .mouseleave(function() {
			var obj_id = $(this).attr("id");
                        startTimer('#sub_' + obj_id);

			$("#" + obj_id + " > a").css({"background-position" : button_pos[obj_id]});
                });
});

