var ce = "chat_inc/chatEngine.php";

function initPing()
{
	checkChat = setInterval("ping()",5000);	
}

function ping()		// every 5 seconds
{
	var callBackObj = 
	{
		success: function(o)
		{
			if(o.responseText=="RINGRING")
			{
				clearInterval(checkChat);
				someonescalling();
			}
		}
	};
	YAHOO.util.Connect.asyncRequest('POST',ce,callBackObj,'ping=1');
}

function someonescalling()		// for callee
{
	var callBack = 
	{
		success: function(o)
		{
			var parts = o.responseText.split("&");
			document.getElementById('cw').style.display='';
			var x = ((YAHOO.util.Dom.getClientWidth() - 380) / 2) +"px";
			var y = ((YAHOO.util.Dom.getClientHeight() - 100) / 2) + "px";
			document.getElementById('cw').style.top = y;
			document.getElementById('cw').style.left= x;
			if(parts[2])
			document.getElementById('chatterPersonPhoto').style.backgroundImage = " ";
			document.getElementById('chatterPerson').innerHTML = parts[1];

			document.getElementById('chatAnswerButton').onclick= function()
			{
				manageCall('answer',parts[0]);
			}
			document.getElementById('chatRejectButton').onclick= function()
			{
				manageCall('reject',parts[0]);
			}
			document.getElementById('chatBlockButton').onclick = function()
			{
				chatBlock(parts[1],parts[0]);
			}
		}
	};
	YAHOO.util.Connect.asyncRequest('POST',ce,callBack,'checkWhoCalling=1');
}

function manageCall(action,id)
{
	if(action=="answer")
	{
		var callBack = 
		{
			success: function()
			{
				//document.location="membersChat.php?c="+id;
				newindow("http://www.webcamrecorder.cn/membersChat.php?c="+id,'450','650','chat','');
				document.getElementById('cw').style.display='none';
			}
		};
		YAHOO.util.Connect.asyncRequest('POST',ce,callBack,'answerCall='+id);
	}
	else
	{
		var callBack	 = 
		{
			success: function(o)
			{
				initPing();
				document.getElementById('cw').style.display='none';
			}
		};
		YAHOO.util.Connect.asyncRequest('POST',ce,callBack,'rejectCall='+id);
	}
}

function startChat(user)
{
	var callBack = 
	{
		success: function(o)
		{
			if(o.responseTextwebcamrecorder.cn=="BLOCKED")
			{
				alert("Sorry, you have been blocked by this user.");
			}
			else if(o.responseText=="NOSELF")
			{
				alert("Sorry, you cannot start a chat with yourself.");
			}
			else
			{
				clearInterval(checkChat);
				checkChat = setInterval("checkCall("+o.responseText+")",3000);
			}
		}
	};
	
	if(confirm("Would you like to initiate a chat with "+user+"?\n" + "Remember: chat window will not show up until "+user+" approve this chat session."))
		YAHOO.util.Connect.asyncRequest('POST',ce,callBack,'startChat='+user);
}

function checkCall(convId)
{
	var callBack = 
	{
		success: function(o)
		{
			if(o.responseText==2)
			{
				//document.location="membersChat.php?c="+convId;
				newindow("http://www.webcamrecorder.cn/membersChat.php?c="+convId,'450','650','chat','');
				clearInterval(checkChat);
			}
			else if(o.responseText==9)
			{
				alert('The user has rejected your call');
				initPing();
				YAHOO.util.Connect.asyncRequest('POST',ce,'','clearRequest='+convId);
			}
		}
	};
	YAHOO.util.Connect.asyncRequest('POST',ce,callBack,'checkCall='+convId);
}

function chatBlock(person,convId)			// username
{
	var callBack = 
	{
		success: function(o)
		{
			if(convId)
			{
				manageCall('reject',convId);
			}
		}
	};
	YAHOO.util.Connect.asyncRequest('POST',ce,callBack,'chatBlock='+person);
}

function newindow(url, height, width, name, parms) 
{
	var left = Math.floor( (screen.width - width) / 2);
	var top = Math.floor( (screen.height - height) / 2);
	var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
	if (parms) { winParms += "," + parms; }
	win = window.open(url, name, winParms);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	// return win;
}
