var tab_counter = 1;
var tab_title, tab_content;

function AddTab(tabtitle, url, id) {
    if (tab_counter > 10) {
        cihoAlert('已经超出最大标签数量，请先关闭部分标签！');
        return;
    }
    //生成tab标签时，设定IFrame的高度 = 窗口高度 - menu高度 - 底部高度 - tab标签高度
    var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
    var content = '<iframe scrolling="no" id="idIframe-' + id + '" frameborder="0" align="center"  src="' + url + '" style="width:100%;height:' + iHeight + 'px;"></iframe>';
    var options = { title: tabtitle,
        content: content,
        id:id,
        //height: iHeight,
        //href:'cggl/cgdd',
        closable: true
    }
    CreateAtab(tabtitle, options,"span_" + id);
}
function CreateAtab(title, options, id) {
    //if (istabexists(title, 'tabs')) {
        //$("#tabs").tabs('select', title).selected = true;
    if (istabexists(title, 'tabs', id)) {
        $("#tabs").tabs('select', title, id).selected = true;
    }
    else {
        $("#tabs").tabs('add', options);
        tab_counter++;
    }
    tabClose();
    tabCloseEven();
}
function istabexists(title, parent, spanId) {
    var r = false;
    if ($(".tabs>li>a:has(span[id=\"" + spanId + "\"])").length > 0) {
        r = true;
    }
    return r;
}
function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children("span").text();
        var subId = $(this).children("span").attr("id");
        //报表中心不能关闭。
        if (subId != "span_p01") {
            //$('#tabs').tabs('close', subtitle);
            $('#tabs').tabs('close', subtitle, subId);
        }
    })
    //生成右键菜单
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX - 5,
            top: e.pageY - window.document.documentElement.clientHeight
        });
        var subtitle = $(this).children("span").text();
        var subId = $(this).children("span").attr("id");
        //$('#mm').data("currtab", subtitle, subId);
        $('#mm').data("currtab", subId);
        return false;
    });
}

function tabCloseEven() {
    //首页，不能关闭。p01.htm ciho
    $('#mm-tabclose').click(function () {
        //var currtab_title = $('#mm').data("currtab");
        //var id = $('.tabs-selected').find('span').attr('id');
        var id = $('#mm').data("currtab");
        if (id != "span_p01") {
            $('#tabs').tabs('close', "", id);
        }
        //$('#tabs').tabs('close', currtab_title);
    })
    //全部关闭
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            //var t = $(n).text();
            var id = $(n).attr('id');
            if (id != "span_p01") {
                $('#tabs').tabs('close', "", id);
            }
            //$('#tabs').tabs('close', t);
        });
    });
    //关闭除当前之外的TAB ciho
    $('#mm-tabcloseother').click(function () {
        //var currtab_title = $('#mm').data("currtab");
        //var currtab_id = $('.tabs-selected').find('span').attr('id');
        var currtab_id = $('#mm').data("currtab");
        $('.tabs-inner span').each(function (i, n) {
            //var t = $(n).text();
            var id = $(n).attr('id');
            //debugger
            if (id != "span_p01" && id != currtab_id) {
                $('#tabs').tabs('close', "", id);
            }
            //if (t != currtab_title)
            //    $('#tabs').tabs('close', t);
        });
    });
    //关闭当前右侧的TAB
    $('#mm-tabcloseright').click(function () {
        //var nextall = $('.tabs-selected').nextAll();
        var currtab_id = $('#mm').data("currtab");
        var nextall = $('#' + currtab_id).parent().parent().nextAll();
        if (nextall.length == 0) {
            return false;
        }
        nextall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            var id = $('a:eq(0) span', $(n)).attr('id');
            if (id != "span_p01") {
                $('#tabs').tabs('close', t, id);
            }
            //$('#tabs').tabs('close', t);
        });
        return false;
    });
    //关闭当前左侧的TAB
    $('#mm-tabcloseleft').click(function () {
        //var prevall = $('.tabs-selected').prevAll();
        var currtab_id = $('#mm').data("currtab");
        var prevall = $('#' + currtab_id).parent().parent().prevAll();
        if (prevall.length == 0) {
            return false;
        }
        prevall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            var id = $('a:eq(0) span', $(n)).attr('id');
            if (id != "span_p01") {
                $('#tabs').tabs('close', t, id);
            }
            //$('#tabs').tabs('close', t);
        });
        return false;
    });
}//*/
/*
function openDlg(wtitle, remoteurl) {
    openmodeldlg(wtitle, remoteurl, 'dlgwindow', 'dlgcontent', document.documentElement.clientWidth - 150, 100);
}
function openDlg(wtitle, remoteurl, iWidth, iHeight) {
    openmodeldlg(wtitle, remoteurl, 'dlgwindow', 'dlgcontent', iWidth, iHeight);
}
function openDlg_kjsz(wtitle, remoteurl) {
    openmodeldlg(wtitle, remoteurl, 'kjsz', 'dlgcontent', 500, 200);
}
function openmodeldlg(dlgtitle, remoteurl, dlgelement, contelement, swidth, sheight) {
    var edlg = $('#' + dlgelement);
    var ecnt = $('#' + dlgelement + ' > #' + contelement);
    var ecnt = edlg.find('#' + contelement);
    var content = '<iframe scrolling="no" frameborder="0" align="left"  src="' + remoteurl + '" style="width:100%;"></iframe>';
    if ($('#' + dlgelement) != null) {
        $('#' + dlgelement).window({
            title: dlgtitle,
            width: swidth, //document.documentElement.clientWidth - 150, //1000,
            modal: true,
            shadow: false,
            closed: false,
            border: false,
            height: 510,
            top: 15,
            left: 100,
            minimizable: false,
            maximizable: false,
            draggable: true,
            collapsible: false
        });
        $('#' + dlgelement).find('#' + contelement).panel({
            //ecnt.panel({
            title: '',
            fit: false,
            minimizable: false,
            maximizable: false,
            collapsible: false,
            cache: false,
            border: false,
            content: content
            //href: remoteurl
        });
        //ecnt.panel('refresh');
    }
}
function closedrdjdlg() {
    //var edlg = $('#dlgwindow');
    //$('#dlgwindow').empty();
    $('#dlgwindow').window('close');
}
//$.fn.datebox.defaults.formatter = function (date) {
//    var y = date.getFullYear();
//    var m = date.getMonth() + 1;
//    var d = date.getDate();
//    return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
//};
//$.fn.datebox.defaults.parser = function (s) {
//    if (s) {
//        var a = s.split('-');
//        var y = new Number(a[0]);
//        var m = new Number(a[1]);
//        var d = new Number(a[2]);
//        var dd = new Date(y, m - 1, d);
//        return dd;
//    } else {
//        return new Date();
//    }
//};
function closeCurrenttab() {
    var currtab = $('#tabs').tabs('getSelected');
    var ct = currtab.panel('options').title;
    $('#tabs').tabs('close', ct);
}
function showFilter(filter, isShow) {
    $('#filter').visable = isShow;
}//*/