﻿//Global var that contains all the drop down buttons
//on the page
var RC_DropDownButtonList = new RCKeyValueCollection();

/****************************************
*            RCDropDownButton
*****************************************/

/// <summary>
/// Richer Components - Drop Down Button
/// </summmary>
function RCDropDownButton( calendar, id, tagElement, style, styleOver, stylePressed, styleDropDownList )
{
    this.ID = id;
    this._tag = tagElement;
    this._timerId = null;
    this.Title = null;
    this._listElement = null;
    this.SelectedItemIndex = -1;
    this.Calendar = calendar;
    
    //styles
    this._style = style;
    this._styleOver = styleOver;
    this._stylePressed = stylePressed;
    this._styleDropDownList = styleDropDownList;
    
    //mouse state
    this._mouseOver = false;
    this._mousePressed = false;

    //collections
    this.Items = new RCKeyValueCollection();
    this.Style = new RCStyle();
    this.Attributes = new RCAttributeCollection();
    this._controls = new Array();
    
    //Set the attributes
    this.Attributes.Add('id', this.ID);
    this.Attributes.Add('class', this._style);
    this.Attributes.Add('onmouseover', 'RC_DropDownButtonList.Get(\'' + this.ID + '\').OnMouseOver()');
    this.Attributes.Add('onmouseout', 'RC_DropDownButtonList.Get(\'' + this.ID + '\').OnMouseOut()');
    this.Attributes.Add('onmousedown', 'RC_DropDownButtonList.Get(\'' + this.ID + '\').OnMouseDown()');
    /*this.Attributes.Add('ondragstart', 'return false');
    this.Attributes.Add('ondrag', 'return false');
    this.Attributes.Add('ondragend', 'return false');
    this.Attributes.Add('onselect', 'return false');
    this.Attributes.Add('onselectstart', 'return false');
    this.Attributes.Add('onselectionchange', 'return false');*/
    
    //Set style
    this.Style.Add( '-moz-user-select', 'none' );
    
    //Attach the mouse up handler
    var thisRef = this;
    var handler = function()
                    {
                        thisRef.OnMouseUp();
                    };
    AttachEventHandler(document, 'mouseup', handler);
    
    //Register this drop down button instance
    RC_DropDownButtonList.Add( this.ID, this );
} // RCDropDownButton

