if (!window.Fetion)
    window.Fetion = { "Web": { "Tool": {} } };
window.FetionWebTool = Fetion.Web.Tool;
Fetion.Web.Tool.msgList = [];
Fetion.Web.Tool.ctrlList = [];
Fetion.Web.Tool.ctrlQueue = [];
Fetion.Web.Tool.clsErrList = {};
Fetion.Web.Tool.JSPath = "script";
Fetion.Web.Tool.defined = function (name)
{
    var className = name;
    var nameAry = name.split(".");
    var cls = window;
    for (var i = 0; i < nameAry.length; i++)
    {
        name = nameAry[i];
        if (!cls[name])
            cls[name] = {};
        cls = cls[name];
    }
    cls.className = className;
    Fetion.Web.Tool[name] = Fetion.Web.Tool[name.toLowerCase()] = Fetion.Web.Tool[name.toUpperCase()] = cls;
    window[className.replace(/\./g, "")] = cls;
    return cls;
};
Fetion.Web.Tool.newCtrl = function (o)
{
    if (o.constructor == String)
    {
	o = { type: o };
	}
    o.src = this.JSPath + "/" + o.type + ".js";
    this.ctrlQueue.push(o);
    if (!this[o.type]){
        this.loadSRC(o);
		}
    return this.createCtrl(o);
};
Fetion.Web.Tool.loadSRC = function (o)
{
    if (o.constructor == String)
        o = o.indexOf(".") == "-1" ? { type: o } : { src: o };
    if (!o.src)
        o.src = this.JSPath + "/" + o.type + ".js";

    if (!window.srcMap)
        window.srcMap = {};
    if (!srcMap[o.src])
    {
        srcMap[o.src] = o;
        var src = null;
        if (o.src.indexOf(".js") != -1)
        {
            src = document.createElement("script");
            src.setAttribute("type", "text/javascript");
            src.setAttribute("src", o.src);
        }
        else if (o.src.indexOf(".css") != -1)
        {
            src = document.createElement("link");
            src.setAttribute("rel", "stylesheet");
            src.setAttribute("type", "text/css");
            src.setAttribute("href", o.src);
        }
        if (src)
        {
            var head = document.getElementsByTagName("head")[0];
            head.appendChild(src);
        }
    }
    if (o.src.indexOf(".js") != -1 && !o.type)
        o.type = o.src.substring(o.src.replace(/\\/g, "/").lastIndexOf("/") + 1, o.src.lastIndexOf(".")).toLowerCase();
};
Fetion.Web.Tool.createCtrl = function (o)
{
    var baseCls = "cardfrm";
    var cls = this.ctrlQueue[0] || baseCls.indexOf(o.type) != -1 ? Fetion.Web.Tool[o.type] : null;
    var obj = null;
    try
    {
        obj = cls.create(o.sub || o);
    }
    catch (e)
    {
        //msgDIV.innerHTML = e.message;
        setTimeout(function () { Fetion.Web.Tool.createCtrl(o) });
        return null;
    }
    if (this.ctrlQueue[0])
        this.ctrlQueue.shift();
    this.clsErrList[o.type] = null;

    if (!o.id)
        o.id = this.newID(o.type);
    obj.cls = cls;
    obj.data = o;
    obj.id = Fetion.Web.Tool.formatID(o.id);
    obj.setAttribute("name", obj.id);
    obj.type = o.type;
    obj.setAttribute("type", o.type);
    obj.lang = '{"type":"' + o.type + '"}';
    if (o.style)
        this.setStyle(obj, o.style);

    Fetion.Web.Tool.scroll();

    obj.onmousedown = function ()
    {
        window.selMsgCtrlMousedown = true;
        window.selObj = null;
        window.imgSrc = null;
        window.selMsgCtrl = this;
        window.pid = this.data.id;
        var rect = this.getBoundingClientRect();
        if (rect.bottom - rect.top > document.documentElement.clientHeight)
            return;
        var scrollBar = Fetion.Web.Tool.body.scrollBar;
        if (scrollBar)
        {
            var tValue = rect.top;
            var bValue = rect.bottom - document.documentElement.clientHeight;
            if (tValue < 0 || tValue < bValue)
                scrollBar.setScrollBar(Fetion.Web.Tool.body.scrollTop + tValue);
            else if (bValue > 0 && tValue > bValue)
                scrollBar.setScrollBar(Fetion.Web.Tool.body.scrollTop + bValue);
        }
    }

    if (false && cls.update)
        setTimeout(function () { cls.update(obj, obj.data); }, 1000);
    return obj
};

Fetion.Web.Tool.newID = function (name)
{
    while (true)
    {
        var tmpName = name + "_" + parseInt(Math.random() * 1000);
        var obj = window[tmpName];
        if (!obj)
            return tmpName;
    }
};

Fetion.Web.Tool.formatID = function (id)
{
    return ("_" + id).replace(/\W/, "_");
};
Fetion.Web.Tool.scroll = function ()
{
    if (!this.body){	
        return;}
    var scrollBar = this.body.scrollBar;
    if (!scrollBar)
    {
        this.body.scrollBar = {};
        this.newCtrl({ type: "scrollbar", parent: this.body });
        return;
    }
    if (scrollBar.setScrollBar)
    {
        var scrollTop = null;
        if (Fetion.Web.Tool.scrollHeight){
            scrollTop = this.body.scrollHeight - Fetion.Web.Tool.scrollHeight;
		}
        scrollBar.setScrollBar(scrollTop);
    }
}
Fetion.Web.Tool.body = window.divAllAppList;
Fetion.Web.Tool.scroll();
document.body.onload = function ()
{
    document.body.style.height =  Fetion.Web.Tool.body.style.height = document.documentElement.clientHeight;
};