/**
 * [OElove] (C)2010-2099 oelove.com biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/05/20 by CL $
*/
$(function(){
    //返回上一页
    $(document).on("click", "[f='back']", function(){
        var $jump_url = $(this).attr("data-url");
        if (typeof($jump_url) == "undefined") {
            $jump_url = "";
        }
        if ($jump_url != "") {
            window.location.href = $jump_url;
        }
        else {
            window.history.go(-1);
        }
    });
    
    //跳转页
    $(document).on("click", "[f='jump']", function(){
        var $jump_url = $(this).attr("data-url");
        if (typeof($jump_url) == "undefined") {
            $jump_url = "";
        }
        if ($jump_url != "") {
            window.location.href = $jump_url;
        }
    });
    
    
    //搜索选中元素
    $(document).on("click", "[f='but_search_item']", function(){
        var $val = $(this).attr("data-val");
        var $input = $(this).attr("data-input");
        $(this).addClass("current").siblings().removeClass("current");
        $("#"+$input).val($val); 
    });
    
    
    //显示隐藏
    $(document).on("click", "[f='but_show_item']", function(){
        if ($(this).find(".icon-jiantouxiaxi").css("display") == "inline" || $(this).find(".icon-jiantouxiaxi").css("display") == "block" ) {
            $(this).find(".icon-jiantouxiaxi").hide();
            $(this).find(".icon-jiantoushangxi").show();
            $(this).parent().find(".item_hide").show();
        }
        else {
            $(this).find(".icon-jiantouxiaxi").show();
            $(this).find(".icon-jiantoushangxi").hide();
            $(this).parent().find(".item_hide").hide();
        }
    });
    
});

//禁止滚动条
function _hide_body_scroll() {
    $("body").css("overflow", "hidden");
    var scroll = $('#pull_box .scroll');
    if(scroll.length > 0) {
        scroll.css("overflow", "hidden");
    }
}
//启用滚动条
function _show_body_scroll() {
    $("body").css("overflow", "auto");
    var scroll = $('#pull_box .scroll');
    if(scroll.length > 0) {
        scroll.css("overflow", "auto");
    }
}

//同步七牛云
function _oeloveFileSynYun($id) {
	if ($id != "") {
		$.ajax({
			type: "POST",
			url:  _ROOT_PATH+"index.php?c=ajax",
			cache: false,
			data: {a:"synyun", id:$id},
			dataType: "json",
			success: function($yundata) {
				$yunjson = eval($yundata);
				$yunresponse = $yunjson.response;
				$yunresult = $yunjson.result;
				if ($yunresponse == "1") {

				}
				else {

				}
			},
			error: function() {
			}
		});
	}
}

//随机数
function get_rndnum(n) {
	var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
	var res = "";
	for(var i = 0; i < n ; i ++) {
		var id = Math.ceil(Math.random()*35);
		res += chars[id];
	}
	return res;
}

//判断字符长度，一个汉字为2个字符
function strlen(s){
	var l = 0;
    if (s != '') {
        var a = s.split("");
        for (var i=0;i<a.length;i++){
            if (a[i].charCodeAt(0)<299) {
                l++;
            } else {
                l+=2;
            }
        }
    }
	return l;
}

//判断字体个数
function strQuantity(s){
	var l = 0;
    if (s != '') {
        var a = s.split("");
        for (var i=0;i<a.length;i++) {
            if (a[i].charCodeAt(0)<299) {
                l++;
            } else {
                l++;
            }
        }
    }
	return l;
}

/**
* 判断是否为合法QQ号码 
* @param:: string qq QQ号码 5-11位数字
* @return:: bool true/false
*/
function isQQ(qq){
	if (qq.length < 5 || qq.length > 11) {
		return false;
	}
	else {
		var is = false;
		var reg = /^[0-9]\d{4,12}$/;
		if (reg.test(qq))is = true;

		if (!is) {
			return false;
		}
		else {
			return true;
		}
	}
}