/// <summary>
/// Richer Components - Drop Down Button Class Prototype
/// </summary>
RCDropDownButton.prototype = 
{
    /// <summary>
    /// Drop down the list
    /// </summary>
    DropDown : function()
    {
        var div = null;
        var table = null;
        var row = null;
        var cell = null;
        var thisElement = null;
        var thisElementPos = null;
        
        //Do not hide the calendar popup on the next mouse up
        this.Calendar._hideOnNextMouseUp = false;
        
        thisElement = $( this.ID );
        thisElementPos = $Pos( this.ID );
        
        div = new RCHTMLObject( 'div' );
        div.Attributes.Add( 'class', this._styleDropDownList );
        div.Style.Add( 'position', 'absolute' );
        div.Style.Add( 'top', ( thisElementPos[1] + GetHeight(thisElement) ) + 'px' );
        div.Style.Add( 'left', thisElementPos[0] + 'px' );
        div.Style.Add( 'z-index', '999999' );
        
        div.Attributes.Add('ondragstart', 'return false');
        div.Attributes.Add('ondrag', 'return false');
        div.Attributes.Add('ondragend', 'return false');
        div.Attributes.Add('onselect', 'return false');
        div.Attributes.Add('onselectstart', 'return false');
        div.Attributes.Add('onselectionchange', 'return false');
        
        table = new RCHTMLObject( 'table' );
        table.Attributes.Add( 'cellpadding', 0 );
        table.Attributes.Add( 'cellspacing', 0 );
        div.AddControl( table );
    
        for (var i = 0; i < this.Items.GetSize(); i++)
        {
            row = new RCHTMLObject( 'tr' );
            cell = new RCHTMLObject( 'td' );
            row.AddControl( cell );
            table.AddControl( row );
            
            //cell.Attributes.Add( 'id', this.ID + '_' + i );
            cell.Attributes.Add( 'key', this.Items.GetKeyAt(i) );
            cell.Attributes.Add( 'index', i );
            cell.Attributes.Add( 'onmouseover', 'RC_DropDownButtonList.Get(\'' + this.ID + '\').Item_OnMouseOver(this)' );
            cell.Attributes.Add( 'onmouseout', 'RC_DropDownButtonList.Get(\'' + this.ID + '\').Item_OnMouseOut(this)' );
            cell.Attributes.Add( 'onmouseup', 'RC_DropDownButtonList.Get(\'' + this.ID + '\').Item_OnMouseUp(this)' );
            cell.AddControl( this.Items.GetValueAt(i) );
            
            if ( this.SelectedItemIndex == i )
                cell.Attributes.Add( 'class', 'selected' );
        }
        
        this._listElement = document.createElement( 'div' );
        this._listElement.innerHTML = div.ToHTML();
        
        document.body.appendChild( this._listElement );
    }, // DropDown
    
    /// <summary>
    /// Item - On Mouse Up Handler
    /// </summary>
    Item_OnMouseUp : function( itemElement )
    {
        $BroadcastMessage( this, 'OnItemSelected', itemElement.getAttribute('key') );
    }, // Item_OnMouseUp
    
    /// <summary>
    /// Item - On Mouse Out Handler
    /// </summary>
    Item_OnMouseOut : function( itemElement )
    {
        if ( itemElement.getAttribute('index') != this.SelectedItemIndex )
            itemElement.className = '';
    }, // Item_OnMouseOut
    
    /// <summary>
    /// Item - On Mouse Over Handler
    /// </summary>
    Item_OnMouseOver : function( itemElement )
    {
        if ( itemElement.getAttribute('index') != this.SelectedItemIndex )
            itemElement.className = 'over';
    }, // Item_OnMouseOver
    
    /// <summary>
    /// On Mouse Up Handler
    /// </summary>
    OnMouseUp : function()
    {
        var element = $( this.ID );
        
        if ( element != null )
        {
            this._mousePressed = false;
            
            if ( this._listElement != null )
            {
                document.body.removeChild( this._listElement );
                this._listElement = null;
            }
            
            //Clear the old time out
            if ( this._timerId != null )
                window.clearTimeout( this._timerId );
            
            if ( this._mouseOver )
                element.className = this._styleOver;
            else
                element.className = this._style;
        }
    }, // OnMouseUp
    
    /// <summary>
    /// On Mouse Down Handler
    /// </summary>
    OnMouseDown : function()
    {
        var element = $( this.ID );
        var thisRef = this;
        
        this._mousePressed = true;
        element.className = this._stylePressed;
        
        //Clear the old time out
        if ( this._timerId != null )
            window.clearTimeout( this._timerId );
            
        //Set the timeout
        this._timerId = window.setTimeout( function() { thisRef.DropDown(); }, 200 );
        
        return false;
    }, // OnMouseDown
    
    /// <summary>
    /// On Mouse Out Handler
    /// </summary>
    OnMouseOut : function()
    {
        var element = $( this.ID );
        
        this._mouseOver = false;
        if ( ! this._mousePressed )
            element.className = this._style;
    }, // OnMouseOut
    
    /// <summary>
    /// On Mouse Over Handler
    /// </summary>
    OnMouseOver : function()
    {
        var element = $( this.ID );
        
        this._mouseOver = true;
        if ( ! this._mousePressed )
            element.className = this._styleOver;
    }, // OnMouseOver
    
    /// <summary>
    /// Add a new HTML markup control to this instance
    /// </summmary>
    AddControl : function( htmlControl )
    {
        this._controls[ this._controls.length ] = htmlControl;
    }, // AddControl
    
    /// <summary>
    /// Returns the HTML markup code
    /// </summary>
    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
} // RCDropDownButton.prototype