$(function () {
	var menu_timeout = null;
	var current_menu = null;
	var y_offset = 32;
    
    var first_menu_img = $("li.parentMenu:first .rollover img", $("div.navMain"))
        
	$("li.parentMenu", $("div.navMain")).hover(function () {
   
		// This is the mouseover event
		window.clearTimeout(menu_timeout);
		if (current_menu) current_menu.hide();

		var nav_item = $(".rollover img", this);
        
		var pos = nav_item.offset();

		var menu_name = nav_item.attr("name").match(/([A-Za-z0-9]*)$/)[0];

		current_menu = $("#menu_"+menu_name);

        var top = pos["top"]+y_offset;
        //var left = pos["left"] + ((nav_x_offsets[first_menu_name]) ? nav_x_offsets[first_menu_name] : 0) + ((!$.browser.msie) ? 1 : 0);
        var left = pos["left"] + ((nav_x_offsets[current_menu]) ? nav_x_offsets[current_menu] : 0) + ((!$.browser.msie) ? 1 : 0);
        
		var viewport_width = $(document).width()
        var nav_right_edge = first_menu_img.offset()["left"] + 176 + 170 + 219 + 121 +4; //left edge of the first nav item + width of the 4 nav items
        
		//if (current_menu.outerWidth() + left > viewport_width)
        if (current_menu.outerWidth() + left > nav_right_edge)
			left -= current_menu.outerWidth() + left - nav_right_edge + (($.browser.msie) ? -21 : 5);

		current_menu.css("top", top).css("left", left);

		// Hide select boxes when the menu is shown for IE <= 6
		if($.browser.msie && /MSIE [4-6]/.test(navigator.appVersion)){
			$('select').css('visibility','hidden');
		}

		current_menu.show();
	
	}, function () {
		// This is the mouseout event
		menu_timeout = window.setTimeout(function() {
			current_menu.hide();

			// Hide select boxes when the menu is shown for IE <= 6
			if($.browser.msie && /MSIE [4-6]/.test(navigator.appVersion)){
				$('select').css('visibility','visible');
			}
		}, 100);
	
	});
	
	$("div.subMenu").hover(function() {
		// This is the mouseover event
		window.clearTimeout(menu_timeout);

	}, function () {
		// This is the mouseout event
		menu_timeout = window.setTimeout(function() {
			current_menu.hide();

			// Hide select boxes when the menu is shown for IE <= 6
			if($.browser.msie && /MSIE [4-6]/.test(navigator.appVersion)){
				$('select').css('visibility','visible');
			}
		}, 100);
	
	});
    
    $(".nav_col1").each(function(){
		var col2_ul = $("#"+this.id.replace("nav_col1_", "nav_col2_")+" > ul");
		var col1_ul_lis = $("#"+this.id+">ul>li");
		var transfer_lis_from = Math.ceil(col1_ul_lis.length/2.0);
//alert( "for "+this.id+" - transfer_lis_from: "+transfer_lis_from+" - col1_ul_lis.length: "+col1_ul_lis.length );
		var i;

		for (i=transfer_lis_from; i<col1_ul_lis.length; i++)
		{
			$(col1_ul_lis[i]).appendTo(col2_ul);
		}
		var menu_height = $("#"+this.id.replace("nav_col1_", "menu_")).height();
		$(this).css('height', (menu_height-20)+"px");
	});
	
	$(".subMenu ul").each(function(){
		if (!$(this).children("li").length) $(this).remove();
	});
	
	
});

