
//获取cookie
function wecaller_getCookie(n){
	var a = document.cookie.match(new RegExp("(^| )" + n + "=([^;]*)(;|$)"));
	if (a != null) return a[2];
	return "";
}

//原网站用户id ---用户接口
var wecaller_id = wecaller_getCookie("wecaller_id");

//原网站用户名或昵称fullname ---用户接口
var wecaller_fn = wecaller_getCookie("wecaller_fn");


//防机器人编码
var wecaller_code = 632678691;

//wecaller所在URL
var wecaller_url = document.getElementsByTagName("script");
wecaller_url = wecaller_url[wecaller_url.length-1].src;

var wecaller_groupid = parseInt(wecaller_url.substring(wecaller_url.indexOf("g=") + 2)); //进入指定的客服组id
wecaller_url = wecaller_url.substring(0, wecaller_url.indexOf("wecaller.js"));

//判断是否为移动设备
var wecaller_mobile = navigator.userAgent.match(/(iPhone|Android|iPod|ios|iPad|Windows ce|Windows mobile|Micromessenger|webOS|Ucweb|UCBrowser|BlackBerry|midp|rv:1.2.3.4)/i);

var wecaller_link = "";

//监听加载样式
window.addEventListener("load", function(){

	//当前页面URL
	var url = window.btoa(window.location.href);

	//根据设备跳转的链接不同
	if(wecaller_mobile){ //mobile
		wecaller_link = "mobile/wecaller.php";
	}else{ //web
		wecaller_link = "wecaller.php";
	}

	wecaller_link = wecaller_url + wecaller_link + "?a=" + wecaller_code + "&group=" + wecaller_groupid + "&id=" + wecaller_id + "&fn=" + wecaller_fn + "&r=" + Math.random() + "&url=" + url;

	var wecaller_targets = document.getElementsByClassName("wecaller");

	for(var i = 0; i < wecaller_targets.length; i++) {

		wecaller_targets[i].onclick = function(e){
			e.preventDefault();

			var open_link = wecaller_link;
			var groupid = parseInt(this.getAttribute("group"));

			if(groupid){
				open_link = wecaller_link.replace(/&group=(\w*)&/g, "&group=" + groupid + "&");
			}

			window.open(open_link);
			return false;
		}
	}
});