//FW Resize Font Javascript//
/**
* FW Resize Font Javascript
* @package Joomla
* @copyright Copyright (C) 2006 Futureworkz Pte. Ltd. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

var tags = new Array('div','p','b','strong','em','a','h1','h2','h3','pre','sub','sup','i','th','cp','ul','ol','li','dt','dd','input');
var defaultSize = 15;
var resizeCounter = 0;
var increment = 2;

function adjustFontSizes(resize) { // adjust fontSize of all appropriate elements by resize * increment
	var docBody;
	var i;
	var allTags;
	var k;
	var size;
	//
	if (!document.getElementById) {
		return;
	}
	if (resize == 0) {
		return;
	}
	if (resize < 0 && resizeCounter + resize < -2) { // if this size reduction would take us more than two steps below normal, do nothing
		return;
	}
	docBody = document.getElementsByTagName('body')[0]; // get the first and only <body> element
	for (i = 0; i < tags.length; i++) { // for each listed tag name
		allTags = docBody.getElementsByTagName(tags[i]); // get all elements inside body having this tag name
		for (k = 0; k < allTags.length; k++) { // for each element
			size = parseInt(allTags[k].style.fontSize.substring(0, allTags[k].style.fontSize.length - 2)); // assumes fontSize value has two-letter suffix e.g. 'px'
			if (!size) {
				size = defaultSize;
			}
			allTags[k].style.fontSize = (size + resize * increment) + 'px'; // do arithmetic, then concatenate
		}
	}
	resizeCounter += resize;
}

function resetFontSizes() {
	adjustFontSizes(-resizeCounter);
}

//cookie functions for remember the font sizes across pages
function createCookie(name, value, days) {
	var expires;
	var date;
	if (days) {
		date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	} else {
		expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) { // return value of cookie with name, else null
	var nameEQ = name + "=";
	var ca = document.cookie.split(';'); // get array of cookie name=value strings
	var i;
	var c;
	//
	for (var i = 0; i < ca.length; i++) {
		c = ca[i];
		while (c.charAt(0) == ' ') { // trim spaces from LHS
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0) { // if it starts with "name="
			return c.substring(nameEQ.length, c.length); // return the value
		}
	}
	return null; // iff no match for name
}

function setUserOptions() {
	var cookie;
	//
	if (cookie = readCookie('FWresizeFont')) {
		adjustFontSizes(parseInt(cookie));
	 //	alert('fetched resizeCounter = ' + parseInt(cookie));
	}
}

function saveSettings() {
	createCookie("FWresizeFont", resizeCounter, 365);
 //	alert('saved resizeCounter = ' + resizeCounter);
}

// "push" these handlers...
window.onload = chainFunc(window.onload, setUserOptions);
window.onunload = chainFunc(window.onunload, saveSettings);

