var webimUserId;
if( document.all ) {
	webimUserId = "cred2127";
} else {
	webimUserId = "cred2127";
}
//亮灯聊天用户
var sellTalkUserId = "cntaobao" + webimUserId;
var needFenliu = true;
var encodeWebimUserId = "cred2127";
var webimSiteId = "cntaobao";
var webimStaticUrl="http://www.im.alisoft.com/onlineservice/0000/00/67/55";
var showLightType = 1;
var webWwUrl = "http://onlineww.im.alisoft.com:8280/wangwang/webim.jsp";

//用户生成代码的标志位
var createCodeFlag = 5;
var newFromSite = "";
if (typeof(fromsite)   !=   "undefined") {
	newFromSite = fromsite;
	if(fromsite == "cntaobao" || fromsite=="cnalichn") {
		createCodeFlag = createCodeFlag & 4;
	}
}
//主动营销的配置
var arrSellConfig = new Array(2,'0','您好!欢迎光临本店，请问有什么可以为您服务的吗?',20,'',parseInt('3'),parseInt('2'),'1',newFromSite);

//判断cookie中是否有用户Id
var t_webim_user_id;
var OnLine = -1 ;
var loadSingleLight=0;
var loadSell = 0;
var loadCaller = 0;

function getWebimUserIdFromServer() {
	document.write("<scr" + "ipt language='javascript' src='http://web3.im.alisoft.com/monitor/getuid.do?' charset='GB2312'></scr" + "ipt>");
}
//取得用户的状态
function loadUserOnlineStatus() {
	document.write("<script language=javascript src='http://amos.im.alisoft.com/userstatus3.aw?uid=" + encodeWebimUserId + "&site=" + webimSiteId + "' charset='GB2312'></script>");
	if ( (createCodeFlag & 2) == 2 ) {
		document.write("<script language=javascript src='http://10.0.64.208/mullidstatus.aw?beginnum=0&uids=" + multiLampUser +  "' charset='GB2312'></script>");
	}
}

//导入一些必须的文件
function onlineInit() {
	document.write("<script language=javascript src=http://www.im.alisoft.com/webim/online/js/util.js charset='GB2312'></script>");
	document.write("<script language=javascript src=http://www.im.alisoft.com/webim/online/js/commonDeal.js charset='GB2312'></script>");
	document.write("<script language=javascript src=http://www.im.alisoft.com/onlineservice/common/tqDragAndCommon.js></script>");
	//cookie操作
	document.write("<script language=javascript src=http://www.im.alisoft.com/onlineservice/common/cookieUtil.js charset='GB2312'></script>");
	//取得用户ID
	document.write("<script language=javascript src=http://www.im.alisoft.com/webim/online/js/webimUserId.js charset='GB2312'></script>");
	if ( (createCodeFlag & 1) == 1 ) { 
    	//单个亮灯的处理
    	document.write("<script language=javascript src=http://www.im.alisoft.com/webim/online/js/singleLamp.js charset='GB2312'></script>");
	}
	if ( (createCodeFlag & 2) == 2 ) {
		//多个亮灯的处理
		document.write("<script language=javascript src=http://www.im.alisoft.com/webim/online/js/multiLamp.js charset='GB2312'></script>");
	}
	if ( (createCodeFlag & 4) == 4 ) { 
		document.write("<script language=javascript src=http://www.im.alisoft.com/webim/online/js/sale.js charset='GB2312'></script>");
	}
	if ( (createCodeFlag & 8) == 8 ) { 
		document.write("<script language=javascript src=http://www.im.alisoft.com/webim/online/js/Callers.js charset='GB2312'></script>");
		document.write("<iframe id='sendMsgFrame' name='sendMsgFrame' height='0' width='0' ></iframe>");
		document.write('<DIV id="tracqfloater1" style="Z-INDEX: 9999; LEFT: 320px; VISIBILITY: hidden; WIDTH: 444px; POSITION: absolute; TOP: 200px; HEIGHT: 185px" name="tracqfloater1">');
		document.write('</DIV>');
	}
	
	//加载用户的亮灯状态
	loadUserOnlineStatus();
	
	document.write("<script language=javascript src='http://www.im.alisoft.com/webim/js/webim.js'  charset='utf-8'></script>");
	document.write('<iframe name="aliwangwangSendmsgShowFrame" id="showFrame" width=0 height=0 src="about:blank"></iframe>');
	
	
	//以下是一些需要用来做对话设置的一些参数，接受聊天，拒绝聊天等等。
	document.write("<script language=JavaScript> var strAgent='abcd';		  var strVisit='';var strLastVisit='';var strSearchWord='';var strLanguage='';				  var strIeCopyRight='';var strSysCopyRight='';var lan=1;</script>");
	//生成一个随机数变量
	document.write("<script language=JavaScript>var pageRandomId = Math.floor(Math.random()*100000);</script>");
	document.write("<iframe width=0 height=0 name=callUrlInnerFrame id=callUrlInnerFrame frameborder=0 src='about:blank'></iframe>");
	document.write("<iframe width=0 height=0 name=clientScript frameborder=0 src='about:blank'></iframe>");
	
}  
onlineInit();


