var callWindowWidth = 750, callWindowHeight = 720, callWindowOptions = "width=" + callWindowWidth + ", height=" + callWindowHeight + ", left=" + (screen.width - callWindowWidth) / 2 + ", top=" + (screen.height - callWindowHeight) / 2 + ", status=0, toolbar=0, menubar=0, titlebar=0, location=0, directories=0, resizable=0, scrollbars=0, dependent=1";
function generateCallDiv(userId, identity) {
	return "<div class=callItem id=call" + userId + " style=\"display:none;\">" +
	"<table><tr><td width=\"80\" rowspan=\"2\"><span class=callerAvatar><img src=\"/pics/photo_small_na.gif\" border=0 alt=\"\"></span></td><td><span class=callerName><strong>User #" + userId + "</strong></span> <span class=callerGender><img class=\"i_gender\" src=\"/pics/ico_f.gif\" alt=\"#\" title=\"#\"></span> <span class=\"callerAge\">#</span></td><td width=\"13\"><img src=\"/pics/icons/ico.incomingcall.gif\" alt=\"#\" title=\"#\"></td></tr><tr><td colspan=\"2\">" +
	"<div class=\"upr\"><img onclick=\"answerCall(" + userId + ", '" + identity + "');\" src=\"/pics/b_vc_answer.gif\" alt=\"\" title=\"\" /> <span class=callAnswer onclick=\"answerCall(" + userId + ", '" + identity + "');\">Ответить</span></div> " +
	"<div class=\"upr\"><img onclick=\"cancelCall(" + userId + ", '" + identity + "');\" src=\"/pics/b_vc_cancel.gif\" alt=\"\" title=\"\" /> <span class=callCancel onclick=\"cancelCall(" + userId + ", '" + identity + "');\">отменить</span></div>" +
	"</td></tr></table>" +
	"</div>";
}

function putCall(userId, identity, userInfo) {
	var callDiv = document.getElementById("call" + userId);
	if(!callDiv) {
		var callListDiv = document.getElementById("callList")
		callListDiv.innerHTML += generateCallDiv(userId, identity);
		callListDiv.style.visibility = "visible";
		if(userInfo && typeof userInfo === "string")
			userInfo = eval("(" + userInfo + ")");
		if(userInfo && typeof userInfo === "object" && userInfo.userName) {
			$("div#callList div#call" + userId + " span.callerName").html(userInfo.userName);
			if(userInfo.userAvatar)
				$("div#callList div#call" + userId + " span.callerAvatar img").attr("src", userInfo.userAvatar);
			if(data.userAge)
				$("div#callList div#call" + userId + " span.callerAge").html(userInfo.userAge);
			if(data.userGender)
				$("div#callList div#call" + userId + " span.callerGender img").attr("src", userInfo.userGender);
			callDiv = document.getElementById("call" + userId);
			callDiv.style.display = "";
		} else {
			$.getJSON("/user_info.php", {"userId": userId},
			function(data, textStatus) {
				if(textStatus == "success") {
					if(data.userName)
						$("div#callList div#call" + data.userId + " span.callerName").html(data.userName);
					if(data.userAvatar)
						$("div#callList div#call" + data.userId + " span.callerAvatar img").attr("src", data.userAvatar);
					if(data.userAge)
						$("div#callList div#call" + data.userId + " span.callerAge").html(data.userAge);
          if(data.userGender)
            $("div#callList div#call" + data.userId + " span.callerGender img").attr("src", data.userGender);
				}
				callDiv = document.getElementById("call" + userId);
				callDiv.style.display = "";
			});
		}
		window.setTimeout(function() {var callDiv = document.getElementById("call" + userId); if(callDiv && callDiv.firstChild.id == identity) cancelCall(userId, identity);}, 140 * 1000);
	}
}
function removeCall(userId) {
	var callDiv = document.getElementById("call" + userId);
	if(callDiv) {
		var callListDiv = document.getElementById("callList")
		callListDiv.removeChild(callDiv);
		if(callListDiv.childNodes.length == 0) {
			callListDiv.style.visibility = "hidden";
			document.getElementById("messageFlash").stopCallTone();
		}
	}
}
function placeCall(userId) {
	top.callWindow = window.open("/call.php?userId=" + userId, "callWindow" /*"placeCall" + userId*/, callWindowOptions);
	top.callWindow.focus();
}
function answerCall(userId, identity) {
	top.callWindow = window.open("/call.php?userId=" + userId + "&callId=" + identity, "callWindow" /*"answerCall" + userId*/, callWindowOptions);
	top.callWindow.focus();
	removeCall(userId);
}
function cancelCall(userId, identity) {
	try {
		removeCall(userId);
	} catch(err) {
	}
	$.post("/call.php?remove", {"callId": identity});
	document.getElementById("messageFlash").cancelCall(identity);
}

