/*
*   Declare & Winner2050
*/

function $get(id)
{
    return document.getElementById(id);
}

var TopWinCMS_JsControl;
if (!TopWinCMS_JsControl)
    TopWinCMS_JsControl = {};
if (!TopWinCMS_JsControl.TabControl)
    TopWinCMS_JsControl.TabControl = {};

/*
*定义 TabPages
*/
TopWinCMS_JsControl.TabControl.TabPages = function (id, frameId)
{
    this.element = $get(id);
    this.register = new Array();
    this.currentItem = null;
    this.TabFrame = $get(frameId);
    this.element.className = 'TabPages';

}
function ShowLoading(btid)
{
    var div = document.createElement("div");
    div.id = "TabLoading_" + btid;
    div.name = "TabLoading_" + btid;
    div.className = "TabloadDiv";
    div.innerHTML = "<img src=\"/Images/Ajax_Loading.gif\" />正在加载，请稍候！";
    $get("TabPageContent").appendChild(div);
}

function HiddenLoading(btid)
{
    var node
    if (GetBrowser() == "IE")
    {
        node = $get("TabLoading_" + btid);

        if (node)
            node.removeNode(true);
    }
    else
    {
        node = $get("TabLoading_" + btid);

        node.parentNode.removeChild(node);
    }
}

//创建新的Iframe
function CreateIframe(btid, canScoll, url)
{
    var iFrame = document.createElement("iframe");
    iFrame.id = "fID_" + btid;
    iFrame.name = "fName_" + btid;
    iFrame.src = url;
    iFrame.scrolling = canScoll;
    iFrame.className = "FrameWindow";

    iFrame.onload = iFrame.onreadystatechange = function ()
    {
        if (GetBrowser() != "IE" || iFrame.document.readyState == "complete")
        {
            IframeHeight(iFrame.id);
            HiddenLoading(btid);

            //解决IE6以上IE浏览器下内容页为非xhtml导致iFrame.style.width=0
            if ($get('TabPageContent').clientWidth < 1 && GetIframe(iFrame.id).body)
            {
                iFrame.style.width = document.frames[iFrame.id].document.body.scrollWidth + "px";
            }
        }
    }



    $get("TabPageContent").appendChild(iFrame);
    ShowLoading(btid);
}

//关闭框架
//function CloseIframe(btid)
//{ 
//    var node;
//    if (GetBrowser() == "IE")
//    {
//        node = $get("fID_" + btid);
//        node.removeNode(true);

//        CollectGarbage(); //清理浏览器内存 
//    }
//    else
//    {
//        node = $get("fID_" + btid);
//        node.parentNode.removeChild(node); 
//    }
//    HiddenLoading(btid);
//}

/*
设置iframe高度
*/
function IframeHeight(iFrameid)
{
    var h;

    var subWeb = GetIframe(iFrameid);
    if (subWeb != null)
    {
        if (subWeb.body == null)
            return;
        h = subWeb.body.scrollHeight;
    }


    if (h < 520)
    {
        h = 520;
    }

    $get(iFrameid).style.height = h + "px";
    $get('TabPageContent').style.height = h + "px";
}
function GetiFrameHref(btid)
{
    return document.frames["fID_" + btid].document.location.href;
}
function GetIframe(iFrameid)
{
    var frm = document.getElementById(iFrameid);
    return document.frames ? document.frames[iFrameid].document : frm.contentDocument;
}
/*
识别浏览器类型
*/
function GetBrowser()
{
    if (window.navigator.userAgent.indexOf("MSIE") >= 1)//如果浏览器为IE
    {
        return "IE";

    }
    else
    {
        if (window.navigator.userAgent.indexOf("Firefox") >= 1)
        {
            return "FF";
        }
        else
        {
            return ""
        }
    }
}
/*
添加Ｔａｂ控件控制栏按钮
*/
TopWinCMS_JsControl.TabControl.TabPages.prototype.addItem = function (item, canScoll, NewUrl)
{

    if (this.validateExist(item.id) == true)//如果按钮已经存在如何处理
    {
        //点击链接以后，如何已经打开过的选项卡就激活
        //
        i = this.register.length;
        for (k = 0; k < i; k++)
        {
            if (this.register[k].id != item.id)
                this.register[k].resetItem(); //初始化样式
            else
                this.register[k].activeItem(); //激活时样式
        }
    }
    else
    {
        item.panel = this;
        this.visitedRegister(item, "add");
        this.element.appendChild(item.element);

        CreateIframe(item.id, canScoll, item.url)
    }
}
/*
删除Ｔａｂ控件控制栏按钮
*/
TopWinCMS_JsControl.TabControl.TabPages.prototype.deleteItem = function (item)
{
    this.visitedRegister(item, "delete");
    this.element.removeChild(item.element);
}
/*
切换Ｔａｂ控件控制栏按钮
*/
var PageLoaded = "";
TopWinCMS_JsControl.TabControl.TabPages.prototype.changeSubPage = function ()
{
    var lists = $get('TabPageContent').getElementsByTagName('iframe');
    for (var i = 0; i < lists.length; i++)
    {
        if (lists[i].id == "fID_" + this.currentItem.id)
        {
            lists[i].style.display = "block"; //显示框架

            IframeHeight(lists[i].id);
        }
        else
        {
            lists[i].style.display = "none";
        }
    }
}
/*
检查Ｔａｂ控件控制栏按钮是否存在
*/
TopWinCMS_JsControl.TabControl.TabPages.prototype.validateExist = function (id)
{
    for (var i = 0; i < this.register.length; i++)
    {
        if (this.register[i].element.id == id)
        {
            return true;
        }
    }
    return false;
}
/*
*function:register visited sequence
*	     type--add | delete | active
*/
TopWinCMS_JsControl.TabControl.TabPages.prototype.visitedRegister = function (item, type)
{
    var i = this.register.length;
    switch (type)
    {
        case "add":
            for (var k = 0; k < i; k++)
            {
                this.register[k].resetItem();
            }
            this.currentItem = item;
            this.register[i] = item;
            this.currentItem.element.className = "active";
            this.changeSubPage();
            break;
        case "delete":
            for (k = 0; k < i; k++)
            {
                if (this.register[k] == item)
                {
                    this.register.splice(k, 1);
                    break;
                }
            }
            if (item == this.currentItem)
            {
                i = this.register.length;
                if (i > 0)
                {
                    this.currentItem = this.register[i - 1];
                    this.currentItem.element.className = "active";
                }
                else
                    this.currentItem = null;
            }
            this.changeSubPage();
            //CloseIframe(item.id);

            break;
        case "active":
            if (this.currentItem == item)
                return;
            else
            {
                for (k = 0; k < i; k++)
                {
                    if (this.register[k] == item)
                    {
                        this.register.splice(k, 1);
                        break;
                    }
                }
                i = this.register.length;
                for (k = 0; k < i; k++)
                {
                    this.register[k].resetItem();
                }
                this.currentItem = item;
                this.register.push(item);
            }
            this.changeSubPage();
            break;
    }

}

