/* this code centers the document and handles resizing events in the browser
 * to keep it centered. */
document.body.autoCenter = {
	init: function() {
		/* how big is the contentarea? */
		this.contentregion = document.getElementById("workarea");
		this.backgroundExt = document.getElementById('background_ext');
		this.copyright = document.getElementById('copyright');

		this.copyright.style.textAlign = 'center';
		this.copyright.style.fontFamily = 'Arial';
		this.copyright.style.fontSize = '10px';
		this.copyright.style.color = 'black';

		/* MSIE: no CSS stylesheet objects I know of */
		if (navigator.appName == "Microsoft Internet Explorer") {
			/* meh, works for me */
			this.contentWidth = this.contentregion.offsetWidth;
		}
		else {
			for (i=0;i < document.styleSheets.length && this.contentWidth == null;i++) {
				ss = document.styleSheets[i];
				/* NTS: Internet Explorer only returns the file name. Others give the full path */
				if (ss.href.indexOf('/style.css') >= 0 || ss.href == "style.css") {
					var rules = ss.cssRules;
					/* NTS: MSIE doesn't have ss.cssRules */
					if (typeof(rules) != 'undefined') {
						for (j=0;j < rules.length && this.contentWidth == null;j++) {
							var rule = rules[j];
							if (rule.selectorText == "div.contentregion") {
								/* this is the one we're looking for */
								this.contentWidth = parseInt(rule.style.width);
							}
						}
					}
				}
			}
		}

		if (this.totalWidth < 1)
			this.totalWidth = 1500;

		if (this.contentWidth < 1)
			this.contentWidth = 843; /* FIXME! */

		document.body.currentDims = this.getBrowserWindowDimensions();
		window.onresize = function() {
			document.body.autoCenter.onresize();
		}
		this.doCenterContent();

	},
	onresize: function() {
		var dims = this.getBrowserWindowDimensions();
		if (dims.w == document.body.currentDims.w && dims.h == document.body.currentDims.h) return;
		document.body.currentDims = dims;
		this.doCenterContent();
	},
	getAbsoluteBoundingPos: function(obj) {
		var parent = obj.parentNode;
		var x=obj.offsetLeft,y=obj.offsetTop;
		var o = { };
		
		while (parent && parent != document.body) {
			x += parent.offsetLeft;
			y += parent.offsetTop;
			parent = parent.parentNode;
		}
		
		o.x = x;
		o.y = y;
		return o;
	},
	getAbsoluteBoundingDim: function(obj) {
		var w=obj.offsetWidth+obj.offsetLeft,h=obj.offsetHeight+obj.offsetTop;
		var o = { },c=obj.childNodes,i,co,cmpo;
		
		for (i=0;i < c.length;i++) {
			/* so getAttribute() is native code and therefore an 'object' not a 'fucntion'. Riiiiight... */
			if (typeof(c[i].getAttribute) != 'undefined' && c[i].getAttribute("gab_dim_ignore") == "1") {
			}
			else {
				cmpo = this.getAbsoluteBoundingDim(c[i]);
				if (w < cmpo.w) w = cmpo.w;
				if (h < cmpo.h) h = cmpo.h;
			}
		}
		
		o.w = w;
		o.h = h;
		return o;
	},
	doCenterContent: function() {
		var dims = document.body.currentDims;
		var ofsx = Math.floor((dims.w - (this.totalWidth + 22)) / 2);
		this.contentregion.style.left = ofsx.toString()+'px';
		var my = dims.h;
		var cy = -1;
		var page = null;
		
/*		if (typeof(document.body.cccc) != 'object') {
			document.body.cccc = document.createElement('div');
			document.body.cccc.style.position = 'absolute';
			document.body.cccc.style.color = '#FFFFFF';
			document.body.cccc.style.left = '0px';
			document.body.cccc.style.top = '0px';
			document.body.appendChild(document.body.cccc);
		} */
		
		if (document.body.pageTracking != null && typeof(document.body.pageTracking) == 'object' && document.body.pageTracking.currentPage) {
			page = document.body.pageTracking.pages[document.body.pageTracking.currentPage];
		}
				
		if (page != null) {
			var po = this.getAbsoluteBoundingPos(page);
			var dobj = this.getAbsoluteBoundingDim(page);
			/* FIXME: 185 = pixels from top to content area */
			cy = (dobj.h+po.y);
/*			document.body.cccc.innerHTML = dobj.w + ' x ' + dobj.h + ' from ' + dims.h + ' cy=' + cy; */
		}
		
		if (page != null && cy < (1103 - this.copyright.offsetHeight)) {
			this.copyright.style.backgroundImage = '';
			this.copyright.style.backgroundColor = '';
		}
		else {
			this.copyright.style.backgroundImage = 'url(defbkgnd_brep.png)';
			this.copyright.style.backgroundColor = '#FFFFFF';
		}
		
		{
			/* use the extender too */
			this.backgroundExt.style.height = '2px';
			this.copyright.style.top = '0px';
			if (typeof(document.body.offsetHeight) != 'undefined' && my < document.body.offsetHeight)
				my = document.body.offsetHeight;
			if (typeof(document.body.clientHeight) != 'undefined' && my < document.body.clientHeight)
				my = document.body.clientHeight;
			if (typeof(document.body.scrollHeight) != 'undefined' && my < document.body.scrollHeight)
				my = document.body.scrollHeight;
			if (typeof(window.innerHeight) != 'undefined' && my < window.innerHeight)
				my = window.innerHeight;
			if (typeof(document.documentElement) == 'object') {
				if (typeof(document.documentElement.scrollHeight) != 'undefined' && my < document.documentElement.scrollHeight)
					my = document.documentElement.scrollHeight;
			}
		}
		
//		if (my < 1103) my = 1103;	/* FIXME */
		if (my < 20) my = 20;	/* FIXME */

		/* buffer for extra measure */
		if (cy < 0) cy = my;
//		my += 16;
//		cy += 16;

		var ey = my - this.backgroundExt.offsetTop;
		if (ey < 32) ey = 32;
		this.backgroundExt.style.height = ey.toString()+'px';
		this.copyright.style.top = cy.toString()+'px';

		/* and hide the menu */
		if (typeof(document.body.pageTracking) != 'undefined' && typeof(document.body.pageTracking.mainDropDown) != 'undefined' &&
			document.body.pageTracking.mainDropDown != null)
			document.body.pageTracking.mainDropDown.hideDropdown();
	},
	getBrowserWindowDimensions: function() {
		var w=-1,h=-1,t;

		if (w < 0) {
			t = window.innerWidth;
			if (typeof(t) != 'undefined' && t > 0)
				w = t;
		}
		if (h < 0) {
			t = window.innerHeight;
			if (typeof(t) != 'undefined' && t > 0)
				h = t;
		}
		if (w < 0) {
			t = document.body.offsetWidth;
			if (typeof(t) != 'undefined' && t > 0)
				w = t;
		}
		if (w < 0) {
			t = document.body.offsetHeight;
			if (typeof(t) != 'undefined' && t > 0)
				h = t;
		}
		if (w < 0 && document.documentElement) {
			t = document.documentElement.clientHeight;
			if (typeof(t) != 'undefined' && t > 0)
				h = t;
		}

		if (w < 800) w = 800;
		if (h < 400) h = 400;
		return {w:w,h:h};
	},
	totalWidth: null,
	contentWidth: null,
	contentregion: null,
	backgroundExt: null
}

document.body.autoCenter.init();


