﻿Type.registerNamespace( 'ControlTests' );

/// <summary>
/// NewsgroupView constructor.
/// </summary>

ControlTests.NewsgroupView = function( element ) {
    
    ControlTests.NewsgroupView.initializeBase( this, [element] );
    
    this._newsgroupButtons = new Array();
    this._newsgroupButtonIDs = null;
    this._newsgroupButtonIDsParsed = false;
    this._threadView = null;
    this._postbackElement = null;
    this._pageRequestManager = null;
    this._currentlySelectedNewsgroupButton = null;
    this._initializeRequestHandler = null;
    this._pageLoadedHandler = null;
    this._newsgroupSelectedHandler = null;
    this._blockMultipleSelectionsOnSameNewsgroup = true;

}

/// <summary>
/// NewsgroupView prototype.
/// </summary>

ControlTests.NewsgroupView.prototype = {

    /// <summary>
    /// Initializes this object. Subscribes to the initializeRequest and 
    /// pageLoaded events of the PageRequestManager. Also listens for the
    /// application load event.
    /// </summary>

    initialize : function() {
    
        ControlTests.NewsgroupView.callBaseMethod( this, 'initialize' );
        
        this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
        this._initializeRequestHandler = Function.createDelegate( this, this._initializeRequest );
        this._pageLoadedHandler = Function.createDelegate( this, this._pageLoaded );
        this._applicationLoadedHandler = Function.createDelegate( this, this._applicationLoaded );
        this._newsgroupSelectedHandler = Function.createDelegate( this, this._newsgroupSelected );
                    
        if ( this._pageRequestManager ) {

            this._pageRequestManager.add_initializeRequest( this._initializeRequestHandler );
            this._pageRequestManager.add_pageLoaded( this._pageLoadedHandler );

        }
        
        Sys.Application.add_load( this._applicationLoadedHandler );

    },
    
    /// <summary>
    /// Handles the application load event. Parses the newsgroup button IDs,
    /// retrieves the client side component associated with the ID, and 
    /// subscribes to the newsgroupSelected event for each.
    /// </summary>
    
    _applicationLoaded : function() {
    
        if ( this._newsgroupButtonIDs && !this._newsgroupButtonIDsParsed ) {

            var idSplit = this._newsgroupButtonIDs.split( '|' );
            for ( i=0; i < idSplit.length; i++ ) {
            
                newsgroupButtonObj = $find( idSplit[ i ] ); 
                Array.add( this._newsgroupButtons, newsgroupButtonObj );
                
                newsgroupButtonObj.add_newsgroupSelected( this._newsgroupSelectedHandler );
                
            }
            
            this._newsgroupButtonIDsParsed = true;
                
        }
    
    },
        
    _initializeRequest : function( sender, args ) {

        this._postbackElement = args.get_postBackElement();
                
    },

    /// <summary>
    /// Handles the pageLoaded event raised by the PageRequestManager.
    /// </summary>
    /// <param name="sender">The PageRequestManager object that raised this 
    /// event.</param>
    /// <param name="args">An EventArgs object containing event data.</param>
    
    _pageLoaded : function ( sender, args ) {
             
//        if ( this._currentlySelectedNewsgroupButton && ( this._postbackElement.id == this._currentlySelectedNewsgroupButton.get_ID() ) ) {
//        }        
                        
    },
    
    /// <summary>
    /// Gets a value indicating whether multiple selections of the same 
    /// newsgroup are processed (i.e. newsgroup is already selected, and
    /// user selects it again).
    /// </summary>
    
    get_blockMultipleSelectionsOnSameNewsgroup : function() {
        return this._blockMultipleSelectionsOnSameNewsgroup;
    },
    
    /// <summary>
    /// Gets a value indicating whether multiple selections of the same 
    /// newsgroup are processed (i.e. newsgroup is already selected, and
    /// user selects it again).
    /// </summary>
    
    set_blockMultipleSelectionsOnSameNewsgroup : function( value ) {
    
        if ( this._blockMultipleSelectionsOnSameNewsgroup !== value ) {
        
            this._blockMultipleSelectionsOnSameNewsgroup = value;
            this.raisePropertyChanged( 'blockMultipleSelectionsOnSameNewsgroup' );
                        
        }
    
    },

    /// <summary>
    /// Gets the concatenated string of newsgroup button IDs.
    /// </summary>
    
    get_newsgroupButtonIDs : function() {
        return this._newsgroupButtonIDs;
    },
    
    /// <summary>
    /// Sets the concatenated string of newsgroup button IDs.
    /// </summary>
    /// <param name="value">The newsgroup button IDs.</param>
    
    set_newsgroupButtonIDs : function( value ) {
    
        if ( this._newsgroupButtonIDs !== value ) {
        
            this._newsgroupButtonIDs = value;
            this.raisePropertyChanged( 'newsgroupButtonIDs' );
                        
        }
    
    },
    
    /// <summary>
    /// Gets the ThreadView component associated with this control.
    /// </summary>
    
    get_threadView : function() {
        return this._threadView;
    },
    
    /// <summary>
    /// Sets the ThreadView component associated with this control.
    /// </summary>
    /// <param name="value">The thread view control ID.</param>

    set_threadView : function( value ) {
    
        if ( this._threadView !== value ) {

            this._threadView = value;
            this.raisePropertyChanged( 'threadView' );
        
        }
    
    },

    /// <summary>
    /// Gets the selected newsgroup button.
    /// </summary>

    get_selectedNewsgroupButton : function() {
        return this._currentlySelectedNewsgroupButton;
    },
    
    /// <summary>
    /// Sets the selected newsgroup button.
    /// </summary>
    /// <param name="value">The selected newsgroup button.</param>

    set_selectedNewsgroupButton : function( value ) {

        if ( this._currentlySelectedNewsgroupButton !== value ) {

            if ( this._currentlySelectedNewsgroupButton ) {
                this._currentlySelectedNewsgroupButton.deselect();
            }

            if ( value ) {
                value.select();
            }
                
            this._currentlySelectedNewsgroupButton = value;
            this.raisePropertyChanged( 'currentlySelectedNewsgroupButton' );
            
        }

    },

    /// <summary>
    /// Handles the newsgroupSelected event raised by the newsgroup buttons
    /// when they are clicked.
    /// </summary>
    /// <param name="sender">The NewsgroupButton object that raised this 
    /// event.</param>
    /// <param name="args">An EventArgs object containing event data.</param>

    _newsgroupSelected : function( sender, eventArgs ) {
                             
        var raiseEvent = false;

        if ( this._currentlySelectedNewsgroupButton ) {
            
            if ( this._currentlySelectedNewsgroupButton != sender || ( this._currentlySelectedNewsgroupButton == sender && !this._blockMultipleSelectionsOnSameNewsgroup ) ) {
                            
                if ( this._currentlySelectedNewsgroupButton != sender ) {
                    this._currentlySelectedNewsgroupButton.deselect();
                }
                
                this._threadView.get_loader().cancelFade();
                this._threadView.loadArticles( sender.get_clickPostbackFunction() );        

                raiseEvent = true;
            
            }
        
        } else {

            this._threadView.loadArticles( sender.get_clickPostbackFunction() );        
            raiseEvent = true;

        }
                      
        this._currentlySelectedNewsgroupButton = sender;
            
        if ( raiseEvent ) {
            
            // raise newsgroup selected event to notify external listeners that
            // a newsgroup has been selected.
            this.raiseNewsgroupSelected( eventArgs );

        }            

    },
    
    /// <summary>
    /// Attaches the provided handler to the newsgroupSelected event.
    /// </summary>
    /// <param name="handler">The handler that is to be attached to the event.</param>

    add_newsgroupSelected : function( handler ) {
        this.get_events().addHandler( 'newsgroupSelected', handler );
    },

    /// <summary>
    /// Detaches the provided handler from the newsgroupSelected event.
    /// </summary>
    /// <param name="handler">The handler that is to be detached from the 
    /// event.</param>

    remove_newsgroupSelected : function( handler ) {
        this.get_events().removeHandler( 'newsgroupSelected', handler );
    },

    /// <summary>
    /// Raises the newsgroupSelected event.
    /// </summary>
    /// <param name="eventArgs">An EventArgs object containing event data.</param>

    raiseNewsgroupSelected : function( eventArgs ) {
    
        var handler = this.get_events().getHandler( 'newsgroupSelected' );

        if ( handler ) {
            handler( this, eventArgs );
        }
    
    },
    
    /// <summary>
    /// Handles the threadsLoaded event raised by the thread view
    /// when its threads have been loaded.
    /// </summary>
    /// <param name="sender">The ThreadView object that raised this 
    /// event.</param>
    /// <param name="args">An EventArgs object containing event data.</param>

    _threadsLoaded : function( sender, eventArgs ) {

        this._buttonShield.style.visibility = 'hidden';

    },
    
    /// <summary>
    /// Release resources before the control is disposed.
    /// </summary>
    
    dispose : function() {

        if ( this._pageRequestManager ) {
        
            this._pageRequestManager.remove_pageLoaded( this._pageLoadedHandler );
            this._pageRequestManager.remove_initializeRequest( this._initializeRequestHandler );
        
        }
        
        if ( this._newsgroupButtons ) {
                
            while ( this._newsgroupButtons.length > 0 ) {
                Array.dequeue( this._newsgroupButtons ).remove_newsgroupSelected( this._newsgroupSelectedHandler );
            }
                    
        } 
        
        if ( this._applicationLoadHandler ) {
            Sys.Application.remove_load( this._applicationLoadHandler );
        }
               
        ControlTests.NewsgroupView.callBaseMethod( this, 'dispose' );
        
    }
           
} // ControlTests.NewsgroupView.prototype

ControlTests.NewsgroupView.registerClass( 'ControlTests.NewsgroupView', Sys.UI.Control );

if ( typeof( Sys ) != 'undefined' ) Sys.Application.notifyScriptLoaded();


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();