//-----------------------------------------------------------
// 文件名称: Web25175_Hotel2.tab.v1.0.js
// 页面名称: Web25175_Hotel2.tab.v1.0
//　
// 描    述: tab选择卡
// 版　　本: 1.0
// 兼容情况: IE6+,Chrome,Mozilla, Netscape, Firefox,Maxthon3+,
// 创建标识: Add By 吴希 2011-10-26
//---------------------------------------------
// Tab切换效果
//---------------------------------------------
function tab_option(index) {
   var divtab = uiGetTag(document,"li");
   var number=0;
   for (i = 0; i < divtab.length; i++) {
        var temp = divtab[i].id;
        var num = temp.indexOf("current");
        if (num==0) {
            number++;
        }
    }
    for (var i = 1; i <= number; i++) {
        uiGet('current' + i).className = '';
        uiGet('content' + i).style.display = 'none';
    }
    uiGet('current' + index).className = 'current';
    uiGet('content' + index).style.display = 'block';
    
}
//---------------------------------------------
// Tab标签Id重新定义Id号
//---------------------------------------------
function tab_add(e){
    var arrtab = new Array();
    var arrtabcontent = new Array();
    var divtab = uiGetTag(document,"li");
    var divtabcontent = uiGetTag(document,"div");
    var tab = uiGet("tab");
    var tabcontent = uiGet("tabcontent");
    //---------------------------------------------
    // Tab标签Id重新定义Id号
    //---------------------------------------------
    var tab_index = 1
    for (i = 0; i < divtab.length; i++) {
        var temp = divtab[i].id;
        var num = temp.indexOf("current");
        if (num == 0) {
            divtab[i].id = "current" + tab_index;
            arrtab.push(new Array(divtab[i].id));
            tab_index++;
        }
    }
    //---------------------------------------------
    // Tab内容Id重新定义Id号
    //---------------------------------------------
    tab_index = 1;
    for (i = 0; i < divtabcontent.length; i++) {
        var temp = divtabcontent[i].id;
        var num = temp.indexOf("content");
        if (num == 0) {
            divtabcontent[i].id = "content" + tab_index;
            divtabcontent[i].style.display="none"
            arrtabcontent.push(new Array(divtabcontent[i].id));
            tab_index++;
        }
    }

    //---------------------------------------------
    // 判断是否重复名称，有就激活，无就增加
    //---------------------------------------------
    var flag = true;
    for (i = 0; i < arrtab.length; i++) {
        if (arrtab[i] != null) {
            if (uiGet(arrtab[i]).pername == e.PerName) {
                uiGet(arrtab[i]).className = "current";
                uiGet(arrtab[i]).onclick();
                flag = false;
            } else {
                uiGet(arrtab[i]).className = "";
            }
        }
    }
    if (flag) {
        //---------------------------------------------
        // 大于 5 Tab 移动出最早出现Id号，之后重新 Tab标签、内容Id号
        //---------------------------------------------
        if (arrtab.length == 5) {
            tab.removeChild(uiGet("current1"));
            tabcontent.removeChild(uiGet("content1"));

            arrtab = new Array();
            arrtabcontent = new Array();
            tab_index = 1
            for (i = 0; i < divtab.length; i++) {
                var temp = divtab[i].id;
                var num = temp.indexOf("current");
                if (num == 0) {
                    divtab[i].id = "current" + tab_index;
                    arrtab.push(new Array(divtab[i].id));
                    tab_index++;
                }
            }
            tab_index = 1;
            for (i = 0; i < divtabcontent.length; i++) {
                var temp = divtabcontent[i].id;
                var num = temp.indexOf("content");
                if (num == 0) {
                    divtabcontent[i].id = "content" + tab_index;
                    divtabcontent[i].style.display = "none";
                    arrtabcontent.push(new Array(divtabcontent[i].id));
                    tab_index++;
                }
            }
        }
        //---------------------------------------------
        // 新Tab构造
        //---------------------------------------------
        var li = null;
        var div = null;
        
        var tabnumber = arrtab.length + 1 //自动增 + 1
        if (window.ActiveXObject) {
            li = document.createElement("<li style=\"cursor: pointer;\" id=\"current" + tabnumber + "\" pername='" + e.PerName + "'></li>");
            li.innerHTML = "<a>" + e.PerName + "</a>";
            li.className = "current";
            li.onclick = function() {
                tab_option(this.id.substring(7));
            }
            div = document.createElement("<div id=\"content" + tabnumber + "\" ></div>");
            div.innerHTML = "<iframe id=\"manFrame" + tabnumber + "\" frameborder=\"no\" name=\"manFrame" + tabnumber + "\" noresize=\"noresize\" scrolling=\"auto\" src=\"" + e.PerPageSite + "\" title=\"manFrame\" width=\"100%\" onload=\"HandleIOFrameResponse(this)\"></iframe>";
        } else {
            li = document.createElement("li");
            li.id = "current" + tabnumber;
            li.pername = e.PerName;
            li.className = "current";
            li.innerHTML = "<a>" + e.PerName + "</a>";
            li.onclick = function() {
                tab_option(this.id.substring(7));
            }
            div = document.createElement("div");
            div.id = "content" + tabnumber;
            div.innerHTML = "<iframe id=\"manFrame" + tabnumber + "\" frameborder=\"no\" name=\"manFrame" + tabnumber + "\" noresize=\"noresize\" scrolling=\"auto\" src=\"" + e.PerPageSite + "\" title=\"manFrame\" width=\"100%\" onload=\"HandleIOFrameResponse(this)\"></iframe>";
        };
        for (i = 0; i < arrtab.length; i++) {
            if (arrtab[i] != null) {
                uiGet(arrtab[i]).onclick = function() {
                    tab_option(this.id.substring(7));
                };
            }
        }
        tab.appendChild(li);
        tabcontent.appendChild(div);
        tab_initmenu();
        tab_contextmenuRemove();
    }
}
//---------------------------------------------
// 安装tab菜单
//---------------------------------------------
function tab_initmenu() {
    var divtab = uiGetTag(document,"li");
    for (i = 0; i < divtab.length; i++) {
        var temp = divtab[i].id;
        var num = temp.indexOf("current");
        if (num == 0) {
            divtab[i].oncontextmenu = function(){
               tab_contextmenu(this);
            };
        }
    }
}
//---------------------------------------------
// 标签菜单构造显示
//---------------------------------------------
function tab_contextmenu(obj) {
    tab_contextmenuRemove();
    var left = uiGetPosition(obj).x;
    var top = uiGetPosition(obj).y + 15;
    
    var contextmenu = "";
    contextmenu += "<a style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=;tab_close('"+obj.id+"'); >关闭 </a>";
    contextmenu += " | <a style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=;tab_close_other('"+obj.id+"'); >关闭其他选项卡</a>";
    
    var div = document.createElement("div");
    div.id = "contextmenu";
    div.innerHTML = contextmenu;
    div.style.position = "absolute";
    div.style.background = "#eeeeee";
    div.style.border = "1px solid #666666";
    div.style.width = "128px";
    div.style.left = left + "px";
    div.style.top = top + "px";
    div.style.textAlign = "center";
    div.style.padding = "3px";
    document.body.appendChild(div);
}
//---------------------------------------------
// 取消文档弹出菜单
//---------------------------------------------
document.oncontextmenu = function() {
    return false;
}
//---------------------------------------------
// 单击文档关闭自定义tab弹出菜单
//---------------------------------------------
document.onclick = function() {
    tab_contextmenuRemove();
}
//---------------------------------------------
// 关闭tab弹出菜单
//---------------------------------------------
function tab_contextmenuRemove() {
    if (uiGet("contextmenu")) {
        document.body.removeChild(uiGet("contextmenu"))
    }
}
//---------------------------------------------
// 关闭tab自己
//---------------------------------------------
function tab_close(obj) {
    var index = obj.substring(7);
    var arrtab = new Array();
    var tab = uiGet("tab");
    var tabcontent = uiGet("tabcontent");

    tab.removeChild(uiGet("current" + index));
    tabcontent.removeChild(uiGet("content" + index));

    //重新定义tab标签id、内容id
    var divtab = uiGetTag(document,"li");
    var divtabcontent = uiGetTag(document,"div");

    //---------------------------------------------
    // Tab标签Id重新定义Id号
    //---------------------------------------------
    var tab_index = 1
    for (i = 0; i < divtab.length; i++) {
        var temp = divtab[i].id;
        var num = temp.indexOf("current");
        if (num == 0) {
            divtab[i].id = "current" + tab_index;
            arrtab.push(new Array(divtab[i].id));
            tab_index++;
        }
    }
    //---------------------------------------------
    // Tab内容Id重新定义Id号
    //---------------------------------------------
    tab_index = 1
    for (i = 0; i < divtabcontent.length; i++) {
        var temp = divtabcontent[i].id;
        var num = temp.indexOf("content");
        if (num == 0) {
            divtabcontent[i].id = "content" + tab_index;
            tab_index++;
        }
    }
    //---------------------------------------------
    // 激活不是最后个Tab,当前删除的后个tab项目
    //---------------------------------------------
    var flag=true
    for (i = 0; i < arrtab.length; i++) {
        if (arrtab[i] != null) {
            if ((i + 1) == index) {
                uiGet(arrtab[i]).className = "current";
                uiGet(arrtab[i]).onclick();
                flag=false;
            }
        }
    }
    if (flag)
    {   
        //---------------------------------------------
        // 激活最后个Tab，当前删除的最后个tab项目
        //---------------------------------------------
        for (i = 0; i < arrtab.length; i++) {
            if (arrtab[i] != null) {
                if ((i + 1) == arrtab.length) {
                    uiGet(arrtab[i]).className = "current";
                    uiGet(arrtab[i]).onclick();
                    flag=false;
                }
            }
        } 
    }
}
//---------------------------------------------
// 关闭tab其他
//---------------------------------------------
function tab_close_other(obj) {
    var index = obj.substring(7);
    var arrtab = new Array();
    //重新定义tab标签id、内容id
    var divtab = uiGetTag(document,"li");
    var divtabcontent = uiGetTag(document,"div");

    var tab = uiGet("tab");
    var tabcontent = uiGet("tabcontent");
    //---------------------------------------------
    // 删除倒序其他
    //---------------------------------------------
    var tab_index = divtab.length
    for (i = divtab.length - 1; 0 <= i; i--) {
        var temp = divtab[i].id;
        var num = temp.indexOf("current");
        if (num == 0) {
            if (divtab[i].id != obj) {
                tab.removeChild(uiGet("current" + tab_index));
                tabcontent.removeChild(uiGet("content" + tab_index));
            }
            tab_index--;
        }
    }
    //---------------------------------------------
    // Tab标签Id重新定义Id号
    //---------------------------------------------
    tab_index = 1
    for (i = 0; i < divtab.length; i++) {
        var temp = divtab[i].id;
        var num = temp.indexOf("current");
        if (num == 0) {
            divtab[i].id = "current" + tab_index;
            arrtab.push(new Array(divtab[i].id));
            tab_index++;
        }
    }
    //---------------------------------------------
    // Tab内容Id重新定义Id号
    //---------------------------------------------
    tab_index = 1
    for (i = 0; i < divtabcontent.length; i++) {
        var temp = divtabcontent[i].id;
        var num = temp.indexOf("content");
        if (num == 0) {
            divtabcontent[i].id = "content" + tab_index;
            tab_index++;
        }
    }
    //---------------------------------------------
    // 激活Tab 第1个
    //---------------------------------------------
    for (i = 0; i < arrtab.length; i++) {
        if (arrtab[i] != null) {
            if ((i + 1) == 1) {
                uiGet(arrtab[i]).className = "current";
                uiGet(arrtab[i]).onclick();
            }
        }
    }
}
