var callWindowWidth = 760, 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; border:1px solid #DDD; width:223px; padding: 2px 0 2px 2px'>" +
    "<table id=\"" + identity + "\" cellspacing='0' cellpadding='0' border='0' width='220'>"+
    "<tr><td rowspan=2 class='C' width='68' height='88' style='border:1px solid #DDD;'><span class=callerAvatar><img src='/img/no-foto.png' border=0 alt=''></span></td>"+
    "<td align='center'><a href='../us/"+userId+"'><b><span class=callerName>User #" + userId + "</span></b></a></td></tr>"+
    "<tr><td class='C'><span class=callAnswer style='color:#0D55B8;' onclick=\"answerCall(" + userId + ", '" + identity + "');\">o&#1090;&#1074;&#1077;&#1090;&#1080;&#1090;&#1100;</span>" +
    " &nbsp; &nbsp; " +
    "<span class=callCancel style='color:#0D55B8;' onclick=\"cancelCall(" + userId + ", '" + identity + "');\">o&#1090;&#1084;&#1077;&#1085;&#1080;&#1090;&#1100;</span>&nbsp;" +
    "</td></tr></table>" +
    "</div>";
}
function putCall(userId, identity, userInfo) {
    scrlCTitle();
    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);
            callDiv = document.getElementById("call" + userId);
            callDiv.style.display = "";
        } else {
            $.getJSON("/AHAH/user_info.jsp", {
                "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);
                }
                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) {
    stopScrlTitle();
    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("/services/call.jsp?userId=" + userId, "callWindow" /*"placeCall" + userId*/, callWindowOptions);
    top.callWindow.focus();
}
/*
 *используется для незалогиненного и залогиненного узера
 **/
function placeLoginCall(userId, loginUserId) {
    if(loginUserId == 0) {
        document.location = "/home/acs.jsp";
    } else {
        placeCall(userId);
    }
}
function answerCall(userId, identity) {
    top.callWindow = window.open("/services/call.jsp?userId=" + userId + "&callId=" + identity, "callWindow" /*"answerCall" + userId*/, callWindowOptions);
    top.callWindow.focus();
    removeCall(userId);
}
function cancelCall(userId, identity) {
    try {
        removeCall(userId);
    } catch(err) {
    }
    $.post("/services/call.jsp?remove", {
        "callId": identity
    });
    document.getElementById("messageFlash").cancelCall(identity);
}

var scrlC = Array('* * * * * * * * * * * * \0','\u041D\u043E\u0432\u044B\u0439 \u0437\u0432\u043E\u043D\u043E\u043A! \0','* * * * * * * * * * * * \0','vsetke.ru \0');
var scrlM = Array('* * * * * * * * * * * * \0','\u041D\u043E\u0432\u043E\u0435 \u0441\u043E\u043E\u0431\u0449\u0435\u043D\u0438\u0435! \0','* * * * * * * * * * * * \0','vsetke.ru \0');
var ind = t = timeoutMID = timeoutCID = 0, sc = scrlM[0], oldTitle = window.document.title;

function stopScrlTitle() {
    clearTimeout(timeoutMID);
    clearTimeout(timeoutCID);
    window.document.title = oldTitle;
}
function scrlMTitle() {
    if (timeoutMID>0)
        stopScrlTitle();
    sc = sc.substring(1, sc.length) + sc.substring(0, 1);
    if (sc.substring(0, 1) == '\0'){
        t++;
        if (t>=1) {
            sc = scrlM[ind = ind + 1 < scrlM.length ? ind + 1 : 0];
            t = 0;
        }
    } 
    document.title = sc.replace(/\0/, '');
    timeoutMID = setTimeout("scrlMTitle()", 300);
}
function scrlCTitle() {
    if (timeoutMID>0)
        clearTimeout(timeoutMID);
    sc = sc.substring(1, sc.length) + sc.substring(0, 1);
    if (sc.substring(0, 1) == '\0'){
        t++;
        if (t>=1) {
            sc = scrlC[ind = ind + 1 < scrlC.length ? ind + 1 : 0];
            t = 0;
        }
    }
    document.title = sc.replace(/\0/, '');
    timeoutCID = setTimeout("scrlCTitle()", 300);
}

