function EventPool(localeVarName) {
    this.eventsCollection = new Object();
    this.localeVarName = localeVarName;
}
EventPool.prototype.addFunctionAtEvent = function(eventTarget, eventName, functionPointer) {
    if (!this.eventsCollection[eventTarget]) {
        this.eventsCollection[eventTarget] = new Object();
    }
    if (!this.eventsCollection[eventTarget][eventName.toLowerCase()]) {
        if (!document.all) {
            eval("window.captureEvents(Event." + eventName.toUpperCase() + ");");
        }
        
        this.eventsCollection[eventTarget][eventName.toLowerCase()] = new Array();
        eval(eventTarget + ".on" + 
             eventName.toLowerCase() + " = function(e) {" + 
             this.localeVarName+ ".handleEvents(e, \"" + eventTarget+
             "\"); return true;}"
        );
    }
    
    this.eventsCollection[eventTarget][eventName.toLowerCase()].push(functionPointer);
}
EventPool.prototype.handleEvents = function(e, eventTarget) {
    var ev = (document.all)? event : e;

    if (this.eventsCollection[eventTarget]) {
        if (this.eventsCollection[eventTarget][ev.type.toLowerCase()]) {
            for (var i in this.eventsCollection[eventTarget][ev.type.toLowerCase()]) {
                try {
                    if (typeof(this.eventsCollection[eventTarget][ev.type.toLowerCase()][i]) == "function") {
                        this.eventsCollection[eventTarget][ev.type.toLowerCase()][i](ev);
                    }
                } catch (ex) {
                    alert(ex);
                }
            }
        }
    }
    
    return true;
}

var globalEventListener = new EventPool("globalEventListener");
//globalEventListener.addFunctionAtEvent("window", "MOUSEDOWN", null);

function Windows(windowId, windowWidth, windowHeight) {
    this.x = 0;
    this.y = 0;
    this.objectId = windowId;
    this.object = document.getElementById(windowId);
    this.objectTitlebar = document.getElementById(windowId + "_titlebar");
    this.objectTitlebarCaption = document.getElementById(windowId + "_titlebar_caption");
    this.mouseDownFlag = false;
    this.isIE = document.all? true : false;
    
    this.delta = {
        x : 0,
        y : 0
    };
    
    this.objectWindow = new Object();
    
    this.dimentions = {
        width : windowWidth,
        height : windowHeight
    };
}
Windows.prototype.init = function() {
    this.object.style.position = "absolute";
    
    this.objectWindow = {
        x : parseInt(this.object.style.left.replace("px", "")),
        y : parseInt(this.object.style.top.replace("px", ""))
    };
}
Windows.prototype._getMouseXY = function(e) {
    var x,y;
    if(this.isIE) {
        x = event.clientX + document.body.scrollLeft;
        y = event.clientY + document.body.scrollTop;
    } else {
        x = e.pageX;
        y = e.pageY;
    }
    
    this.delta.x = x - this.x;
    this.delta.y = y - this.y;
    
    this.x = x;
    this.y = y;
}
Windows.prototype.mouseMove = function(e) {
    this._getMouseXY(e);
    if (this.mouseDownFlag) {
        with (this.object) {
            style.top = parseInt(this.objectWindow.y) + parseInt(this.delta.y) + "px";
            style.left = parseInt(this.objectWindow.x) + parseInt(this.delta.x) + "px";
            
            this.objectWindow.y += parseInt(this.delta.y);
            this.objectWindow.x += parseInt(this.delta.x);
        }
    }
}
Windows.prototype.mouseDown = function(e) {
    this.mouseDownFlag = !this.mouseDownFlag;
}
Windows.prototype.close = function() {
    this.object.style.display = "none";
}
Windows.prototype.open = function() {
    with (this.object) {
        style.top = (screen.availHeight - this.dimentions.height) / 2 + "px";
        style.left = (screen.availWidth - this.dimentions.width) / 2 + "px";
        
        style.display = "inline";
        this.init();
    }
}
Windows.prototype.setTitlebarCaption = function(text) {
    this.objectTitlebarCaption.innerHTML = text;
}