$(document).ready(function() {
        
    //On Hover Over
    function megaHoverOver(that){
        stopSwitch(); //Stop the rotation           
        left10 = 1; // padding of div#container
        prev = that.prev();        
        while(typeof(prev.get(0)) != "undefined") {
            if(prev.get(0).tagName != "DIV")
                left10 += prev.width();
            prev = prev.prev();
        }      
        that.find(".sub10").css({'left' : left10, 'width': that.width() }).stop().show();       
        that.find(".sub20").stop().show();
    }
    
    //On Hover Out
    function megaHoverOut(that){
        that.find(".sub10").stop().hide();    
        that.find(".sub20").stop().hide();
        /*$(this).find(".sub20").stop().fadeTo(100, 0, function() { //Fade to 0 opactiy
          $(this).hide();  //after fading, hide it
        });*/
        rotateSwitch();
    }
    
    $("ul#topnav li .sub20").css({'display':'none'}); //Fade sub nav to 0 opacity on default
    $("ul#topnav li .sub10").css({'display':'none'}); //Fade sub nav to 0 opacity on default     

    $("ul#topnav li .sub20").each(function() {
        $(this).show();
        rowHeight = 0;
        //Calculate row
        $(this).find("ul").each(function() { //for each ul...
            if($(this).height() > rowHeight)
                rowHeight = $(this).height(); //Add each ul's width together
        });
        $(this).css({'height' : rowHeight});
        $(this).hide();
    });
    $("ul#topnav li").hover(
        function() {        
            megaHoverOver($(this));
        },
        function() {
            megaHoverOut($(this));
        }
    );
    
    if($.browser.msie && $.browser.version < 7){
        $("ul#topnav li .sub10").css({ "border": "1px solid #BBBBBB","border-bottom": 0 });
        $("ul#topnav li .sub20").css({ "border": "1px solid #BBBBBB" });        
    } else {
        $("ul#topnav li .sub10").shadowOn({ imageset: 102, autoresize: false });        
        $("ul#topnav li .sub20").shadowOn({ imageset: 2, autoresize: false });
        function isOver(e,that) {
            var x = e.pageX-that.get(0).offsetLeft;
            var y = e.pageY-that.get(0).offsetTop;
            var el = that.offset();
            if(typeof(el) == "undefined")
                return true;
            return (((x < el.left || x > (el.left+that.width())) && y < el.top+that.height()) || y < el.top);            
        }
        $("ul#topnav li .sub10 div.shadowOnBit").mousemove(
            function(e) {
                if(isOver(e,$(this).parent().find(".sub11"))) {
                    megaHoverOut($(this).parent().parent());        
                }    
            }
        );
    }    

});
