
//全局变量 Begin
var oldnum="";
var temp=1;
var IAXC_CALL_STATE_FREE = 0;
var IAXC_CALL_STATE_ACTIVE = 2;     // (1<<1)
var IAXC_CALL_STATE_OUTGOING = 4;   // (1<<2)
var IAXC_CALL_STATE_RINGING = 8;    // (1<<3)
var IAXC_CALL_STATE_COMPLETE = 16;  // (1<<4)
var IAXC_CALL_STATE_SELECTED = 32;  // (1<<5)

var IV_STATE_HANGUP = 0;
var IV_STATE_ONHOOKIDLE = 1;
var IV_STATE_ONHOOKRINGING = 2;
var IV_STATE_OFFHOOKRINGING = 3;
var IV_STATE_OFFHOOKDIALING = 4;
var IV_STATE_OFFHOOKACTIVE = 5;

var bInit = false;
var bCanChangeVolume = false;
var IVPhoneState = IV_STATE_HANGUP;
//全局变量 End

$(document).ready(function(){
 

	$("#indexWeb800").click(function () {

		offset1 = $(this).offset();
		var shuxing1 = "position:absolute;top:" + (offset1.top + 18) + "px;left:" + (offset1.left-300) + "px";
		$("#phone_lu").attr("style", shuxing1);
		$("#phone_lu").show();
	//	$("#phone_kj").show();
		$("#phone_lu_web800").show();
		$.ajax({
			type:"get",
			cache:false,
			 url:"../call.do?method=returnSecretString1&username= uniebiz" + "&password=123456" + "&phone= 018868700068 "+ "&r=" + Math.random(),
			//url:"../call.do?method=returnSecretString1&username= 12346" + "&password= 123456" + "&phone=051088258866" + "&r=" + Math.random(),
			async:false,
			processData:false,
			success:function (data) {
			
			
				terminateCall();
				initWeb800(data);
			
				ctrlMex('请点击拨打按钮呼叫...');
			}
		});
		
	});
	//点击分机键盘按钮
	$('#gg_lu').toggle(
		function(){
			$('#yincang_lu').show();
			$('#gg_lu').attr("src","phoneimg/fenji_ph_up.jpg"); 
		},
		function(){
			$('#yincang_lu').hide();
			$('#gg_lu').attr("src","phoneimg/fenji_ph.jpg"); 
		}
	);
	
	//点击拨打
	$('#dialweb800_lu').click(function(){
		web800.Dial();				//呼叫
		oldnum="";					//置空号码
		$('#phone_close_lu').hide();	
	});
	
	//点击挂断
	$('#hangphone_lu').click(function(){
		terminateCall();
	});
	
	//点击关闭窗口
	$('#phone_close_lu').click(function(){
		term_web800();
		$('#phone_lu').hide();
	});
	
	//点击分机数字
	//$('#phone_jiapanee a').each(function(i){
	$('#phone_jiapanee_lu a').each(function(i){
		if(i<9){
			$(this).click(function(){sendDTMF(i+1);});
		}else{
			if(i==9)$(this).click(function(){sendDTMF('*');});
			if(i==10)$(this).click(function(){sendDTMF('0');});
			if(i==11)$(this).click(function(){sendDTMF('#');});
		}
	});
	control_web800();
	
});

//初始800服务
function initWeb800(str){

	var nInit = web800.InitIAX(str);
	if (nInit == 0) bInit = true;
	if (!bInit)
	{	
		ctrlMex('初始化失败...');
		return(false);
	}
	web800.Register();
	return (true);
}
//屏幕显示
function ctrlMex(msg){
	$('#pingmu_text_lu').text(msg);
	if(msg=='电话挂断中...')
		$('#phone_close_lu').show();
}
//防初始崩溃事件
function control_web800(){

	web800.InitIAX("9095AB29A886316EE0F4B1F3729DCD4A39C6680ED84A66B160A062A042A4A0B08041BEFC47CB4422E0F009AEC3BFA06A97852D7A2F3649B926DC00A1604958E13C79C642BC9300B2F09D8C6135A5883E86B4272AFCFB6A3B645B4D67E0A6AA4979B0CCCF");
	terminateCall();
	web800.Shutdown();
	return(true);
}

//分机键盘事件
function sendDTMF(n){
	if (!bInit)
		return(false);
	oldnum=oldnum+n;
    ctrlMex(oldnum);
	web800.sendDTMF(n);
	return(true);
}
//关闭webcall
function term_web800(){

	if (!bInit)
		return(false);
    terminateCall();
	web800.Shutdown();
    return(true);
}
//挂断事件
function terminateCall(){
	if (!bInit)
		return(false);
    web800.SelectLine(0);
	web800.HangUp();

	ctrlMex('电话挂断中...');
	oldnum="";
	$('#phone_close_lu').show();
	return(true);
}

//更新状态
function updateStatus(IVPhoneState, CallNo, sRemote, sRemoteName, sLocal, sLocalContext){
	if (CallNo == 0)
		{
			switch (IVPhoneState)
			{
				case IV_STATE_HANGUP:
					web800.StopSound();
					ctrlMex('电话已挂断...');
					$('#phone_close_lu').show();
					break;
				case IV_STATE_ONHOOKIDLE:
					ctrlMex('Press Call button in order to Call.');
					break;
				case IV_STATE_ONHOOKRINGING:
					web800.PlaySounds("ringin.wav",1);
					ctrlMex('Incomming Call.');
					break;
				case IV_STATE_OFFHOOKRINGING:
					ctrlMex('响铃中...');
					web800.PlaySounds("ringout.wav",1);
					break;
				case IV_STATE_OFFHOOKDIALING:
					web800.StopSound();
					ctrlMex('呼叫中，请稍候...');
					break;
				case IV_STATE_OFFHOOKACTIVE:
					web800.StopSound();
					ctrlMex('已接通，通话中...');
					break;
			}
		}
}

//获取状态
function onIVStateEvents(CallNo, State, sRemote, sRemoteName, sLocal, sLocalContext){

var bFree = false;
		var bActive = false;
		var bSelected = false;
		var bComplete = false;
		var bRinging = false;
		var bOutgoing = false;

		IVPhoneState = IV_STATE_HANGUP;

		bFree = State & IAXC_CALL_STATE_FREE;
		bActive = State & IAXC_CALL_STATE_ACTIVE;
		bSelected = State & IAXC_CALL_STATE_SELECTED;
		bComplete = State & IAXC_CALL_STATE_COMPLETE;
		bRinging = State & IAXC_CALL_STATE_RINGING;
		bOutgoing = State & IAXC_CALL_STATE_OUTGOING;

		if (bFree)
		{
			IVPhoneState = IV_STATE_ONHOOKIDLE;
		}
		else if (bRinging)
		{
			if (bOutgoing)
			{
				if (bActive)
				{
					IVPhoneState = IV_STATE_OFFHOOKRINGING; 
				}
			}
		}
		else
		{
			if (bOutgoing)
			{
				if (bActive && bComplete)
				{
					IVPhoneState = IV_STATE_OFFHOOKACTIVE;
				}
				else if (bActive && !bComplete)
				{
					IVPhoneState = IV_STATE_OFFHOOKDIALING;
				}
			}
		}
		updateStatus(IVPhoneState, CallNo, sRemote, sRemoteName, sLocal, sLocalContext);

}

