/**  -*- mode:javascript-generic; -*-
  *
  *  Copyright 2007 Halloo Communications, Inc.
  *  All rights reserved.
  *
  **/

Rico.loadModule('AjaxEngine');

/**************************************************************
 * LiveCallUpdater class. Interacts with the AjaxEngine and UI
 **************************************************************/

var LiveCallUpdater = Class.create();
LiveCallUpdater.prototype = 
{
	initialize: function( hooksw) 
	{
		this.state  = "IDLE";
		this.ccxid  = "";
		this.hooksw = $(hooksw);
		this.caller = "";
		this.pex    = null;
	},

/* Process status updates from FlexlCallC2C */ 

	ajaxUpdate: function( ajaxResponse)
	{
		var conns  = ajaxResponse.getElementsByTagName('connections');
		var acalls = ajaxResponse.getElementsByTagName('connection');
		var agerrs = ajaxResponse.getElementsByTagName('error');

		if (conns==null || conns.length==0)
		{
//			alert( "ccxid not found:"+this.ccxid);
			this.ccxid = "";
			this.state  = "IDLE";
			this.hooksw.style.backgroundColor = "";
			this.hooksw.innerHTML = "Call";

			if (this.pex != null) 
			{
				this.pex.stop();
				this.pex = null;
			}
		}

		if (acalls.length==0 || agerrs.length>0)
		{
			var msg = agerrs[0].getAttribute('msg');
			if ( (msg!=null) && (msg.length > 0))
				alert( msg);
		}
		else
		{
//			var dbgstr = "";

			this.ccxid = this.getEncAttrib( conns[0], 'ccxmlid');
			for( var ii=0; ii<acalls.length; ii++)
			{
			    var ctc = acalls[ii];			
				var remote = this.getEncAttrib( ctc,'remote');
				var st     = this.getEncAttrib( ctc,'state');
				if (remote.indexOf( this.caller) < 0) continue;

				this.state = st;

				if (st == "PROGRESSING")
				{
					this.hooksw.style.backgroundColor = "#ffffcf";
					this.hooksw.innerHTML = "Hang up";
				}
				else if (st == "CONNECTED")
				{
					this.hooksw.style.backgroundColor = "#cfffcf";
					this.hooksw.innerHTML = "Hang up";
				}
				else
				{
					this.hooksw.style.backgroundColor = "";
					this.hooksw.innerHTML = "Call";
				}
			}

//			$('debugDiv').innerHTML = dbgstr;
		}
	},

	getEncAttrib: function( ctc, attr)
	{
		var rattr = ctc.getAttribute( attr);
		if (rattr == null) rattr = "";
		return rattr;
	},

	periodic: function( pe)
	{
//		if (gLcUpdater.ccxid.length > 0)
		{
			gAjaxEngine.sendRequest( 'FlexCallC2C', 
									 "cmd=listconnections",
									 "ccxmlid="+gLcUpdater.ccxid );
		}
	},

/* Process UI Events*/ 

	riaCreateCall: function( dest, callerid, callername)
	{
		this.caller = callerid;
		gAjaxEngine.sendRequest( 'FlexCallC2C', 
								"cmd=callcreate",
								"to="+dest,
								"from="+callerid,
								"fname="+callername	);

		this.pex = new PeriodicalExecuter( this.periodic, 2);
	},

	riaDisconnect: function ()
	{
		this.state = "IDLE";
		if (this.pex != null) 
		{
			this.pex.stop();
			this.pex = null;
		}
		if (this.ccxid.length > 0)
		{
			gAjaxEngine.sendRequest( 'FlexCallC2C', 
									"cmd=hangup",
									"ccxmlid="+this.ccxid );
			this.ccxid = "";

		}
		else
			alert( "Call not active");
	}
}

/**************************************************************
 * Miscellaneous functions
 **************************************************************/
function btnEnab( btn, tf)
{
	btn.disabled    = !tf;
	btn.style.color = tf ? '#000000' : '#aaaaaa'; 
}

function colorObj( objID, col)
{
    myref = $(objID);
	if (!myref)	return false;
	if (myref.style) myref = myref.style;
	myref.background = col;
	myref.backgroundColor = col;
	myref.bgColor = col;
	return true;
}

function hilightObj( objID, col)
{
    myref = $(objID);
	if (!myref)	return false;
	if (myref.style) myref = myref.style;
	myref.borderColor = col;
	return true;
}

function hookSw( callto, cselid, tboxid /*, nboxid*/)
{
	if (gLcUpdater.state != "IDLE")
		gLcUpdater.riaDisconnect();
	else
	{
		var co = $(cselid).value.split('/')[0];
		gLcUpdater.riaCreateCall( callto, 
								  co + $(tboxid).value.replace( /[^\d]/g,''), 
								  '' );
//								  $(nboxid).value );
	}
}

function keyFilter( cselid, pboxid)
{
	var csel = $(cselid);
	var pbox = $(pboxid);
	var pn   = pbox.value.replace( /[^\d]/g, '');

	if (csel.value.split('/')[0]=='1')
	{
		if (pn.charAt(0)=='1' || pn.charAt(0)=='0') pn=pn.substr(1);
		pn = pn.substr(0,10);
		var pnl = pn.length;
		var rep = (pnl < 4) ? "($1" : ((pnl < 7) ? "($1) $2" : "($1) $2-$3");

		pn = pn.replace( /(\d{1,3})(\d{0,3})(\d{0,4})/, rep);
		pbox.value = pn;
	}
}

var gAjaxEngine;
var gLcUpdater;

