﻿var BrowserSize = new Object();

var _div;
var mW = 0;
var mH = 0;
var isNetscape = navigator.appName.indexOf("Netscape")  != -1;

// initialize this here using the id of the div tag that is going to be
// changed, and it will add the listener and
BrowserSize.initialize = function(divID, minW, minH)
{
	mW = minW;
	mH = minH;
	
	_div = document.getElementById(divID);
	window.onresize = BrowserSize.stageResize;
	BrowserSize.stageResize();
}

// called every time the stage resizes, if the stage is too small then lock
// in the pixel size at the minimum - if it's too big the just use 100%
BrowserSize.stageResize = function(evt)
{
	var width  = BrowserSize.getWidth();
	var height = BrowserSize.getHeight();
	
	_div.style.width  = width  > mW ? "100%" : String(mW) + "px";
	_div.style.height = height > mH ? "100%" : String(mH) + "px";
}

// use this to set the div's minumum width
BrowserSize.setWidth = function(val)
{
	mW = val;
	BrowserSize.stageResize();
	setTimeout(BrowserSize.stageResize, 50); // fix for google chrome
}

// use this to set the div's minimum height
BrowserSize.setHeight = function(val)
{
	mH = val;
	BrowserSize.stageResize();
	setTimeout(BrowserSize.stageResize, 50); // fix for google chrome
}

// use this to get the div's minumum width
BrowserSize.getWidth = function()
{
	return isNetscape ? window.innerWidth : document.body.clientWidth;
}

// use this to get the div's minimum height
BrowserSize.getHeight = function()
{
	return isNetscape ? window.innerHeight : document.body.clientHeight;
}

// this will read how much the browser is scrolled up and down at the moment
BrowserSize.getScrollY = function()
{
	var scroll = 0;

	if (typeof(window.pageYOffset) == 'number')
		scroll = window.pageYOffset;
	else if (document.body && document.body.scrollTop)
		scroll = document.body.scrollTop;
	else if (document.documentElement && document.documentElement.scrollTop)
		scroll = document.documentElement.scrollTop;
	
	return scroll;
}

// this will read how much the browser is scrolled left and right at the moment
BrowserSize.getScrollX = function()
{
	var scroll = 0;

	if (typeof(window.pageXOffset) == 'number')
		scroll = window.pageXOffset;
	else if (document.body && document.body.scrollLeft)
		scroll = document.body.scrollLeft;
	else if (document.documentElement && document.documentElement.scrollLeft)
		scroll = document.documentElement.scrollLeft;
	
	return scroll;
}

// this will set how much the browser is scrolled vertically at the moment.
BrowserSize.setScrollY = function(scroll)
{
	window.scrollTo(BrowserSize.getScrollX(), scroll);
}

// this will set how much the browser is scrolled horizontally at the moment.
BrowserSize.setScrollX = function(scroll)
{
	window.scrollTo(scroll, BrowserSize.getScrollY());
}
