/*
    WebControl object:
        Represents a web control. It can be used to do some UI modification with JS
*/
WebControl = Class.create();

WebControl.prototype = {
    
    WebControls : {},
    
    WebControlIndex : 0,
    
    initialize : function(config)
    {
        this.config = config;
    },

    /*
        1. Gets the HTML source if it is specified in the config object called 'url'.
        (this.config.url) and put it into the div if it is specified in the config
        object called 'container'. (this.config.container)
        2. Runs the Show method specified in the in the config object called 'show'.
    */
    Show : function()
    {
        if(this.config.container != null && !this.config.container.empty() && this.config.url != null && !this.config.url.empty())
        {
            new Ajax.Updater(
                this.config.container,
                this.config.url,
                {
                    onComplete: this.ShowOnComplete.bind(this)
                }
            );
        }
        else
        {
            for(var i = 0; i < this.WebControlIndex; i++)
            {
                this.WebControls[i].Show();
            }
            
            if(this.config.Show != null) this.config.Show();
            if(this.config.AddEvents != null) this.config.AddEvents();
        }
    },

    ShowOnComplete : function(originalRequest)
    {
        for(var i = 0; i < this.WebControlIndex; i++)
        {
            this.WebControls[i].Show();
        }
        
        if(this.config.Show != null) this.config.Show();
        if(this.config.AddEvents != null) this.config.AddEvents();
    },
    
    Refresh : function()
    {
        $(this.config.container).update("");
        this.Show();
    },
    
    // Adds a WebControl to this WebControl.
    AddWebControl : function(webControl)
    {
        this.WebControls[this.WebControlIndex++] = webControl;
    }
    
}

