// CLASS CONSTRUCTOR
function DuWindow(title, htmlContent, windowOptions, targetContainer){
    this.wid = WindowManager.addWindow(this);
    this.parentObject = targetContainer;
    this.setOptions(windowOptions);
    this.init(title, htmlContent);
    // this.show();
}

DuWindow.prototype.setOptions = function(rcOptions){
    // DEFAULT OPTIONS - OVERRIDDEN BY PASSED VALUES
    this.options = {
        border:true,//done
        borderfocusstyle:'1px solid #AAAAAA',//done
        borderunfocusstyle:'1px solid #555555',//done
        contentfocusclass:'',
        contentunfocusclass:'',
        positionx:0,//done
        positiony:0,//done
        sizew:50,//done
        sizeh:0,//done
        titlebar:true,//done
        titlebaricon:true,//done
        titlebariconurl:'../favicon.ico',//done
        titlebarfocusclass:'',
        titlebarunfocusclass:'',
        windowEventListener:null
    };
    // var optionsMsg = "";
    for(var key in rcOptions){
        this.options[key] = rcOptions[key];
        // optionsMsg += key+"="+rcOptions[key]+"\n";
    }
}

DuWindow.prototype.setListener = function(rcListener){
    this.options.windowEventListener = rcListener;
}

DuWindow.prototype.init = function(rcTitle, rcHtmlContent){
    this.container = document.createElement("DIV");
    this.container.zIndex = WindowManager.maxZindex++;
    this.container.id = "WIN"+this.wid;
    this.container.className = 'ducontainer';
    this.container.style.position = 'absolute';
    this.container.style.display = 'none';
    this.container.style.visibility = 'hidden';
    this.container.style.left = this.options.positionx;
    this.container.style.top = this.options.positiony;
    this.container.style.overflow = "auto";
    if( this.options.border ){
        this.container.style.border = this.options.borderfocusstyle;
    }
    this.container.style.background = '#444444';
    /*  DOUBLE CLICKING
    this.container.onclick = function(e){
        WindowManager.handleDblClick(this);
    }
    */

    this.titlebar = document.createElement('DIV');
    this.titlebar.id = "TITLE"+this.wid;
    if( !this.options.titlebarfocusclass ){
        this.titlebar.style.background = '#ffffff';
        this.titlebar.style.color = '#000000';
        this.titlebar.style.borderStyle = "solid";
        this.titlebar.style.borderWidth = "0px 1px 1px 1px";
        this.titlebar.style.borderColor = "#000000";
        this.titlebar.width='100%';
        this.titlebar.style.fontFamily = "'Comic Sans MS', 'Courier New', sans-serif";
        this.titlebar.style.padding='0px 2px 0px 2px';
        this.titlebar.style.margin = '1px';
    }else{
        this.titlebar.className = this.options.titlebarfocusclass;
    }
    if( !this.options.titlebar ){
        this.titlebar.style.display="none";
        this.titlebar.style.visibility="hidden";
    }

    var lcTable = document.createElement("TABLE");
    lcTable.width = "100%";
    lcTable.cellpadding = "0px";
    lcTable.cellspacing = "0px";
    lcTable.style.margin = "0px";
    lcTable.style.fontFamily = "inherit";
    lcTable.style.color = "inherit";
    var lcTBody = document.createElement("TBODY");
    lcTBody.width = "100%";
    var lcTRow = document.createElement("TR");

    var lcICell = document.createElement("TD");
    if( !this.options.titlebaricon ){
        lcICell.style.display="none";
        lcICell.style.visibility="hidden";
    }
    lcICell.style.padding = "0px";
    lcICell.style.margin = "0px";
    lcICell.innerHTML = "<img src='"+this.options.titlebariconurl+"' width='16px' height='16px' border='0px'/>";

    var lcTCell = document.createElement("TD");
    lcTCell.id = "TCELL"+this.wid;
    lcTCell.className = "movable";
    lcTCell.width = "100%";
    lcTCell.style.cursor = "move";
    lcTCell.style.whiteSpace = "nowrap";
    lcTCell.style.fontFamily = "inherit";
    lcTCell.style.fontSize = "10pt";
    lcTCell.style.fontWeight = "bold";
    lcTCell.style.verticalAlign = "top";
    lcTCell.style.padding = "0px 0px 0px 4px";
    lcTCell.style.margin = "0px";
    lcTCell.innerHTML = rcTitle;
    lcTCell.onmousedown = function(e){
        WindowManager.selectWindow(this, "TCELL");
        var win = WindowManager.selected.container;
        var oldbg = win.style.background;
        win.style.background = "#ffff00";
        var mpos =  WindowManager.mousePosition(e); 
        WindowManager.mx = mpos.x;
        WindowManager.my = mpos.y;
        document.onmousemove = WindowManager.moveWindow;
        document.onmouseup = function(){
            document.onmousemove = null;
            document.onmouseout = null;
            document.onmouseup = null;
            win.style.background = oldbg;
        };
    };

    var lcCCell = document.createElement("TD");
    lcCCell.id = "CCELL"+this.wid;
    lcCCell.style.fontSize = "10pt";
    lcCCell.style.fontWeight = "bold";
    lcCCell.style.padding = "0px 3px 0px 23px";
    lcCCell.style.margin = "0px";
    lcCCell.style.cursor = "hand";
    lcCCell.style.cursor = "pointer";
    lcCCell.style.verticalAlign = "top";
    lcCCell.onclick = function(){
        var parentWin = document.getElementById(this.id.replace("CCELL","WIN"));
        parentWin.style.display = "none";
        parentWin.style.visibility = "hidden";
    };
    lcCCell.innerHTML = "X";

    lcTable.appendChild(lcTBody);
    lcTBody.appendChild(lcTRow);
    lcTRow.appendChild(lcICell);
    lcTRow.appendChild(lcTCell);
    lcTRow.appendChild(lcCCell);

    this.titlebar.appendChild(lcTable);

    this.container.appendChild(this.titlebar);

    this.content = document.createElement('DIV');
    this.content.style.background = '#AAAABA';
    this.content.style.padding = '2px';
    this.content.style.margin = '1px';
    // this.content.innerHTML = "Test Content";
    // this.content.innerHTML = rcHtmlContent;
    this.content.appendChild(rcHtmlContent);
    this.container.appendChild(this.content);

    if( !this.parentObject ){
        this.parentObject = findNodeDown(document, "BODY");
    }

    if( this.parentObject ){
        this.parentObject.appendChild(this.container);
    }

    this.resize();
}
DuWindow.prototype.focus = function(){
   this.container.style.zIndex = WindowManager.maxZindex++;
   if( this.options.border ){
       this.container.style.border = this.options.borderfocusstyle;
   }
   if( this.options.titlebarfocusclass ){
       if( !this.options.titlebarunfocusclass ){
           this.titlebar.style.background = null;
           this.titlebar.style.color = null;
           this.titlebar.style.borderBottom = null;
       }
       this.titlebar.className = this.options.titlebarfocusclass;
   }else{
       this.titlebar.style.background = '#5588BB';
       this.titlebar.style.color = '#000000';
       this.titlebar.style.borderBottom = '1px solid #666666';
   }
}
DuWindow.prototype.unfocus = function(){
   if( this.options.border ){
       this.container.style.border = this.options.borderunfocusstyle;
   }
   if( this.options.titlebarunfocusclass ){
       if( !this.options.titlebarfocusclass ){
           this.titlebar.style.background = null;
           this.titlebar.style.color = null;
           this.titlebar.style.borderBottom = null;
       }
       this.titlebar.className = this.options.titlebarunfocusclass;
   }else{
       this.titlebar.style.background = '#666666';
       this.titlebar.style.color = '#AAAAAA';
       this.titlebar.style.borderBottom = '1px solid #AAAAAA';
   }
}
DuWindow.prototype.hideWindow = function(obj){
    if( !obj ){
        obj = this.container;
    }
    obj.style.display = 'none';
    obj.style.visibility = 'hidden';
    if( this.options.windowEventListener != null ){
        if( this.options.windowEventListener.hideEvent ){
            this.options.windowEventListener.hideEvent();
        }
    }
}
DuWindow.prototype.show = function(){
    if( !this.parentObject ){
        this.parentObject = findNodeDown(document, "BODY");
        if( this.parentObject){
            this.parentObject.appendChild(this.container);
        }
    }
    this.container.style.display = '';
    this.container.style.visibility = 'visible';
    // Resize the container to fit the titlebar if the titlebar is too wide
    // titlebar width = width of the table in the titlebar.
    if(this.titlebar.firstChild.offsetWidth > this.container.offsetWidth){
        this.container.style.width = this.titlebar.firstChild.offsetWidth + 5;
    }
    if(this.titlebar.firstChild.offsetWidth > this.content.offsetWidth){
        this.content.style.width = this.titlebar.firstChild.offsetWidth;
    }
    if( this.options.windowEventListener != null ){
        if( this.options.windowEventListener.showEvent ){
            this.options.windowEventListener.showEvent();
        }
    }
}
DuWindow.prototype.destroy = function(){
    this.hideWindow();
    if( typeof(this.container.parentNode) != null ){
        this.container.parentNode.removeChild(this.container);
    }
    this.container = null;
    if( this.options.windowEventListener != null ){
        if( this.options.windowEventListener.destroyEvent ){
            this.options.windowEventListener.destroyEvent();
        }
    }
}
DuWindow.prototype.shade = function(){
    this.content.style.display = 'none';
    this.content.style.visibility = 'hidden';
    if( this.options.windowEventListener != null ){
        if( this.options.windowEventListener.shadeEvent ){
            this.options.windowEventListener.shadeEvent();
        }
    }
}
DuWindow.prototype.unshade = function(){
    this.content.style.display = '';
    this.content.style.visibility = 'visible';
    if( this.options.windowEventListener != null ){
        if( this.options.windowEventListener.unshadeEvent ){
            this.options.windowEventListener.unshadeEvent();
        }
    }
}
DuWindow.prototype.resize = function(w,h){
    if( typeof(w) != 'undefined' ){
        this.container.style.width = w;
    }else{
        if( this.options.sizew ){
            this.container.style.width = this.options.sizew;
        }
    }

    if( typeof(h) != 'undefined' ){
        this.container.style.height = h;
    }else{
        if( this.options.sizeh ){
            this.container.style.height = this.options.sizeh;
        }
    }
}
DuWindow.prototype.move = function(dx, dy){
    if(!dx) dx = 0;
    if(!dy) dy = 0;
    this.container.style.left = this.container.offsetLeft + dx;
    this.container.style.top = this.container.offsetTop + dy;
}