//多个亮灯的处理
//访客接待的处理
//聊天记录查询的处理
//主动营销的处理
var webimTatalTime = 0;
//*****************************单个亮灯 start*************************************************
//漂浮对象的参数【单个亮灯的】
var floatSingleLamp;
function dealSingleLight() {
	if (loadSingleLight == 0) {
		//alert("loadSingleLight=" + loadSingleLight);
		setTimeout("dealSingleLight()",200);
	} else {
		singleLamp.init();
		floatSingleLamp = new floatObj(singleLamp.mainDiv);
		window.setInterval("s_dealFloat(floatSingleLamp)",40);
		setTimeout("setWebimUserPic()",200);
	}
}
//alert("d=" + (createCodeFlag & 1));
//如果开通了单个亮灯
if ( (createCodeFlag & 1) == 1 ) { 
	setTimeout("dealSingleLight()",100);
}
//******************************单个亮灯 end******************************************

//主动营销
function dealSells() {
	if (loadSell == 0) {
		setTimeout("dealSells()",200);
	} else {
		var marketingConfig = new MarketingConfig(arrSellConfig[0],arrSellConfig[1],arrSellConfig[2],arrSellConfig[3],arrSellConfig[4],arrSellConfig[5],arrSellConfig[6],arrSellConfig[7],arrSellConfig[8]);
		OnlineSale.setConfig(marketingConfig);
		if(!OnlineSale.isShow){
			OnlineSale.start();
		}
	}
}

//*****************************用户只设置的主动营销没有在页面上面显示多亮灯******************


//*****************************公用的处理 start *******************************
/**
 * 判断用户状态，显示相应的亮灯图片
 */
function setWebimUserPic() {
	webimTatalTime++;
	if (OnLine == -1) {
		if (webimTatalTime < 8){
			setTimeout("setWebimUserPic()",500);
		}
	} else {
		//如果单个亮灯加载经来了而且用户开通了当个亮灯
		if (loadSingleLight == 1 && ((createCodeFlag & 1) == 1)) {
			singleLamp.changeStatus(OnLine);
		}
		//如果用户在线而且用户开通了主动营销
		if (OnLine == 1 && ((createCodeFlag & 4) == 4)) {
			//页面上面也不显示多亮灯
			if ((createCodeFlag & 2) == 0 ) {
				//如果用户是多亮灯用户
				if ((showLightType & 2) == 2) {
					needFenliu = false;
				} else {
					needFenliu = true;
				}
				dealSells();
			}
			
		}
	}
}
setTimeout("setWebimUserPic()",200);

//主动营销处理
function dealMultiLamp() {
	if(online[0] != -1) {
		var needShowSale = false;
		var index 
		for ( index = 0 ;index < online.length; index++) {
			if (online[index] == 1) {
				needShowSale = true;
				break;
			}
		}
		if (needShowSale) {
			var userlist = multiLampUser.split(";");
			sellTalkUserId = userlist[index];
			needFenliu = false;
			dealSells();
		}
	} else {
		setTimeout("dealMultiLamp()",200)
	}
}
//只有用户需要创建多亮灯的时候才处理，避免在淘宝上，但是是需要显示多亮灯的情况
if((createCodeFlag & 2) == 2 && (createCodeFlag & 4) == 4){
	setTimeout("dealMultiLamp()",200);
}

//*****************************公用的处理 end *******************************

/******************************访客接待的处理*********************************/
function dealCaller() {
	if (loadCaller == 0) {
		setTimeout("dealCaller()",200);
	} else {
		var tmpCallerConfig = new CallerConfig(arrCallConfig[0],arrCallConfig[1],arrCallConfig[2],arrCallConfig[3],t_webim_user_id,arrCallConfig[5],arrCallConfig[6]);
		caller.setConfig(tmpCallerConfig);
		caller.init();
	}
}
/**
 * 设置帐号的ID
 **/
