$(document).ready(autoSizePage);
$(document).ready(setupMenu);
$(window).resize(function() { autoSizePage(); setupMenu(); });

function autoSizePage()
{
    // our gradient needs a bit of space to breathe!
    if ($('#sea_bk').height() < 600)
        $('#sea_bk').height(600);
    $('#logo').css('marginLeft', (($('#land_bk').width() / 2) - 550) + 'px');
}

function setupMenu()
{
    $('#main_menu a').each(positionSubMenu);
    $('#main_menu a').mouseenter(peekAtSubMenu).mouseleave(hideSubMenu);
    $('.sub_menu').mouseenter(peekAtSubMenu).mouseleave(hideSubMenu);
}

function subMenuMatch(el) {
    if (el != null) {
        if (el.id != null && el.id.match(/^(sub|mnu)_/))
            return el.id;

        if (el.parentNode != null)
            return subMenuMatch(el.parentNode);
        else
            return null;
    }
    else
        return null;
}

function getSubMenuId(menuItem) {
    var id = '#';

    //for (var x in menuItem) {
       // $('.contentText').html($('.contentText').html() + '<br />' + x + ' = ' + menuItem[x]);
    //}

    var originalId = subMenuMatch(menuItem.originalTarget);
    var srcId = subMenuMatch(menuItem.srcElement);

    if (menuItem.originalTarget != null && originalId != null)
        id += originalId;
    else if (menuItem.srcElement != null && srcId != null)
        id += srcId;
    else if (menuItem.id && menuItem.id.match(/^sub_/))
        id += menuItem.id;
    else if (menuItem.id)
        id += 'sub_' + menuItem.id;
    else if (menuItem.target && menuItem.target.id.match(/^sub_/))
        id += menuItem.target.id;
    else if (menuItem.target)
        id += 'sub_' + menuItem.target.id;

    id = id.replace(/^#mnu_/, '#sub_mnu_').replace(/_icon$/, '');
    
    if (id == '#' || id == '#sub_')
        id = null;

    return id;       
}

function positionSubMenu(index, menuItem)
{
    var subMenuItem = $(getSubMenuId(menuItem));
    $(subMenuItem).css('left', ($(menuItem).offset().left + $(menuItem).width() - 460) + 'px');
    $(subMenuItem).css('top', ($(menuItem).offset().top + $(menuItem).height() + 3) + 'px');
}

function peekAtSubMenu(event) {
   
   //$('.contentText').html($('.contentText').html() + '<br />peekAtSubMenu: ' + getSubMenuId(event));

    if (getSubMenuId(event) == null)
        return;

    if (fadeAwayTimer != null) {
        clearInterval(fadeAwayTimer);
    }

    if (!$(getSubMenuId(event)).is(':visible')) {
        $('.sub_menu:visible').hide();
        $(getSubMenuId(event)).fadeIn('fast');
    }

    $(getSubMenuId(event).replace(/^#sub_/, '#')).fadeTo('fast', 1.00);
    $(getSubMenuId(event).replace(/^#sub_/, '#') + '_icon').fadeTo('fast', 1.00);
    $('#main_menu a:not(#main_menu a[id^=' + getSubMenuId(event).replace(/^#sub_/, '') + '])').fadeTo('fast', 0.33);
}

var fadeAwayTimer = null;

function hideSubMenu(event) {
    //$('.contentText').html($('.contentText').html() + '<br />hideSubMenu: ' + getSubMenuId(event));
    if (getSubMenuId(event) == null)
        return;

    fadeAwayTimer = setTimeout(
        function() {
            //$('.contentText').html($('.contentText').html() + '<br />fadeAway: ' + getSubMenuId(event));
            fadeAwayTimer = null;
            $(getSubMenuId(event)).fadeOut('fast');
            $('#main_menu a').fadeTo('fast', 1.00);
        }, 400);
}