﻿(function ($) {
    $.jbecool = {};

    /*产生GUID*/
    $.jbecool.newGuid = function () {
        var guid = "";
        for (var i = 1; i <= 32; i++) {
            var n = Math.floor(Math.random() * 16.0).toString(16);
            guid += n;
            if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                guid += "-";
        }
        return guid;
    };

    /*获取一个指定长度随机数*/
    $.jbecool.random = function (len) {
        if (!len) len = 5;
        var r = Math.random().toString();
        return r.substr(r.length - len);
    };
    /*在指定Url后加入随机数，防止页面缓存*/
    $.jbecool.randomUrl = function (url) {
        if (url.indexOf("?") > 0)
            url += "&" + $.jbecool.random(10);
        else url += "?" + $.jbecool.random(10);
        return url;
    };

    /*q:参数名; url:可选*/
    $.jbecool.urlParam = function (q, url) {
        if (!url) url = window.location + '';
        else url += '';
        var reg = new RegExp("[?&](" + q + ")=([^&]+)", "i");
        var re = reg.exec(url);
        if (re) return unescape(re[2]);
        else return "";
    };
    $.jbecool.refresh = function () {
        document.location = document.location;
    };
    $.jbecool.go = function (url) {
        document.location = url;
    };
    //添加cookie
    $.jbecool.setCookie = function (name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        cookieVal = name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
        //alert(cookieVal);    
        document.cookie = cookieVal;
    };

    $.jbecool.getCookie = function (name) {
        var cs = document.cookie.split(";");
        for (var i = 0; i < cs.length; i++) {
            var c = cs[i].split("=");
            if (name == c[0])
                return unescape(c[1]);
        }
        return null;
    };
    $.jbecool.isNullOrUndefine = function (src) {
        return src == null || src == undefined;
    };
    $.jbecool.isNullOrEmpty = function (src) {
        if ($.jbecool.isNullOrUndefine(src) == true)
            return true;
        if ($.trim(src) == "")
            return true;
        return false;
    };
    $.jbecool.removeBeginWith = function (src, first) {
        if ($.jbecool.isNullOrEmpty(src) == true)
            return "";
        if ($.jbecool.isNullOrEmpty(first) == true)
            return src;
        if (src.indexOf(first) == 0)
            return src.substr(first.length);
        return src;
    }
    $.jbecool.removeEndWith = function (src, last) {
        if ($.jbecool.isNullOrEmpty(src) == true)
            return "";
        if ($.jbecool.isNullOrEmpty(last) == true)
            return src;
        if (src.lastIndexOf(last) == (src.length - last.length))
            return src.substr(0, src.length - last.length);
        return src;
    }
    //将HTML标签数据转换为对象，如attr = width:10,height:12,url:'xx.jpg'
    //字符串中不能出现逗号或者冒号（暂时）
    $.jbecool.attrToObject = function (attrValue) {
        if ($.jbecool.isNullOrEmpty(attrValue) == true)
            return null;

        //将逗号左边加引号，将冒号右边加引号，且将单引号变成双引号
        var tmp = "{\"" + attrValue.replace(/,/g, ",\"").replace(/:/g, "\":").replace(/'/g, "\"") + "}";
        return $.evalJSON(tmp);
    };

    /*核心：结果类*/
    $.jbecool.InterMsg = function () {
        this.result = {};
        this.isSuccess = true;
        this.errorMsg = "";
    };

    /*核心：条件类*/
    $.jbecool.Condition = function (fieldName, operation, value, type) {
        var c = {};
        c.FieldName = fieldName;
        c.Operation = operation;
        c.Value = value;
        c.Type = type ? type : "AND";
        return c;
    };
    $.jbecool.Condition.New = function (fieldName, operation, value, type) {
        return [new $.jbecool.Condition(fieldName, operation, value, type)];
    };
    $.jbecool.Condition.Add = function (conditions, fieldName, operation, value, type) {
        conditions.push(new $.jbecool.Condition(fieldName, operation, value, type));
        return conditions;
    };
    $.jbecool.Condition.And = function (conditions, fieldName, operation, value, type) {
        return conditions.push(new $.jbecool.Condition(fieldName, operation, value, "AND"));
    };
    $.jbecool.Condition.Or = function (conditions, fieldName, operation, value, type) {
        return conditions.push(new $.jbecool.Condition(fieldName, operation, value, "OR"));
    };

    /*核心：排序类*/
    $.jbecool.Orderby = function (fieldName, sortType) {
        var ob = {};
        ob.FieldName = fieldName;
        ob.SortType = sortType ? sortType : "ASC";
        return ob;
    };
    $.jbecool.Orderby.New = function (fieldName, sortType) {
        return [new $.jbecool.Orderby(fieldName, sortType)];
    };
    $.jbecool.Orderby.Add = function (orderbys, fieldName, sortType) {
        return orderbys.push(new $.jbecool.Orderby(fieldName, sortType));
    };

    $.jbecool.fillProperty = function (html, obj) {
        var pattern = /\{_obj.[A-Za-z0-9|\_]+\}/g;
        while ((arr = pattern.exec(html)) != null) {
            var attr = arr[0].substr(6);
            attr = $.jbecool.removeEndWith(attr, "}");
            if (obj.hasOwnProperty(attr))
                html = html.replace(arr[0], obj[attr]);
        }
        return html;
    };
})(jQuery);
