function BrowserWindowManager(window) {
	this.window = window;
	this.resetFrameSize();
}
BrowserWindowManager.prototype.getClientWidth = function () { return this.window.innerWidth != null? this.window.innerWidth : this.window.document.body != null? this.window.document.body.clientWidth : null;  }
BrowserWindowManager.prototype.getClientHeight = function () { return this.window.innerHeight != null? this.window.innerHeight : this.window.document.body != null? this.window.document.body.clientHeight : null; }
BrowserWindowManager.prototype.getClientLeft = function () { return typeof this.window.pageXOffset != 'undefined' ? this.window.pageXOffset : this.window.document.documentElement.scrollLeft ? this.window.document.documentElement.scrollLeft : this.window.document.body.scrollLeft ? this.window.document.body.scrollLeft : 0; }
BrowserWindowManager.prototype.getClientTop = function () { return typeof this.window.pageYOffset != 'undefined' ? this.window.pageYOffset : this.window.document.documentElement.scrollTop ? this.window.document.documentElement.scrollTop : this.window.document.body.scrollTop ? this.window.document.body.scrollTop : 0; }
BrowserWindowManager.prototype.getClientRight = function () { return this.getClientLeft() + this.getClientWidth(); }
BrowserWindowManager.prototype.getClientBottom = function () { return this.getClientTop() + this.getClientHeight(); }
BrowserWindowManager.prototype.getLeft = function () { return this.window.document.all ? this.window.screenLeft : this.window.screenX; }
BrowserWindowManager.prototype.getTop = function () { return this.window.document.all ? this.window.screenTop : this.window.screenY; }
BrowserWindowManager.prototype.getWidth = function () { return this.getClientWidth() + this.getFrameWidth(); }
BrowserWindowManager.prototype.getHeight = function () { return this.getClientHeight() + this.getFrameHeight(); }
BrowserWindowManager.prototype.getFrameWidth = function () { this.calculateFrameSize(); return this.frameWidth; }
BrowserWindowManager.prototype.getFrameHeight = function () { this.calculateFrameSize(); return this.frameHeight; }
BrowserWindowManager.prototype.isFrameSizeCalculated = function () { return this.frameWidth != null && this.frameHeight != null; }
BrowserWindowManager.prototype.resetFrameSize = function () { this.frameWidth = null; this.frameHeight = null; }
BrowserWindowManager.prototype.calculateFrameSize = function () {
	if (this.isFrameSizeCalculated()) {
		return;
	}
	// To calculate the frame size we need to resize the window once and then restore it. 
	// This is because we can read the client area but only set the browser size including the frame.
	var oldClientWidth = this.getClientWidth();
	var oldClientHeight = this.getClientHeight();
	// Resize entire window to the old client area
	var w = oldClientWidth;
	var h = oldClientHeight;
	this.window.resizeTo(w, h);
	this.frameWidth = w - this.getClientWidth();
	this.frameHeight = h - this.getClientHeight();
	// Restore the window size
	this.window.resizeTo(oldClientWidth + this.frameWidth, oldClientHeight + this.frameHeight);
}

BrowserWindowManager.prototype.resizeAndPositionToCornerIfNeeded = function (requiredClientWidth, requiredClientHeight, position) {
    requiredClientWidth = Math.max(requiredClientWidth, this.getClientWidth());
    requiredClientHeight = Math.max(requiredClientHeight, this.getClientHeight());
    var windowIsOutsizeScreen = (this.getLeft() + this.getClientWidth()) > screen.availWidth || 
								(this.getTop() + this.getClientHeight()) > screen.availHeight ||
								this.getLeft() < 0 ||
								this.getTop() < 0;
    var windowHasTooSmallClientSize = this.getClientWidth() < requiredClientWidth || 
									  this.getClientHeight() < requiredClientHeight;
    if (windowIsOutsizeScreen || windowHasTooSmallClientSize) {
		var browserWidth = requiredClientWidth + this.getFrameWidth();
		var browserHeight = requiredClientHeight + this.getFrameHeight();
	    browserWidth = Math.min(browserWidth, screen.availWidth);
	    browserHeight = Math.min(browserHeight, screen.availHeight);
		switch (position) {
		case "topleft": x = 0; y = 0; break;
		case "topright": x = screen.availWidth - browserWidth; y = 0; break;
		case "bottomleft": x = 0; y = screen.availHeight - browserHeight; break;
		case "bottomright": x = screen.availWidth - browserWidth; y = screen.availHeight - browserHeight; break;
		default: x = 0; y = 0; break;
		}
		this.window.moveTo(x, y);
		this.window.resizeTo(browserWidth, browserHeight);
	}
}



