$package('ejs.manager',
    $class('ComponentManager').$define(
        $private({
            managerList:null    
        }),
        $public({
            construct:function(){
                if(ejs.manager.ComponentManager.__instance){
                    throw new Error('Class ComponentManager is the singleton mode.');
                }
                this._$managerList = {};
            },
            add:function(/*Component*/component){
                if(component.id!="" && !this._$managerList[component.id]){
                    this._$managerList[component.id] = component;
                    return true;
                }
                else
                    return false;
            },
            remove:function(/*string*/id){
                if(this._$managerList[id]){
                    delete this._$managerList[id];
                }
                else{
                    return false;
                }
            },
            get:function(/*string*/id){
                return this._$managerList[id];    
            }
        }),
        $static({
            __instance:null,
            getInstance:function(){
                if(!ejs.manager.ComponentManager.__instance){
                    ejs.manager.ComponentManager.__instance = new ejs.manager.ComponentManager();
                }
                return ejs.manager.ComponentManager.__instance;
            }
        })
    )        
);
$package('ejs.controls',
    $class('Component').$implements(ejs.events.IEventDispatcher).$define(
        $protected({
            width:'auto',
            height:'auto',
            createComponent:function(){
                if(!this.renderTo)
                    return false;
                return true;
            } 
        }),
        $public({
            id:'',
            renderTo:null,
            skinClass:'',
            $self:null,
            construct:function(){
                this.id = arguments[0];
                this.$self = null;
                this._width = 'auto';
                this._height = 'auto';
                this.dispatchEvent(new ejs.events.Event(ejs.events.Event.INITIALIZE));
                ejs.manager.ComponentManager.getInstance().add(this);
            },
            render:function(){
                if(this._createComponent())
                    this.dispatchEvent(new ejs.events.Event(ejs.events.Event.COMPLETE));
            },
            setWidth:function(value){
                this._width = ejs.isNumeric(value)?value:'auto';
            },
            setHeight:function(value){
                this._height = ejs.isNumeric(value)?value:'auto';
            },
            getWidth:function(){
                return this._width;
            },
            getHeight:function(){
                return this._height;
            }
        })
    )      
);
//根据id获得控件
ejs.getComponent = function(id){
    return ejs.manager.ComponentManager.getInstance().get(id);
};
