var FormViewHelper = Class.create({
    initialize: function(form)
    {       
        form = $(form);
        this.el = 
        {
            form: form,
            inputs: form.getInputs(),
            indicator: null,
            message_div: null
        };      
    },

    setMessageDiv: function(message_div)
        { this.el.message_div = $(message_div); },
            
    createMessageDiv: function()
    {
        this.el.message_div = new Element('div', { 'class':'message' });
        this.el.form.insert({ before:this.el.message_div });
    },
               
    showMessage: function(message, status, milliseconds)
    {
        if (null === this.el.message_div)
            this.createMessageDiv();
            
        if ('undefined' == typeof(milliseconds))
            milliseconds = 0;

        this.el.message_div.update(message).writeAttribute({ className:'message ' + status}).show();

        if (milliseconds > 0)
            setTimeout(function() { this.hideMessage(); }.bind(this), milliseconds);
    },

    showGeneralMessage: function(message, milliseconds)
        { this.showMessage(message, 'general', milliseconds); },
        
    showSuccessMessage: function(message, milliseconds)
        { this.showMessage(message, 'success', milliseconds); },
    
    showWarningMessage: function(message, milliseconds)
        { this.showMessage(message, 'warning', milliseconds); },
        
    showErrorMessage: function(message, milliseconds)
        { this.showMessage(message, 'error', milliseconds); },

    showLoadingMessage: function(message, milliseconds)
    {
        if (null === this.el.indicator)
            this.createIndicator();
            
        var span = new Element('span').update(this.el.indicator.show());
        if ('undefined' != typeof(message))
            span.insert({ bottom:' ' + message });
        this.showGeneralMessage(span.innerHTML, milliseconds);
    },
        
    hideMessage: function()
    {
        if (null !== this.el.message_div)
            this.el.message_div.hide();
    },
   
    // input_errors must be an associative array. a numerically indexed array does not create a valid hash and each() will cycle through all elements including methods
    showInputErrors: function(input_errors)
    {
        input_errors = $H(input_errors);
        input_errors.each(function(error)
        {
            var input = error.key;

            var messages = $H(error.value);
            if (messages.size() == 0)
                return;
                
            var ul = new Element('ul', { 'class':'errors' });
            messages.each(function(message)
            {
                ul.insert({ bottom:new Element('li').update(message.value) });
            });

            $(input).insert({ after:ul });    
        }.bind(this));
    },
    
    clearInputErrors: function()
    {
        $$('ul.errors').invoke('remove');
    }, 
          
    createIndicator: function()
    {
        this.el.indicator = new Element('img', { src:'/shared/images/icons/loading/indicator.gif', 'class':'indicator' });
    }
});