<!--
/**
 * operator.js
 * @fileoverview 客服管理类，包括客服对象类{@link Operator}、客服管理类{@link Operators} 
 * @author Lynk Li
 */

/**
 * @class Operator 客服信息对象类
 * @constructor
 * @author Lynk Li
 * @param String id 客服ID
 * @param String company 公司ID
 * @param String active 客服是否禁止登陆
 * @param String account 客服帐号
 * @param String name 客服显示名
 * @param String email 客服Email
 * @param String status 客服状态
 * @param String type 客服类型"A"管理员
 * @param String talkstatus 客服交谈状态
 * @param String login 客服上次登陆时间
 * @param String logout 客服上次登出时间
 * @param String onlinetime 客服在线时间
 * @param String session 客服SessionID
 * @param String hello 客服欢迎语-问候语
 * @param String bye 客服欢迎语-道别语
 * @param String movieStatus 客服视频状态
 * @param String voiceStatus 客服语音状态
 * @param String picurl 客服图片URL
 * @param String helptype 客服
 * @param String automsg 客服自动回复语
 * @param String passwd 客服密码
 * @param String phone 客服手机号码
 * @param String telephone 客服电话号码
 * @param String position 客服职称
 * @param String memo 客服备注
 * @param String ip_mt 上次修改客服信息的IP
 * @param String ip 客服IP
 */
function Operator(id, company, active, account, name, email, status, type, talkstatus, login, logout, onlinetime, session, hello, bye, movieStatus, voiceStatus, picurl, helptype, automsg, passwd, phone, telephone, position, memo, ip_mt, ip) {
	/**
	 * 客服ID
	 */
	this.id = id;
	/**
	 * 公司ID
	 */
	this.company = company;
	/**
	 * 客服是否禁止登陆
	 */
	this.active = active;
	/**
	 * 客服帐号
	 */
	this.account = account;
	/**
	 * 客服显示名
	 */
	this.name = name;
	/**
	 * 客服电子邮件地址
	 */
	this.email = email;
	/**
	 * 客服联机状态
	 */
	this.status = status;
	/**
	 * 客服类型"A"管理员
	 */
	this.type = type;
	/**
	 * 客服交谈状态
	 */
	this.talkstatus = talkstatus;
	/**
	 * 客服上次登陆时间
	 */
	this.login = login;
	/**
	 * 客服上次登出时间
	 */
	this.logout = logout;
	/**
	 * 客服在线时间
	 */
	this.onlinetime = onlinetime;
	/**
	 * 客服SessionID
	 */
	this.session = session;
	/**
	 * 客服欢迎语-问候语
	 */
	this.hello = hello;
	/**
	 * 客服欢迎语-道别语
	 */
	this.bye = bye;
	/**
	 * 客服视频状态
	 */
	this.movieStatus = movieStatus;
	/**
	 * 客服语音状态
	 */
	this.voiceStatus = voiceStatus;
	/**
	 * 客服图片地址
	 */
	this.picurl = picurl;
	/**
	 * 客服帮助类型
	 */
	this.helptype = helptype;
	/**
	 * 客服自动回复语
	 */
	this.automsg = automsg;
	/**
	 * 客服密码
	 */
	this.passwd = passwd;
	/**
	 * 客服手机号码
	 */
	this.phone = phone;
	/**
	 * 客服电话号码
	 */
	this.telephone = telephone;
	/**
	 * 客服职称
	 */
	this.position = position;
	/**
	 * 客服备注
	 */
	this.memo = memo;
	/**
	 * 上次修改客服信息的IP
	 */
	this.ip_mt = ip_mt;
	/**
	 * 客服IP
	 */
	this.ip = ip;
	this.show = Operator_show;
}

/**
 * 在客服列表中显示客服
 */
function Operator_show() {
	var id = this.id;
	var name = this.name;
	var status = this.status;
	
	var opStatus = _common.getArrayObject(_common.arrOperatorStatus, "value", status);
	if (opStatus != null && (opStatus.value == "C" || opStatus.value == "D")) {
		var parent = document.getElementById("left_oplist");
		var child = document.createElement("div");
		parent.style.textAlign = "left";
		child.id = "left_oplist__" + id;
		eval("child.onclick = function() {_operators.showOperatorInfo('" + id + "', event);};");
		eval("child.oncontextmenu = function() {_operators.showOperatorMenu('" + id + "', event);};");
		var opStatus = _common.getArrayObject(_common.arrOperatorStatus, "value", status);
		if (opStatus == null) {
			child.innerHTML = "<span id='left_oplist__" + id + "_status_img'><img src=\"" + _setting.imagesPath + "op_s_A.gif\" alt=\"" + _langPackage.operator.offline + "\" /></span>&nbsp;" + name;
		} else {
			child.innerHTML = "<span id='left_oplist__" + id + "_status_img'><img src=\"" + _setting.imagesPath + "op_s_" + opStatus.value + ".gif\" alt=\"" + opStatus.label + "\" /></span>&nbsp;" + name;
		}
		
		parent.insertBefore(child, null);
	}
}

