//取得url参数
SrcUrl = function(url) {
	this.url = url;
};
SrcUrl.prototype.getParameter = function(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(this.url);
	if(results === null) {
		return null;
	} else {
		return results[1];
	}
};

//window.onerror = function(){return true;}
var mgtOnline={
	showFloat:true //弹出窗口关闭口，是否出现浮动窗口。
}
var canLogin = false;
var scripts = document.getElementsByTagName('script');
var onlineAddress = "";
var globalCp = "";
var onlineWin = null;
var canScroll = false;
var common_is_ie = ((navigator.userAgent.toLowerCase()).indexOf("msie")!=-1 && document.all);

var companyId = "";
var useDefaulIcon = true;
var defaultDelay = 10;
var autoInvite = false;
var version = "2007818";

var selfIcon = "";
var inviteDelay = '1';
var isDebug = false;

var resource = {
	zh_CN : {
		onlineService : "在线客服",
		ignore : "忽略",
		accept : "接受",
		mainTitle : "今目标管理",
		inviteTitle : "欢迎您朋友",
		inviteWord  : "欢迎访问亿企通网站，请问有什么可以帮助您？",
		iconFile : "images/onlines/onlinefu.gif"
	},
	zh_TW : {
		onlineService : "在綫客服",
		ignore : "忽略",
		accept : "接受",
		mainTitle : "今目標管理",
		inviteTitle : "歡迎您朋友",
		inviteWord  : "歡迎訪問億企通網站，請問有什麽可以幫助您？",
		iconFile : "images/zh_TW/onlinefu.gif"
	},
	en_US : {
		onlineService : "",
		ignore : "",
		accept : "",
		mainTitle : "",
		inviteTitle : "",
		inviteWord  : "",
		iconFile : ""
	}
};
var positions = {
	topRight : {
		y : "top",
		x : "right"
	},
	topLeft : {
		y : "top",
		x : "left"
	},
	bottomRight : {
		y : "bottom",
		x : "right"
	},
	bottomLeft : {
		y : "bottom",
		x : "left"
	}
};
var language = resource.zh_CN;
mainTitle = language.mainTitle;
inviteTitle = language.inviteTitle;
inviteWord = language.inviteWord;
var position = positions.topRight;


//动态取得online的Address和globalCp以及document.domain
/************************************/
for(var i = 0; i < scripts.length; i++){
	var one = scripts[i].src+"";
	if( one.indexOf("online.js") != -1 ) {
		var lastIndex = one.indexOf("scripts/online/");
		onlineAddress = one.substring(0,lastIndex);
		var tempSplit = onlineAddress.split("/");
		var tempGlobalCp = ""
		for(var j = 0; j < tempSplit.length; j++){
			if( tempSplit[j] != "" ){
				tempGlobalCp = tempSplit[j];
			}
		}
		globalCp = "/"+tempGlobalCp;
		
		var srcUrl = new SrcUrl(one);
		companyId = srcUrl.getParameter("cid");
		useDefaulIcon = (srcUrl.getParameter("defalt") == 'true') ? true : false;
		defaultDelay = srcUrl.getParameter("invd");
		autoInvite = (srcUrl.getParameter("auti") == 'true') ? true : false;
		var p = srcUrl.getParameter("position");
		if (positions[p]) position = positions[p];
		var lng = srcUrl.getParameter("language");
		if (resource[lng]) language = resource[lng];
		version = srcUrl.getParameter("version");
		
		var _t1 = srcUrl.getParameter("showFloat");
		if(_t1 && _t1 =="false")
		    mgtOnline.showFloat = false;
		
		break;
	}
}
defaultDelay = 1;
inviteDelay = 1;
selfIcon = onlineAddress + language.iconFile;

if (common_is_ie && window.attachEvent) {
	window.attachEvent("onload", onloadOnline);
}else{
	window.addEventListener("load", onloadOnline, true);
}

function onloadOnline(){
	if( autoInvite ){
		setTimeout(function(){
			useAutoInvite();
		}, inviteDelay * 1000);
	}else{
		setTimeout(function(){
			noUseAutoInvite();
		}, defaultDelay * 1000);
	}
	loadCssFile(onlineAddress + "styles/online/online.css", "online_CSS");
	
	try{
		var tOnliceDiv = document.createElement("div");
		tOnliceDiv.innerHTML = "";
		tOnliceDiv.id = "onlineMain";
		tOnliceDiv.style.display = 'none';
		document.body.appendChild(tOnliceDiv);
		canScroll = true;
	}catch(ex){ }
}

/*
 * 自动邀请
 */
function useAutoInvite(){
	var onliceDiv = document.getElementById("onlineMain");
	onliceDiv.innerHTML = getInviteDiv();
	
	onliceDiv.style.position = "absolute";
	onliceDiv.style.width = screen.availWidth/2;
	onliceDiv.style.height = screen.availHeight/2;
	onliceDiv.style.left = top.document.body.offsetWidth / 3 + "px";
	onliceDiv.style.top = top.document.body.offsetHeight / 6 + "px";
	onliceDiv.style.display = 'block';
}

function getInviteDiv(){
	var divString = "";
	divString = '<div class="invite" id="inviteMain"><p class="ti">'+ mainTitle +'</p><div class="invite_te">' + inviteTitle +'</span><br><br>' + 
	'<span>' + inviteWord + '</span></div>' + 
	'<div class="invite_btn"><input type="button" class="send" value="' + language.ignore + '" onclick="cancelOnlice();">&nbsp;&nbsp;<input type="button" class="send" value="' + language.accept + '" onclick="goToOnlice();"></div>';
	'</div>';
	return divString;
}



