NFi.WndManager = {
    windows: new Hash(),
    attachWnds: function(parent){
        NFi.WndManager.attachCtrls('CtrlMdl',NFi.WndManager.attachCtrlMdl,parent);
        NFi.WndManager.attachCtrls('CtrlCnfrm',NFi.WndManager.attachCtrlCnfrm,parent);
        NFi.WndManager.attachCtrls('CtrlWnd',NFi.WndManager.attachCtrlWnd,parent);
        NFi.WndManager.attachCtrls('CtrlTip',NFi.WndManager.attachCtrlTip,parent);
        NFi.WndManager.attachCtrls('CtrlHvr',NFi.WndManager.attachCtrlHvr,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 options = {
            className: 'window',
            draggable: true,
            closeOnClick: false
        };
        NFi.WndManager.extendOptions(options,e,'CtrlWnd');
        options.className += ' ctrlCntr';
        options.afterInitialize = NFi.WndManager.indexWnd;
        options.onRemoteContentLoaded = NFi.WndManager.initWndContent;
	    var ctrl = new Control.Window(
	        $(e),
	        options
	    );
    },
    attachCtrlMdl: function(e){
        var modal_top = new Element('div',{  
            className: 'top'  
        }); 
        var closeLnk = new Element('a');
        closeLnk.onclick = function(){
            NFi.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: 'middle'  
        }); 
        var modal_bottom = new Element('div',{  
            className: 'bottom'  
        }); 
        
        //start with the default options
        var options = {
            className: 'modalbox',
            closeOnClick: 'overlay',
            insertRemoteContentAt: modal_middle,
            fade: true,
            fadeDuration: 0.15,
            reloadContent: true
        };
        
        //if extended options were specified in the rel
        //attribute use those
        NFi.WndManager.extendOptions(options,e,'CtrlMdl');
        
        //reset or force the options we must have to operate
        //so the rel tag doesn't override them
        options.className += ' ctrlCntr';
        options.afterInitialize = NFi.WndManager.indexWnd;
        options.onRemoteContentLoaded = NFi.WndManager.initWndContent;
        
        var ctrl = new Control.Modal(
            $(e),
            options
        );
        ctrl.container.insert(modal_top);
        ctrl.container.insert(modal_middle);
        ctrl.container.insert(modal_bottom);
    },
    attachCtrlCnfrm: function(e){
        var modal_top = new Element('div',{  
            className: 'top'  
        }); 
        var title = new Element('h2');
        if(e.title)
        {
             title.insert(e.title);
        }
        else
        {
        	title.insert("Confirm?");
        }
        modal_top.insert(title);
        var modal_middle = new Element('div',{  
            className: 'middle'  
        }); 
        var modal_bottom = new Element('div',{  
            className: 'bottom'  
        }); 
        
        //start with the default options
        var options = {
            className: 'confirmbox',
            closeOnClick: 'overlay',
            insertRemoteContentAt: modal_middle,
            fade: true,
            fadeDuration: 0.15,
            position: 'relative',
            offsetLeft: -240,
            offsetTop: 10,
            overlayOpacity: 0
            //constrainToViewport: true
        };
        
        //if extended options were specified in the rel
        //attribute use those
        NFi.WndManager.extendOptions(options,e,'CtrlCnfrm');
        
        options.className = options.className.gsub('modalbox', ' ');
        
        //reset or force the options we must have to operate
        //so the rel tag doesn't override them
        options.className += ' ctrlCntr';
        options.afterInitialize = NFi.WndManager.indexWnd;
        options.onRemoteContentLoaded = NFi.WndManager.initWndContent;
        
        var ctrl = new Control.Window(
            $(e),
            options
        );
        ctrl.container.insert(modal_top);
        ctrl.container.insert(modal_middle);
        ctrl.container.insert(modal_bottom);
    },
    attachCtrlTip: function(e){
        var options = {
            className: 'tooltip',
            afterOpen: function(){this.attachedElement.title="";},
            offsetLeft: 25,
            position: 'relative'
        };
        NFi.WndManager.extendOptions(options,e,'CtrlTip');
        options.className += ' ctrlCntr';
        if(e.title){
	        var ctrl = new Control.ToolTip(
	            $(e),
	            e.title,
	            options
	        );
	        ctrl.attachedElement = e;
	   }
    },
    attachCtrlHvr: function(e){
        var options = {
            className: 'hoverbox',
            offsetLeft: 10,
            offsetTop: 10,
            position: 'mouse',
            hover: true
        };
        NFi.WndManager.extendOptions(options,e,'CtrlHvr');
        options.className += ' ctrlCntr';
	    var ctrl = new Control.Window(
	        $(e),
	        options
	    );
    },
    extendOptions: function(options,element,type){
    	
        if(element.rel){
        
            var extendedOpts = eval('(' + element.rel + ').' + type);
            if(extendedOpts){
                Object.extend(options,extendedOpts);
            }
        }
    },
    indexWnd: function(){
        NFi.WndManager.windows.set(this.container.id,this);
    },
    initWndContent: function(){
        NFi.init(this.container);
    },
    closeWindow: function closeWindow(e){
	    var cntr = e.up('.ctrlCntr');
	    if(cntr){
	        var w = NFi.WndManager.windows.get(cntr.id);
	        if(w instanceof Control.Modal){
	           Control.Modal.close();
	        }
	        else if(w instanceof Control.Window){
	           w.close();
	        }
	    }
	    
	}
};
