﻿/*
    MasterPage.master 页面顶部导航条脚本。
*/
$(document).ready(function () {
    /* 导航条菜单按钮鼠标移入时事件 */
    function megaHoverOver() {
        $(this).children("a")
            .css("background-color", "#F6F6F6")
            .css("color", "Black")
            .css("margin-top", "2px")
            .css("padding-top", "11px");
        //使导航按钮背景变色。

        if ($(this).find(".sub").length > 0) {
            $(this).find(".sub").stop().fadeTo('fast', 1).show();
            //Calculate width of all ul's
            (function ($) {
                jQuery.fn.calcSubWidth = function () {
                    rowWidth = 0;
                    //Calculate row

                    $(this).find("ul").each(function () {
                        rowWidth += $(this).width() + 1;
                    });
                    /*
                    $(this).children().each(function () {
                    rowWidth += $(this).width();
                    });
                    */
                };
            })(jQuery);

            if ($(this).find(".row").length > 0) { //If row exists...
                var biggestRow = 0;
                //Calculate each row
                $(this).find(".row").each(function () {
                    $(this).calcSubWidth();
                    //Find biggest row
                    if (rowWidth > biggestRow) {
                        biggestRow = rowWidth;
                    }
                });
                //Set width
                $(this).find(".sub").css({ 'width': biggestRow });
                $(this).find(".row:last").css({ 'margin': '0' });

            }
            else { //If row does not exist...
                $(this).find(".sub").calcSubWidth();
                //Set Width
                $(this).find(".sub").css({ 'width': rowWidth });
            }


            //xiefang 2010/07/11 使宽度不超过屏幕设置的宽度。
            var subPanel = $(this).find(".sub");
            var offset = subPanel.offset();
            var width = subPanel.width();
            var BigScreenObj = $(document).find("#webtheme");
            var BigPictureRight = BigScreenObj.offset().left + BigScreenObj.width();
            var parentObj = subPanel.parent();
            if (subPanel != null && parentObj != null && BigScreenObj != null) {
                //如果超出了大图片右方，则向左移。
                //+20的原因是因为左右边距均有 padding 像素。内容的-20也是这个原因。
                if (offset.left + width + 20 > BigPictureRight) {
                    subPanel.css("left", offset.left - parentObj.offset().left + (BigPictureRight - (offset.left + width) - 20));
                }
            }
        }
    }
    /* 导航条菜单按钮鼠标移出时事件 */
    function megaHoverOut() {
        $(this).children("a")
            .css("background-color", "#800000")
            .css("color", "#fff")
            .css("margin-top", "0px")
            .css("padding-top", "13px");
        $(this).find(".sub").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }

    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 10, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 100, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#topnav li .sub").css({ 'opacity': '0' });
    $(".navbutton").hoverIntent(config);
});