/**
 * @class Operators 客服管理类
 * @constructor
 * @author Lynk Li
 */
function Operators() {
	/**
	 * 存储客服列表的数组
	 */
	this.operators = new Array();
	this.sort = Operators_sort;
	this.insert = Operators_insert;
	this.compare = Operators_compare;
	this.show = Operators_show;
	this.changeStatus = Operators_changeStatus;
	this.showOperatorMenu = Operators_showOperatorMenu;
	this.showOperatorInfo = Operators_showOperatorInfo;
	this.changeOperatorInfoInterface = Operators_changeOperatorInfoInterface;
}

/**
 * 比较两个客服的大小，在线 > 在线不交谈 > 离线，然后根据显示名
 * @param Operator opA 客服A
 * @param Operator opB 客服B
 * @returns 0-A=B;1-A>B;-1-A<B
 */
function Operators_compare(opA, opB) {
	if (opA.status == "A") {
		if (opB.status != "A") return -1;
		if (opB.name > opA.name) return 1;
		if (opB.name < opA.name) return -1;
		return 0;
	}
	if (opA.status == "C") {
		if (opB.status != "C") return 1;
		if (opB.name > opA.name) return 1;
		if (opB.name < opA.name) return -1;
		return 0;
	}
	if (opA.status == "D") {
		if (opB.status == "C") return -1;
		if (opB.status == "A") return 1;
		if (opB.name > opA.name) return 1;
		if (opB.name < opA.name) return -1;
		return 0;
	}
}

/**
 * 客服排序, 首先根据在线状态，然后根据显示名
 * 客服状态 A:离线B:C:在线D:在线不交谈
 */
function Operators_sort() {
	for (var i = 0; i < this.operators.length; i++) {
		for (var j = i + 1; j < this.operators.length; j++) {
			if (this.compare(this.operators[i], this.operators[j]) < 0) {
				var op = this.operators[i];
				this.operators[i] = this.operators[j];
				this.operators[j] = op;
			}
		}
	}
}

/**
 * 在客服数组中插入一个客服
 * @param String id 客服ID
 * @param String company 公司ID
 * @param String active 客服是否禁止登陆
 * @param String account 客服帐号
 * @param String name 客服显示名
 * @param String email 客服Email
 * @param String status 客服状态
 * @param String type 客服类型"A"管理员
 * @param String talkstatus 客服交谈状态
 * @param String login 客服上次登陆时间
 * @param String logout 客服上次登出时间
 * @param String onlinetime 客服在线时间
 * @param String session 客服SessionID
 * @param String hello 客服欢迎语-问候语
 * @param String bye 客服欢迎语-道别语
 * @param String movieStatus 客服视频状态
 * @param String voiceStatus 客服语音状态
 * @param String picurl 客服图片URL
 * @param String helptype 客服
 * @param String automsg 客服自动回复语
 * @param String passwd 客服密码
 * @param String phone 客服手机号码
 * @param String telephone 客服电话号码
 * @param String position 客服职称
 * @param String memo 客服备注
 * @param String ip_mt 上次修改客服信息的IP
 * @param String ip 客服IP
 * @see Operator
 */
function Operators_insert(id, company, active, username, name, email, status, type, talkstatus, login, logout, onlinetime, session, hello, bye, movieStatus, voiceStatus, picurl, helptype, automsg, passwd, phone, telephone, position, memo, ip_mt, ip) {
	var op = new Operator(id, company, active, username, name, email, status, type, talkstatus, login, logout, onlinetime, session, hello, bye, movieStatus, voiceStatus, picurl, helptype, automsg, passwd, phone, telephone, position, memo, ip_mt, ip);
	this.operators.push(op);
}

/**
 * 在左边列表中显示客服
 */
function Operators_show() {
	var parent = document.getElementById("left_oplist");	
	parent.innerHTML = "";
	
	for (var i = 0; i < this.operators.length; i++) {
		var op = this.operators[i];
		op.show();
	}
}


