/**
* @file
* @ingroup group_ajax
* @ingroup group_widget
* @ingroup widget_chat
* @brief chat ajaxscript
*
* @see chat
*/

/**
* chatablak frissítése
* @see updchat.php
*/
function chatObj()
{
    /**
 *chat frissítési időköz
 */
    var chatinterval=5000;
    var _self=this;
    var lastid=0;
    var timer;
    var chattalk=new Array();
    if (document.getElementById("chatwindow")){
        var chatwin=document.getElementById("chatwindow");
    }
    else{
        window.notify.write("Hiba a chat inicializálása során");
        window.notify.show(2000);
    }
    this._constructor = function(){
        httpobj_send=new Ajax("_modules");
        httpobj_update=new Ajax("_modules");
        this.update();
    }
    this.update=function(){
        httpobj_update.sendRequest(_self.feedback,
            "module", "chat",
            "action", "update",
            "l", lastid);
    }
    this.send=function (){        
        var str = document.getElementById("chathsz").value;
        str=encodeURIComponent(trim(str));
        if (str!="")
        {
            httpobj_send.sendRequest(_self.showOwnMsg,
                "module", "chat",
                "action", "send",
                "msg" , str);
        }
        document.getElementById("chathsz").value="";        
        clearTimeout(timer);
        chatinterval=3000;
        this.update();        
        return false;
    }
    this.showOwnMsg=function(response){
        
    }
    this.feedback=function(response){        
        if (response==0){
            //window.notify.write("Hiba a chat frissítése során");
            //window.notify.show(2000);
        }
        else{            
            if (chatwin){                
                if (response!="-") {                                   
                    newchat = response.split('-:|:-');                                   
                    lastid=parseInt(newchat.shift(),10);
                    chattalk= newchat.concat(chattalk).slice(0,20);
                    chatwin.innerHTML=chattalk.join("");
                }
                else {
                    if (chatinterval<90000)
                    {
                        chatinterval=chatinterval*2;
                    }
                }                
            }
        }
        timer = setTimeout(_self.update,chatinterval);
    }
    this._constructor.apply(this, arguments);
}
function toggleChatSmiley(){
    if (document.getElementById("chatsmileybox")){
        targ=document.getElementById("chatsmileybox");
        if (targ.style.display!="block"){
            targ.style.display="block";
        }
        else{
            targ.style.display="none";
        }
    }    
}

function chatSmiley(smiley){    
    if (document.getElementById("chathsz")){        
        document.getElementById("chathsz").value=document.getElementById("chathsz").value+smiley;
    }
}

