var AjaxChat = {

    nick:'',
    timer1:'',
    timer2:'',
    
    login:function(nick)
    {
        if (nick)
        {
            this.nick = nick;
            new Ajax.Request(
                "login.php",
                {
                    postBody: 'nick='+nick,
                    onComplete:function(r)
                    {
                        
                    },
                    onSuccess:function(r) 
                    { 
                        AjaxChat.loginCheck(nick,r.responseText);
                    },
                    onFailure:function() 
                    { 
                        $('error').innerHTML = "Error:\nKeine Verbindung zur Datenbank!";
                    }
                }
            );
        }
        else
        {
            Field.activate('nick');
            $('error').innerHTML = "ERROR: Bitte geben Sie einen Nicknamen an.";
        }
    },
    
    loginCheck:function(nick,flag)
    {
        if (flag!='error')
        {
            Element.hide('login');
            Element.show('msg','usr','ctrl');
            $('error').innerHTML = "";
            AjaxChat.updateContent();
            AjaxChat.updateUsrList();
        }
        else
        {
            Field.activate('nick');
            $('error').innerHTML = "ERROR: Dieser Nickname existiert bereits.";
        }
    },
    
    updateContent:function()
    {
        this.timer1 = new Ajax.PeriodicalUpdater(
            "",
            "updateMsgList.php",
            {
                onSuccess:function(r) 
                {
                    var root = r.responseXML.getElementsByTagName("chat");
                    var text = root[0].getElementsByTagName("data");
                    if (text.length>0)
                    {
                        for (var i=0; i<text.length; i++)
                        {
                            addP = document.createElement("p");
                            var nval = text[i].childNodes[0].firstChild.nodeValue;
                            var span = (nval=='SYSTEM:') ? "<span class=\"system\">" : "<span>";
                            addP.innerHTML = span+nval+"</span>&nbsp;";
                            addP.innerHTML += text[i].childNodes[1].firstChild.nodeValue;
                            with($('msg'))
                            {
                                appendChild(addP);
                                scrollTop = scrollHeight-offsetHeight;
                            }
                        }
                    }                  
                },
                onFailure:function() 
                { 
                    alert("Error:\nKeine Verbindung zur Datenbank!\t");
                }
            }
        );
    },
    
    updateUsrList:function()
    {
        this.timer2 = new Ajax.PeriodicalUpdater(
            "",
            "updateUsrList.php",
            {
                onSuccess:function(r) 
                {                  
                    var root = r.responseXML.getElementsByTagName("chat");
                    var text = root[0].getElementsByTagName("usr");
                    //$('usr').innerHTML = ""; 
                    for (var i=0; i<text.length; i++)
                    {
                        addP = document.createElement("p");
                        addP.innerHTML = text[i].childNodes[0].firstChild.nodeValue;
                        $('usr').appendChild(addP);
                    }  
                },
                onFailure:function() 
                { 
                    alert("Error:\nKeine Verbindung zur Datenbank!\t");
                }
            }
        );
    },
    
    insertMsg:function(text)
    {
        new Ajax.Request(
            "insert.php",
            {
                postBody: 'nick='+this.nick+'&text='+text,
                onFailure:function() 
                { 
                    alert("Error:\nKeine Verbindung zur Datenbank!\t");
                }
            }
        );
    },
    
    logout:function()
    {
        if (this.timer1) this.timer1.stop();
        if (this.timer2) this.timer2.stop();
        new Ajax.Request(
            "logout.php",
            {
                onSuccess:function() 
                {
                    Element.show('login');
                    Element.hide('msg','usr','ctrl');
                    $('msg').innerHTML = "";
                    $('usr').innerHTML = ""; 
                },
                onFailure:function() 
                { 
                    alert("Error:\nKeine Verbindung zur Datenbank!\t");
                }
            }
        );
    }
};


window.onload = function()
{
    new Draggable(
        'chat',
        {
            handle:'head'
        }    
    );
    document.forms[0].onsubmit = function()
    {
        AjaxChat.login(document.forms[0].nick.value);
        return false;
    }
    document.forms[1].onsubmit = function()
    {
        AjaxChat.insertMsg(document.forms[1].text.value);
        document.forms[1].text.value = "";
        return false;
    }
};
window.onunload = function() 
{ 
    AjaxChat.logout();
}

