$(function () {
    openWindow("基本信息", "memberinfo/member_basicinfo.aspx", false);

    setLayout();
    $(window).resize(function () {
        setLayout();
    });

    initTabs();
});

function setLayout() {
    var windowHeight = $(this).height();
    var windowWidth = $(this).width();
    var headerHeight = $("#header").outerHeight();
    var footerHeight = $("#footer").outerHeight();
    var mainHeight = windowHeight - headerHeight - footerHeight - 0;
    $("#tabsPanel").height(mainHeight);

    //重置标签控件的大小；
    $('#tabs1').tabs('resize');
}

//初始化页面主体区域标签页控件；
function initTabs() {
    //标签双击关闭
    $('#tabs1 ul.tabs').bind("dblclick", function (e) {
        var tabTitle = $(e.target).text();
        //alert(tabTitle);
        e.stopPropagation(); //阻止事件冒泡；

        //关闭标签；
        $('#tabs1').tabs('close', tabTitle);
    });

    //给tabs控件绑定鼠标右键事件
    $(".tabs-header .tabs-wrap .tabs ").bind('contextmenu', function () {
        event.preventDefault();
        $("#contextMenu").menu('show', { left: event.pageX, top: event.pageY });
    });
}

//显示“关于”信息；
function showAbout() {            
    $('#divAboutPanel').modal({
        containerCss: { "width": "400px", "height": "230px" },
        //opacity: 30,
        closeHTML: '',
        onClose: closeAboutPane
    });
}
function closeAboutPane() {
    $.modal.close();
}
function changePassword() {
    openFrameWindow("修改密码", 440, 260, "/sysmembership/ChangeMyPassword.aspx", false);
}
function logout() {
    if (confirm("确定要退出系统吗？")) {
        window.location.href = "logout.aspx";
    }
}


//添加选项卡；
//pageTitle : "窗口1"
//pageURL: /modules/cyt/member/member_editlist.aspx
//openType : self | blank
function openWindow(pageTitle, pageURL, closable) {
    
    var openType = "auto";
    var windowCountLimit = 20;

    openType = openType.toLowerCase();

    if (openType == "blank") {
        window.open(pageURL);

    } else if (openType == "self"){

        /*如果已经打开过，则不再重复打开，并定位到该标签*/
        var exists = $("#tabs1").tabs('exists', pageTitle);;
        if (exists) {
            $("#tabs1").tabs('select', pageTitle);
            refreshActivatedTabWindow();
            return;
        }

        //先关闭所有tab
        var tabs = $('#tabs1').tabs("tabs"); //注意：返回的是dom对象，不是JQuery对象；
        var tabTitles = new Array();
        for (var i = 0; i < tabs.length; i++) {
            var title = tabs[i].panel('options').tab.text();
            if (title != "首页") {
                tabTitles.push(title);
            }
        }
        for (var i = 0; i < tabTitles.length; i++) {
            $("#tabs1").tabs("close", tabTitles[i]);
        }
    } else if (openType == "auto") { //只保留最后5个窗口 
   
        //console.log(openType);
        //console.log(windowCountLimit);

        /*如果已经打开过，则不再重复打开，并定位到该标签*/
        var exists = $("#tabs1").tabs('exists', pageTitle);;
        if (exists) {
            $("#tabs1").tabs('select', pageTitle);
            refreshActivatedTabWindow();
            return;
        }

        //先关闭所有tab
        var tabs = $('#tabs1').tabs("tabs"); //注意：返回的是dom对象，不是JQuery对象；
        var tabTitles = new Array();
        for (var i = 0; i < tabs.length; i++) {
            var title = tabs[i].panel('options').tab.text();
            if (title != "首页") {
                tabTitles.push(title);
            }
        }            
        if (tabTitles.length >= windowCountLimit) {
            $("#tabs1").tabs("close", tabTitles[0]);
        }     
      
    }        
        
    var isCloseable = true;
    if (arguments.length == 2) {
        isCloseable = true;            
    }
    else if (arguments.length == 3) {
        isCloseable = closable;
    }
        
    // add a new tab panel    
    $('#tabs1').tabs('add', {
        title: pageTitle,
        style: 'padding:10px;overflow-x:hidden;border:0px;',
        content: createFrame(pageURL),
        //content: '<iframe src="' + pageURL + '" scrolling="no" style="width:100%;height:100%;border:0px;margin:0px;padding:0px;"  ></iframe>',
        closable: isCloseable,
        //tools: [{
        //    iconCls: 'icon-mini-refresh',
        //    handler: function () {
        //        refreshTabWindow();
        //    }
        //}]
    });
}

