﻿/****************************************
*        RCAttributeCollection
*****************************************/

/// <summary>
/// Richer Components - Attribute Collection Class
/// </summmary>
function RCAttributeCollection()
{
    this._keys = new Array();
    this._values = new Array();
}; // RCStyle

/// <summary>
/// Richer Components - Attribute Collection - Prototype
/// </summmary>
RCAttributeCollection.prototype = 
{
    /// <summary>
    /// Add a new attribute
    /// </summmary>
    /// <param name="key">Key</param>
    /// <param name="value">Value</param>
    Add : function( key, value )
    {
        this._keys[ this._keys.length ] = key;
        this._values[ this._values.length ] = value;
    }, // Add
    
    /// <summary>
    /// Returns the attribute
    /// </summmary>
    /// <param name="key">Key</param>
    Get : function( key )
    {
        var value = null;
        
        for (var i = 0; this._keys.length && value == null; i++)
            if ( this._keys[i] == key )
                value = this._values[i];
        
        return value;
    }, // Get
    
    /// <summary>
    /// Returns this instance HTML markup value
    /// </summmary>
    ToHTML : function()
    {
        var toHtml = '';
    
        for (var i = 0; i < this._keys.length; i++)
        {
            if ( i > 0 )
                toHtml += ' ';
        
            toHtml += this._keys[i];
            toHtml += '="';
            toHtml += this._values[i];
            toHtml += '"';
        }
        
        return toHtml;
    } // ToHTML
}

/****************************************
*               RCStyle
*****************************************/

// <summary>
/// Richer Components - Style Class
/// </summmary>
function RCStyle()
{
    this._keys = new Array();
    this._values = new Array();
} // RCStyle

/// <summary>
/// Richer Components - Style - Prototype
/// </summmary>
RCStyle.prototype = 
{
    /// <summary>
    /// Add a new attribute
    /// </summmary>
    /// <param name="key">Key</param>
    /// <param name="value">Value</param>
    Add : function( key, value )
    {
        this._keys[ this._keys.length ] = key;
        this._values[ this._values.length ] = value;
    }, // AddAttribute
    
    /// <summary>
    /// Returns this instance HTML markup value
    /// </summmary>
    ToHTML : function()
    {
        var toHtml = '';
    
        if ( this._keys.length > 0 )
        {
            toHtml += 'style="';
            
            for (var i = 0; i < this._keys.length; i++)
            {
                toHtml += this._keys[i];
                toHtml += ':';
                toHtml += this._values[i];
                toHtml += ';';
            }
            
            toHtml += '"';
        }
        
        return toHtml;
    } // ToHTML
}

/****************************************
*               RCHTMLObject
*****************************************/

/// <summary>
/// Richer Components - HTML Class
/// </summmary>
function RCHTMLObject( tag )
{
    this.Tag = tag;
    this.Style = new RCStyle();
    this.Attributes = new RCAttributeCollection();
    
    this.Controls = new Array();
} // RCHTMLObject

/// <summary>
/// Add a new HTML markup control to this instance
/// </summmary>
RCHTMLObject.prototype.AddControl = function( htmlControl )
{
    this.Controls[ this.Controls.length ] = htmlControl;
} // AddControl

/// <summary>
/// Returns the HTML markup for this instance
/// </summmary>
RCHTMLObject.prototype.ToHTML = function()
{
    var html = '';
    
    html += '<';
    html += this.Tag;
    html += ' ';
    html += this.Attributes.ToHTML();
    html += ' ';
    html += this.Style.ToHTML();
    html += '>';
    
    //Get the controls
    for (var i = 0; i < this.Controls.length; i++)
    {
        if ( eval( this.Controls[i].ToHTML ) )
            html += this.Controls[i].ToHTML();
        else
            html += this.Controls[i];
    }
    
    html += '</';
    html += this.Tag;
    html += '>';
    
    return html;
} // ToHTML