$(document).ready(function() {

var LOAD_DURATION = 250; // ms
var MENU_CLOSE_TIMEOUT = 1000; //ms
var B2B_UNEXPANDED_LINK_HTML = B2B_MENU_TITLE + ' <img src="/cheap/themes/travelfusion/images/icons/icn_right_arrow.png" alt="Expand list" title="' + B2B_MENU_LONG_TITLE + '">';

// Call this function to close the B2B menu
var CLOSE_MENU = function() {
	$("#b2b_options").slideUp(LOAD_DURATION);
	$("#b2b_link").html(B2B_UNEXPANDED_LINK_HTML);
};

// Clicking the B2B link should expand the menu and clicking it again should unexpand it
$("#b2b_link").click(function () {
	if ($("#b2b_options").is(':visible')) {
		$("#b2b_options").slideUp(LOAD_DURATION);
		$("#b2b_link").html(B2B_UNEXPANDED_LINK_HTML);
	} else {
		$("#b2b_options").slideDown(LOAD_DURATION);
		$("#b2b_link").html(B2B_MENU_TITLE + ' <img src="/cheap/themes/travelfusion/images/icons/icn_down_arrow.png" alt="Unexpand list" title="' + B2B_MENU_LONG_TITLE + '">');
	}
	return false;
});

$("#b2b_menu").hover(
	// Clear the time out every time the mouse hovers over the menu
	function() {
    	clearTimeout($(this).data('timeout'));
    },
    // When the mouse stops hovering over the menu, start a timeout which will close it
    function() {
    	var timeout = setTimeout(function() {
    	$("#b2b_options").slideUp(LOAD_DURATION);
    	$("#b2b_link").html(B2B_UNEXPANDED_LINK_HTML);
    }, MENU_CLOSE_TIMEOUT);
    $(this).data('timeout', timeout);
});

// Hover behaviour for B2B drop-down menu options
$("#b2b_options li").hover(
	// Highlight the menu option
	function() {
		$(this).addClass('b2bOptionHover');
	},
	// Stop highlighting the menu option
	function() {
		$(this).removeClass('b2bOptionHover');
	}
);

// Click behaviour for B2B drop-down menu options
$("#b2b_options li").click(function(e) {
	// Send the user to the location specified in the "link" attribute
	location.href = $(this).attr("link");
});

});