function createFrame(url) {
    return '<iframe src="' + url + '" style="width:100%;height:100%;border:0px;margin:0px;padding:0px;"  ></iframe>';
}

function refreshActivatedTabWindow() {        
    var currTab = $('#tabs1').tabs('getSelected'); //获得当前tab
    var url = $(currTab.panel('options').content).attr('src');        
    $('#tabs1').tabs('update', {
        tab: currTab,
        options: {
            content: createFrame(url)
        }
    });
}

function refreshTargetTabWindow(windowTitle) {
    var tabs = $('#tabs1').tabs("tabs"); //注意：返回的是dom对象，不是JQuery对象；        
    for (var i = 0; i < tabs.length; i++) {
        var title = tabs[i].panel('options').tab.text();
        if (title == windowTitle) {
            var currTab = tabs[i];
            var url = $(currTab.panel('options').content).attr('src');
            $('#tabs1').tabs('update', {
                tab: currTab,
                options: {
                    content: createFrame(url)
                }
            });
            break;
        }
    }        
}

function closeActivatedTabWindow() {
   
    var tabs = $('#tabs1').tabs("tabs"); //注意：返回的是dom对象，不是JQuery对象；
    var selectedTab = $('#tabs1').tabs('getSelected');
    var selectedTabTitle = selectedTab.panel('options').tab.text();
        
    if (selectedTabTitle != "首页") {
        $('#tabs1').tabs('close', selectedTabTitle);
    }
}

function closeTargetTabWindow(windowTitle) {
    var tabs = $('#tabs1').tabs("tabs"); //注意：返回的是dom对象，不是JQuery对象；        
    for (var i = 0; i < tabs.length; i++) {
        var title = tabs[i].panel('options').tab.text();
        if (title == windowTitle) {
            if (windowTitle != "首页") {
                $('#tabs1').tabs('close', windowTitle);
            }
        }
    }
}

/*标签页右键菜单处理程序*/
function menuHandler(item) {
    var tabs = $('#tabs1').tabs("tabs"); //注意：返回的是dom对象，不是JQuery对象；
    var selectedTab = $('#tabs1').tabs('getSelected');
    var selectedTabIndex = $('#tabs1').tabs('getTabIndex', selectedTab);
    var selectedTabTitle = selectedTab.panel('options').tab.text();

    if (item.name == "refresh") {
        if (selectedTabTitle == "首页") {
            refreshWelcomePage();
        } else {
            refreshActivatedTabWindow();
        }
    }
    else if (item.name == "closeCurrent") {
        if (selectedTabTitle != "首页" && selectedTabTitle != "点餐台") {
            $('#tabs1').tabs('close', selectedTabTitle);
        }
    }
    else if (item.name == "closeAll") {
        var tabTitles = new Array();
        for (var i = 0; i < tabs.length; i++) {
            var title = tabs[i].panel('options').tab.text();
            if (title != "首页" && title != "点餐台") {
                tabTitles.push(title);
            }
        }
        for (var i = 0; i < tabTitles.length; i++) {
            $("#tabs1").tabs("close", tabTitles[i]);
        }
    }
    else if (item.name == "closeAllExceptCurrent") {
        var tabTitles = new Array();
        for (var i = 0; i < tabs.length; i++) {
            var title = tabs[i].panel('options').tab.text();
            if (title != selectedTabTitle) {
                if (title != "首页" && title != "点餐台") {
                    tabTitles.push(title);
                }
            }
        }
        for (var i = 0; i < tabTitles.length; i++) {
            $("#tabs1").tabs("close", tabTitles[i]);
        }
    }
}