// Copyright (C)2007 Windy Road
// This work is licensed under a Creative Commons Attribution 2.5 License.  See http://creativecommons.org/licenses/by/2.5/au/


window.onload= function()
	{
	fitToScreen();
 }

var timeout = null;

window.onresize= function() {
	clearTimeout( timeout );
	timeout = setTimeout( fitToScreen, 10 );
};

function windowWidth() {
	return document.getElementById( 'page-width' ).offsetWidth;
}


function textWidth() {
	return document.getElementById( 'content' ).offsetWidth;
}

function displayDims() {
	targetWidth = windowWidth();
	mainW = document.getElementById( 'content' ).offsetWidth;
	deltaW2 = (deltaW/100) * (mainW / targetWidth) * 100 ;
	document.getElementById( 'debug' ).innerHTML = 
		"mainW " + mainW + " : targetW "  + targetWidth + "<br/>"
		+ "deltaW "  + deltaW + "<br/>"
		+ "deltaW2 "  + deltaW2 + "<br/>"
		+ "currentW "  + currentWidth + "<br/>";
	
}

var currentWidth = 0;
function fitToScreen() {
	if( windowWidth() != currentWidth ) {
		resetLayout();
		targetWidth = windowWidth();
		mainW = document.getElementById( 'content' ).offsetWidth;
		deltaW = targetWidth / mainW * 100;
		setFontSize( deltaW );
		setImageSize( deltaW );
		currentWidth = windowWidth();
		newMainW = document.getElementById( 'content' ).offsetWidth;
		displayDims();
		if( mainW > currentWidth ) {
			deltaW2 = (deltaW/100) * (mainW / targetWidth) * 100 ;
			setFontSize( deltaW2 );
			setImageSize( deltaW2 );
			document.getElementById( 'header' ).style.width=mainW + "px";
			document.getElementById( 'footer' ).style.width=mainW + "px";
			currentWidth = windowWidth();
		}
	}
}

function setFontSize( size ) {
	document.body.style.fontSize=size + '%';
}


function resetLayout() {
	setFontSize( 100 );
	document.getElementById( 'header' ).style.width=null;
	document.getElementById( 'footer' ).style.width=null;
}

function setContentWidth() {
	content = document.getElementById( 'content' ).offsetWidth;
	sidebar = content
	content = (mainW + sidebar)+1;
	document.getElementById( 'content' ).style.width = content + 'px';
	return content;
}
