var a = "欢迎使用";
var b = 5;
$(function() {
    if (_menus === undefined || _menus === null) {
        $.messager.alert("系统提示", "<font color=red><b>出现系统错误，得到用户模块出错，请与管理员联系!</b></font>", "warning", 
        function() {
            location.href = '/ajax/loginout.ashx'
        })
    };
    if (_menus) {
        initNav();
        tabClose();
        addTab("欢迎使用", "welcome.html", "icon-house_star")
    } else {
        $.messager.alert("系统提示", "<font color=red><b>您没有任何权限！请联系管理员。</b></font>", "warning", 
        function() {
            location.href = '/ajax/loginout.ashx'
        })
    };
    $('#editpass').click(function() {
        editMyPass()
    });
    $('body').layout({
        onExpand: function() {
            $('body').layout('resize')
        }
    });
    $('#closeMenu').menu({
        onClick: function(m) {
            closeTab(m.id)
        }
    })
});
function initNav() {
    if (sys_config.navType) {
        switch (sys_config.navType) {
        case "Accordion":
            c.InitLeftMenu();
            break;
        case "Tree":
            f.init();
            break;
        case "AccordionTree":
            d.init();
            break;
        default:
            c.InitLeftMenu();
            break
        }
    } else {
        c.InitLeftMenu()
    };
    $('#tabs').tabs({
        tools:
        [{
            title:
            "首页",
            iconCls: "icon-house_star",
            handler: function() {
                addTab("Welcome", "welcome.html", "icon-house_star");
                return false
            }
        },
        {
            iconCls: 'icon-arrow_refresh',
            handler: function() {
                var m = $('#tabs').tabs('getSelected');
                if (m.panel('options').title != a) closeTab('refresh');
                else return false
            }
        },
        {
            iconCls: 'panel-tool-close',
            handler: function() {
                if (confirm('亲，确认要关闭所有窗口吗？')) {
                    closeTab("closeall")
                }
            }
        }],
        onContextMenu: function(m, n) {
            if (n !== a) {
                m.preventDefault();
                $('#closeMenu').menu('show', {
                    left: m.pageX,
                    top: m.pageY
                });
                $('#tabs').tabs('select', n)
            } else {
                return false
            }
        }
    })
};
var c = {
    addNav: function(m) {
        $.each(m, 
        function(p, q) {
            var r = "";
            r += '<ul>';
            $.each(q.children, 
            function(s, t) {
                var u = 'icon icon-application_osx';
                if (t.IconCss) {
                    u = t.IconCss
                };
                r += '<li><div><a ref="' + t.Id + '" href="#" rel="' + t.NavigateUrl + '" ><span  iconCls="' + u + '" class="icon ' + u + '" >&nbsp;</span><span class="nav">' + t.FullName + '</span></a></div></li> '
            });
            r += '</ul>';
            $('#wnav').accordion('add', {
                title: q.FullName,
                content: r,
                iconCls: 'icon ' + q.IconCss,
                border: false
            })
        });
        var n = $('#wnav').accordion('panels');
        var o = n[0].panel('options').title;
        $('#wnav').accordion('select', o)
    },
    InitLeftMenu: function() {
        $("#wnav").accordion({
            animate: false,
            fit: true,
            border: false
        });
        c.addNav(_menus);
        $('.accordion li').click(function() {
            var m = $(this).children('div').children('a');
            var n = $(m).children('.nav').text();
            var o = $(m).attr("rel");
            var p = $(m).attr("ref");
            var q = $(m).children('.icon').attr('class');
            addTab(n, o, q);
            $('.accordion li div').removeClass("selected");
            $(this).children('div').addClass("selected")
        }).hover(function() {
            $(this).children('div').addClass("hover")
        },
        function() {
            $(this).children('div').removeClass("hover")
        })
    }
};
var d = {
    init: function() {
        $.each(_menus, 
        function(m, n) {
            var o = 'icon icon-application_osx';
            if (n.iconCls) {
                o = n.iconCls
            };
            $('#wnav').append('<div style="padding:0px;" title="' + n.text + '" data-options="border:false,iconCls:\'' + o + '\'"><ul id="nt' + m + '"></ul></div>')
        });
        $("#wnav").accordion({
            fit: true,
            border: false,
            onSelect: function(m, n) {
                $('#nt' + n).tree({
                    lines: false,
                    animate: true,
                    data: _menus[n].children,
                    onClick: function(o) {
                        if (o.attributes.url != "" && o.attributes.url != '#') {
                            addTab(o.text, o.attributes.url + '?navid=' + o.id, o.iconCls)
                        } else {
                            $('#nt' + n).tree('toggle', o.target)
                        }
                    }
                })
            }
        })
    }
};
var e = {
    initMenuItem: function(m, n) {
        $.each(m.children, 
        function(o, p) {
            if (p.children.length > 0) {
                n += '<div>';
                n += '<span iconCls="' + p.iconCls + '">' + p.title + '</span><div style="width:120px;">';
                n = e.initMenuItem(p, n);
                n += '</div></div>'
            } else n += '<div iconCls="' + p.iconCls + '" id="' + p.url + '">' + p.title + '</div>'
        });
        return n
    },
    addNav: function(m) {
        var n = "";
        var o = '';
        $.each(m, 
        function(p, q) {
            n += String.format('<a href="javascript:void(0)" id="mb{0}" class="easyui-menubutton" menu="#mm{0}" iconCls="{1}">{2}</a>', (p + 1), q.iconCls, q.title);
            if (q.children.length > 0) {
                o += '<div id="mm' + (p + 1) + '" style="width:120px;">';
                o = e.initMenuItem(q, o);
                o += '</div>'
            }
        });
        $('#wnav').append(n).append(o)
    },
    init: function() {
        e.addNav(_menus);
        $('#wnav').css({
            'float': 'left',
            'width': '100%',
            'height': '30px',
            'padding': '3px 0px 0px 20px',
            'background': '#6ABEFA url(/images/datagrid_title_bg.png)'
        });
        if (theme == "gray") {
            $('#wnav').css('background', 'url(Scripts/easyui/themes/gray/images/tabs_enabled.gif)')
        };
        var m = $('body').layout('panel', 'north');
        m.panel('resize', {
            height: 103
        });
        $('body').layout('resize');
        var n = $('#wnav .easyui-menubutton').menubutton();
        $.each(n, 
        function(o, p) {
            $($(p).menubutton('options').menu).menu({
                onClick: function(q) {
                    var r = q.text;
                    var s = q.id;
                    var t = q.iconCls;
                    addTab(r, s, t);
                    return false
                }
            })
        })
    }
};
var f = {
    init: function() {
        $("#wnav").tree({
            animate: true,
            lines: true,
            data: _menus,
            onClick: function(m) {
                if (m.attributes.url != '#' && m.attributes.url != '') {
                    addTab(m.text, m.attributes.url + '?navid=' + m.id, m.iconCls)
                } else {
                    $('#wnav').tree('toggle', m.target)
                }
            }
        })
    }
};
function addTab(m, n, o) {
    if (n == "" || n == "#") return false;
    var p = $('#tabs').tabs('tabs').length;
    var q = $('#tabs').tabs('exists', m);
    var add = function() {
        if (!q) {
            $('#tabs').tabs('add', {
                title: m,
                content: createFrame(n),
                closable: (m != a),
                icon: o
            })
        } else {
            $('#tabs').tabs('select', m);
        }
    };
    if (p > b && !q) {
        var r = '<b>亲，你打开的页面太多了，如果继续打开会影响程序的运行效率！</b>';
        $.messager.confirm("系统提示", r, 
        function(s) {
            if (s) {
                add()
            } else {
                return false
            }
        })
    } else {
        add()
    };
    return false
};
function createFrame(m) {
    var n = '<iframe scrolling="auto" frameborder="0"  style="width:100%;height:100%;" src="' + m + '" ></iframe>';
    return n
};
function tabClose() {
    $(".tabs-inner").live('dblclick', 
    function() {
        var m = $(this).children(".tabs-closable").text();
        if (m != a && m != "") $('#tabs').tabs('close', m)
    })
};
function closeTab(m) {
    var n = $('#tabs').tabs('tabs');
    var o = $('#tabs').tabs('getSelected');
    var p = [];
    $.each(n, 
    function(s, t) {
        p.push($(t).panel('options').title)
    });
    var q;
    var r;
    switch (m) {
    case "refresh":
        var s = $(o.panel('options').content);
        var t = s.attr('src');
        $('#tabs').tabs('update', {
            tab: o,
            options: {
                content: createFrame(t)
            }
        });
        break;
    case "close":
        q = o.panel('options').title;
        $('#tabs').tabs('close', q);
        break;
    case "closeall":
        $.each(p, 
        function(u, v) {
            if (v != a) {
                $('#tabs').tabs('close', v)
            }
        });
        break;
    case "closeother":
        q = o.panel('options').title;
        $.each(p, 
        function(u, v) {
            if (v != q && v != a) {
                $('#tabs').tabs('close', v)
            }
        });
        break;
    case "closeright":
        r = $('#tabs').tabs('getTabIndex', o);
        if (r == n.length - 1) {
            alert('亲，后边没有啦 ^@^!!');
            return false
        };
        $.each(p, 
        function(u, v) {
            if (u > r) {
                if (v != a) {
                    $('#tabs').tabs('close', v)
                }
            }
        });
        break;
    case "closeleft":
        r = $('#tabs').tabs('getTabIndex', o);
        if (r == 1) {
            alert('亲，起始页关闭不了的哟。 ^@^!!');
            return false
        };
        $.each(p, 
        function(u, v) {
            if (u < r) {
                if (v != a) {
                    $('#tabs').tabs('close', v)
                }
            }
        });
        break;
    case "exit":
        $('#closeMenu').menu('hide');
        break
    }
};
var g = '<table class="grid" id="ePForm">';
g += '<tr><td>登录名：</td><td><span id="loginName"></span></td></tr>';
g += '<tr><td>原密码：</td><td><input  validType="safepass"  required="true" id="txtOldPassword" name="oldPassword" type="password" class="txt03" /></td></tr>';
g += '<tr><td>新密码：</td><td><input  validType="safepass"  required="true" id="txtNewPassword" name="newPassword" type="password" class="txt03" /></td></tr>';
g += '</table>';
var editMyPass = function() {
    top.$('#w').hWindow({
        width: 300,
        height: 210,
        title: '修改密码',
        iconCls: 'icon-key',
        html: g,
        submit: function() {
            if ($('#txtNewPassword').validatebox('isValid')) {
                $.ajaxtext('ajax/editpass.ashx', "newPassword=" + $('#txtNewPassword').val() + '&oldPassword=' + $('#txtOldPassword').val(), 
                function(m) {
                    if (m == '1') {
                        alert('成功修改登录密码，请重新登录！');
                        location.href = 'ajax/loginout.ashx'
                    } else {
                        $.messager.alert('温馨提示', m, 'warning')
                    }
                })
            }
        }
    });
    $('#loginName').text($('#curname').text());
    $('#txtNewPassword').validatebox();
    $('#txtOldPassword').validatebox();
    $('#txtOldPassword').focus()
};