/**
 * 客服状态改变
 * @param String id 客服ID
 * @param String statusid 客服状态ID
 */
function Operators_changeStatus(id, statusid) {
	var opStatus = _common.getArrayObject(_common.arrOperatorStatus, "id", statusid);
	if (opStatus == null) return;
	
	var op = "";
	for (var i = 0; i < this.operators.length; i++) {
		var optmp = this.operators[i];
		if (optmp.id == id) {
			op = optmp;
			//op.status = opStatus.value;
			//opName = op.name;
			break;
		}
	}
	
	if (op.status != opStatus.value) {
		op.status = opStatus.value;
		_operators.sort();
		_operators.show();
		
		if (id != _personSession.id && opStatus.value == "C") {
			_common.popPrompt(3, _langPackage.operator.login_1 + op.name + _langPackage.operator.login_2); 
			
			_common.ocxPrompt(3, _langPackage.operator.login_1 + op.name + _langPackage.operator.login_2, "");
		}
	}
}

/**
 * 显示客服操作菜单
 * @param String id 客服ID
 * @param event e 鼠标右键事件
 */
function Operators_showOperatorMenu(id, e) {
	_common.clearPopDiv();
	var popDiv = document.getElementById("popDiv__list");
	var strMenu = "";
	var strInvite = "";
	if (_personSession.id != id) {
		var opObj = _common.getArrayObject(this.operators, "id", id);
		if (opObj != null && (opObj.status == "C" || opObj.status == "D")) {
			strInvite = "onclick=\"_common.clearPopDiv();_chat.sendChatRequestToOperator('" + id + "')\"";
		}
	}
	strMenu += "<div id='popDiv__list_menu_sendReq2Op' class=\"menu_r_text_align_o\" onmouseover=\"this.className='menu_r_on_bg_o'\" onmouseout=\"this.className='menu_r_text_align_o'\" " + strInvite + ">" + _langPackage.operator.invite + "</div>";
	popDiv.innerHTML = strMenu;
	popDiv.className = "menu_r";
	popDiv.style.position = "absolute";
	popDiv.style.top = e.y;
	popDiv.style.left = e.x;
	popDiv.style.display = "";
}

/**
 * 切换客服信息
 * @param int index 1 or 2，显示第一页还是第二页
 */
function Operators_changeOperatorInfoInterface(index) {
	var info_1 = document.getElementById("popDiv__list_opinfo_1");
	var info_2 = document.getElementById("popDiv__list_opinfo_2");
	if (index == 1) {
		info_1.style.display = "";
		info_2.style.display = "none";
	} else {
		info_1.style.display = "none";
		info_2.style.display = "";
	}
}

/**
 * 显示客服信息
 * @param String id 客服ID
 * @param event 鼠标点击事件
 */
function Operators_showOperatorInfo(id, e) {
	_common.clearPopDiv();
	var opObj = _common.getArrayObject(this.operators, "id", id);
	
	var strAdmin = _langPackage.operator.no;
	if (opObj.type == "A") strAdmin = _langPackage.operator.yes;
	
	var strDep = "";
	for (var ii = 0; ii < _common.lists.l9006.length; ii ++) {
		//depopid;companyid;depid;opid;isadmin;
		if (opObj.id == _common.lists.l9006[ii][3]) {
			var depId = _common.lists.l9006[ii][2];
			//depid;language;depname;email;companyid;default
			var arrDep = _common.getListObjByPropertyValue(9003, 0, depId);
			if (arrDep != null) {
				if (strDep != "") strDep += "/";
				strDep += arrDep[2];
			}
		}
	}
	
	var popDiv = document.getElementById("popDiv__list");
	var data = {
		images_path	: _setting.imagesPath,
		opObj		: opObj,
		strAdmin	: strAdmin,
		strDep		: strDep
		};
	var str = TrimPath.processDOMTemplate("operator_info_tpl", data);
	popDiv.innerHTML = str;
	popDiv.className = "v_b_bg";
	popDiv.style.position = "absolute";

	
	if (document.documentElement.clientHeight > 280 && e.y > document.documentElement.clientHeight - 280) {
		popDiv.style.top = document.documentElement.clientHeight - 280;
	} else {
		popDiv.style.top = e.y;
	}
	if (document.documentElement.clientWidth > 280 && e.x > document.documentElement.clientWidth - 280) {
		popDiv.style.left = document.documentElement.clientWidth - 280;
	} else {
		popDiv.style.left = e.x + 2;
	}
	
	popDiv.style.display = "";
}

var _operators = new Operators();
//-->