﻿///<reference path="jquery-1.3.2-vsdoc.js">
///<reference path="../js/plugins/input.js">

var TimerAddIMIcon=null

function FunctionAddIMIcon() {
    clearTimeout(TimerAddIMIcon);
    var hrefs = $('span[im_id]');
    var ids = new Array();
    hrefs.each(function(i) {
        var href = $(this).attr("im_id")
        ids.push(href);
    }
    )
    var chatService = new IChatService();
        if (ids.length>0)
        chatService.GetAgentsState(ids.join(","), OnGetAgentsState)
    
}
function GetAgentsStateImages(status, sid, show_link, div) {
    var ret = "";
    var link_1 = "";
    var link_2 = "";
    var div_status = $(".sip_status", div);
    var div_text = $(".sip_text", div);
    switch (status) {
        //OffLine=1, Online=2, Buy=3, Unkonw=0, Away=4 
        case 1: //OffLine
            ret = '<img src="' + site_path + 'images/comm/offline1.png" class="icon" width="18" height="16" title="online чат"> <span class="chat_status" title="online чат">отсутствует</span>';
            break;
        case 2: //Online
                if (show_link)
                    link_1 = '<a sid="' + sid + '" href="#" onclick="return OpenIMChat(\'' + sid + '\');" title="online чат">';
                ret = '<img src="' + site_path + 'images/comm/available1.png" class="icon" width="18" height="16" title="online чат"> <span class="chat_status">доступен</span>';
                if (show_link)
                    link_2 = '</a>';
            break;
        case 3: //Busy
            ret = '<img src="' + site_path + 'images/comm/busy1.png" class="icon" width="18" height="16" title="online чат"> <span class="chat_status" title="online чат">занят</span>' ;
            break;
        case 0: //Unkonw
            //href.html("<img src='" + site_path + "images/comm/comm04.gif' alt='Не беспокоить' title='Не беспокоить'/> Не беспокоить");
            break;
        case 4: //Away
            if (show_link)
                link_1 = '<a sid="' + sid + '" href="#" onclick="return OpenIMChat(\'' + sid + '\');" title="online чат">';
             ret = '<img src="' + site_path + 'images/comm/away1.png" class="icon" width="18" height="16" title="online чат"> <span class="chat_status">временно отсутствует</span>';
            if (show_link)
                link_2 = '</a>';
            break;
        default:
            ret = status;
    }
    if (div_status.length > 0) {
        div_status.html(ret);
        div.html(link_1 + '<span class="sip_text">' + div_text.html() + '</span>' + link_2);
    } else {
        div.html(link_1 + ret + link_2);
    }
    
    return ret;
}
function OnGetAgentsState(result) {
    for (var id in result) {
        var v=result[id];
        if (!v.Key) continue;
        var href = $("span[im_id='" + v.Key + "']");
        GetAgentsStateImages(v.Value, v.Key, true, href);
    }
    TimerAddIMIcon = setTimeout(FunctionAddIMIcon, 25000);
}
function OpenIMChat(sid) {
    var width = "335", height = "414";
    var left = (screen.width / 2) - width / 2;
    var top = (screen.height / 2) - height / 2;
    if (sid == "g") {
        window.open(root_path + "webchat.aspx?g=1", "chatwindow" + (new Date()).getTime(), 'location=0,status=0,scrollbars=0,toolbar=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top);
    } else if (sid.indexOf("g:") == 0) {
        var gid = sid.substring(2);
        window.open(root_path + "webchat.aspx?g=1&gid=" + gid, "chatwindow" + (new Date()).getTime(), 'location=0,status=0,scrollbars=0,toolbar=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top);
    } else {
    window.open(root_path + "webchat.aspx?sid=" + encodeURI(sid), "chatwindow" + (new Date()).getTime(), 'location=0,status=0,scrollbars=0,toolbar=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top);
}
    return false;
}

