var tags = new Array( 'div','td','tr','p','b','table','strong','emphasis','a','h1','h2','h3','pre','sub','sup','i','th','cp','ul','ol','li','dt','dd');
var pixelArray =  new Array('9','10','11','12','14','16','18');
var emArray =  new Array('0.7','0.9','1.0','1.5','2.0','2.5','3');

var countOfPixels = pixelArray.length;
var initSize = 2; // Array position of inital px size

function fontSizer(inc) {

	var size = readCookie('size');
	size = parseInt(inc)+parseInt(size);
	if (size < 0 ) { size = 0; }	
	if (size > countOfPixels ) { size = countOfPixels; }
	
	doFontSizing(size);	
	createCookie("size", size, 365);
}

function fontSizerOnLoad(preferredSize) {	
	var size = readCookie('size');	
	if (size < 0 ) { size = 0; }
	if (size > countOfPixels ) { size = countOfPixels; }
	
	doFontSizing(size);
}

function doFontSizing(theFontSize) {			
	// Bugfix: Elements inside table would not resize with only the code below, so we do them seperatly.
	// NOTE: This only resizes the font, nothing else.
	// Resize by ID does not work with this.
	aTables = document.getElementsByTagName('table');
	for(i = 0; i < aTables.length; i++){
			aTables[i].style.fontSize = pixelArray[theFontSize];
	}
	
	// if you rather want to size an individual element by ID, use this:
	// resizeContainer = document.getElementById('nameOfConainingElement');	
	// and uncomment the if statements at the start of each function
	
	//resizeContainer = document.getElementsByTagName('body').styles.fontSize;alert(resizeContainer);		
	//document.getElementsByTagName('body').style.fontSize ='20px';
	var size = theFontSize;
	getBody = document.getElementsByTagName('body')[0];
	for (i = 0 ; i < tags.length ; i++ ) {
		getallTags = getBody.getElementsByTagName(tags[i]);
	for (k = 0 ; k < getallTags.length ; k++) 
		//getallTags[k].style.fontSize = (unit=='px') ? pixelArray[size]+unit: emArray[size]+unit;
		getallTags[k].style.fontSize = pixelArray[size]+'px';
	}
	
}

function normalSize() {
	var size = 2;
	
	doFontSizing(size);	
	createCookie("size", size, 365);
}

function normalSizePrint() {
	var size = 2;
	doFontSizing(size);	
}

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 initSize;
}

window.onload = function(e) {
	fontSizerOnLoad(readCookie("size"));
}
