(function(){
    
    IApp_Portlet_Composite = function(){
        this._instances = [];    
        this._calls = [];
        this.registerMethods( IApp_Portlet.prototype );
    };
    
    IApp_Portlet_Composite.prototype = {
        addInstance : function( portlet ){
            if( ! this.hasInstance( portlet ) ){
                this._instances.push( portlet );
                this.registerMethods( portlet );
                
                for( var j = 0; j < this._calls.length; j++ ){
                    if( typeof portlet[ this._calls[j][0] ] == 'function' )
                        portlet[ this._calls[j][0] ].apply( portlet, this._calls[j][1] );
                }
            }
        },
    
        hasInstance : function( portlet ){
            if( $.inArray( portlet, this._instances) < 0 )
                return false;
            
            return true;
        },
        
        registerMethods : function( portlet ){
            var self = this;
            for( x in portlet ){
                if( typeof portlet[x] == 'function' && typeof this[x] == 'undefined' )
                    this.registerMethod( x );
            }
        },
        
        registerMethod : function ( method ){
            var self = this;
            this[method] = function(){ self.compositeMethod( method, arguments ) };
        },
        
        compositeMethod : function( method, args ){
            this._calls.push( arguments );
            $.each( this._instances, function(){ 
                this[method].apply( this, args );
            });
        }
    };
    
})(jQuery);
