active_call=0;

var is_register=0;


function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? document[movieName] : document[movieName];  
}


function call()
{

	if (active_call==0) active_call=1;
	else active_call=0;

	
	if (active_call==1)
	{
		if (document.getElementById("sip1") != null)
		{
		var dest_value = "sip:" + document.getElementById("sip1").value + "@193.238.20.14";
		}
		else
		{
		var dest_value = "sip:flash2481@193.238.20.14";
		}
		getFlashMovie('phone1').callProperty('call', 'invite', dest_value);
		setTimeout('play(\'/callme/phone-calling.mp3\')', 500);
		document.getElementById('call_button').innerHTML='Завершить';
	}
	else 
	{
		getFlashMovie('phone1').callProperty('call', 'bye');
		getFlashMovie('phone1').setProperty('publish', null);
		getFlashMovie('phone1').setProperty('play', null);
		stop();
		document.getElementById('call_button').innerHTML='Позвонить';
	}
	
}

function register()
{
	if (is_register==0)
	{
		var randomnumber=Math.floor(Math.random()*1000);
		var server = "80.70.70.213:80";
		
		var user = randomnumber+"@localhost";
		var authname = "";
		var authpass = "";
		var displayname = "";
		var rate = 8;
		var rate_name = "narrowband";
		var src_value = "rtmp://" +server+ "/sip/" +user+ "?rate=" +rate+ "&bidirection=true"
			  + "&arg=" +authname+ "&arg=" +authpass+ "&arg=" +displayname+ "&arg=" +rate_name;
		getFlashMovie('phone1').setProperty('src', src_value);
	}
	else
	{
		call();
	}
	
}


function onCreationComplete(event) { 
	
} 
function onPropertyChange(event) { 

	if (event.property == 'nearID') 
	{
		is_register=1;
		call();
	}

 }


//<![CDATA[
var myListener = new Object();

/**
 * Initialisation
 */
myListener.onInit = function()
{
	this.position = 0;
};

myListener.onUpdate = function()
{
				if (this.isPlaying=='true' && (this.duration/2)<this.position) 
				{
					getFlashObject().SetVariable("method:setPosition", '2');
				}
};
function getFlashObject()
{
	return document.getElementById("PhoneMusic");
}
function play(file)
{
	if (myListener.position == 0) {
		getFlashObject().SetVariable("method:setUrl", file);
	}
	getFlashObject().SetVariable("method:play", "");
	getFlashObject().SetVariable("enabled", "true");
}
function pause()
{
	getFlashObject().SetVariable("method:pause", "");
}
function stop()
{
	getFlashObject().SetVariable("method:stop", "");
}
function onCallback(event) {
  //  log(event.method + "(" + event.args + ")");
	//alert(event.method);
    if (event.method == "accepted") {
		
		stop();
        getFlashMovie('phone1').setProperty('publish', 'local');
        getFlashMovie('phone1').setProperty('play', 'remote');
    } 
    if (event.method == "byed") {
        getFlashMovie('phone1').setProperty('publish', null);
        getFlashMovie('phone1').setProperty('play', null);
		document.getElementById('call_button').innerHTML='Позвонить';
		active_call=0;
    }
	if (event.method == "cancelled") {
		stop();
		document.getElementById('call_button').innerHTML='Позвонить';
		active_call=0;
	}
	if (event.method == "rejected") {
		stop();
		document.getElementById('call_button').innerHTML='Позвонить';
		active_call=0;
	}
}
//]]>
