
// Chat Requests
window.chatRequests=Array();
window.chatWindows=Array();


function loadChatScriptUrl(u, n){
    if (window.ChatLoaded){
        addSystemChatMessage(u);
    }
    loadScript(u,n);
}


function getUnixTimeStamp(){
    var date=new Date();
    return date.getTime();

}

function acceptChatInvitation(user_id, request_id){
    cClick();
    return openChatWindow(user_id, request_id);
}

function setInvitationAccept(id){
    _url=getPresenceServerBaseURL() + "acceptInvite?id="+ id+"&t="+t+"&d="+window._domain;
    loadChatScriptUrl(_url, 1);
}




function rejectChatRequest(user_id, id){
    cClick();  
    t=getUnixTimeStamp();
    _url=getPresenceServerBaseURL() + "recjectInvite?id="+ id+"&t="+t+"&d="+window._domain;
    loadChatScriptUrl(_url, 2);
}

function openChatWindow(user_id, request_id){
    if (request_id) {
        url="/modules/membership/chat.php?req_id="+request_id+"&u="+user_id;
    } else {
        url="/modules/membership/chat.php?u="+user_id;
    }

    url="http://"+location.host+url;
    window_id="ChatWindow_"+user_id;


    params="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no";
    var options = "width=" + 800 + ",height=" + 600 + "," + params;

    window.chatWindows[window_id]=window.open(url, window_id, options);
    window.self.name = "main";
    window.chatWindows[window_id].focus();
}


function handleChatRequest(id, sender, sender_name){
    if (window.onChat==1){
        return ;
    }
    if (window.chatRequests[sender]) {

    } else {
        title="Incoming Chat request";
        text="Chat request from "+Base64.decode(sender_name);
        text+="<input type=button value='Accept' onclick='acceptChatInvitation("+sender+","+id+")'> <input type=button value='Reject' onclick='rejectChatRequest("+sender+","+id+")'> ";
        showOverlib(text, title);
        window.chatRequests[sender]=id;
    }
}                                                                                                                                           

function handleTest(){
    showOverlib("test", "TEST");
}


function getPresenceServerBaseURL(){
    return window.chat_server_base_url + "/chatserver/";
}


function announcePresence(ms){
    if (!window.sid) {
        window.sid=getCookie("xoops_session");
    }
    t=getUnixTimeStamp();
    if (!window._domain) {
        window._domain=window.location.hostname;
    }
    presence_url=getPresenceServerBaseURL() + "presence?u="+ window.uid+"&s="+window.sid+"&t="+t+"&d="+window._domain;
    if (ms>0) {
        setTimeout("loadChatScriptUrl(presence_url)", ms);
    } else {
        loadChatScriptUrl(presence_url, 1);
    }
}

function checkIMsFromBuddy(bid, ms){
    return;

    t=getUnixTimeStamp();
    _url=getPresenceServerBaseURL() + "getIMs?u="+ window.uid+"&s="+bid+"&t="+t+"&d="+window._domain;
    //addSystemChatMessage(_url);
    if (ms>0) {
        setTimeout("loadChatScriptUrl(_url, 1)", ms);
    } else {
        loadChatScriptUrl(_url, 1);
    }
}



function sendIMToBuddy(bid, msg){
    t=getUnixTimeStamp();
    _url=getPresenceServerBaseURL() + "sendIM?s="+ window.uid+"&r="+bid+"&t="+t+"&d="+window._domain + "&m="+Base64.encode(msg);
    loadChatScriptUrl(_url, 1);
}

function sendIMInviteToBuddy(bid, n){
    t=getUnixTimeStamp();
    _url=getPresenceServerBaseURL() + "sendIMInvite?s="+ window.uid+"&r="+bid+"&t="+t+"&d="+window._domain + "&n="+Base64.encode(n);
    addSystemChatMessage(_url);
    loadChatScriptUrl(_url, 1);
}

function checkInviteStatusForId(id){
    t=getUnixTimeStamp();
    _url=getPresenceServerBaseURL() + "checkInviteStatus?id="+ id+"&t="+t+"&d="+window._domain;;
    addSystemChatMessage(_url);
    loadChatScriptUrl(_url, 1);
}
