﻿$(document).ready(function() {

    // Allow menus to show over the top of form elements in IE6
    // by placing an equally sized iframe behind the sub-menu list
    if($.browser.msie && $.browser.version == '6.0') {
        $("#nav > li").each( function() {
            var list = $(this);
            
            list.append('<iframe/>').find('iframe').css({
                position: 'absolute',
                top: list.css('top'),
                left: 0,
                height: 0,
                width: list.css('width'),
                padding: 0,
                margin: 0,
                border: 'none',
                'z-index': 90
            });
        });
    }
    
    // Correct IE6/7 z-index bug by dynamically applying a high z-index on menu hover action
    // http://richa.avasthi.name/blogs/tepumpkin/2008/01/11/ie7-lessons-learned/    
    if($.browser.msie && ($.browser.version == '6.0' || $.browser.version == '7.0') ) {
        $("#nav > li > ul").parents().each(function() {
            var parentElement = $(this);
            var parentElementPositionValue = parentElement.css("position");

            if(parentElementPositionValue == "relative"
               || parentElementPositionValue == "absolute"
               || parentElementPositionValue == "fixed")
            {
                parentElement.hover(
                    function() {
                        $(this).addClass("on-top");
                    },
                    function() {
                        $(this).removeClass("on-top");
                    }
                );
            }
        });
    }
    
    /* Navigation menu animations */
    $('#nav > li').hover(
        function() {
            var lineHeight = 16;
            var padding = 10;
            var items = $(this).find('ul li');
            var dropDownHeight = (items.size() * (lineHeight + padding)) + (items.filter('.line-wrap').size() * lineHeight) + 2;
 
            $(this).addClass('selected').find('ul, iframe').stop().animate({ height: dropDownHeight }, 'fast', 'linear');
        },
        function() {
            $(this).find('ul, iframe').stop().animate(
                { height: 0 }, 
                'fast', 
                'linear', 
                function(){
                    $(this).parent('.selected').removeClass('selected');
                }
            );
        }
    );
    
});
