/**************************************************************************
*
*  @@@BUILDINFO@@@ 03broadcaster-2.jsx 2.0.0.54  08-Feb-2007
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
* and technical concepts contained herein are proprietary to  Adobe Systems 
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
* Patents,patents in process,and are protected by trade secret or copyright 
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from 
* Adobe Systems Incorporated.
**************************************************************************/

function Broadcaster()
{
    this.clients        = [];
    
    this.inBroadcast    = false;
    this.delayedRemove  = [];
}

//-----------------------------------------------------------------------------
// 
// registerClient(...)
// 
// Purpose: un/register client object to receive notifications
//          (client objects have to support a function "onNotify(reason)" )
// 
//-----------------------------------------------------------------------------

Broadcaster.prototype.registerClient = function( clientObj )
{
    if( clientObj && clientObj.onNotify )
    {
        for( var i=0; i<this.clients.length; i++ )
            if( this.clients[i] == clientObj )
                return true;
                
        this.clients.push( clientObj );            
        
        return true;
    }
    
    return false;
}

Broadcaster.prototype.unregisterClient = function( clientObj )
{
    for( var i=0; i<this.clients.length; i++ )
    {
        if( this.clients[i] == clientObj )
        {
            if( this.inBroadcast )
                this.delayedRemove.push( this.clients[i] );
            else
                this.clients.splice( i, 1 );
        }
    }
}

Broadcaster.prototype.unregisterAllClients = function()
{
    while( this.clients.length > 0 )
        this.clients.pop();
}

//-----------------------------------------------------------------------------
// 
// notifyClients(...)
// 
// Purpose: notify registered client object about changes related to the
//          targets list
// 
//-----------------------------------------------------------------------------

Broadcaster.prototype.notifyClients = function( reason, param01, param02, param03, param04, param05 )
{
	if( appInShutDown && reason != 'shutdown' )
		return;

    this.inBroadcast = true;
    		
    for( var i=0; i<this.clients.length; i++ )
    {
        var removed = false;
        
        if( this.delayedRemove.length > 0 )
        {
            for( var r=0; r<this.delayedRemove.length && !removed; r++ )
                removed = ( this.delayedRemove[r] == this.clients[i] );
        }
        
        if( !removed )
            this.clients[i].onNotify( reason, param01, param02, param03, param04, param05 );
    }
    
    this.inBroadcast = false;
    
    if( this.delayedRemove.length > 0 )
    {
        while( this.delayedRemove.length > 0 )
        {
            this.unregisterClient( this.delayedRemove[0] );
            this.delayedRemove.splice( 0, 1 );
        }
    }
}