function changeTmpUserId() {
	//如果还没有取到用户ＩＤ
	if(!t_webim_user_id) {
		setTimeout("changeTmpUserId()", 200);	
	} else {
		caller.tWebimId = t_webim_user_id;
	}
}

//如果开通了访客接待
if ( (createCodeFlag & 8) == 8 ) { 
	setTimeout("dealCaller()",300);
}

/******************************访客接待的处理 end*********************************/
/******************************多个亮灯的处理 start *********************************/
//漂浮对象的参数【多个亮灯的】
var floatMultiLamp;
function dealMultiLight() {
	if (loadMultiLight == 0) {
		setTimeout("dealMultiLight()",200);
	} else {
		var multiLampConfig = new MultiLampConfig(online,arrMultiLampConfig[1],arrMultiLampConfig[2],arrMultiLampConfig[3],arrMultiLampConfig[4],arrMultiLampConfig[5],arrMultiLampConfig[6],arrMultiLampConfig[7]);
		//alert("aaa=" + multiLampConfig.groupInfo.groupName);
		multiLamp.init(multiLampConfig);
		multiLamp.initTable();
		floatMultiLamp = new floatObj(multiLamp.mainDiv);
		window.setInterval("s_dealFloat(floatMultiLamp)",40);
		setTimeout("setWebimUserPic()",200);
	}
}
//如果需要显示多亮灯,也开通了多个亮灯
if ( (createCodeFlag & 2) == 2 ) { 
	setTimeout("dealMultiLight()",100);
}

/******************************多个亮灯的处理 end *********************************/
/**
 * 发送消息
 */
function webimOnlineSend(){
	var webimClientUserId =t_webim_user_id;
	var realOnLine = OnLine;
	if(realOnLine == -1) {
		realOnLine = 0;
	}
	var nanonymSiteId = "cnanonym";
	if (!sellTalkUserId)
	{
		return;
	} 
	if (sellTalkUserId.length <= 8)
	{
		return;
	}
	var sendSiteId = sellTalkUserId.substr(0,8);
	var sendUserId = sellTalkUserId.substr(8,sellTalkUserId.length);
	if (needFenliu) {
		onlinewangWangSend(nanonymSiteId,webimClientUserId,sendSiteId,sendUserId,realOnLine,"");
	} else {
    	//判断本地客户端版本
    	var version = getInstalledVersion(sellTalkUserId);
    	if(version!=DEFAULT_VERSION){
    		onlinewangWangSendMsg(nanonymSiteId,webimClientUserId,sendSiteId,sendUserId,status,"");
    	}else{
    		//打开webww
    		var webwangwangURL  = webWwUrl+"?memberid="+webimClientUserId+"&targetid="+encodeURI(sellTalkUserId);
    		window.open(webwangwangURL);
    	}
	}
	//这里在发送消息，它已经接收到了
	if ( (createCodeFlag & 8) == 8 ) { 
		caller.onNetscreenOk(0);
	}
	//这里在发送消息，它已经接收到了
	if ( (createCodeFlag & 4) == 4 ) {
		OnlineSale.reopen();
	}
}

/**
 * 发送消息
 * @param wwid   from site
 * @param status 状态
 */
function webimOnlineSendB(wwid,status){
	var webimClientUserId =t_webim_user_id;
	var realOnLine = OnLine;
	if(realOnLine == -1) {
		realOnLine = 0;
	}
	if (!wwid)
	{
		return;
	} 
	if (wwid.length <= 8)
	{
		return;
	}
	var sendSiteId = wwid.substr(0,8);
	var sendUserId = wwid.substr(8,wwid.length);
	var nanonymSiteId = "cnanonym";
	status=1;
	//判断本地客户端版本
	var version = getInstalledVersion(webimUserId);
	if(version!=DEFAULT_VERSION){
		onlinewangWangSendMsg(nanonymSiteId,webimClientUserId,sendSiteId,sendUserId,status,"");
	}else{
		//打开webww
		var webwangwangURL  = webWwUrl+"?memberid="+webimClientUserId+"&targetid="+encodeURI(wwid);
		window.open(webwangwangURL);
	}
	//这里在发送消息，它已经接收到了
	if ( (createCodeFlag & 8) == 8 ) {
		caller.onNetscreenOk(0);
	}
	//这里在发送消息，它已经接收到了
	if ( (createCodeFlag & 4) == 4 ) {
		OnlineSale.reopen();
	}
}