var WindowManager = {
    winArray:new Object(),
    selected:null,
    mx:0,
    my:0,
    eventCtr:0,
    maxZindex:0,
    selectWindow:function(containerWin, idStr){
        if( !idStr ){
            idStr = "WIN";
        }
        WindowManager.selected = WindowManager.winArray[containerWin.id.replace(idStr,"")];
        for(var x in WindowManager.winArray){
            if( typeof(WindowManager.winArray[x].unfocus) != 'undefined' ){
                WindowManager.winArray[x].unfocus();
            }
        }
        WindowManager.selected.focus();
    },
    closeAll:function(){
        for(var x in WindowManager.winArray){
            if( typeof(WindowManager.winArray[x].destroy) != 'undefined' ){
                WindowManager.winArray[x].destroy();
                delete(WindowManager.winArray[x]);
            }
        }
    },
    hideAll:function(){
        for(var x in WindowManager.winArray){
            if( typeof(WindowManager.winArray[x].hideWindow) != 'undefined' ){
                WindowManager.winArray[x].hideWindow();
            }
        }
    },
    showAll:function(){
        for(var x in WindowManager.winArray){
            if( typeof(WindowManager.winArray[x].show) != 'undefined' ){
                WindowManager.winArray[x].show();
            }
        }
    },
    shadeAll:function(){
        for(var x in WindowManager.winArray){
            if( typeof(WindowManager.winArray[x].shade) != 'undefined' ){
                WindowManager.winArray[x].shade();
            }
        }
    },
    unshadeAll:function(){
        for(var x in WindowManager.winArray){
            if( typeof(WindowManager.winArray[x].unshade) != 'undefined' ){
                WindowManager.winArray[x].unshade();
            }
        }
    },
    addWindow:function(duWindowObj){
        var wid = 10240 + Math.round(10240*Math.random());
        while( typeof(WindowManager.winArray[wid]) != 'undefined' ){
            wid = 10240 + Math.round(10240*Math.random());
        }
        WindowManager.winArray[wid] = duWindowObj;
        return wid;
    },
    mousePosition:function(rcEvent){
        if (!rcEvent) var rcEvent = window.event;
        var pos = {x:0,y:0};
        if(rcEvent.pageX || rcEvent.pageY){
            pos.x = rcEvent.pageX;
            pos.y = rcEvent.pageY;
        }else{
            var rel = document.documentElement;
            if(!rel){
                rel = document.body;
            }else if(rel.clientLeft || rel.clientTop){
                pos.x -= rel.clientLeft;
                pos.y -= rel.clientTop;
            }
            pos.x += rcEvent.clientX + rel.scrollLeft;
            pos.y += rcEvent.clientY + rel.scrollTop;
        }
        return pos;
    },
    moveWindow:function(e){
        if( WindowManager.eventCtr % 2 == 0 ){
            var loc = WindowManager.mousePosition(e);
            WindowManager.selected.move( (loc.x-WindowManager.mx), (loc.y-WindowManager.my) );
            WindowManager.mx = loc.x;
            WindowManager.my = loc.y;
        }
        WindowManager.eventCtr++;
    }
    
}

