function setupTopMenuItemVisibility() {

	jQuery("li.menu_item, li.submenu_item").mouseenter(
			function() {
				jQuery('>.submenu', this).show();

			}).mouseleave(function() {
		jQuery('>ul.submenu', this).hide();
	})

	jQuery('li.menu_item').mouseover(
			function() {
				// add classes only to direct children of menu item, not all siblings
				jQuery('>span', this).addClass('hover');
			}).mouseout(function() {
		// remove classes only to direct children of menu item, not all siblings
		jQuery('>span', this).removeClass('hover');
	});

	jQuery('li.submenu_item').each(function() {
		
		var link = jQuery(this).find(".submenu_item:first a")

	});
}

function setupLeftMenuVisibility() {
	// add mouse events to menu items
	jQuery('div.menu_second_level_item').mouseover(
			function() {
				if (jQuery(this).hasClass('selected'))
					return false;
				// add classes only to direct children of menu item, not all siblings
				jQuery('div[name="first"]', this).removeClass('menu_second_level_item_first');
				jQuery('div[name="second"]', this).removeClass('menu_second_level_item_second');
				jQuery('div.[name="middle"]', this).removeClass('menu_second_level_item_middle');

				jQuery('div[name="first"]', this).addClass('menu_second_level_item_first_hover');
				jQuery('div[name="second"]', this).addClass('menu_second_level_item_second_hover');
				jQuery('div.[name="middle"]', this).addClass('menu_second_level_item_middle_hover');
			}).mouseout(function() {
		if (jQuery(this).hasClass('selected'))
			return false;
		// remove classes only to direct children of menu item, not all siblings
		jQuery('div[name="first"]', this).removeClass('menu_second_level_item_first_hover');
		jQuery('div[name="second"]', this).removeClass('menu_second_level_item_second_hover');
		jQuery('div.[name="middle"]', this).removeClass('menu_second_level_item_middle_hover');

		jQuery('div[name="first"]', this).addClass('menu_second_level_item_first');
		jQuery('div[name="second"]', this).addClass('menu_second_level_item_second');
		jQuery('div.[name="middle"]', this).addClass('menu_second_level_item_middle');
	});

	jQuery('div[name="menu_third_level_item"]').mouseover(
			function() {
				if (jQuery(this).hasClass('selected'))
					return false;
				// add classes only to direct children of menu item, not all siblings
				jQuery('a[name="menu_third_level_item_middle"]', this).removeClass('menu_third_level_item_middle');
				jQuery('a[name="menu_third_level_item_middle"]', this).addClass('menu_third_level_item_middle_hover');
			}).mouseout(function() {
		if (jQuery(this).hasClass('selected'))
			return false;
		// remove classes only to direct children of menu item, not all siblings
		jQuery('a[name="menu_third_level_item_middle"]', this).removeClass('menu_third_level_item_middle_hover');
		jQuery('a[name="menu_third_level_item_middle"]', this).addClass('menu_third_level_item_middle');
	});
}

function menu_button_link() {
	jQuery(".menu_second_level_item").click(function () {
		var newurl = jQuery('a', this).attr('href');
		document.location.href = newurl;
	});
}

/**
 *  Recalculates the widths of the top menu items in order to stretch them nicely to fit the page width.
 */
function recalculate_top_menu_items_widths() {

	var list_items_width = 0;

	// Calculate total width of list items
	var list_items = jQuery('ul.top.menu li.menu_item:not(.admin_item)');

	var menu_bar_width = jQuery('ul.top.menu').width() - (2 * (list_items.length - 1));
	list_items.each(function() {

        var textWidth = jQuery('span.menu_item a', this ).textWidth();
		list_items_width += textWidth;
        jQuery(this).width(textWidth);
        jQuery(this).css({minWidth: "" + textWidth + "px"} );
	});

	var width_supplement = Math.round(((menu_bar_width - list_items_width) / list_items.length) );

	var isFirst = true;
	list_items.each(function() {

        var textWidth = jQuery(this).css("min-width");
        jQuery(this).css({minWidth: "" + (width_supplement + parseInt(textWidth)) + "px"} );
	});
}

function autoMenuItemPadding() {

	var list_items_width = 0;

	// Calculate total width of list items
	var list_items = jQuery('ul.top.menu li.menu_item:not(.admin_item)');

	var menu_bar_width = jQuery('ul.top.menu').width() - (2 * (list_items.length - 1));
	list_items.each(function() {
		list_items_width += jQuery(this).width();
	});

	var required_padding = Math.round(((menu_bar_width - list_items_width) / list_items.length) / 2);

	// To account for rounding errors, the error is going to be forced into the first tab.
	var rounding_error = (required_padding * 2 * list_items.length) + list_items_width - menu_bar_width;

	var isFirst = true;
	list_items.each(function() {
		var item_container = jQuery(this);
		var margin = required_padding;
		
		if (isFirst) {
			margin = margin - rounding_error;
			item_container.css('margin-left', margin + 'px');
			item_container.css('margin-right', margin + 'px');
			isFirst = false;
		}
		else {
			item_container.css('margin-left', margin + 'px');
			item_container.css('margin-right', margin + 'px');
		}
	});
}

jQuery(function() {
	setupTopMenuItemVisibility();
	setupLeftMenuVisibility();

	menu_button_link();

	if (jQuery("ul.top.menu").hasClass("auto_padding"))
		autoMenuItemPadding();
});