/*
 * 不用自动邀请。 用悬挂的
 */
function noUseAutoInvite(){
	_onLoadGroup();
}

window.onscroll = scrollOnline

function scrollOnline(){
	if( canScroll ){
		var tOnliceDiv = document.getElementById("onlineMain");
		if( tOnliceDiv ){
			if (position.y == "top") tOnliceDiv.style[position.y] = (document.body.scrollTop + 100 + document.documentElement.scrollTop) + "px";
			if (position.y == "bottom") tOnliceDiv.style[position.y] = (document.body.scrollTop + 100 - document.documentElement.scrollTop) + "px";
		}
	}
	var imgDiv = document.getElementById("imgDiv");
	if( imgDiv ){
		if (position.y == "top") imgDiv.style[position.y] = (document.body.scrollTop + 90 + document.documentElement.scrollTop) + "px";
		if (position.y == "bottom") imgDiv.style[position.y] = (document.body.scrollTop + 70 - document.documentElement.scrollTop) + "px";
	}
}

function showImgDiv(list){
	var imgDiv = document.getElementById("imgDiv");
	if( !imgDiv ){
		var tDiv = document.createElement("div");
		tDiv.id = "imgDiv";
		tDiv.style.position  = "absolute";
		
		if (position.y == "top") tDiv.style[position.y] = (document.body.scrollTop + 90 + document.documentElement.scrollTop) + "px";
		if (position.y == "bottom") tDiv.style[position.y] = (document.body.scrollTop + 70 - document.documentElement.scrollTop) + "px";
		tDiv.style[position.x]  = "10px";
		var srcLocation = "";
		document.body.appendChild(tDiv);
		onLineCallback(list);
	}
}
function onLineCallback(list) {
	var grouphtml = "";
	var imgdiv =  document.getElementById('imgDiv');
	try{
		for(var i = 0; i<list.length; i++) {
			grouphtml += '<div style="padding: 3px 0 0; line-height: 1.8; cursor: pointer;" onclick="goToOnlice('+list[i].id+')">'+
							  	'<img border="0" align="left" src="http://img.jingoal.cn/portal_online/100429/online_group_ico.png" />'+list[i].name+
						'</div>';	
		}
		if(grouphtml != '') {
			imgdiv.style.fontSize="12px";
			imgdiv.style.lineHeight="1.4";
			imgdiv.style.width ="155px";
			var hint = 	'<div><img src="http://img.jingoal.cn/portal_online/100429/online_group_t.png" /></div>'+
					   	'<div style="background: url(http://img.jingoal.cn/portal_online/100429/online_group_m.png) repeat-y; padding: 2px 9px 2px 10px; text-indent: 0.5em;">'+
							grouphtml +
						'</div>'+
						'<div><img src="http://img.jingoal.cn/portal_online/100429/online_group_b.png" /></div>';
			imgdiv.innerHTML =hint; 
		}else {
			imgdiv.innerHTML ="<img src='"+selfIcon+"' onclick='goToOnlice();' style='cursor: pointer''>";
		}
	}catch(e){
		imgdiv.innerHTML ="<img src='"+selfIcon+"' onclick='goToOnlice();' style='cursor: pointer''>";
	}
}

/*
	取消在线客服功能
*/
function cancelOnlice() {
	var tOnliceDiv = document.getElementById("onlineMain");
	tOnliceDiv.style.display = "none";
	canScroll = false;
	if(mgtOnline.showFloat)
		_onLoadGroup();
}

/*
	接受在线客服功能
*/
function goToOnlice(groupId) {
	try{
		var parsGroupId = '';
		if(groupId && groupId >0)
			parsGroupId = '&gId='+groupId;
		var inviteMain = document.getElementById("onlineMain");
		if( inviteMain ){
			inviteMain.style.display = 'none';
		}
		if( typeof onlineWin == "undefined" || !onlineWin ){
		//	var sub = "width=344,height=232,top="+screen.height/3+",left="+screen.width/3+",toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no";
			var sub = "fullscreen=2,width=500,height=400,top="+screen.height/3+",left="+screen.width/3+",toolbar=0,location="+(common_is_ie?0:1)+",status=1,menubar=0,resizable=0,scrollbars=0";
			onlineWin = window.open(onlineAddress +"login/online/main.jsp?cid="+companyId+(isDebug ? "&imlog=123456" : "")+parsGroupId, language.onlineService, sub);
		}else{
			try{
				onlineWin.focus();
			}catch(ex){
				onlineWin = null;
				if(groupId){
					goToOnlice(groupId);
				}else {
					goToOnlice();
				}
				return;
			}
		}
		canScroll = false;
		if(mgtOnline.showFloat)
			_onLoadGroup();
	}catch(ex){
	}
}


function loadCssFile(url, uniqueId)
{
  if(document.getElementById(uniqueId)) return;
  if(/\w+\.\w+(\?|$)/.test(url))
  {
    var link  = document.createElement("LINK");
    link.href = url;
    link.id   = uniqueId;
    link.type = "text/css";
    link.rel  = "Stylesheet";
    document.getElementsByTagName("HEAD")[0].appendChild(link);
  }
};

function getWindowID () {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 16;
	var randomStr = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomStr += chars.substring(rnum,rnum+1);
	}
	return randomStr + new Date().getTime();
}
function _onLoadGroup() {
	var s=document.createElement('SCRIPT');
	s.src='http://web.jingoal.com'+globalCp+'/onlineGroupServlet.js?companyId='+companyId;
	document.body.appendChild(s);
}
window.onload = function(){
	_onLoadGroup();
}