/** 
    @ Name : Careff Javascript Library 
    @ Author : 
    @ Date : 2017-5-1
    @ Email : 
    @ Version : 1.0 
    @ Update : 
    功能： 开始建立自己的JS类库，需要按照功能进行分类；
     */
(function () {
    if (!window.usopen) {
        window.usopen = {};
    };
    window.usopen = {
        NAME: "usopen",
        VERSION: '1.0',
        DATE: '2017-12-16',
        INDEX: 10,


        /** 
             @ Name :判断是否是电脑浏览器 
             @ Update : 
             功能： 
             */
        UsSystemIsPC: function () {
            var userAgentInfo = navigator.userAgent;
            var Agents = ["Android", "iPhone",
                        "SymbianOS", "Windows Phone",
                        "iPad", "iPod"];
            var flag = true;
            for (var v = 0; v < Agents.length; v++) {
                if (userAgentInfo.indexOf(Agents[v]) > 0) {
                    flag = false;
                    break;
                }
            }
            return flag;
        }
        /** 
        @ Name :判断显示区域的宽度 
        @ Update : 
        功能： 
        */
            , UsSystemBodyWidth: function () {
                var width = $(document).width();
                return width;
            }
        /** 
        @ Name :判断显示区域的高度 
        @ Update : 
        功能： 
        */
             , UsSystemBodyHeight: function () {
                 var height = $(document).height();
                 return height;
             }
        /** 
        @ Name :获取编辑窗口的显示宽度； 
        @ Update : 
        功能：宽度支持自己输入和系统自动 支持w1,w2,w3,w4四种模式
        支持h1,h2,h3,h4四种高度模式，支持自己输入高度，自己输入高度，不需要带px；
        */
            , UsSystemGetOpenArea: function (wm, hm) {
                //h1,h2,h3,h4;窗口大小模式

                //#region 1.窗口高度

                var sheight = hm;
                var theight = 600;//显示窗口高度
                if (sheight == null) {
                    sheight = "h1";
                }
                sheight = sheight.toLowerCase();
                if (sheight == "h1") {
                    theight = 350;//超小
                }
                else if (sheight == "h2") {
                    theight = 550;//小
                }
                else if (sheight == "h3") {
                    theight = 700;//正常
                }
                else if (sheight == "h4") {
                    theight = 1000;//大
                } else if (sheight == "full") {
                    theight = "100000";
                }
                else {
                    theight = sheight;
                }

                //#endregion

                //#region 1.窗口宽度

                var swidth = wm;
                var twidth = 1020;//显示窗口高度
                if (swidth == null || swidth == "") {
                    swidth = "w3";
                }
                swidth = swidth.toLowerCase();
                if (swidth == "w1") {
                    twidth = 360;//超小
                }
                else if (swidth == "w2") {
                    twidth = 700;//小
                }
                else if (swidth == "w3") {
                    twidth = 1020;//正常
                }
                else if (swidth == "w4") {
                    twidth = 1300;//大
                }
                else if (swidth == "full") {
                    twidth = "100000";
                }
                else {
                    twidth = swidth;
                }

                //#endregion


                var isPC = usopen.UsSystemIsPC();
                var area = ['100%', '100%'];

                var height = usopen.UsSystemBodyHeight();
                var widthSys = usopen.UsSystemBodyWidth();
                //alert(widthSys +","+height)
                
                if (parseInt(theight) + 60 > height) {
                    theight = '100%';
                }
                else {
                    theight = theight + 'px';
                }


                if (parseInt(twidth) + 200 > widthSys) {
                    twidth = '100%';
                }
                else {
                    twidth = twidth + 'px';
                }
                if (!isPC) {
                    twidth = '100%';
                    theight = '100%';
                }
                area = [twidth, theight];
                //area = ['100%', '100%'];
                return area;
            }
        , UsGetParent: function () {
            //var lay = parent.parent.layui;
            //if (lay == null) {
            //    lay = parent.layui;
            //    if (lay == null) {
            //        lay = layui;
            //    }
            //}
            var lay = parent.layui;
            if (lay == null) {
                lay = layui;
            }
            return lay;
        }
        /** 
        @ Name :打开内部URL地址 
        @ Update : 
        功能： 
        */
             , UsSystemOpenURL: function (title, url, width, hm, cb) {
                 //layer = parent.layer === undefined ? layui.layer : parent.layer;
                 //alert(title);
                 //console.debug(usopen.UsSystemGetOpenArea(width, hm))
                 var pIndex = layui.layer.open({
                     title: title,
                     type: 2,
                     maxmin: true,
                     scrollbar: false,
                     area: usopen.UsSystemGetOpenArea(width, hm),
                     content: url,
                     cancel: function (index, layero) {
                         return usopen.UsCancelLayer(index, layero);
                     },
                     success: function (layero, index) {
                         //setTimeout(function () {
                         //    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                         //        tips: 2, time: 1000
                         //    });
                         //}, 1000)
                     }, end: function () {
                         
                         if (cb != null)
                         {
                             cb();
                         }
                     }
                 });
                 
                 return pIndex;
             }, UsSystemOpenURLParent: function (title, url, width, hm, cb) {
                 var lay = usopen.UsGetParent();
                 var pIndex = lay.layer.open({
                     title: title,
                     type: 2,
                     maxmin: true,
                     scrollbar: false,
                     area: usopen.UsSystemGetOpenArea(width, hm),
                     content: url,
                     cancel: function (index, layero) {
                         return usopen.UsCancelLayer(index, layero);
                     },
                     success: function (layero, index) {
                         //setTimeout(function () {
                         //    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                         //        tips: 2, time: 1000
                         //    });
                         //}, 1000)
                     }, end: function () {

                         if (cb != null) {
                             cb();
                         }
                     }
                 });

                 return pIndex;
             }

        , UsSystemOpenURLV2: function (title, url)
        {
            var pIndex = layui.layer.open({
                title: title,
                type: 2,
                maxmin: true,
                area: usopen.UsSystemGetOpenArea('full', 'full'),
                content: url,
                scrollbar: false,
                cancel: function (index, layero) {
                    return usopen.UsCancelLayer(index, layero);
                },
                success: function (layero, index) {
                     
                }, end: function () {

                   
                }
            });
            return pIndex;
           
        }
        , UsSystemOpenURLV3: function (url, cb) {
            //采用全屏的方式打开页面，必须有parent;
            var lay = usopen.UsGetParent();
            var pIndex = lay.layer.open({
                title: false,
                type: 2,
                closeBtn:0,
                maxmin: false,
                area: usopen.UsSystemGetOpenArea('full', 'full'),
                content: url,
                scrollbar: false,
                cancel: function (index, layero) {
                    return usopen.UsCancelLayer(index, layero);
                },
                success: function (layero, index) {

                }, end: function () {
                    if (cb != null) {
                        cb();
                    }
                }
            });
            return pIndex;

        }, UsSystemOpenURLV4: function (title, url, width, hm, usshowclose,cb) {
            //layer = parent.layer === undefined ? layui.layer : parent.layer;
            //alert(title);
            var maxmin = true;
            var closeBtn = 1;
            if (usshowclose == "0") {
                title = false;
                maxmin = false;
                closeBtn = 0;
            }
            var pIndex = layui.layer.open({
                title: title,
                type: 2,
                maxmin: maxmin,
                area: usopen.UsSystemGetOpenArea(width, hm),
                content: url,
                scrollbar: false,
                closeBtn: closeBtn,
                cancel: function (index, layero) {
                    return usopen.UsCancelLayer(index, layero);
                },
                success: function (layero, index) {
                    //setTimeout(function () {
                    //    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                    //        tips: 2, time: 1000
                    //    });
                    //}, 1000)
                }, end: function () {

                    if (cb != null) {
                        cb();
                    }
                }
            });
            return pIndex;
        }
        , UsCancelLayer: function (index, layero) {
            var breturn = true;
            if (typeof (usclosecjmsg) == "function") {
                var r = false;
                eval("r=usclosecjmsg()");
                //console.debug("r=" + r)
                if (r != "") {
                    usalert.UsConfirm(r, function (b) {
                        if (b) {
                            layer.close(index);
                        }
                        else {

                        }
                    });
                    return false;
                }
                else {
                    return breturn;
                }
            }
            return breturn;
        }
    };

})();