﻿// Declare a namespace.
Type.registerNamespace("MyComponents");

MyComponents.ChatComponent = function() {
    MyComponents.ChatComponent.initializeBase(this);

    this._input = null;
    this._filter = null;
    this._sendBtn = null;
    this._messages = null;
    this._div = null;
    this._lastMsg = -1;
    this._max = 500;
    this._room = null;
    this._clearInput = false;
    this._setFocus = false;
    this._msgLength = 1000;
}

///Create prototype
MyComponents.ChatComponent.prototype = {

    initialize: function() {
        MyComponents.ChatComponent.callBaseMethod(this, 'initialize');

        if (this._input) {
            var input = $get(this._input);
            if (input) $addHandler(input, "keydown", Function.createDelegate(this, this._onInputKeyDown));
        }

        if (this._filter) {
            var filter = $get(this._filter);
            if (filter) $addHandler(filter, "keydown", Function.createDelegate(this, this._onFilterKeyDown));
        }

        if (this._sendBtn) {
            var btn = $get(this._sendBtn);
            if (btn) $addHandler(btn, "click", Function.createDelegate(this, this._onSendMsg));
        }

    },

    dispose: function() {
        if (this._input) {
            var input = $get(this._input);
            if (input) $clearHandlers(input);
        }

        if (this._filter) {
            var filter = $get(this._filter);
            if (filter) $clearHandlers(filter);
        }

        if (this._sendBtn) {
            var btn = $get(this._sendBtn);
            if (btn) $clearHandlers(btn);
        }

        MyComponents.ChatComponent.callBaseMethod(this, 'dispose');
    },

    //
    // Event delegates
    //

    _onInputKeyDown: function(ev) {
        var k = ev.keyCode ? ev.keyCode : ev.rawEvent.keyCode;
        if (k == Sys.UI.Key.enter && ev.target.value != "") {
            this._sendMessage(ev.target.value);
            ev.stopPropagation();
            return false;
        }
    },

    _onFilterKeyDown: function(e) {
        //do nothing
    },

    _onSendMsg: function(e) {
        this._setFocus = true;
        this._sendMessage($get(this._input).value);
    },

    _sendMessage: function(str) {
        if (str && str != "") {
            if (this._msgLength && this._msgLength < str.length) {
                alert("Message too long");
            }
            else {
                this._clearInput = true;
                MessagesManager.GetMessages(this._room, str, this._lastMsg, this._max, 
                    Function.createDelegate(this, this._succeededCallback), function() { });
            }
        }
    },

    _succeededCallback: function(result) {
        var inserted = false;
        for (var i = 0; i < result.length; i++) {
            var m = result[i];
            if (this._lastMsg != m.id) {
                this._lastMsg = m.id;
                this._insertRow(m.dt, m.user, m.color, m.message);
                inserted = true;
            }
        }
        var input = $get(this._input);
        if (input) {
            if (this._clearInput) {
                input.value = "";
                this._clearInput = false;
            }
            if (this._setFocus)
                input.focus();
        }
        this._setFocus = false;
    },

    _insertRow: function(dt, user, color, str) {
        var table = $get(this._messages);
        var row = table.insertRow(-1);

        var cell = row.insertCell(0);
        cell.appendChild(document.createTextNode("[" + dt + "] "));
        var userElem = document.createElement("b");
        userElem.appendChild(document.createTextNode(user));
        cell.appendChild(userElem);
        cell.appendChild(document.createTextNode(": " + str));

        $get(this._div).scrollTop = 100000;
    },

    _clearTable: function() {
        var table = $get(this._messages);
        //Sys.Debug.trace("start deleting: " + table.rows.length);
        var cnt = table.rows.length;
        for (var i = 0; i < cnt; i++) {
            //Sys.Debug.trace("delete: " + i);
            table.deleteRow(-1);
        }
    },

    OnActivate: function(f) {
        this._setFocus = f;
        MessagesManager.GetMessages(this._room, null, this._lastMsg, this._max,
            Function.createDelegate(this, this._succeededCallback), function() { });
    },

    getTopMessages: function(count) {
        this._clearTable();
        MessagesManager.GetTopMessages(this._room, count,
            Function.createDelegate(this, this._succeededCallback), function() { });
    },

    //
    // component properties
    //

    get_MaxMessageLength: function() {
        return this._msgLength;
    },

    set_MaxMessageLength: function(value) {
        if (this._msgLength !== value) {
            this._msgLength = value;
            this.raisePropertyChanged('MaxMessageLength');
        }
    },

    get_Room: function() {
        return this._room;
    },

    set_Room: function(value) {
        if (this._room !== value) {
            this._room = value;
            this.raisePropertyChanged('Room');
        }
    },

    get_input: function() {
        return this._input;
    },

    set_input: function(value) {
        if (this._input !== value) {
            this._input = value;
            this.raisePropertyChanged('input');
        }
    },

    get_filter: function() {
        return this._filter;
    },

    set_filter: function(value) {
        if (this._filter !== value) {
            this._filter = value;
            this.raisePropertyChanged('filter');
        }
    },

    get_sendBtn: function() {
        return this._sendBtn;
    },

    set_sendBtn: function(value) {
        if (this._sendBtn !== value) {
            this._sendBtn = value;
            this.raisePropertyChanged('sendBtn');
        }
    },

    get_messages: function() {
        return this._messages;
    },

    set_messages: function(value) {
        if (this._messages !== value) {
            this._messages = value;
            this.raisePropertyChanged('messages');
        }
    },

    get_div: function() {
        return this._div;
    },

    set_div: function(value) {
        if (this._div !== value) {
            this._div = value;
            this.raisePropertyChanged('div');
        }
    }
}

// Register the class as a type that inherits from Sys.UI.Control.
MyComponents.ChatComponent.registerClass('MyComponents.ChatComponent', Sys.Component);

if (typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded();

