/*
MegaMenu.js

The all-singing, all-dancing menu that's dangerously close to becoming self aware... I'm pretty sure this is how Terminator 2 started.
*/


jQuery(document).ready(function () {

    /* populate the info section with data from a hidden div */
    jQuery('#MegaMenu .level_two li').hover(showMenuInfo, hideMenuInfo);

    /* show menu */
    function showMenuInfo() {
        var info = jQuery(this).parent().siblings('.info');
        jQuery('.appended', info).remove(); //remove any appended content
        jQuery('li', info).hide();  //hide the section description

        //get the id of this element so that we can display the correct description
        var myId = jQuery(this).find('a').attr('id').substr(8);

        if (jQuery('#menuInfo' + myId).length)
            jQuery(this).parent().siblings('.info').append('<li class="appended">' + jQuery('#menuInfo' + myId).html() + '</li>');

        if (jQuery.browser.version == "7.0") {
            //in ie7 sometimes the info box doesn't bottom align with the menu items
            var level_2 = jQuery(this).parent().parent();
            var diff = info.outerHeight() - level_2.outerHeight();
            if (diff > 0) {
                var new_padding = parseInt(level_2.css('padding-bottom')) + diff; //old padding
                level_2.css('padding-bottom', new_padding); //add extra padding to bottom align 
            }
        }
    };

    /* hide menu */
    function hideMenuInfo() {
        //remove the appended content and show the section description
        var info = jQuery(this).parent().siblings('.info');
        jQuery('.appended', info).remove();
        jQuery('li', info).show();
    };



    /* Right attached menus */

    jQuery('#MegaMenu li.right.attached').each(function () {
        /* the sub-menu (span.level_two) has to be positioned absolutely - so to align the right side edge of the top li element (#MegaMenu ul li) we just take the left pos of the top li relative to the top ul and add the width - easy! :D */

        var level_two = jQuery('.level_two', this);
        level_two.css('width', level_two.width()); //fix the width

        var level_two_xPos = jQuery(this).position().left + jQuery(this).width() - level_two.outerWidth();
        level_two.css('display', 'block').css('left', level_two_xPos).css('display', 'none'); //'move' level_two
        jQuery(this).hover(
			function () { jQuery('.level_two', this).show(); },
			function () { jQuery('.level_two', this).hide(); }
		);

    });


    //internet explorer only code
    if (jQuery.browser.msie) {

        if (jQuery.browser.version == "6.0") {
            //alert('msie 6 only code');

            //insert a div/clear for info menus
            jQuery('#MegaMenu ul li.info_menu .level_two ul.info').each(function () {
                jQuery(this).after('<div class="clear" />');
            });

            jQuery('#MegaMenu li.attached').each(function () {
                //not for right attached
                if (jQuery(this).hasClass('right'))
                    return;

                var parentLeftVal = jQuery(this).position().left;
                jQuery('.level_two', this).css('left', parentLeftVal);

            });

        } //msie6

        if (jQuery.browser.version == "7.0") {
            //alert('msie 7 only code:');

            jQuery('.attached .level_two').each(function () {
                //if the closest attached menu doesn't have class right,
                if (!jQuery(this).closest('.attached').hasClass('right'))
                //set the left pos of this to the left pos of the closest attached
                    jQuery(this).css('left', jQuery(this).closest('.attached').position().left);
            }
			);

        } //msie7
    } //end msie


    // non-ie6 code:
    if (jQuery.browser.version != '6.0' && jQuery.browser.msie != 'true') {
        //if the browser isn't msie6, we want to add the last class so that there's a right border
        jQuery('#MegaMenu > ul > li > a:last').addClass('last');
    }

});

