// html font size manipulation abstraction

var hf = null;
var cookieName = 'fontsize';

function setDefaultFontValues(){
	
	eraseCookie(cookieName);
	hf.ResetFontSize();
}


function presetHtmlFont()
{
	if (hf == null) 
	{
		hf = new HtmlFont(12);
		
		hf.registerElementByTagName('p');
		hf.registerElementByTagName('h1');
		hf.registerElementByTagName('h2');
		hf.registerElementByTagName('h3');
		hf.registerElementByTagName('h4');
		hf.registerElementByTagName('h5');
		hf.registerElementByTagName('h6');
		hf.registerElementByTagName('ul');
		hf.registerElementByTagName('ol');
		hf.registerElementByTagName('span');
		hf.registerElementByTagName('font');
		hf.registerElementByTagName('td');
		
	}
}

function increaseFontSize(step)
{
	presetHtmlFont(); 
	hf.increaseFontSize(step);
	storeTagsFontSizeInCookie();
}

function decreaseFontSize(step)
{
	presetHtmlFont(); 
	hf.decreaseFontSize(step);
	storeTagsFontSizeInCookie();
}

function storeTagsFontSizeInCookie()
{
	var value = '';
	var cookieExpiryDays = 365;
	var tagNames = hf.getRegisteredTagNames();
	
	for (tagName in tagNames)
	{
		if (value != '')
			value += ',';
		
		value += tagName + ':' + tagNames[tagName];
	}
	createCookie(cookieName, value, cookieExpiryDays);
}

function updateRegisteredElementFontSize()
{
	presetHtmlFont(); 
	var vValue;
	var cookie = readCookie(cookieName);

	if (cookie)
	{
		var cookieValues = cookie.split(',');
		
		for (cookieValue in cookieValues)
		{
			var nameValuePairArr = cookieValues[cookieValue].split(':');
			hf.registerElementByTagName(nameValuePairArr[0], nameValuePairArr[1]);				
		}
		hf.updateRegisteredElementFontSize();
	}
}

function tagVisibility(tagName, visible)
{
	var elem = document.getElementsByTagName(tagName);
		
	for(n=0; n<elem.length; n++)
	{ 
		elem[n].style.visibility = (visible) ? 'visible' : 'hidden';
	}
}

function onPageLoad()
{
	updateRegisteredElementFontSize();
}

// HtmlFont class
function HtmlFont(defaultFontSize)
{
	var gFSizeReference = defaultFontSize;
	var tagNames = new Array();
	var browser = new Browser();
	
	// add tags on which an action is to be performed
	this.registerElementByTagName = function(tagName, value)
	{
		if (!value)
			value = gFSizeReference;
			
		tagName = trim(tagName);
		tagNames[tagName] = value;
	}
	
	this.updateRegisteredElementFontSize = function()
	{
		for (tagName in tagNames)
		{
			var elem = document.getElementsByTagName(tagName);
			var finalSize = tagNames[tagName];

			for(n=0; n<elem.length; n++)
			{ 
				if (finalSize < 5)
					finalSize = 5;
				else if (finalSize > 48)
					finalSize = 48;
					
				elem[n].style.fontSize = finalSize + "px";
			}
		}
	}
	
	this.getRegisteredTagNames = function()
	{
		return tagNames;
	}

	this.increaseFontSize = function(step)
	{
		changeFontSize(Math.abs(step));
	}
	
	this.decreaseFontSize = function(step)
	{	
		if (step > 0)
			step *= -1;
			
		changeFontSize(step);
	}	
	
	function changeFontSize(step)
	{
		for (tagName in tagNames)
		{			
			var elem = document.getElementsByTagName(tagName);
			
			for(n=0; n<elem.length; n++)
			{    
				if (browser.getName() == BrowserEnum.IE)				
				{
					fSize = elem[n].currentStyle['fontSize'].replace("px", "").replace("pt", "");
				}
				else 
				{
					fSize = document.defaultView.getComputedStyle(elem[n],null).getPropertyValue("font-size").replace("px", "").replace("pt", "");
				}
						
				if ((fSize == undefined) || (fSize == ""))
				{
					fSize = gFSizeReference;
				}
					
				var finalSize = parseInt(fSize) + parseInt(step);
				
				tagNames[tagName] = finalSize;
				
				if ((finalSize > 5) && (finalSize < 48))
					elem[n].style.fontSize = finalSize + "px";  
			}
			if(elem.length == 0) {
				//CHANGE FONT FOR TAGS NOT FOUND IN THIS PAGE
				tagNames[tagName] = parseInt(tagNames[tagName]) + parseInt(step);
			}
		}
	}
	
	this.ResetFontSize = function()
	{
		for (tagName in tagNames)
		{
			var elem = document.getElementsByTagName(tagName);

			for(n=0; n<elem.length; n++)
			{ 				
				elem[n].style.fontSize = "";
			}
		}
	}
}


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 var expires = "";
	
	var cookie = name+"="+value+expires+"; path=/";

	document.cookie = cookie;
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}


// represents a Browser enumerate
var BrowserEnum = 
{
	NotKnown:"Not Known",
	IE:"Microsoft Internet Explorer",
	FF:"Netscape"
}

// Browser class
function Browser()
{
	// START - private  fields
	var name = "";
	var version = "";
	// END - private  fields
	
	// START - do initialization
	detectBrowser();
	// END - do initialization
	
	// START - private functions
	function detectBrowser()
	{
		var bName = navigator.appName;
		version = parseFloat(navigator.appVersion);		
		
		switch (bName)
		{
			case BrowserEnum.IE:
				name = BrowserEnum.IE;
				break;
			case BrowserEnum.FF:
				name = BrowserEnum.FF;
				break;
			default: // store whatever appName is
				name = bName;
		}
	}	
	// END - private functions
	
	// START - public functions
	this.getName = function()
	{
		return name;
	}	
	
	this.getVersion = function()
	{
		return version;
	}
	// END - public functions
}

/*
	String functions
*/

// Left trim
function lTrim(value)
{
	return value.replace( /^\s+/g, "" );// strip leading
}

// Right trim
function rTrim(value)
{
	return value.replace( /\s+$/g, "" );// strip trailing
}

// Full trim
function trim(value)
{
	return lTrim(rTrim(value));
}