#include utility.js
#include watcher.js

var AmiSubmitBox = function(parent) {
    var pub = {},
        watcher = new AmiWatcher,
        maxLengthValue = 0,
        click = function(ev) {
            watcher.call(pub, [ev]);
        };

    var button = $e({
            tag: 'input',
            attrs: {
                type: 'button',
                value: 'Send',
                id: 'sendmsg',
                onclick: function(ev) { click(ev); },
            },
        }),
        curLength = $e({
            tag: 'span',
            child: '0',
        }),
        maxLength = $e({
            tag: 'span',
            child: maxLengthValue.toString(),
        }),
        container = $e({
            attrs: { id: 'submitButtonContainer' },
            child: [
                {
                    tag: 'span',
                    attrs: { id: 'messageLengthCounter' },
                    child: [curLength, '/', maxLength],
                },
                ' ',
                button,
            ],
        });

    parent.appendChild(container);

    pub.click = click;
    pub.watch = function(callback) {
        watcher.watch(callback);
    };
    pub.unwatch = function(watcher) {
        watcher.unwatch(callback);
    };

    pub.setCurrentLength = function(num) {
        num = parseInt(num);
        curLength.textContent = num.toLocaleString();
        if(num >= maxLengthValue && !curLength.style.color)
            curLength.style.color = '#c00';
        else if(num < maxLengthValue && curLength.style.color)
            curLength.style.color = null;
    };
    pub.setMaxLength = function(num) {
        maxLengthValue = parseInt(num);
        maxLength.textContent = maxLengthValue.toLocaleString();
    };

    return pub;
};