﻿function PanelClose(node) {node.parentNode.removeChild(node); }
function PanelShowHidden(node) {
    var button = node;
    node = node.parentNode.nextSibling;
    if (node.style.display == 'none') { node.style.display = ''; node.style.visibility = 'visible'; button.innerHTML = "_"; } else { node.style.display = 'none'; node.style.visibility = 'hidden'; button.innerHTML = "+"; }
}

(function() {
    if (!window.mofung) { window["mofung"] = function() { }; }

    var $ = function(id) {
        return document.getElementById(id);
    }
    mofung["$"] = $;

    function StringBuilder() {
        this.str = new Array();
    }
    StringBuilder.prototype.Append = function(txt) {
        this.str.push(txt);
        return this;
    }
    StringBuilder.prototype.toString = function() {
        if (arguments.length == 1)
            return this.str.join(arguments[0]);
        return this.str.join("");
    }
    mofung["StringBuilder"] = StringBuilder;

    var Event = {
        addEvent: function(obj, type, fn) {
            if (obj.attachEvent) {
                obj['e' + type + fn] = fn;
                obj[type + fn] = function() { obj['e' + type + fn](window.event); }
                obj.attachEvent('on' + type, obj[type + fn]);
            } else
                obj.addEventListener(type, fn, false);
        },
        removeEvent: function(obj, type, fn) {
            if (obj.detachEvent) {
                obj.detachEvent('on' + type, obj[type + fn]);
                obj[type + fn] = null;
            } else
                obj.removeEventListener(type, fn, false);
        }
    }
    mofung["Event"] = Event;

    var validate = {
        isEmpty: function(val) {
            return /^\W*$/.test(val);
        },
        isInt: function(val) {
            return /^\d+$/.test(val);
        },
        isValidStr: function(val) {
            return /^[-_\w]+$/.test(val);
        },
        isMoney: function(val) {
            return /^\d+\.\d+$/.test(val);
        },
        overBig: function(val, len) {
            return val.length > len;
        },
        overSmall: function(val, len) {
            return val.length < len;
        },
        isEmail: function(val) {
            return /^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/.test(val);
        }
    }
    mofung["Validate"] = validate;

    var SHBtn = function(id, forId) {
        var node = $(forId);
        Event.addEvent($(id), "click", function() { if (node.style.display == 'node') { node.style.display = ''; node.style.visibility = 'visible'; } else { node.style.display = 'none'; node.style.visibility = 'hidden'; } });
        if (arguments.length == 2) {
            if (arguments.length == "hidden") { node.style.display = "none"; node.style.visibility = 'hidden'; }
        }
    }
    mofung["SHBtn"] = SHBtn;

    if (!window.mofung.Controls) { window["mofung"]["Controls"] = function() { }; }

    var msgFlg = 0;
    function MsgBox() {
        this.Node = null;
    }
    MsgBox.prototype.Init = function() {
        id = "itemMsg" + msgFlg++;
        document.write(new mofung.StringBuilder().Append("<span id='").Append(id).Append("'></span>").toString());
        this.Node = mofung.$(id);
        this.Hidden();
        return this;
    }
    MsgBox.prototype.Show = function(msg) {
        with (this.Node) {
            style.color = "#999999";
            innerHTML = msg;
            style.visibility = "visible";
            style.display = "";
        }
    }
    MsgBox.prototype.ShowRight = function(msg) {
        this.Show(msg);
        this.Node.style.color = "green";
    }
    MsgBox.prototype.ShowError = function(msg) {
        this.Show(msg);
        this.Node.style.color = "red";
    }
    MsgBox.prototype.Hidden = function() {
        with (this.Node) {
            style.visibility = "hidden";
            style.display = "none";
        }
    }

    function ItemMsg() { this.Obj = null; this.Ref = null; this.Msg = new MsgBox(); }
    ItemMsg.prototype.Init = function(obj) {
        if (!obj) throw "对象不能为空！";
        this.Obj = obj;
        this.Ref = mofung.$(this.Obj.Ref);
        this.Msg.Init();
        this.Show(this.Obj.Msg);
        return this;
    }
    ItemMsg.prototype.Show = function(msg) { this.Msg.Show(msg); }
    ItemMsg.prototype.ShowError = function(msg) { this.Msg.ShowError(msg); }
    ItemMsg.prototype.ShowRight = function(msg) { this.Msg.ShowRight(msg); }
    ItemMsg.prototype.Validate = function() {
        if (!this.Obj) return;
        var val = this.Ref.value || this.Ref.innerText;
        for (var i = 0; i < this.Obj.validate.length; i++) {
            var o = this.Obj.validate[i];
            o["param"] = o.param || null;
            if (o.valErr) { if (o.func(val, o.param)) { this.ShowError(o.errMsg); return false; } }
            else { if (!o.func(val, o.param)) { this.ShowError(o.errMsg); return false; } }
        }
        this.ShowRight(this.Obj.rightMsg);
        return true;
    }
    mofung.Controls["ItemMsg"] = ItemMsg;

    function MsgBar() {
        this.Msg = new MsgBox();
    }
    MsgBar.prototype.Init = function() { this.Msg.Init(); return this; }
    MsgBar.prototype.ShowError = function(msg) { this.Msg.ShowError(msg); this.Msg.Node.className = "errMsg"; }
    MsgBar.prototype.ShowRight = function(msg) { this.Msg.ShowRight(msg); this.Msg.Node.className = "rightMsg"; }
    MsgBar.prototype.Hidden = function() { this.Msg.Hidden(); }
    mofung.Controls["MsgBar"] = MsgBar;

    function Panel() {
        this.bNode = null;
        this.tNode = null;
        this.cNode = null;
        this.panelHtml = null;
    }
    var panelFlg = 0;
    Panel.prototype.Init = function(obj, param) {
        var panelWidth = obj.width || "auto";
        var panelBorder = obj.border || "2px";
        var isRound = obj.isRound || "false";
        var panelTitleBackground = obj.background || "#077DF6";
        var panalContentBackground = obj.cBackground || "#fff";
        var hasClose = obj.hasClose || "false";
        var hasShowHidden = obj.hasShowHidden || "false";
        var hasCheckBtn = obj.hasCheckBtn || "false";
        var hasCancelBtn = obj.hasCancelBtn || "false";
        var checkCallback = obj.checkCallback || null;
        var cancelCallback = obj.cancelCallback || null;
        var isShow = obj.isShow || "false";
        var tColor = obj.tColor || "#fff";
        var iColor = obj.iColor || "#fff";

        var panelTileId = "panelTileId" + panelFlg++;
        var panelContentId = "panelContentId" + panelFlg++;
        var panelBoxId = "panelBoxId" + panelFlg++;

        var panelHtml = new mofung.StringBuilder().Append("<div id='").Append(panelBoxId).Append("' style='background:").Append(panelTitleBackground).Append(";width:").Append(panelWidth).Append("'>")
        //定义头部
        .Append("<div style='padding:3px 5px;color:").Append(tColor).Append(";height:20px;'><span  id='").Append(panelTileId).Append("' class='left' style='font-weight:bold;'></span>")
        //定义按钮
        .Append(function() { if (hasClose == "true") { return "<b class='right' style='cursor:pointer;border:1px solid " + iColor + ";height:15px;width:15px;line-height:100%;text-align:center;padding-top:2px;margin:1px;' onclick='PanelClose(this.parentNode.parentNode);" + (!obj.closeFunc ? "" : obj.closeFunc) + ";'>x</b>" } else { return "" } } ())
        .Append(function() { if (hasShowHidden == "true") { return "<b class='right' style='cursor:pointer;border:1px solid " + iColor + ";height:13px;width:15px;line-height:100%;text-align:center;padding:2px 0px;margin:1px;' onclick='PanelShowHidden(this);" + (!obj.showHiddenFunc ? "" : obj.showHiddenFunc) + "'>" + (function() { if (isShow == "false") { return "+"; } else { return "_" } } ()) + "</b>" } else { return "" } } ())
        .Append("</div>")
        //定义内容
        .Append("<div style='margin:0px ").Append(panelBorder).Append(";background:").Append(panalContentBackground).Append(";padding:3px;'id='").Append(panelContentId).Append("'><div></div><div style='text-align:center;'>")
        //定义按钮
        .Append(function() { if (hasCheckBtn == "true") { return "<input type='button' value='确 认' style='padding:1px 0px 1px 1px !important;line-height:100%;' onclick='PanelClose(this.parentNode.parentNode.parentNode);" + checkCallback + "'/>" } else { return ""; } } ())
        .Append(function() { if (hasCancelBtn == "true") { return "<input type='button' value='取 消' style='padding:1px 0px 1px 1px !important;line-height:100%;'  onclick='PanelClose(this.parentNode.parentNode.parentNode);" + cancelCallback + "'/>" } else { return ""; } } ())
        .Append("</div></div><div style='height:").Append(panelBorder).Append(";line-height:0px;font-size:0px;'></div>")
        .Append("</div>");

        var func = arguments.length == 3 ? arguments[2] : function(txt) { document.write(txt); }
        func(panelHtml.toString());

        this.bNode = mofung.$(panelBoxId);
        this.tNode = mofung.$(panelTileId);
        this.cNode = mofung.$(panelContentId);
        this.Show(param);
        if (isShow == "false") this.Hidden();
    }
    Panel.prototype.Show = function(param) {

        this.cNode.style.visibility = "visible";
        this.cNode.style.display = "";
        if (param) {
            this.tNode.innerHTML = param.title;
            this.cNode.firstChild.innerHTML = param.content;
        }

    }
    Panel.prototype.Hidden = function() {
        this.cNode.style.visibility = "hidden";
        this.cNode.style.display = "none";
    }
    Panel.prototype.Close = function() {
        this.bNode.parentNode.removeChild(this.bNode);
    }
    mofung.Controls["Panel"] = Panel;


    var PopBox =
    {
        virsion: "popWin v1.0",
        _filterDiv: null,
        _msgDiv: null,
        _msgBox: null,
        Init: function() {
            if (this._filterDiv != null) document.body.removeChild(this._filterDiv);
            if (this._msgDiv) document.body.removeChild(this._msgDiv);

            this._filterDiv = document.createElement("div");
            this._filterDiv.className = "filterDiv Hidden";
            this._msgDiv = document.createElement("div");
            this._msgDiv.className = "msgDiv Hidden";
            this._msgBox = document.createElement("div");
            this._msgBox.className = "msgBox";
            this._msgDiv.appendChild(this._msgBox);
            document.body.insertBefore(this._msgDiv, document.body.firstChild);
            document.body.insertBefore(this._filterDiv, document.body.firstChild);

            var msgBoxStyle = {}
            if (arguments.length == 1) msgBoxStyle = arguments[0];
            this._msgBox.style.width = msgBoxStyle["width"] || "288px";
            this._msgBox.style.background = msgBoxStyle["background"] || "none";
            return this;
        },
        Show: function(innerHTML) {
            this._filterDiv.className = "filterDiv Show";
            this._msgDiv.className = "msgDiv Show";
            this._msgBox.innerHTML = innerHTML;
        },
        Hidden: function() {
            this._filterDiv.className = "filterDiv Hidden";
            this._msgDiv.className = "msgDiv Hidden";
        }
    }
    mofung.Controls["PopBox"] = PopBox;

    var PopWin = {
        Init: function(obj, param) {

            PopBox.Init({ width: (obj["width"] || "400px") });

            obj["width"] = "auto";
            obj["isShow"] = "true";
            obj["hasClose"] = "true";
            obj["closeFunc"] = "mofung.Controls.PopBox.Hidden();";
            obj["hasShowHidden"] = "true";
            var msgPanel = new Panel().Init(obj,
            param, function(txt) { PopBox.Show(txt) });
            return this;
        },
        Close: function() { PopBox.Hidden(); }
    }
    mofung.Controls["PopWin"] = PopWin;



    var alert = function(msg) {
        PopBox.Init();
        var msgPanel = new Panel().Init({ isShow: "true", hasCheckBtn: "true", checkCallback: "mofung.Controls.PopBox.Hidden();", hasClose: "true", closeFunc: "mofung.Controls.PopBox.Hidden();" },
        { title: "消息提示", content: "<img src='http://passport.ghtml.net/App_Themes/Default/200792611269222.gif'/> " + msg }, function(txt) { PopBox.Show(txt) });
    }
    mofung["alert"] = alert;

    var confirm = function(msg) {
        PopBox.Init();
        var msgPanel = new Panel().Init({ isShow: "true", hasCheckBtn: "true", checkCallback: (arguments.length != 2 ? "" : (arguments[1] + ":")) + "mofung.Controls.PopBox.Hidden();",
            hasCancelBtn: "true", cancelCallback: (arguments.length != 3 ? "" : (arguments[2] + ":")) + "mofung.Controls.PopBox.Hidden();",
            hasClose: "true", closeFunc: "mofung.Controls.PopBox.Hidden();"
        },
        { title: "消息提示", content: "<img src='http://passport.ghtml.net/App_Themes/Default/200792611268482.gif'/> " + msg }, function(txt) { PopBox.Show(txt) });
    }
    mofung["confirm"] = confirm;



})();


