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.mouseDownFlag = false;
    this.isIE = document.all? true : false;
    
    this.preventMouseDown = 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) {
	if (!this.preventMouseDown) {
    	this.mouseDownFlag = true;
    }
}
Windows.prototype.mouseUp = function(e) {
	this.mouseDownFlag = false;
	this.preventMouseDown = false;
}
Windows.prototype.close = function() {
    this.object.style.display = "none";
}
Windows.prototype.open = function() {
    var pageWidth = null;
    var pageHeight = null;
    var pageScrollTop = null;
    var pageScrollLeft = null;

    try {       
        if(window.innerHeight && window.scrollMaxY ) {// Firefox
            pageWidth = window.innerWidth;
            pageHeight = window.innerHeight;
            pageScrollTop = window.scrollY;
            pageScrollLeft = window.scrollX;
        } else {// works in Explorer 6 Strict, Mozilla (not FF) and Safari
            pageWidth = document.documentElement.clientWidth;
            pageHeight = document.documentElement.clientHeight;
            pageScrollTop = document.documentElement.scrollTop;
            pageScrollLeft = document.documentElement.scrollLeft;
        }    
    } catch (e) {
        //
    }   
       
    with (this.object) {
        style.top = ((pageHeight - this.dimentions.height) / 2 + pageScrollTop) + "px";
        style.left = ((pageWidth - this.dimentions.width) / 2 + pageScrollLeft) + "px";
        
        style.display = "inline";
    }
    
    this.init();
}
Windows.prototype.openAtElement = function(obj) {
    var x = 0;
    var y = 0;
    
    if (document.all) {
        y = document.body.scrollTop + obj.offsetTop;
        x = document.body.scrollLeft + event.x - this.dimentions.width / 2;
    } else {
        y = obj.offsetTop - this.dimentions.height / 2;;
        x = obj.offsetLeft - this.dimentions.width / 2;
    }
    with (this.object) {
        style.top = y + "px";
        style.left = x + "px";
        
        style.display = "inline";
        this.init();
    }
}