//校验邮箱格式
function isEmail(str) {
    if (str != '') {
        var reg = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        
        if (reg.test(str)) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

//校验日期xxxx-xx-xx格式
function isDate(str) {
    if (str != '') {
        var reg = /^[\d]{4}-[\d]{2}-[\d]{2}$/;
        if (reg.test(str)) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

//预载图片
function preload_img() {
    var img = $("[preload='0']");
    if (typeof(img) == "undefined"){
        return;
    }
    var imgEl = [];
    $.each(img, function(i, v) {
        var $_src_img = $(img[i]).attr('data-src');
        if (typeof($_src_img) == "undefined") {
            $_src_img = "";
        }
        if ($_src_img.length > 0) {
            imgEl[i] = new Image();
            imgEl[i].onload = function(){
                $(img[i]).attr('src', $(img[i]).attr('data-src'));
                $(img[i]).attr("preload", "1");
            };  
            imgEl[i].src = $(img[i]).attr('data-src');
        }
    });
}

//清空cookies保存的新消息数字 全局用 0.04.01
function _clearMyNews() {
    $.ajax({
        type: "POST",
        url:  _ROOT_PATH+"index.php?m=wap",
        cache: false,
        data: {
            c:'ajax', a:"clearnews"
        },
        dataType: "json",
        success: function($data) {
            var $json = eval($data);
            var $response = $json.response;
            
        },
        error: function() {
        }
    });
}

//加载图集库
function _loadPhotoJs() {
    var $swipe_js = "<script src='"+_ROOT_PATH +"tpl/static/oeui/js/oeui.photoswipe.min.js?v="+VERSION+"'></script>"; 
    $("body").append($swipe_js);
}

//加载wxapi js 文件
function _loadWxApiJs() {
    var $wx_js = "<script src='"+_ROOT_PATH +"tpl/static/weixin/jweixin_api.js?v="+VERSION+"'></script>"; 
    $("body").append($wx_js);
}

//判断是否为iphone x系列
function isPhoneX() {
    let need_fit = 0;
    let isIphone = /iphone/gi.test(window.navigator.userAgent); //匹配是否为iphone
    let windowW = window.screen.width; //分辨率宽
    let windowH = window.screen.height; //分辨率高
    let pixelRatio = window.devicePixelRatio; //像素比
    let isIPhoneX = isIphone && pixelRatio && pixelRatio === 3 && windowW === 375 && windowH === 812;
    let isIPhoneXSMax = isIphone && pixelRatio && pixelRatio === 3 && windowW === 414 && windowH === 896;
    let isIPhoneXR = isIphone && pixelRatio && pixelRatio === 2 && windowW === 414 && windowH === 896;
    
    if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) {
        need_fit = 1;
    }
    return need_fit;
}

//set cookie 0.05.20
function _setCookie($name, $value, $days) {
    if (typeof($days) == "undefined") {
        $days = 30;
    }
    $exp = new Date();
    $exp.setTime($exp.getTime()+($days*24*60*60*1000));
    document.cookie = $name+"="+escape($value)+";expires="+$exp.toGMTString();
}
//set cookie 按分钟保存  0.05.20
function _setCookieMin($name, $value, $min) {
    if (typeof($min) == "undefined") {
        $min = 1; //默认1分钟....
    }
    $exp = new Date();
    $exp.setTime($exp.getTime()+($min*60*1000));
    document.cookie = $name+"="+escape($value)+";expires="+$exp.toGMTString();
}

//get cookie 0.05.20
function _getCookie($name) {
    var arr, reg = new RegExp("(^| )"+$name+"=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    }
    else {
        return "";
    }
}
//del cookie 0.05.20
function _delCookie($name) {
    $exp = new Date();
    $exp.setTime($exp.getTime() - 1);
    $cookie_val = _getCookie($name);
    if ($cookie_val != null) {
        document.cookie= $name+"="+$cookie_val+";expires="+$exp.toGMTString();
    }
}