/**
 * (C) GOSS Ltd. 2006.  http://www.gossinteractive.com
 *
 * A dynamic font resizing tool for GOSS sites.  For usage refer to accompanying
 * documentation.
 */
function FontSizer(maxIncrease, changePercent) {
	this.maxDecrease = 0;
	this.maxIncrease = maxIncrease;
	this.changePercent = changePercent;
	this.currentPercent = 100;
	
	// Get back any previously persisted size
	var toPercent = this.ReadCookie("GossDynFontSizerCookie");
	if (toPercent != null) {
		// Got a size to jump to - so do that(!)
		this.SetFontSize(toPercent);
	}
}

FontSizer.prototype.IncreaseFont = function() {
	var proposedSize = this.currentPercent + this.changePercent;
	this.SetFontSize(proposedSize);
}

FontSizer.prototype.DecreaseFont = function() {
	var proposedSize = this.currentPercent - this.changePercent;
	this.SetFontSize(proposedSize);
}

FontSizer.prototype.GetCurrentStep = function() {
	return Math.round((this.currentPercent - 100) / this.changePercent);
}

FontSizer.prototype.SetCurrentStep = function(step) {
	var proposedSize = 100 + (step * this.changePercent);
	this.SetFontSize(proposedSize);
}

/**
 * PRIVATE - see IncreaseFont(), DecreaseFont(), GetCurrentStep() and SetCurrentStep(step)
 * Sets the current font size to a new percentage.  The percentage always starts
 * at 100% so to increase from 100% to 120% set the size to 120.  The font
 * sizer will find the percentile difference between the current size and the
 * proposed size and increase/decrease accordingly.
 */
FontSizer.prototype.SetFontSize = function(fontSize) {
	// Check not gone past the max or before the min
	if ((fontSize > (100 + this.maxIncrease)) || (fontSize < (100 + this.maxDecrease))) {
		// Quit it - gone tooooo far!
		return;
	}
	
	// Work out the percentile multipler thingy (the percentage difference between
	// the starting size and the destination size).
	var percentDiff = fontSize / this.currentPercent;
	
	// Need to run through the elements we want to change and multiply their
	// existing percentage size by our percentile thingy.
	this.ResizeElementsByTagName("body", percentDiff);
	
	// Remember our new size and stick it in a cookie
	this.currentPercent = fontSize;
	this.CreateCookie("GossDynFontSizerCookie", this.currentPercent, null);
}

/**
 * PRIVATE - see SetFontSize(fontSize)
 * Resizes all the elements with the specified name by the specified multiplier
 */
FontSizer.prototype.ResizeElementsByTagName = function(tagName, multiplier) {
	if (!document.getElementsByTagName) {
		// Browser don't do it.  So don't try it.
		return;
	}

	// Get alll the elements with the specified tag name and change them in turn
	var elements = document.getElementsByTagName(tagName);
	for (var num = 0; num < elements.length; num++) {
		var el = elements[num];
		var elSize;
	
		// Try and get the current size of the element (browser specific nonsense)
		if (el.currentStyle) {
			// This is IE syntax
			elSize = el.currentStyle['fontSize'];
		} else if (window.getComputedStyle) {
			// This is Opera and Firefox syntax
			elSize = document.defaultView.getComputedStyle(el, null).getPropertyValue('font-size');
		} else {
			// Dunno what to do - go to next one
			continue;
		}
		
		// Strip out any characters in the resulting size to get just the size
		var newSize = this.DropAlpha(elSize);
		
		// String out any numbers in the resulting size to get just the units
		var elUnits = this.DropNumerics(elSize);
		
		// A lot of effort just to find out the size of an element...  Oh well...  Can
		// at last set its size to something increased or decreased by the specified
		// multipler.
		el.style.fontSize = (newSize * multiplier) + elUnits;
	}
}

/**
 * PRIVATE - see ResizeElementsByTagName(tagName, multiplier)
 * Removes chars from a string representing a number
 */
FontSizer.prototype.DropAlpha = function(str) {
	var retVal = new String();
	var validChars = "0123456789.";

	for (var i = 0; i < str.length; i++) {
		if(validChars.indexOf(str.charAt(i)) > -1) {
			retVal += str.charAt(i);
		} else {
			//drop out of loop - NAN reached
			break;
		}	
	}
	
	return retVal;
}

/**
 * PRIVATE - see ResizeElementsByTagName(tagName, multiplier)
 * Removes numbers from a string leaving just the string bits
 */
FontSizer.prototype.DropNumerics = function(str) {
	var retVal = new String();
	var invalidChars = "0123456789.";

	for (var i = 0; i < str.length; i++) {
		if(invalidChars.indexOf(str.charAt(i)) <= -1) {
			retVal += str.charAt(i);
		}	
	}
	
	return retVal;
}

/**
 * PRIVATE - see SetFontSize(fontSize)
 * Sets a cookie into the clients browser.
 */
FontSizer.prototype.CreateCookie = function(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else {
		var expires = "";
	}
	
	document.cookie = name + "=" + value + expires + "; path=/";
}

/**
 * PRIVATE - see FontSizer constructor
 * Gets a cookies value from the clients browser.
 */
FontSizer.prototype.ReadCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		
		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
		
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	
	return null;
}
