/*
    Copyright (c) 2005 Joost Diepenmaat, Zeekat Softwareontwikkeling, 
    all rights reserved.

    Permission is hereby granted, free of charge, to any person obtaining 
    a copy of this software and associated documentation files (the 
    "Software"), to deal in the Software without restriction, including 
    without limitation the rights to use, copy, modify, merge, publish, 
    distribute, sublicense, and/or sell copies of the Software, and to 
    permit persons to whom the Software is furnished to do so, subject to 
    the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
    OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
    OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


    simplechat.js $Revision: 1.10 $

    A javascript chatbox using XMLHttpRequest.

    The webpage for this code is at http://zeekat.nl/downloads/chatbox/
*/



(function(){

    var xjs = nl.zeekat.xjs;
    var chat = nl.zeekat.chat;

    chat.SimpleChat = xjs.constructor({
        
        prototype: new chat.ChatBase(),
        
        init: function ( baseUrl, elementId ) {
            if (arguments.length > 0) {
                this.messagesUrl = baseUrl + "/messages.xml";
            }
        },
        
        send: function(message) {
            var chatbox = this;
            chatbox.inputBox.value="";
            chatbox.inputBox.focus();
            this.getRequest(
                this.baseUrl+"/chatbox.cgi?message="+escape(message)+"&author="+escape(chatbox.authorBox.value),
                function (resp) {
                    chatbox.updateStatus("Message posted");
                    chatbox.secondsLeft = 3; 
                    chatbox.countDown();
                },
                null
            );
        },
 

        parseMessage: function(node) {
            var index = 0;
            var nindex = 0;
            var html = "";
            var text = node.firstChild.data;
            this.updateStatus("msg "+text);
            var author = node.getAttribute("author");
            var emote = (text.indexOf("/me") == 0 || text.indexOf("/me") == 1);
            if (emote) {
                text = text.substring(4,text.length);
            }
            html = this.htmlEscape(text);
            html = html.replace(/(http:\/\/\S*)/g,'<a href="$1">$1</a>'); 
            var r = {
                emote: emote,
                html:  html,
                author: author
            };
            return r;
        },
        
       
        createInterface: function() {
            chat.SimpleChat.superclass.createInterface.apply(this);
            this.authorBox = document.createElement("input");
            this.authorBox.type = 'text';
            this.authorBox.value = "anonymous";
            this.form.insertBefore(this.authorBox,this.form.firstChild);
        }
    });

})();

