﻿/*
	Comsite Ajax Library - http://www.ComSite.org
 	by Jay Kappel, founder of ComSite.

	Permission is freely granted to copy, modify, merge, publish, distribute,
	sublicense, and/or sell copies of this Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL 
	THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
	CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
	DEALINGS IN THE SOFTWARE.
*/

// Dependencies for csAjaxWindows.js:

/// <reference path="csAjax.js" />
/// <reference path="csAjaxEvent.js" />
/// <reference path="csAjaxDrag.js" />
/// <reference path="csAjaxUtils.js" />
/// <reference path="csAjaxWindowsConfig.js" />
    
    csAjax.windows = {};
    csAjax.windows.loaded       = true;
    csAjax.windows.windowList   = new Array();
    csAjax.windows.activeWindow = null;

    
    csAjax.windows.instanciate = function() {
        if (csAjax.drag.loaded) {
            csAjax.drag.minWidth = csAjax.windows.config.minimumWindowWidth;
            csAjax.drag.minHeight = csAjax.windows.config.minimumWindowHeight;
        }
        
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','afterInstanciate',this)) return;
        return true;
    }
    
    csAjax.windows.getUniqueID = function() {
        var oDate = new Date(); 
        return oDate.getTime();
    }
        
    csAjax.windows.window = function(options) {
        options = options || {};
        
        var _this = this;
        this.loading = true;
        this.element;
        
        this.id             = unescape(csAjax.utils.nvl(options.id,'csAjaxWindow'+csAjax.windows.getUniqueID()));
        this.className      = unescape(csAjax.utils.nvl(options.className,csAjax.windows.config.defaultWindowClassName));
        this.width          = csAjax.utils.nvl(options.width, csAjax.windows.config.minimumWindowWidth);
        this.height         = csAjax.utils.nvl(options.height, csAjax.windows.config.minimumWindowHeight); 
        this.left           = csAjax.utils.nvl(options.left, '0px');
        this.top            = csAjax.utils.nvl(options.top, '0px');
        this.windowState    = csAjax.utils.nvl(options.windowState, 'normal');
        this.title          = unescape(csAjax.utils.nvl(options.title, 'New Window'));
        this.content        = unescape(csAjax.utils.nvl(options.content, ''));
        
        this.locked         = (typeof options.locked =='boolean')? options.locked : false;
        this.allowDrag      = (typeof options.allowDrag =='boolean')? options.allowDrag : true;
        this.allowLock      = (typeof options.allowLock =='boolean')? options.allowLock : true;
        this.allowMenu      = (typeof options.allowMenu =='boolean')? options.allowMenu : true;
        this.allowClose     = (typeof options.allowClose =='boolean')? options.allowClose : true;
        this.allowResize    = (typeof options.allowResize =='boolean')? options.allowResize : true;
        this.allowMinimize  = (typeof options.allowMinimize =='boolean')? options.allowMinimize : true;
        
        this.elms           = {};           // Namespace for all the child elements to load under
        this.dragObjs       = new Array();  // Array for tracking all the drag objects associated with the window
        this.render();

        csAjax.windows.registerWindow(this);
        this.loading=false; 

        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',this);
            if (!csAjax.eventTracker('windows','afterWindowCreated',this)) return;
        }
    }
    
    
    csAjax.windows.window.prototype.render = function(parent) {
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','beforeWindowRender',this)) return;
        if (!parent) parent=document.getElementsByTagName('body')[0];
        
        this.elms.window = document.createElement('div');
        parent.appendChild(this.elms.window);
        
        // Create all the window objects
        for (var c=0; c < csAjax.windows.config.defaultWindowObjects.length; c++) {
            var propName = csAjax.windows.config.defaultWindowObjects[c];
            
            this.elms[propName] = document.createElement('div');
            this.elms[propName].defaultClassName = propName;
            this.elms[propName].className = propName;
            this.elms.window.appendChild(this.elms[propName]);
            
            if (propName.substring(0,2)=='tb')
                csAjax.event.add(this.elms[propName],'click',this.toolButtonClick,true);
        }

        // Create the content Frame
        this.elms.iFrame = document.createElement('iframe');
        this.elms.iFrame.setAttribute("allowtransparency", "true");
        this.elms.iFrame.setAttribute("frameBorder", "0");
        this.elms.iFrame.className='contentFrame';
        this.elms.iFrame.id=this.id+'Frame';
        this.elms.window.appendChild(this.elms.iFrame);        
        this.elms.window.jsWindow = this;

        csAjax.event.add(this.elms.window,'mousemove',this.mouseMove,true);
        csAjax.event.add(this.elms.window,'mouseover',this.mouseOver,true);
        csAjax.event.add(this.elms.window,'mouseout',this.mouseOut,true);
        csAjax.event.add(this.elms.focusHandler,'click',this.activate,true);

        this.refresh();
        csAjax.windows.activateWindow(this);
        if (csAjax.winBar.loaded) csAjax.winBar.registerWindow(this);
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','afterWindowRender',this)) return;
    }
    

    csAjax.windows.window.prototype.refresh = function() {
        var elm = this.elms.window;
        var gs = csAjax.windows.config.dragGridSize;
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','beforeWindowRefresh',this)) return;
        
        elm.id              = this.id;
        elm.className       = this.className;
        elm.style.width     = (parseInt(parseInt(this.width)/gs)*gs)+'px';
        elm.style.height    = (parseInt(parseInt(this.height)/gs)*gs)+'px';
        elm.style.left      = (parseInt(parseInt(this.left)/gs)*gs)+'px';
        elm.style.top       = (parseInt(parseInt(this.top)/gs)*gs)+'px';
        elm.style.position  = 'absolute';
        elm.style.display   = 'block';
        
        this.elms.titleBar.innerHTML = this.title;
        if (this.elms.miniWin) this.elms.miniWin.innerHTML = this.title;
        this.setContent(this.content);
        this.dragObjs = new Array();
                    
        if (this.allowDrag) {
            if (csAjax.drag.loaded) this.dragObjs.push(csAjax.drag.addDragItem(this.elms.titleBar,this.elms.window,'drag'));
            this.elms.titleBar.className = this.elms.titleBar.defaultClassName;
        } else {
            if (csAjax.drag.loaded) csAjax.drag.removeDragItem(this.elms.titleBar);
            this.elms.titleBar.className = this.elms.titleBar.defaultClassName+'Locked';
        }
        
        if (this.allowResize && csAjax.drag.loaded) {
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleTL,this.elms.window,'resizeTL'));
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleTC,this.elms.window,'resizeT'));
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleTR,this.elms.window,'resizeTR'));
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleCL,this.elms.window,'resizeL'));
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleCR,this.elms.window,'resizeR'));
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleBL,this.elms.window,'resizeBL'));
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleBC,this.elms.window,'resizeB'));
            this.dragObjs.push(csAjax.drag.addDragItem(this.elms.resizeHandleBR,this.elms.window,'resizeBR'));
        } else if (csAjax.drag.loaded) {
            csAjax.drag.removeDragItem(this.elms.resizeHandleTL);
            csAjax.drag.removeDragItem(this.elms.resizeHandleTC);
            csAjax.drag.removeDragItem(this.elms.resizeHandleTR);
            csAjax.drag.removeDragItem(this.elms.resizeHandleCL);
            csAjax.drag.removeDragItem(this.elms.resizeHandleCR);
            csAjax.drag.removeDragItem(this.elms.resizeHandleBL);
            csAjax.drag.removeDragItem(this.elms.resizeHandleBC);
            csAjax.drag.removeDragItem(this.elms.resizeHandleBR);
        }

        // Register the tool button actions
        if (this.allowClose) {
            this.elms.tbClose.title='close';
            this.elms.tbClose.className = this.elms.tbClose.defaultClassName;
            csAjax.event.add(this.elms.tbClose,'click',this.close,true);
        } else {
            this.elms.tbClose.title='';
            this.elms.tbClose.className += 'Disabled';
            csAjax.event.remove(this.elms.tbClose,'click',this.close,true);
        }
        
        if (this.allowLock) {
            this.elms.tbLock.title='lock/unlock';
            this.elms.tbLock.className = this.elms.tbLock.defaultClassName;
            csAjax.event.add(this.elms.tbLock,'click',this.toggleLock,true);
            if (this.locked) {
                this.locked=false;
                this.elms.tbLock.click();
            }
        } else {
            this.elms.tbLock.title='';
            this.elms.tbLock.className += 'Disabled';
            csAjax.event.remove(this.elms.tbLock,'click',this.toggleLock,true);
        }

        if (this.allowMenu) {
            this.elms.tbMenu.title='menu';
            this.elms.tbMenu.className = this.elms.tbMenu.defaultClassName;
        } else {
            this.elms.tbMenu.title='';
            this.elms.tbMenu.className += 'Disabled';
        }

        if (this.allowMinimize) {
            this.elms.tbHide.title='minimize';
            this.elms.tbHide.className = this.elms.tbHide.defaultClassName;
        } else {
            this.elms.tbHide.title='';
            this.elms.tbHide.className += 'Disabled';
        }

        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',this);
            if (!csAjax.eventTracker('windows','afterWindowRefresh',this)) return;
        }
    }


    csAjax.windows.window.prototype.setContent = function(sContent) {
        var oWin = this;
        if (!oWin.elms && editWindow) oWin = editWindow;
        if (csAjax.eventTracker) csAjax.eventTracker('windows','beforeSetContent',oWin);
        if (!sContent) sContent='';
        
        oWin.content = sContent;
        oWin.elms.iFrame.src='about:blank';

        if (csAjax.utils.isUrl(sContent)) {
            oWin.contentMode='url';
            oWin.elms.iFrame.src=sContent;
        } else {
            oWin.contentMode='html';
            var doc = oWin.elms.iFrame.contentWindow.document;
            doc.open(); doc.write(sContent); doc.close();
        }
        
        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',oWin);
            csAjax.eventTracker('windows','afterSetContent',oWin);
        }
    }
        
        
    csAjax.windows.window.prototype.setTitle = function(sTitle) {
        if (csAjax.eventTracker) csAjax.eventTracker('windows','beforeSetTitle',this);

        this.title = sTitle;
        this.elms.titleBar.innerHTML = this.title;

        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',this);
            csAjax.eventTracker('windows','afterSetTitle',this);
        }
    }

    
    csAjax.windows.window.prototype.setClassName = function(sClass) {
        if (csAjax.eventTracker) csAjax.eventTracker('windows','beforeSetClassName',this);

        this.className = sClass;
        this.elms.window.className = sClass;

        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',this);
            csAjax.eventTracker('windows','afterSetClassName',this);
        }
    }
    
    
    csAjax.windows.window.prototype.serialize = function(includeContent) {
        var elm = this.elms.window;
        if (!elm) return '';

        var sData = '';
        sData += "id: '" + escape(this.id) + "',";
        sData += "className: '" + escape(elm.className) + "',";
        sData += "top: '" + escape(elm.style.top) + "',";
        sData += "left: '" + escape(elm.style.left) + "',";
        sData += "width: '" + escape(elm.style.width) + "',";
        sData += "height: '" + escape(elm.style.height) + "',";
        sData += "title: '" + escape(this.title) + "',";
        sData += "windowState: '" + escape(this.windowState) + "',";
        
        sData += "locked: " + this.locked + ",";
        sData += "allowDrag: " + this.allowDrag + ",";
        sData += "allowLock: " + this.allowLock + ",";
        sData += "allowMenu: " + this.allowMenu + ",";
        sData += "allowClose: " + this.allowClose + ",";
        sData += "allowResize: " + this.allowResize + ",";
        sData += "allowMinimize: " + this.allowMinimize;
        
        if (includeContent) sData += ",content: '" + escape(this.content) + "'";
        
        //clipboardData.setData("Text", sData);
        return sData;
    }
    

    // *********************************     
    // *  Window Object's Methods
    // *********************************
    csAjax.windows.window.prototype.mouseMove = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = evnt.target.parentNode.jsWindow;
        if (csAjax.eventTracker) csAjax.eventTracker('windows','mouseMove',jsWin);
    }

    csAjax.windows.window.prototype.mouseOver = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = evnt.target.parentNode.jsWindow;
        if (csAjax.eventTracker) csAjax.eventTracker('windows','mouseOver',jsWin);
    }

    csAjax.windows.window.prototype.mouseOut = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = evnt.target.parentNode.jsWindow;
        if (csAjax.eventTracker) csAjax.eventTracker('windows','mouseOut',jsWin);
    }
    
    
    csAjax.windows.window.prototype.toolButtonClick = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        if (!evnt.target.parentNode) return;
        
        var jsWin = evnt.target.parentNode.jsWindow;
        var btn = evnt.target.className;
        
        csAjax.windows.activateWindow(jsWin);
        if (csAjax.eventTracker) csAjax.eventTracker('windows','toolButtonClick',jsWin, btn);
    }
    
    
    csAjax.windows.window.prototype.close = function(evnt, options) {
        options = options || {};
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = options.window || evnt.target.parentNode.jsWindow;
        if (csAjax.eventTracker && !options.silent) if (!csAjax.eventTracker('windows','beforeWindowClose',jsWin)) return;
        
        jsWin.elms.window.style.display='none';
        csAjax.windows.unRegisterWindow(jsWin);
        jsWin.windowState='closed';
        
        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',jsWin);
            if (!csAjax.eventTracker('windows','afterWindowClose',jsWin)) return;
        }
        jsWin.elms.window.innerHTML='';
        jsWin=null;
    }


    csAjax.windows.window.prototype.lock = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = evnt.target.parentNode.jsWindow;
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','beforeWindowLock',jsWin)) return;
        
        jsWin.locked=true;
        jsWin.elms.tbLock.className='tbLocked';
        for (var c=0; c < jsWin.dragObjs.length; c++) jsWin.dragObjs[c].locked=true;
        
        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',jsWin);
            if (!csAjax.eventTracker('windows','afterWindowLock',jsWin)) return;
        }
    }


    csAjax.windows.window.prototype.unlock = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = evnt.target.parentNode.jsWindow;
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','beforeWindowUnlock',jsWin)) return;
        
        jsWin.locked=false;
        jsWin.elms.tbLock.className='tbLock';
        for (var c=0; c < jsWin.dragObjs.length; c++) jsWin.dragObjs[c].locked=false;
        
        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',jsWin);
            if (!csAjax.eventTracker('windows','afterWindowUnlock',jsWin)) return;
        }
    }

    csAjax.windows.window.prototype.toggleLock = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = evnt.target.parentNode.jsWindow;
        
        if (jsWin.locked) jsWin.unlock(evnt);
        else jsWin.lock(evnt);
    }    
    
    csAjax.windows.window.prototype.activate = function(evnt) {
        evnt = csAjax.event.getHandler(evnt);
        var jsWin = evnt.target.parentNode.jsWindow;
            
        csAjax.windows.activateWindow(jsWin);
    }
   

    // *********************************     
    // *  Support Methods
    // *********************************
    csAjax.windows.activateWindow = function(jsWin) {
        if (csAjax.windows.activeWindow == jsWin) return;
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','beforeWindowActivate',jsWin)) return;
        
        csAjax.windows.deactivateWindow();
        csAjax.windows.activeWindow = jsWin;
        jsWin.elms.focusHandler.style.display='none';
        jsWin.elms.window.style.zIndex = csAjax.utils.getMaxZindex();
        
        if (csAjax.eventTracker) {
            csAjax.eventTracker('windows','stateChanged',jsWin);
            if (!csAjax.eventTracker('windows','afterWindowActivate',jsWin)) return;
        }
    }
    
    
    csAjax.windows.deactivateWindow = function(jsWin) {
        jsWin = jsWin || csAjax.windows.activeWindow;
        
        if (jsWin==null) return;
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','beforeWindowDeactivate',jsWin)) return;
        
        var fw = jsWin.elms.focusHandler;
        fw.style.backgroundColor='transparent';
        fw.style.backgroundImage='url(images/transparent.gif)';
        fw.style.position   = 'absolute';
        fw.style.height     = '100%';
        fw.style.width      = '100%';
        fw.style.zIndex     = jsWin.elms.window.style.zIndex+1;
        fw.style.display    = 'block';
        
        csAjax.windows.activeWindow = null;
        if (csAjax.eventTracker) if (!csAjax.eventTracker('windows','afterWindowDeactivate',jsWin)) return;
    }   
    
    
    csAjax.windows.registerWindow = function(oWindow) {
        csAjax.windows.windowList.push(oWindow);
    }
    
    
    csAjax.windows.unRegisterWindow = function(oWindow) {
        for (var c=0; c < csAjax.windows.windowList.length; c++) {
            var p = csAjax.windows.windowList[c];
            if (p.id == oWindow.id) csAjax.windows.windowList.splice(c,1);
        }
    }
    
   