/*
－－－－－－－－－－－－－－－－－－－－
以下为 定义TabControl.TabPage的各种属性
－－－－－－－－－－－－－－－－－－－－
*/

/*
创建TabPage
*/
TopWinCMS_JsControl.TabControl.TabPage = function (id, command, title, url, flag)
{
    this.element = this.$C();
    this.panel = {};
    this.id = id + Math.random();
    this.command = command;
    this.title = title;
    this.url = url;
    this.flag = flag;
    this.init();
};
/*
*初始化Ｔａｂ控件按钮
*/
TopWinCMS_JsControl.TabControl.TabPage.prototype.init = function ()
{
    this.element.setAttribute('id', this.id);
    this.element.className = "default";
    var mirror = this;
    if (!this.flag)
    {
        //<span class='ico'>我</span>
        this.element.innerHTML = "<nobr class='title' title='" + this.title + "' id='nobr_" + this.id + "'>" + this.title + "</nobr>" + "<span class='close' title='关闭'></span>";
        this.addEventListener(this.$ES(this.element, 'nobr')[0], 'click', function () { mirror.activeItem(); });
        this.addEventListener(this.$ES(this.element, 'span')[0], 'click', function () { mirror.destroyItem(); });
        this.addEventListener(this.$ES(this.element, 'nobr')[0], 'dblclick', function () { mirror.destroyItem(); });
    }
    else
    {
        this.element.innerHTML = "<nobr class='title' title='" + this.title + "' id='nobr_" + this.command + "'>" + this.title + "</nobr>";
        this.addEventListener(this.$ES(this.element, 'nobr')[0], 'click', function () { mirror.activeItem(); });
    }
}
/*
激活Ｔａｂ控件控制栏按钮
*/
TopWinCMS_JsControl.TabControl.TabPage.prototype.activeItem = function ()
{
    this.panel.visitedRegister(this, "active");
    this.element.className = "active";
}
/*
初始化Ｔａｂ控件控制栏按钮样式
*/
TopWinCMS_JsControl.TabControl.TabPage.prototype.resetItem = function ()
{
    this.element.className = "default";
}
/*
关闭Ｔａｂ控件控制栏按钮时销毁相关的控件
*/
TopWinCMS_JsControl.TabControl.TabPage.prototype.destroyItem = function ()
{
    this.panel.deleteItem(this);
}
/*
Ｔａｂ控件按钮添加事件
*/
TopWinCMS_JsControl.TabControl.TabPage.prototype.addEventListener = function (element, type, handler)
{
    if (element.addEventListener)
        element.addEventListener(type, handler, true);
    else
        element.attachEvent("on" + type, handler, true);
}
/*
添加元素
*/
TopWinCMS_JsControl.TabControl.TabPage.prototype.$C = function (tag)
{
    if (tag && typeof tag == "string")
        return document.createElement(tag);
    else
        return document.createElement('li');
}
/*
取得元素
*/
TopWinCMS_JsControl.TabControl.TabPage.prototype.$ES = function (element, tag)
{
    return element.getElementsByTagName(tag);
}

/*
添加选项卡
*/
function AddTabPage(command, title, url, canScoll)
{
    if (canScoll == null)
        canScoll = "no";

    TabPages.addItem(new TopWinCMS_JsControl.TabControl.TabPage(command, command, title, url, false), canScoll, url);
    return false;
}
//****************供外部使用**************************
function NewTabPage(command, title, url, canScoll)
{

    TabPages.addItem(new TopWinCMS_JsControl.TabControl.TabPage(command, command, title, url, false), canScoll, url);

    return false;
}
TopWinCMS_JsControl.TabControl.TabPages.prototype.DeleteTagPage = function (id)
{
    var mirror = null;
    for (var i = 0; i < this.register.length; i++)
    {
        if (this.register[i].element.id == id)
        {
            mirror = this.register[i];
        }
    }
    mirror.destroyItem();
    return false;
}
function CloseTagPage(id)
{
    return TabPages.DeleteTagPage(id);
}