﻿
// =======================================================================
// Creates a custom control lifecycle
// =======================================================================


// Declare a namespace.
Type.registerNamespace("WebTemplate");


WebTemplate.WebTemplatePage = function(cid)
{
    WebTemplate.WebTemplatePage.initializeBase(this);

}


// indicate that the page load is finished
WebTemplate.WebTemplatePage.get_finished = function()
{
    return WebTemplate.WebTemplatePage._finished;
}

WebTemplate.WebTemplatePage.set_finished = function(value)
{
    WebTemplate.WebTemplatePage._finished = value;
}

// reference to the function to call once all our controls are initialized
WebTemplate.WebTemplatePage.get_inithandler = function()
{
    return WebTemplate.WebTemplatePage._inithandler;
}

WebTemplate.WebTemplatePage.set_inithandler = function(value)
{
    WebTemplate.WebTemplatePage._inithandler = value;
}

// keep an interval reference to our control
WebTemplate.WebTemplatePage.get_controls = function()
{
    if(WebTemplate.WebTemplatePage._controls == null || WebTemplate.WebTemplatePage._controls == undefined)
    {
        WebTemplate.WebTemplatePage._controls = new Array();
    }
    return WebTemplate.WebTemplatePage._controls;
    
}

// the control calls this function to register itself once it is finished loading
// it should pass back the control object and id
WebTemplate.WebTemplatePage.registercontrol = function(controlid, controlobj)
{
    var ctlRef = WebTemplate.WebTemplatePage.get_controlref(controlid);
    ctlRef.loaded = true;
    ctlRef.control = controlobj;
    
    // check if this control registration completes the load process
    if(WebTemplate.WebTemplatePage.isLoadComplete())
    {
        WebTemplate.WebTemplatePage.processFinish();
    }

}

// used to initiate the control registration process
// this kicks off any load behaviour and indicates that this control must be loaded
// before page initialization begins
WebTemplate.WebTemplatePage.loadcontrol = function(controlid)
{
    var ctlRef = WebTemplate.WebTemplatePage.get_controlref(controlid);
        
    var loadfunc = eval(controlid + "_load");
    loadfunc(this);
}

// finds the control in the list of control references and creates
// new ones if needed
WebTemplate.WebTemplatePage.get_controlref = function(controlid)
{
    var controls = WebTemplate.WebTemplatePage.get_controls();
    
    for (var i in controls)
    {
        if(controls[i].id == controlid)
        {
            return controls[i];
        }
    }  
    
    // if we didn't find the controlref, create a new one
    var newRef =  new WebTemplate.ControlRef(controlid)
    Array.add(WebTemplate.WebTemplatePage._controls,newRef);
    return newRef;
}

// calls the "finish" event handler after all controls are done loading
// this is called after every control is loaded
WebTemplate.WebTemplatePage.finish = function(inithandler)
{   
    // indicate that we are through laoding controls and thus are finished
    WebTemplate.WebTemplatePage.set_finished(true);
    
    // if passed an init handler, set it
    if (inithandler != null)
    {
        WebTemplate.WebTemplatePage.set_inithandler(inithandler);
    }
    
    if(WebTemplate.WebTemplatePage.isLoadComplete())
    {
        WebTemplate.WebTemplatePage.processFinish();
    }
}

// runs the init() function on each of our client controls
// using the initialize function of the SYS.UI.CONTROL object was problematic
// since this occurs before some of our components are finished loading
WebTemplate.WebTemplatePage.processFinish = function()
{
    // run each of our init handlers
    var controls = WebTemplate.WebTemplatePage.get_controls();
    for (var i in controls)
    {
        controls[i].control.init();
    }  
    
    // run our on finish handler
    WebTemplate.WebTemplatePage.get_inithandler()(WebTemplate.WebTemplatePage);

}


// this checks if all the controls are completely loaded   
WebTemplate.WebTemplatePage.isLoadComplete = function()
{

    if(WebTemplate.WebTemplatePage.get_finished())
    {
        var controls = WebTemplate.WebTemplatePage.get_controls();
        
        // walk over the controls
        for (var i in controls)
        {
            if(controls[i].loaded == false)
            {
                return false;
            }
        } 
        return true; 
    }
    else
    {
        return false;
    }
    
}

// this checks if all the controls are completely loaded   
WebTemplate.WebTemplatePage.findControl = function(controlid)
{

    var controls = WebTemplate.WebTemplatePage.get_controls();
    
    // walk over the controls
    for (var i in controls)
    {
        if(controls[i].id == controlid)
        {
            return controls[i].control;
        }
    } 
    return true; 

    
}


// Declare the base this class inherits from.
WebTemplate.WebTemplatePage.inheritsFrom(Sys.Component);

// Register the class as derived from Sys.Component.
WebTemplate.WebTemplatePage.registerClass('WebTemplate.WebTemplatePage', Sys.Component);



// =======================================================================
// Struct (ish) object to track client controls
// =======================================================================



// Declare a namespace.
Type.registerNamespace("WebTemplate");

// Define a simplified component.
WebTemplate.ControlRef = function(cid)
{
    WebTemplate.ControlRef.initializeBase(this);


    this.id = cid;
    this.control = null;
    this.loaded = false;
}

// Create protytype.
WebTemplate.ControlRef.prototype = 
{

} // End of prototype definition.


// Declare the base this class inherits from.
WebTemplate.ControlRef.inheritsFrom(Sys.Component);

// Register the class as derived from Sys.Component.
WebTemplate.ControlRef.registerClass('WebTemplate.ControlRef', Sys.Component);
