/* @file SipWebCall.js
 * @brief  web phone base sip 
 * @author zhuangzhikun
 * $Date: 2006/05/19 02:09:38 $
 * $Revision: 1.6 $
 */
 
var SipWebCall = Class.create();

SipWebCall.prototype = {

	initialize: function (settings, oDbg)
	{
		this.sipAgent = null;
		this.settings = settings;
		this.oDbg = oDbg;
		this.chatTimmer = null;
	}
	
}

SipWebCall.prototype.SendDTMF = function (num)
{
	this.sipAgent.DigitDTMF(num); 
}	


SipWebCall.prototype.setSIPAgent = function (sipAgent)
{
	this.sipAgent = sipAgent;	
	this.setOnline();
}

SipWebCall.prototype.setOnline = function ()
{
	if (this.settings)
	{
		this.sipAgent.SetDomainKey("TRIAL-LICENSE-KEY");
	
		var FromURI = this.settings['vi_loginid'] + 
					  " <sip:" + this.settings['vi_loginid'] + "@" 
		   			  + this.settings['vi_sipproxy'] + ">";	  
		var MyIP = this.sipAgent.GetMyIP();
		if(! this.sipAgent.Initialize(MyIP, 5060, MyIP, 7000, FromURI, 
			this.settings['vi_outbound_proxy'], this.settings['vi_sipproxy'], 
			this.settings['vi_loginid'], this.settings['vi_password']))
		{
			alert("SipWebCall.setOnline() initialize error");
		}
		else
		{
			this.sipAgent.DeselectAllVoiceCodec();
			
			// Setting codecs priority, GSM729 at highest priority;		
			this.sipAgent.SelectVoiceCodec(4);
			this.sipAgent.SelectVoiceCodec(0);
			this.sipAgent.SelectVoiceCodec(1);
			this.sipAgent.SelectVoiceCodec(2);
			this.sipAgent.SelectVoiceCodec(3);
			
			if(! this.sipAgent.RegisterToProxy(5000))
				alert("SipWebCall.setOnline() register error");
		}
		
	}
}

SipWebCall.prototype.setOffline = function ()
{
	if (this.sipAgent)
		this.sipAgent.UnInitialize();
}

SipWebCall.prototype.Dial = function ()
{
	var ToURI = "sip:" + this.settings['vi_dial_prefix'] + 
		this.settings['vi_dial_phone'] + "@" + this.settings['vi_sipproxy'];
				
	if(!this.sipAgent.Connect(ToURI, -1, -1))
	{
		alert("fail dial!");
	}
}

SipWebCall.prototype.HangUp = function ()
{
	if(!this.sipAgent.Disconnect())
		alert("fail hangup!");
}	

SipWebCall.prototype.SetAudio = function (myName, audioID)
{
    if(myName=="mic"){
         this.sipAgent.SetMicVolume(audioID * 31.875)
    }else if(myName == "speaker"){
        this.sipAgent.SetSpkVolume(audioID * 31.875);
    }
}

SipWebCall.prototype.StartTiming = function (callback)
{
	if (! this.chatTimmer)
	   this.chatTimmer = new PeriodicalExecuter(callback, 1); 		
	this.chatTimmer.currentlyExecuting = false;
}

SipWebCall.prototype.StopTiming = function ()
{
	if (this.chatTimmer)
		this.chatTimmer.currentlyExecuting = true;
}

