// JavaScript Document
$(document).ready(function() {

    /*------------------------------------------------------------------------------------------------------*/
    /* browser detection */
    var client = parseInt(jQuery.browser.version);

    if ($.browser.msie && client <= 6) {
        // nothing
        } else {

        // layout helper + click hide on a
        $('#mainNavigation1 a').wrapInner('<span />').click(function() {
            $('#mainNavigation1 ul.l1').hide();
        })
        // layout exceptions
        $('#mainNavigation1 li.sT').next().children('a').css('padding-top', 0);
        $('#mainNavigation1 li.sT').next().children('ul').css('top', -3);
        $('#mainNavigation1 li.sB').prev().css('border', 'none').children('a').css('padding-bottom', 0);

        // ie7 layout helper (ie7 stack bug)
        if ($.browser.msie && client == 7) {
            $("#mainNavigation1 li").filter(":not('.sT')").filter(":not('.sB')").mouseenter(function() {
                $(this).css('position', 'relative').css('z-index', 3000);
            }).mouseleave(function() {
                $(this).css('position', '').css('z-index', '');
            })
        }

        /*------------------------------------------------------------------------------------------------------*/
        /* honor viewport */
        var slotX1 = 0;
        var slotX2 = 0;
        var ulLeftNew = -1;
        var ulRightNew = -1;

        $('#mainNavigation1 li[class!=home]').mouseenter(function() {

            // reset honor viewport position
            $(this).children('ul').css('left', '');

            // only remove class flyUnder if parent has not moved over
            if (!$(this).parent().hasClass('movedOver')) {
                $('#mainNavigation1 ul.l1.flyUnder').removeClass('flyUnder');
            }

            // reset parent movedOver switch
            $(this).children('ul.movedUnder').removeClass('movedOver');

            // reset honor viewport position
            ulLeftNew = 0;

            ulRightNew = 0;
            var tar = $(this).children('ul').eq(0);

            // need show() for ie to calculate layout-position, ie calculate fix
            tar.show();
            var offTar = tar.offset();
            var ulLeft = offTar.left;
            var ulRight = ulLeft + tar.width();
            // console.info(ulLeft, '-----------------');
            // remember slot on l1
            if ($(this).parent().hasClass('l0')) {
                slotX1 = ulLeft - 2;
                slotX2 = ulRight + 2;
            }
            // console.info(slotX1, ' x1');
            // console.info(slotX2, ' x2');
            // remove ie calculate fix
            tar.css('display', '');

            var vpw = $(window).width() + 10;
            // honor shadowBox, we have more space
            // console.info(vpw, ' vpw');
            // if viewport is to small and we are not on first level
            if (ulRight > vpw && !$(this).children('ul').hasClass('l1')) {
                tar.css('left', -210);
                // -208
            }
            // dimm underlayed
            var tarNew = $(this).children('ul').eq(0);

            tarNew.show();
            var offTarNew = tarNew.offset();
            var ulLeftNew = offTarNew.left;
            // console.info(ulLeftNew, '   ulLeftNew');
            var ulRightNew = ulLeftNew + tarNew.width();
            // console.info(ulRightNew, '   ulRightNew');
            tarNew.css('display', '');
            if (ulLeftNew >= slotX1 && ulRightNew <= slotX2 && !$(this).parent().hasClass('l0')) {
                $('#mainNavigation1 ul.l1').addClass('flyUnder');
                tar.addClass('movedOver');
            }
        });

        /*------------------------------------------------------------------------------------------------------*/
        /* dimm content */


        $('#page').append('<div id="flyoutDimm"></div>');
        $('#flyoutDimm').hide();

        $('#mainNavigation1 > ul.l0 > li.hasSub').mouseenter(function() {
            // dimm
            var h = $('#content').height();
            var off = $('#content').offset();
            var top = off.top;
            var diff = top - 155;
            var vph = $(window).height();
            h = h + diff;
            if (h < vph) {
                h = vph - 155;
            }
            $('#flyoutDimm').height(h);
            $('#flyoutDimm').stop().fadeTo(500, 0.7);
            $('#mainNavigation1').addClass('doFly');
            // ie hack for swf-stack-bug
            //if ($.browser.msie) {
                $('object').css('visibility', 'hidden');
                $('object').parent().css('background', '#eee');
            //}
        })

        $('#mainNavigation1').mouseleave(function() {
            // unDimm
            $('#flyoutDimm').stop().fadeOut(500,
            function() {
                // ie un-hack for swf-stack-bug
                //if ($.browser.msie) {
                    $('object').css('visibility', '');
                //}
            });

            $('#mainNavigation1').removeClass('doFly');
        })

        $('#mainNavigation1 li.home').mouseenter(function() {
            // unDimm
            $('#flyoutDimm').stop().fadeOut(500,
            function() {
                // ie un-hack for swf-stack-bug
                //if ($.browser.msie) {
                    $('object').css('visibility', '');
                //}
            });

        })
    }

})
