$(document).ready(function() {
    buildMenus();
    $('a.topmenu').mouseover(openMyDropdown);
    $('body').click(closeAllDropdowns);
    $('.headerdropdown > a').mouseover(closeAllDroprights);
    $('a.droprighttrigger').mouseover(openMyDropright);
});

function buildMenus() {
    // Sets the horizontal position of each dropdown based on its parent menu's position
    $('.headerdropdown').each(function() {
        var headermenuid = this.id.replace('dropdown_', 'headermenu_');
        this.style.left = $('#' + headermenuid)[0].offsetLeft + 'px';
    });

    // Add a dropright indicator where appropriate
    var arrow = "<span style='float:right'>&gt;</span>";
    $('a.droprighttrigger').prepend(arrow);
}

function openMyDropdown(e) {
    // Find the corresponding dropdown div for this menu and display it
    closeAllDropdowns();
    var id = this.id.replace('headermenu_', 'dropdown_');
    $('#' + id).css('display', 'block');
}

function closeAllDropdowns() {
    closeAllDroprights();
    $('.headerdropdown').css('display', 'none');
}

function closeAllDroprights() {
    $('.headerdropright').css('display', 'none');
}

function openMyDropright(event) {
    var dropright = $(this).next('div.headerdropright');
    dropright.css('display', 'block');
    dropright.css('top', this.offsetTop + "px");
    dropright.css('left', "200px");
}


