// Text Zoomer by James Newbery : http://www.tinnedfruit.com
// http://www.tinnedfruit.com/sandbox/textzoom.html
// Version 0.1 published 05.02.2006


// Simon Willison's addLoadEvent - remove if you already have this in your library
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// Similar function for window resize events
function addResizeEvent(func) {
	var oldresize = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
			oldresize();
			func();
		}
	}
}

// Text Zoom initialisation function called by window.onload
function textZoomInit() {
	// eliminate non-supporting browsers
	if (!document.getElementById || !document.body.style) {
		return false;
	}
	tzInitVars();
	tzResize();
	addResizeEvent(tzResize);
}

// Get the element references from the supplied IDs
function tzInitVars() {
	for (var i = 0; i < tzTargets.length; i++) {
		tzTargets[i].elTarget = (tzTargets[i].strTarget == "") ? document.body : document.getElementById(tzTargets[i].strTarget);
		tzTargets[i].elReference = (tzTargets[i].strReference =="") ? document.body : document.getElementById(tzTargets[i].strReference);
		// if we don't have a valid pair of references, disable this text zoomer
		if (tzTargets[i].elTarget == null || tzTargets[i].elReference == null) tzTargets[i].status = false;
	}
	tzGoodToGo = true; // everything ready, resizing can be done.
}

function tzResize() {
	if (tzGoodToGo) {
		for (var i = 0; i < tzTargets.length; i++) {
			if (tzTargets[i].status == false) continue; // don't bother if we don't have valid element references
			// calculate text size
			var newTextSize = tzTargets[i].elReference.offsetWidth * tzTargets[i].intRatio; 
			// assign new text size if it is between the minimum and maximum sizes
			if (tzTargets[i].intMaxSize == 0) tzTargets[i].intMaxSize = Number.MAX_VALUE;
			tzTargets[i].elTarget.style.fontSize = (newTextSize < tzTargets[i].intMinSize) ? tzTargets[i].intMinSize.toString() + "%" : (newTextSize > tzTargets[i].intMaxSize) ? tzTargets[i].intMaxSize.toString() + "%" : newTextSize.toString() + "%";
		}
	}
}

function TextZoom(ref, targ, ratio, minSize, maxSize) {
	this.strReference = ref; // ID of reference element
	this.strTarget = targ; // ID of target element 
	this.intRatio = ratio; // width to text size ratio
	this.intMinSize = minSize; // minimum text size for target element in percentage units
	this.intMaxSize = maxSize; // maximum text size for target element in percentage units
	this.status = true;	// activate this text zoomer?
	this.elReference = null; // reference node
	this.elTarget = null; // target node
	tzTargets[tzTargets.length] = this; // add this zoomer to the list
}

var tzTargets = new Array();
var tzGoodToGo = false;
