window.addEvent('load', function() {
    function unfilled(element, after) {
        var oldSpellcheck;
        function handler() {
            element.spellcheck = oldSpellcheck;
            element.removeEvent('click', handler);
            element.removeEvent('focus', handler);
            element.value = '';
            element.removeClass('unfilled');
            if (after != undefined) {
                after();
            }
        }
        element = $(element);
        if (element.hasClass('unfilled')) {
            oldSpellcheck = element.spellcheck;
            element.spellcheck = false;
            element.addEvent('click', handler);
            element.addEvent('focus', handler);
        }
    }
    function unfilledPassword(element, after) {
        element = $(element);
        unfilled(element, function() {
            element.type = 'password';
            if (after != undefined) {
                after();
            }
        });
    }
    function setError(error) {
        var element = $$('.statustext')[0];
        element.empty();
        element.addClass('error');
        element.appendText(error);
    }
    function everythingFilled() {
        return !$('tweetmsg').hasClass('unfilled') &&
               !$('tweetusername').hasClass('unfilled') &&
               !$('tweetpassword').hasClass('unfilled') &&
               $('tweetmsg').value &&
               $('tweetusername').value &&
               $('tweetpassword').value;
    }
    function countChars() {
        var maxLength = prtmMaxLength - 4 - $('tweetusername').value.length;
        return maxLength - $('tweetmsg').value.length;
    }
    function currentError() {
        if (!everythingFilled()) {
            return 'Please enter your username, password, and a message.';
        } else if (countChars() < 0) {
            return 'Ack! Too many characters!';
        } else {
            return null;
        }
    }
    function processControls() {
        function update() {
            $('tweetcount').empty();
            $('tweetcount').appendText(countChars());
            
            if (currentError()) {
                $('tweetsubmit').addClass('disabled');
                $('tweetusername').set('autocomplete', 'off');
                $('tweetpassword').set('autocomplete', 'off');
            } else {
                $('tweetsubmit').removeClass('disabled');
                $('tweetusername').set('autocomplete', 'on');
                $('tweetpassword').set('autocomplete', 'on');
            }
        }
        function handleSubmit(ev) {
            var error = currentError();
            if (error) {
                setError(error);
                ev.preventDefault();
            }
        }
        unfilled('tweetusername', update);
        unfilledPassword('tweetpassword');
        $('tweetmsg').addEvent('keyup', update);
        $('tweetmsg').addEvent('keypress', update);
        $('tweetusername').addEvent('keyup', update);
        $('tweetusername').addEvent('keypress', update);
        $('tweetpassword').addEvent('keyup', update);
        $('tweetpassword').addEvent('keypress', update);
        $('tweetform').addEvent('submit', handleSubmit);
        update();
        update();
    }
    unfilled('tweetmsg', function() {
        var div = new Element('div');
        div.id = 'tweetfirstcontrols';
        var username = new Element('input');
        username.type = 'text';
        username.name = 'username';
        username.id = 'tweetusername';
        username.value = 'username';
        username.addClass('unfilled');
        div.grab(username);
        
        var password = new Element('input');
        password.type = 'text';
        password.name = 'password';
        password.id = 'tweetpassword';
        password.value = 'password';
        password.addClass('unfilled');
        div.grab(password);
        
        var rightDiv = new Element('div');
        rightDiv.id = 'tweetcountandsubmit';
        
        var submit = new Element('input');
        submit.type = 'submit';
        submit.id = 'tweetsubmit';
        submit.value = 'send';
        rightDiv.grab(submit);

        var count = new Element('span');
        count.id = 'tweetcount';
        rightDiv.grab(count);
        
        div.grab(rightDiv);
        
        $('tweetform').grab(div, 'top');
        $('tweetform').tween('height', originalFormHeight, $('tweetform').scrollHeight);
        processControls();
    });
    var originalFormHeight = $('tweetform').scrollHeight
    $('tweetform').setStyles({
        'overflow': 'hidden',
        'height': $('tweetform').scrollHeight + 'px'
    });
    if ($('tweetfirstcontrols')) {
        processControls();
    }
});
