jQuery(document).ready(function () {

    if (jQuery.browser.msie && jQuery.browser.version >= 6.0) {
        jQuery('.subnavlist li a').css({ 'display': 'inline' });
        jQuery('.subnav li h2 a').css({ 'display': 'block', 'height': 'auto' });
    }



    if (jQuery.client.os == "Mac") {
        if (jQuery.client.browser == "Mozilla" || jQuery.client.browser == "Safari")
            jQuery('.menu > a').addClass('macsafari').css({ 'color': '#171715', 'display': 'block', 'padding': '8px 31px', 'text-decoration': 'none' });
        jQuery('.menu > a, .menu').bind('mouseover', function () {

            jQuery('#navigation-menu li.menu:hover > a').css({ 'padding': '8px 31px 8px 30px', 'color': '#02569e' });
            jQuery('.menu > a:hover').css({ 'padding': '8px 31px 8px 30px', 'color': '#02569e' });
        });
    }

    if (jQuery.client.os == "Mac" && jQuery.client.browser == 'Chrome') {
        jQuery('.menu > a').addClass('macsafari').css({ 'color': '#171715', 'display': 'block', 'padding': '8px 31px', 'text-decoration': 'none' });
        jQuery('.menu > a, .menu').bind('mouseover', function () {

            jQuery('#navigation-menu li.menu:hover > a').css({ 'padding': '8px 31px 8px 30px', 'color': '#02569e' });
            jQuery('.menu > a:hover').css({ 'padding': '8px 31px 8px 30px', 'color': '#02569e' });
        });
    }

    if (jQuery.client.os == 'Mac' && jQuery.client.browser == 'Chrome') {
        jQuery('.menu > a, .menu').bind('mouseleave', function () {
            jQuery('.menu > a').css({ 'color': '#171715' });
            jQuery('a.macsafari', jQuery(this)).css({ 'color': '#171715', 'display': 'block', 'padding': '8px 31px', 'text-decoration': 'none' });
        });

    }


    if (jQuery.browser.msie)
        if (jQuery.browser.version == "6.0") {
            jQuery('.menu > a').addClass('ie6').css({ 'color': '#171715', 'display': 'block', 'padding': '8px 33px', 'text-decoration': 'none' });
            jQuery('.subnav > li').css({ 'width': '178px', 'margin': '15px 3px 0 5px', 'padding': '0', 'float': 'left', 'list-style': 'none', 'text-align': 'left' });
        }

    jQuery('#navigation-menu > li:last-child').css({ 'border-right': '0' });
    jQuery('#navigation-menu > li:last-child').bind('mouseover', function () {
        /* jQuery(this).css({ 'border-right': '1px solid #ccc', 'border-top': '1px solid #ccc', 'border-left': '1px solid #ccc', 'border-bottom': '0' });*/

    });


    jQuery('#navigation-menu > li:last-child').bind('mouseout', function () {
        jQuery(this).css({ 'border': '0', 'border-right': '0' });

    });

    var flag = true;
    var stackCreated = true;
    jQuery('#navigation-menu li.menu').each(function (index) {

        jQuery(this).bind('mouseover', function () {

            if (jQuery.browser.msie)
                if (jQuery.browser.version == "6.0") {
                    jQuery('#navigation-menu li.menu:last-child').css({ 'border-right': '0' });
                }


            if (flag) {
                flag = false;
                if (stackCreated) {
                    if (jQuery('.subnav li.empty', jQuery(this)).length != 0) {
                        var stack = '<li style="width:178px; margin:15px 3px 0px 5px; float:left; list-style:none; text-align:left;"><ul class="stack">';
                        jQuery('.subnav li.empty', jQuery(this)).each(function () {
                            stack += '<li>' + jQuery(this).html() + '</li>';
                            jQuery(this).remove();
                        });
                        stack += '</ul></li>';
                        jQuery('.subnav', jQuery(this)).append(stack);
                        jQuery('.stack li h2', jQuery(this)).css({ 'width': 'auto', 'height': 'auto', 'margin': '0px', 'line-height': '1em', 'background': 'none', 'border': '0' });
                        jQuery('.stack', jQuery(this)).css({ 'padding': '0px', 'margin-top': '-4px', 'margin-left': '1px' });
                        jQuery('.stack li', jQuery(this)).css({ 'width': 'auto', 'list-style': 'none', 'margin': '3px -1px 0px' });
                        jQuery('.subnav li').css({ 'filter': 'Alpha(Opacity="100")' });
                    }
                    stackCreated = false;

                }

            }
            var len = jQuery('.subnav > li', jQuery(this)).length;
            if (len > 3) {
                jQuery('.subnav', jQuery(this)).css('width', 3 * 186 + 'px');
            }
            else {
                jQuery('.subnav', jQuery(this)).css('width', len * 186 + 'px');
            }

            var pos = jQuery(this).position();

            var centerw = parseInt(jQuery('#center,#content-wrapper').css('width').replace("px", ""));

            var diff = centerw - pos.left;
            var width = jQuery('.subnav', jQuery(this)).css('width');
            //console.log(width+'/'+maxwidth+'/'+w+'/'+diff);
            if (width != null) {
                var ulwidth = parseInt(width.replace("px", ""));
                if (diff < ulwidth) {
                    //var displacement = (diff - ulwidth) + jQuery(this).width()+30;
                    var displacement = pos.left + jQuery(this).width() - ulwidth -  2;
                    jQuery('.subnav', jQuery(this)).css({ 'left': displacement + 'px' })
                    jQuery('.subnav', jQuery(this)).css({ 'position': 'absolute' })
                }

                if (jQuery.browser.msie)
                    if (jQuery.browser.version == "6.0" && index < 2) {
                        jQuery('.subnav', jQuery(this)).css({ 'left': '0px' })
                        jQuery('.subnav', jQuery(this)).css({ 'position': 'absolute' })

                    }

            }

            jQuery('.subnav', jQuery(this)).fadeTo('fast', 1).show();
            jQuery('.subnavlistwrap', jQuery(this)).fadeTo('fast', 1).show();

            //Fixing uneven height lists in subnav
            var maxHeight = jQuery('.subnav > li:eq(0)', jQuery(this)).height();
            jQuery('.subnav > li', jQuery(this)).each(function (i) {

                if ((jQuery(this).height() > maxHeight)) {
                    maxHeight = jQuery(this).height();
                }
            });

            jQuery('.subnav > li', jQuery(this)).each(function (j) {
                jQuery(this).height(maxHeight);

            });


        }); // end of mouseover

        jQuery('#navigation-menu li.menu').each(function (index) {

            jQuery(this).bind('mouseover', function () {

                if (jQuery.browser.msie)
                    if (jQuery.browser.version == "6.0") {
                        jQuery('.stack li h2', jQuery(this)).css({ 'background': 'none', 'border': '0', 'padding': '0', 'height': '1em', 'margin': '0px 0px 0px 0px' });
                        jQuery('.stack', jQuery(this)).css({ 'padding': '0px', 'margin': '0px 0px 0px 0px', 'margin-left': '1px' });
                        jQuery('.stack li', jQuery(this)).css({ 'list-style': 'none', 'padding': '0px 0px 0px 0px' });
                        jQuery('> li', '.stack', jQuery(this)).css({ 'filter': 'Alpha(Opacity="100")' });
                    }
            });
        });



        jQuery('#navigation-menu li.menu:last-child').bind('mouseout', function () {
            jQuery(this).css({ 'border': '0', 'border-right': '0' });
        });


        jQuery('#navigation-menu li.menu:last-child').bind('mouseleave', function () {

            if (jQuery.browser.msie)
                if (jQuery.browser.version == "6.0") {
                    jQuery('#navigation-menu li.menu:last-child', jQuery(this)).css({ 'border-right': '0px' });
                }
        });



        jQuery(this).bind('mouseleave', function () {
            if (jQuery.browser.msie)
                if (jQuery.browser.version == "6.0") {

                    jQuery('a.ie6', jQuery(this)).css({ 'color': '#171715', 'display': 'block', 'padding': '8px 33px', 'text-decoration': 'none' });
                    jQuery(this).css({ 'margin': '0', 'background': 'none', 'border': '0', 'border-right': '1px solid #CCCCCC', 'display': 'inline', 'float': 'left', 'font-size': '12px', 'font-weight': 'bold', 'position': 'relative', 'text-align': 'center', 'padding-bottom': '0px', 'padding-top': '2px' });
                }

            if (jQuery.client.os == "Mac") {
                if (jQuery.client.browser == "Mozilla" || jQuery.client.browser == "Safari")
                    jQuery('a.macsafari', jQuery(this)).css({ 'color': '#000', 'display': 'block', 'padding': '8px 31px', 'text-decoration': 'none' });

            }

            jQuery('.subnav', jQuery(this)).fadeTo('slow', 1).hide();
            jQuery('.subnavlistwrap', jQuery(this)).fadeTo('fast', 1).hide();
            flag = true;
            stackCreated = true;

        });

    });


});
