//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.
*
* Modified by Yuen Li
* VPRI Internetdiensten
*/
function FontSizeLimit(resize, size) {
	if (size) {
		if (resize > 0)
			return ((size + increment) > maxSize) ? false : true;
		else {
			return ((size - increment) < minSize) ? false : true;
		}
	}
	else {
		return true;
	}
}
function FWresizeFont(resize) {
	if (!document.getElementById) {
		return;
	}
	for (x = 0 ; x < docTags.length ; x++ ) {
		
		if (docTags.length == 1 && docTags[0] == 'body') {
			docBody = document.getElementsByTagName('body')[0];
		}
		else if (document.getElementById(docTags[x])) {
			docBody = document.getElementById(docTags[x]);
		}
		else {
			continue;
		}
		
		for (i = 0 ; i < tags.length ; i++ ) {
			allTags = docBody.getElementsByTagName(tags[i]);
			if (resize > 0) {
				for (k = 0 ; k < allTags.length ; k++) {	
					var size = parseInt(allTags[k].style.fontSize.substring(0,allTags[k].style.fontSize.length-2));
					if (!FontSizeLimit(resize, size)) {
						return;	
					}
					if (size) {
						allTags[k].style.fontSize = (size + increment) + 'px';
					}
					else {
						allTags[k].style.fontSize = defaultSize + 'px';
					}
				}			
			}
			else {
				for (k = 0 ; k < allTags.length ; k++) {
					var size = parseInt(allTags[k].style.fontSize.substring(0,allTags[k].style.fontSize.length-2));
					if (!FontSizeLimit(resize, size)) {
						return;	
					}
					if (size && size > 2) {
						allTags[k].style.fontSize = (size - increment) + 'px';
					}
					else {
						allTags[k].style.fontSize = defaultSize + 'px';
					}
				}			
			}
		}
	}
	
	resize > 0 ? resizeCounter++ : resizeCounter--;
}
function setFontSize(resizeCounter) {
	for (x = 0 ; x < docTags.length ; x++ ) {
		if (!document.getElementById(docTags[x])) {
			continue;
		}
		docBody = (docTags.length == 1 && docTags[0] == 'body') ? document.getElementsByTagName('body')[0] : document.getElementById(docTags[x]);
		for (i = 0 ; i < tags.length ; i++ ) {		
			allTags = docBody.getElementsByTagName(tags[i]);
			for (k = 0 ; k < allTags.length ; k++) {
				var size = parseInt(allTags[k].style.fontSize.substring(0,allTags[k].style.fontSize.length-2));
				if (!FontSizeLimit(resizeCounter)) {
					return;	
				}
				var resize = size + resizeCounter;			
				if (!size) {
					allTags[k].style.fontSize = (parseInt(defaultSize) + parseInt(resizeCounter)) + 'px';
				} else if (size && resize > 0) {
					allTags[k].style.fontSize = resize + 'px';
				}
			}
		}
	}
}
function resetFontSize() {
	for (x = 0 ; x < docTags.length ; x++ ) {
		if (!document.getElementById(docTags[x])) {
			continue;
		}
		docBody = (docTags.length == 1 && docTags[0] == 'body') ? document.getElementsByTagName('body')[0] : document.getElementById(docTags[x]);
		for (i = 0 ; i < tags.length ; i++ ) {		
			allTags = docBody.getElementsByTagName(tags[i]);		
			for (k = 0 ; k < allTags.length ; k++) {
				allTags[k].style.fontSize = defaultSize + 'px';
			}
		}
	}
	resizeCounter = 0;
}
//cookie functions for remember the font sizes across pages
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
		else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(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;
}
function setUserOptions() {
	cookie = readCookie("FWresizeFont");
	if (cookie) {
		recallCounter = cookie;
	} else {
		return null;
	}
	setFontSize(recallCounter);
	resizeCounter = recallCounter;
}
function saveSettings() {
  createCookie("FWresizeFont", resizeCounter, 365);
}
window.onload = setUserOptions;
window.onunload = saveSettings;


