var flag = false;
var minWidth = 750;
var table;
var wrapper;	

// Minimum Width Limit
// Prevents the content pane from getting narrower than 750px in IE
// Other browsers support the css min-width property

function widthLimiter() { 

	wrapper=document.getElementById('pagecontainer');	
	table=document.getElementById('statstable');
	
	if(table) {
		limitWidthByTable();
	} else {
		if(document.getElementById && navigator.appVersion.indexOf("MSIE")>-1 && !window.opera) {
			if(window.attachEvent) {
				window.attachEvent("onresize",limitWidth);
				window.attachEvent("onload",limitWidth);
			}
			else {
				onload=limitWidth;
				onresize=limitWidth;
			}
		}
	}
	
}

function limitWidthByTable() {

	// If there's a statistics navigation table, adjust width so as to fit it

	if(table) {
		var width=table.clientWidth;
		if(width > 550) {
			minWidth = 200 + width
		}
	}

	doLimitWidth();
	
}

function getWidestStatsImage() {
	
	// Returns the width of the widest image on the current page of a stats item
	
	var imageWidth=0;

	for (var i = 0; i < document.images.length; i++) {
		if ((document.images[i].className) &&
		    (document.images[i].className.indexOf("bodyImage" > -1)) &&
		    (document.images[i].width > imageWidth)) {
		    	imageWidth = document.images[i].width;
		}
	}
	
	return imageWidth;
	
}

function limitWidth() {

	// Adjusts width of page to accomodate wide stats images
	// This only needs to be evaluated once upon loading the page, hence the flag

	if (!flag) {

		imageWidth = getWidestStatsImage();

		if(imageWidth > 350) {
			minWidth = 550 + imageWidth;
			flag = true;
		}
		
		doLimitWidth();
		
	}
	
}

function doLimitWidth() {

	// Sets the width of the page wrapper div if it gets too low

	if(wrapper && document.body && document.body.clientWidth) {
	
		if(parseInt(document.body.clientWidth)<=minWidth) {
			wrapper.style.width=minWidth+"px";
		} else {
			wrapper.style.width="auto";
		}
	}
	
}
