
/*
 * Available widgets
 */

var RemixWidgets = {};

/*
 *   Base for RemixWidgets
 * 
 */

var RemixWidgetsClass = {};

/*
 * Configuration. Filled by the server code
 */

var RemixWidgetsConfig = {};

/*
 * Creates a new instance for the widget. It will use
 * methods defined in widget_definition
 */
function RW_base_class(html_id, form_name, widget_name, widget_definition)
{
    this.html_id = html_id;
    this.form_name = form_name;
    this.widget_name = widget_name;

    /* Update this object methods with methods defined
     * in widget_definition
     */
    for (attr in widget_definition) {
        this[attr] = widget_definition[attr];
    }

    /* Call the initialize functions, if any */
    if(typeof(this.initialize) == 'function')
        this.initialize();
}

/* Change the value of the widget */
RW_base_class.prototype.set_value = function(value)
{
    $(this.html_id).value = value;
}

/* Returns the value of the widget */
RW_base_class.prototype.get_value = function () { return $(this.html_id).value; }

/* Returns the ID of the widget */
RW_base_class.prototype.get_id = function () { return this.html_id; }

/* Returns the DOM node of the widget */
RW_base_class.prototype.domNode = function () { return $(this.html_id); }

/* Disable the widget */
RW_base_class.prototype.disable = function (html_id) {
    Element.addClassName(html_id || this.html_id, "rw_disabled");
    $(html_id || this.html_id).disabled = true;
}

/* Enable the widget */
RW_base_class.prototype.enable = function (html_id) {
    Element.removeClassName(html_id || this.html_id, "rw_disabled");
    $(html_id || this.html_id).disabled = false;
}

/* Focus the widget */
RW_base_class.prototype.focus = function () { $(this.html_id).focus(); }


/* Shows the widget */
RW_base_class.prototype.show = function (html_id)
{
    html_id = html_id || this.html_id;
    var w = $(html_id);
    if (w.style.display == 'none') {
        w.style.display = this._previous_display ? this._previous_display : '';

        if((w = $('label_for_' + html_id))) {
            w.style.display = this._previous_label_display ? this._previous_label_display : '';
        }
    }
}

/* Hides the widget */
RW_base_class.prototype.hide = function (html_id)
{
    html_id = html_id || this.html_id;
    var w = $(html_id);
    if (w.style.display != 'none') {
        this._previous_display = w.style.display;
        w.style.display = 'none';

        if((w = $('label_for_' + html_id))) {
            this._previous_label_display = w.style.display;
            w.style.display = 'none';
        }
    }

}

/* Returns the text used as the label for the widget */
RW_base_class.prototype.label_text = function () 
{
    var label = $("label_for_" + this.html_id);
    return label ? label.innerHTML : this.widget_name;
}

/* Returns an array with errors detected in the widget value */
RW_base_class.prototype.check_value = function() 
{
    return [];
}

/* Sets/unsets the error state of the widget */
RW_base_class.prototype.mark_erroneous = function (state)
{

}

/* Add an event to the widget */
RW_base_class.prototype.add_event = function (eventname, func)
{
    return Event.observe(this.domNode(), eventname, func);
}

/* Adds the spinner */
RW_base_class.prototype.add_spinner = function () {

    if (this.current_spinner)
        return;

    var widget = $(this.html_id);
    var img = new Image();
    var img = document.createElement('img');
    img.src = RemixWidgetsConfig.base_url + "/images/spinner.gif";
    img.style.marginLeft = -20;
    img.style.position = 'absolute';
    widget.parentNode.insertBefore(img, widget);

    this.current_spinner = img;

}

RW_base_class.prototype.remove_spinner = function () {

    if (this.current_spinner) {
        var cs = this.current_spinner;
        this.current_spinner.parentNode.removeChild(this.current_spinner);
        this.current_spinner = null;
    }

}

/*
 * Send an Ajax request to a widget
 */
RemixWidgets.call_ajax = function (form_name, widget_name, action_name, return_proc) {

    var form = document.forms[form_name];
    if (!form)
        return;

    form['__rw_ajax_action_name'].value = action_name;
    form['__rw_ajax_action_widget'].value = widget_name;

    new Ajax.Request(form.action, 
        {
            asynchronous: true,
            method: 'post',
            parameters: Form.serialize(form),
            onComplete: function(req) { return_proc(req.responseText); }
        });

    form['__rw_ajax_action_name'].value = '';
    form['__rw_ajax_action_widget'].value = '';
}
