// Copyright ©2008 Peter Harris, all rights reserved.
// Permission to copy and use this code directly is denied.
// Permission to study and learn from this code is happily given.

// ---+- add my onload functions to any existing "onloads" on this page
//    |  based on code from: http://simonwillison.net/2004/May/26/addLoadEvent/
//    |    addLoadEvent(function() {
//    |      /* more code to run on page load */ 
//    +-   });

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) { oldonload(); }
			func();
		}
	}
}
function addResizeEvent(func) {
	var oldevents = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
			if (oldevents) { oldevents(); }
			func();
		}
	}
}

addLoadEvent(css_setScreenClass);
addResizeEvent(css_setScreenClass);

function css_setScreenClass() {
	var xx = document.documentElement.clientWidth;
	var yy = document.documentElement.clientHeight;
	var classy =
		(xx>240 && xx<=320)	? "iPortrait" :
		(xx>320 && xx<=480)	? "iLandscape" :
		(xx>480 && xx<=640)	? "tiny" :
		(xx>640 && xx<=1080)	? "one_column" :
		(xx>1080 && xx<=1570)	? "two_column" :
					  "three_column";

	// +[reporting]: for reporting only; this block is not necessary
	var shorthand = { "iPortrait":"iV", "iLandscape":"iH", "tiny":"x", "one_column":"I", "two_column":"II", "three_column":"III" };
	var node = document.getElementById("screensize");
	if ( node && node.firstChild && node.firstChild.nodeType==3 ) {
		node.firstChild.nodeValue = xx+"."+yy+"."+shorthand[classy];
	}
	// -[reporting]

	document.body.className=classy;
};

