function calcWidth(height)
{
	if (ratio_offset != null)
		height=height-ratio_offset;
	return Math.floor(height * ratio);
}

function calcHeight(width)
{
    var temp = Math.floor(width / ratio);
    if (ratio_offset != null)
    	temp = temp + ratio_offset;
    return temp;
}

function isWindow(obj)
{
	if (!document.all)
		//return obj instanceof Window; // this does not work in Safari.
		return obj == window;
	else
		return obj.status != undefined;
}

function ObjDims(obj)
{
	this.LAYOUT_WIDE = 0;
	this.LAYOUT_TALL = 1;
	this.LAYOUT_SQUARE = 2;

	this.width = 0;
	this.height = 0;
	this.top = 0;
	this.left = 0;

	this.layout = null;

	if (isWindow(obj))
	{
		if ((typeof (window.innerWidth) == "number"))
		{
			this.width = obj.innerWidth;
			this.height = obj.innerHeight;
		}
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			this.width = document.documentElement.clientWidth;
			this.height = document.documentElement.clientHeight;
		}
		else if (document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			this.width = document.body.clientWidth;
			this.height = document.body.clientHeight;
		}

		this.top = 0;
		this.left = 0;
	}
	else
	{
		this.width = parseInt(obj.style.width);
		this.height = parseInt(obj.style.height);
		this.top = parseInt(obj.style.top);
		this.left = parseInt(obj.style.left);
	}

	if (this.width > this.height)
		this.layout = this.LAYOUT_WIDE
	else if (this.width < this.height)
		this.layout = this.LAYOUT_TALL
	else
		this.layout = this.LAYOUT_SQUARE

	return this;
}

function setDims(width,height)
{
	flashObj = document.getElementById("flash");
	flashObj.style.width = width+"px";
	flashObj.style.height = height+"px";
}

function setPos(topPos,leftPos)
{
	flashObj = document.getElementById("flash");
	flashObj.style.top = topPos+"px";
	flashObj.style.left = leftPos+"px";
}

function getCenter(w1,w2)
{

	if (w1 - w2 == 0)
		return 0;
	else
		return Math.floor((w1 - w2) / 2);
}

function gameWindowInit()
{
	document.getElementById("flash").style.position = "absolute";
	setDims(0,0);
	gameWindowResize();
}

function gameWindowResize()
{
	//console.log('resizing');
	
	var width = 0;
	var height = 0;
	var topPos = 0;
	var leftPos = 0;

	var ws = new ObjDims(window);
	var fs = new ObjDims(document.getElementById("flash"));

	//console.log(ws.layout);

	if (ws.layout == ws.LAYOUT_WIDE)
	{
		height = ws.height;
		width = calcWidth(height);
		topPos  = 0;

		if (width > ws.width)
		{
			//console.log('overflow');
			width = ws.width;
			height = calcHeight(width);
		}
		
		leftPos = getCenter(ws.width,width);
		topPos = getCenter(ws.height,height);
	}
	else
	{
		width = ws.width;
		height = calcHeight(width);
		leftPos = 0;
		topPos =  getCenter(ws.height,height);
	}

	setDims(width,height);
	setPos(topPos,leftPos);
}

