﻿/// <reference path="GmrJsLib.js" />

/// <reference path="XmlApi.js" />

/// <reference path="GmrMotion.js" />
/// Gmr插件集

(function ()
{
    if (Gmr && Gmr.Plugins) { return; }
    Gmr.Plugins = new Object();
    Gmr.Plugins.TextBoxSelect = window.gmrTextBoxSelect = function (initConfig)
    {
        /// <summary>文本框下拉框插件,给文本框生成一个智能下拉选择框</summary>
        /// <param name="initConfig">初始配置参数</param>
        ///#region
        var config = { isAutoInit: true, //是否自动初始化
            cssName: gmrGetProperty(initConfig, "cssName", ""), //容器默认附加类名
            readOnly: gmrGetProperty(initConfig, "readOnly", true)//是否允许文本框输入
        };
        var selectObj = $(initConfig.select);
        var tboxObj = $(initConfig.textbox);
        tboxObj.readOnly = config.readOnly;
        selectObj.style.display = "none";
        if (tboxObj)
        {
            if (tboxObj.gmrDivSelect)//如果已经显示就不要重复显示了
            {
                return;
            }
            var css_divItem = "";
            var css_divItemHover = "";
            var pDiv = document.createElement("div"); //下拉框容器
            pDiv.className = "divSelect" + (config.cssName.length > 0 ? (" " + config.cssName) : "");
            pDiv.style.width = tboxObj.offsetWidth + "px";
            pDiv.style.top = Gmr.Style.GetObjTopY(tboxObj) + tboxObj.offsetHeight + "px";
            pDiv.style.left = Gmr.Style.GetObjLeftX(tboxObj) + "px";

            pDiv.onmouseover = function () { this.ishover = true; }
            pDiv.onmouseout = function () { this.ishover = false; tboxObj.focus(); }

            for (var i = 0; i < selectObj.options.length; i++)
            {
                var op = selectObj.options[i];
                var cDiv = document.createElement("div");
                cDiv.innerHTML = op.text;
                cDiv.v = op.value;
                cDiv.index = i;
                cDiv.className = "divOption";

                cDiv.onmouseover = function ()
                {
                    this.className = "divOption divOptionHover";
                    pDiv.ishover = true;
                }
                cDiv.onmouseout = function ()
                {
                    this.className = "divOption";
                    pDiv.ishover = false;
                }

                cDiv.onclick = function ()
                {
                    /// <summary>子项点击</summary>            
                    tboxObj.value = this.innerHTML;
                    tboxObj.selectedValue = this.v;
                    selectObj.options[this.index].selected = true;

                    pDiv.style.display = "none";
                    pDiv.ishover = false;
                    selectObj.fireEvent("onchange");
                    tboxObj.focus();
                    tboxObj.select();
                }
                pDiv.appendChild(cDiv);
            }
            //gmrGetBodyDocumentElement(window).appendChild(pDiv);
            document.body.appendChild(pDiv);
            tboxObj.isShowSelect = true;
            tboxObj.gmrDivSelect = pDiv;
            tboxObj.ondblclick = function () { tboxObj.select(); }
            SysAddEvent(tboxObj, "onclick", function () { pDiv.style.display = "block"; });
            SysAddEvent(tboxObj, "onblur", function ()
            {
                /// <summary>文本框失去焦点</summary>        
                if (pDiv.ishover)
                {
                    return;
                }
                if (pDiv.style.display == "none") { return; }
                tboxObj.value = selectObj.options[selectObj.selectedIndex].text;
                tboxObj.selectedValue = selectObj.options[selectObj.selectedIndex].value;
                selectObj.fireEvent("onchange");
                setTimeout(function () { pDiv.style.display = "none"; }, 100);


            });
            tboxObj.onkeyup = function ()
            {
                /// <summary>鼠标弹起,进行搜索,自动匹配选项中符合条件的记录</summary>
                //if (tboxObj.value == '') { return; }
                pDiv.style.display = "block";

                var cDivs = pDiv.getElementsByTagName("div");
                for (var i = 0; i < cDivs.length; i++)
                {
                    cDivs.className = css_divItem
                }
                var strTexts = tboxObj.value.split(" ");
                var strReg;
                for (var i = 0; i < cDivs.length; i++)
                {
                    var odiv = cDivs[i];
                    var strText = odiv.innerHTML;
                    var ismatch = true;
                    for (var j = 0; j < strTexts.length; j++)
                    {
                        if (strTexts[j] && strTexts[j].length > 0)
                        {

                            if (strText.indexOf(strTexts[j]) >= 0)
                            {
                                continue;
                            }
                            else
                            {
                                ismatch = false;
                                break;
                            }
                        }
                    }
                    if (ismatch)
                    {
                        cDivs[i].style.display = "block";
                    }
                    else
                    {
                        cDivs[i].style.display = "none";
                    }
                }

            }
            if (config.isAutoInit)
            {
                if (selectObj.selectedIndex >= 0)
                {
                    var opt = selectObj.options[selectObj.selectedIndex];
                    tboxObj.value = opt.text;
                    tboxObj.v = opt.value;
                }
            }

        }
        ///#endregion 
    }
    Gmr.Plugins.Tab = window.gmrTab = function ()
    {
        /// <summary>选项卡插件</summary>
        return {
            init: function (tabObj)
            {
                /// <summary>初始化一个选项卡控件</summary>
                /// <param name="tabObj">要操作的对象</param>
                //#region 
                var tab = $(tabObj); if (!tab || tab.hasTab) { return; }
                var titles = $C("divTabTitleItem", tab, "div");
                var contents = $C("divTabContentItem", tab, "div");
                var isActived = false;
                gmrBatch(titles, function (obj, index)
                {
                    obj.onmouseover = function ()
                    {
                        if (gmrCheckCssName(this, "active")) { return; }
                        gmrBatch(titles, function (o, i)
                        {
                            if (i != index)
                            {
                                o.className = o.className.replace(/ active|active /, "");
                            }
                            else
                            {
                                if (!gmrCheckCssName(o, "active"))
                                {
                                    o.className += " active";
                                }
                            }
                        });
                        gmrBatch(contents, function (c, index2)
                        {
                            if (index != index2) { c.style.display = "none"; }
                            else { c.style.display = "block"; }
                        });
                    }
                    if (gmrCheckCssName(obj, "active"))
                    {
                        contents[index].css("display", "block");
                        isActived = true;
                    }
                });
                if (!isActived)
                {
                    titles[0].className += " active";
                    contents[0].css("display", "block");
                }
                tab.hasTab = true;
                //#endregion 

            },
            initDefault: function ()
            {
                /// <summary>执行默认初始化</summary>
                gmrBatch($C("divTab"), function (obj, index)
                {
                    gmrTab.init(obj);
                });
            }
        }
    } ();
    SysAddEvent(window, "onload", function () { gmrTab.initDefault(); });
    Gmr.Plugins.ContextMenu = window.gmrContextMenu = function ()
    {
        var curMenu = null; //当前活动菜单
        var evt = null; //事件对象,兼容FF处理
        SysAddEvent(document, "onclick", function () { gmrContextMenu.hideMenu(curMenu); });
        SysAddEvent(document, "oncontextmenu", function (evt)
        {
            if (!curMenu) { return; }
            var src = gmrGetEventSrc(evt);
            if (src == curMenu.config.src) { return; }
            gmrContextMenu.hideMenu(curMenu);
        });
        return {
            setMenu: function (initConfig)
            {
                var thisObj = this;
                var menu = new Object();
                var config = { src: null, //要绑定的事件源
                    menuList: []//菜单内容对象
                }
                config.src = (initConfig.src);
                config.menuList = initConfig.menuList;
                var doc = window.top.document;
                var dCanv = doc.createElement("div");
                dCanv.className = "gmrContextMenu";
                window.top.document.body.appendChild(dCanv);
                dCanv.innerHTML = "";
                var ul = doc.createElement("ul");
                gmrBatch(config.menuList, function (o, index)
                {
                    var li = doc.createElement("li");
                    li.innerHTML = "<a href='#none'>" + o.text + "</a>";
                    li.onmouseover = function () { this.className = "hover"; }
                    li.onmouseout = function () { this.className = ""; }
                    $T("a", li)[0].onclick = function ()
                    {
                        gmrContextMenu.hideMenu();
                        if (o.onclick) { o.onclick(this); }
                        return false;
                    }

                    ul.appendChild(li);
                });
                dCanv.appendChild(ul);
                config.src.oncontextmenu = function (e)
                {
                    evt = e;
                    gmrContextMenu.showMenu(menu);
                    return false;
                }
                /*设置菜单变量*/
                menu.canv = dCanv;
                menu.config = config;
                return menu;
            },
            showMenu: function (menu)
            {
                /// <summary>显示菜单</summary>
                if (curMenu) { gmrContextMenu.hideMenu(curMenu); }
                var mxy = gmrPub.getMousePosition();
                menu.canv.style.left = mxy.x + "px";
                menu.canv.style.top = mxy.y + "px";
                menu.canv.style.display = "block";
                gmrSys.cancelBubble();
                //event.returnValue = false;
                curMenu = menu;
            },
            hideMenu: function (menu)
            {
                /// <summary>隐藏菜单</summary>
                var menu = menu ? menu : curMenu;
                if (menu)
                {
                    menu.canv.style.display = "none";
                    curMenu = null;
                }
            }
        }
    } ();
})();
