/*
    WebApplication object:
        Represents a web application. It could contain Webpages(WebPage objects)
*/
WebApplication = Class.create({
    
    initialize : function(config)
    {
        this.config = config;
        this.webPages = {};
        this.webControls = {};
        this.webPageIndex = 0;
        this.webControlIndex = 0;
        this.activePageIndex = 0;
    },
    
    // Adds a WebPage Object to this WebApplication
    AddWebPage : function(webPage)
    {
        this.webPages[this.webPageIndex++] = webPage;
    },
    
    // Adds a WebControl Object to this WebApplication
    AddWebControl : function(webControl)
    {
        this.webControls[this.webControlIndex++] = webControl;
    },
    
    // Sets the active webpage by name
    SetActivePage : function(name)
    {
        for(var i = 0; i < this.webPageIndex; i++)
        {
            var currentPage = this.webPages[i];
            if(currentPage.config.name == name)
            {
                this.activePageIndex = i;
                currentPage.Show();
            }
        }
    },
    
    GetPage : function(name)
    {
        for(var i = 0; i < this.webPageIndex; i++)
        {
            var currentPage = this.webPages[i];
            if(currentPage.config.name == name)
            {
                return currentPage;
            }
        }
        return null;
    },
    
    // Shows the active WebPage
    Run : function()
    {
        Event.observe( window, 'load', this.Loaded.bind(this) );
    },
    
    Loaded: function()
    {
        if(this.webPageIndex > 0)
        {
            this.webPages[this.activePageIndex].Show();
        }
        
        for(var i = 0; i < this.webControlIndex; i++)
        {
            this.webControls[i].Show();
        }
    }
});
