var MENU_TIMEOUT = 1;
hide_menu = 0;

$(function(){
    /* IE needs the parent div to have a CSS height before it will allow sub-divs to use
       height: 100%. Set the CSS height to the actual height to get around this.
    */
    $('#content').css('height', $('#content').outerHeight());

    $('#menu TD').mouseover(toggle_menu);
    $('#menu TD, .submenu LI').click(click_sub_link);
    $('#menu A').bind('click', function(e){e.stopPropagation();return true});

    // This is added to stop propagation to the body click handler that hides the submenu
    $('.submenu INPUT, .submenu A').bind('click', function(e){e.stopPropagation();return true});

    $('.submenu LI, #menu TD').bind('mouseover', menu_item_mouseover);
    $('.submenu LI, #menu TD').bind('mouseout', menu_item_mouseout);

    $('.submenu LI[banner], #menu TD[banner]').each(function(){
        $('<img>').attr('src', $(this).attr('banner'));
        });

    var errors = $('#errors');
    if (errors.length){
        errors.find('DIV').each(function(){
            var element = $(this).attr('element');
            var elements = element.split(/,/);
            for (var i=0;i<elements.length;i++){
                var element = elements[i];
                if (!element) continue;

                var input = $('INPUT[name='+element+']');
                input.addClass('error');
                if (elements.length == 1){
                    var tr    = input.parents('TR');
                    var cols  = tr.find('TD').length || 1;
                    tr.find('TD[colspan]').each(function(){cols += $(this).attr('colspan');});
                    tr.before('<tr class="errors"><td colspan="'+cols+'">'+$(this).html()+'</td></tr>');
                    $(this).remove();
                    }
                }
            });
        if (!errors.find('DIV').length){
            errors.remove();
            }
        }
    });

function toggle_menu(e){

    var $was_active = $('#menu .active');
    var $new_active = $(this).filter('TD');

    // Handle any previously active menu
    if ($new_active.length &&
        $was_active.length &&
        $new_active.get(0) == $was_active.get(0)){

        return;
        }

    $('BODY').unbind('click', toggle_menu);

    $was_active.removeClass('active');

    $('DIV.submenu INPUT').css('visibility','hidden');
    $('DIV.submenu').css('display','none');

    // Handle situations where the menu goes away (done above)
    if (!$new_active.length){
        return;
        }

    $('BODY').unbind('click', toggle_menu);

    $new_active.addClass('active');
    var $new_submenu = $new_active.find('.submenu');

    if ($new_submenu.length){
        if (!$new_submenu.get(0).target_height){
            $new_submenu.get(0).target_height = $new_submenu.outerHeight();
            }

        var target_height = $new_submenu.get(0).target_height;
        $new_submenu.css('display', 'block');
        $new_submenu.css('height', 0);
        $new_submenu.animate({
            height:    target_height
            },
            400,
            function (){
                var $active = $('#menu .active')
                if (!$active.length || $new_active.get(0) != $active.get(0)){
                    $new_submenu.css('display', 'none');
                    }
                }
            );
        $new_submenu.find('INPUT').css('visibility','visible');

        var focusers = $new_submenu.find('INPUT[menu_focus]');
        if (focusers.length){
            focusers.get(0).focus();
            }

        $('BODY').bind('click', toggle_menu);
        }
    else {
        $('#menu TD').bind('mouseover', toggle_menu);
        }

    e.stopPropagation();

    return false;
    }

function menu_item_mouseover(){
    var $this = $(this);

    $this.addClass('hover');

    var banner = $this.attr('banner');

    var image = $('DIV.image IMG.background');

    if (!image.attr('orig_src')){
        image.attr('orig_src', image.attr('src'));
        }

    if (!banner){
        banner = image.attr('orig_src');
        }

    $('DIV.image IMG.background').attr('src', banner);

    return true;
    }

function menu_item_mouseout(){
    $(this).removeClass('hover');
    }

function click_sub_link(){
    var $links = $(this).find('A');
    if ($links.length == 1 && $links.attr('href')){
        window.location = $links.attr('href');
        }
    }

