var makandra; if (!makandra) { makandra = new Object(); }

makandra.Console = {

    MAX_FOCUS_RESCHEDULE_ATTEMPTS: 10,
    focusRescheduleAttemps: 0,

    focusCommandTextField: function() {
        /*
        var commandField = this.getCommandTextField();
        if (commandField && commandField.focus) {
          Form.Element.focus(commandField); //.focus();
          return true;
        } else {
          return false;
        }
        */
        return (this.getCommandTextField() && this.getCommandTextField().focus()); //Form.Element.focus(this.getCommandTextField()));
    },
        
    scrollToBottom: function() {
        window.scrollBy(0, 9999999); // 9999999 should be enough for anyone
    },
    
    executePrompt: function(command) {
        if (command) this.setCommand(command);
        var url = this.getJavaScriptActionField().value + 
            "?command=" + encodeURIComponent(this.getCommand()) + 
            "&node_name=" + encodeURIComponent(this.getNodeName());
        this.getCommandContainer().update(this.getCommand());
        new Ajax.Request(url, {
            method: 'get',
            onComplete: function(transport) {
                var newPart = new Element("div").update(transport.responseText);
                var downloadLinks = newPart.select('.console_link');
                if (downloadLinks && downloadLinks[0]) {
                    parent.makandra.openDownloadLink(downloadLinks[0].toString());
                }
                this.getConsoleContainer().appendChild(newPart);
                this.outputAdded();
            }.bind(this)
        });
    },
        
    outputAdded: function() {
        this.scheduleFocusCommandTextField();
        $$('img').each(function(img) {
            Event.observe(img, "load", this.scrollToBottom.bind(this));
        }.bind(this));
        this.scrollToBottom();
    },
        
    getConsoleContainer: function() {
        return $('console');
    },
        
    getPromptContainer: function() {
        return $$('.prompt').last();
    },
        
    getCommand: function() {
        return this.getCommandTextField().value;
    },
        
    setCommand: function(command) {
        this.getCommandTextField().value = command;
    },
        
    getJavaScriptActionField: function() {
        return this.getPromptContainer().select('input[name="javascript_action"]')[0];
    },

    getCommandTextField: function() {
        if (this.getPromptContainer()) return this.getPromptContainer().select('input[name="command"]')[0];
    },

    getCommandContainer: function() {
        return this.getPromptContainer().select('.command')[0];
    },

    getNodeName: function() {
        return this.getPromptContainer().select('input[name="node_name"]')[0].value;
    },
        
    getPromptForm: function() {
        return this.getPromptContainer().select('form')[0];
    },
        
    scheduleFocusCommandTextField: function() {
        setTimeout(function() {
            var reschedule = false;
            try {
                reschedule = !this.focusCommandTextField();
            } catch(e) {
                reschedule = true;
            }
            if (reschedule && this.focusRescheduleAttemps < this.MAX_FOCUS_RESCHEDULE_ATTEMPTS) {
                this.focusRescheduleAttemps++;
                this.scheduleFocusCommandTextField();
            }
        }.bind(this), 300);
    }

};

Event.observe(window, "load", makandra.Console.scheduleFocusCommandTextField.bind(makandra.Console));
Event.observe(document, "keydown", makandra.Console.focusCommandTextField.bind(makandra.Console));
Event.observe(document, "click", makandra.Console.scheduleFocusCommandTextField.bind(makandra.Console));
