Memberfuse.WndManager ={
    windows: new Hash(),
    attachWnds: function(parent){
        Memberfuse.WndManager.attachCtrls('CtrlMdl',Memberfuse.WndManager.attachCtrlMdl,parent);
        Memberfuse.WndManager.attachCtrls('CtrlWnd',Memberfuse.WndManager.attachCtrlWnd,parent);
        Memberfuse.WndManager.attachCtrls('CtrlTip',Memberfuse.WndManager.attachCtrlTip,parent);
    },
    attachCtrls: function(className,attachFunc,parent){
        if(parent){
            if(Object.isElement(parent)){
                $A($(parent).getElementsByClassName(className)).each(function(e){
                    attachFunc(e);
                });
            }
        }
        else{
            $$('.' + className).each(function(e){
                attachFunc(e);
            });
        }
    },
    attachCtrlWnd: function(e){
	    var ctrl = new Control.Window(
	        $(e),
	        {
	            className: 'window ctrlCntr',
	            draggable: true,
	            closeOnClick: false,
	            afterInitialize: Memberfuse.WndManager.indexWnd,
	            onRemoteContentLoaded: Memberfuse.WndManager.initWndContent
	        }
	    );
    },
    attachCtrlMdl: function(e){
        var modal_top = new Element('div',{  
            className: 'modalbox_top'  
        }); 
        var closeLnk = new Element('a');
        closeLnk.onclick = function(){
            Memberfuse.WndManager.closeWindow(this);
        };
        closeLnk.insert('Close');
        modal_top.insert(closeLnk);
        if(e.title){
             var title = new Element('h1');
             title.insert(e.title);
             modal_top.insert(title);
        }
        var modal_middle = new Element('div',{  
            className: 'modalbox_middle'  
        }); 
        var modal_bottom = new Element('div',{  
            className: 'modalbox_bottom'  
        }); 
        var ctrl = new Control.Modal(
            $(e),
            {
                className: 'modalbox ctrlCntr',
                closeOnClick: 'overlay',
                afterInitialize: Memberfuse.WndManager.indexWnd,
                insertRemoteContentAt: modal_middle,
                fade: true,
                fadeDuration: 0.15,
                onRemoteContentLoaded: Memberfuse.WndManager.initWndContent
            }
        );
        ctrl.container.insert(modal_top);
        ctrl.container.insert(modal_middle);
        ctrl.container.insert(modal_bottom);
    },
    attachCtrlTip: function(e){
        if(e.title){
	        var ctrl = new Control.ToolTip(
	            $(e),
	            e.title,
	            {
	                className: 'tooltip ctrlCntr',
	                afterOpen: function(){this.attachedElement.title="";}
	            }
	        );
	        ctrl.attachedElement = e;
	   }
    },
    indexWnd: function(){
        Memberfuse.WndManager.windows.set(this.container.id,this);
    },
    initWndContent: function(){
        Memberfuse.init(this.container);
    },
    closeWindow: function closeWindow(e){
	    var cntr = e.up('.ctrlCntr');
	    if(cntr){
	        var w = Memberfuse.WndManager.windows.get(cntr.id);
	        if(w instanceof Control.Modal){
	           Control.Modal.close();
	        }
	        else if(w instanceof Control.Window){
	           w.close();
	        }
	    }
	    
	}
};



