/*------------------------------------------------------------
	Document Text Sizer- Copyright 2003 - Taewook Kang.  All rights reserved.
	Coded by: Taewook Kang (txkang.REMOVETHIS@hotmail.com)
	Web Site: http://txkang.com
	Script featured on Dynamic Drive (http://www.dynamicdrive.com)
	
	Please retain this copyright notice in the script.
	License is granted to user to reuse this code on 
	their own website if, and only if, 
	this entire copyright notice is included.
--------------------------------------------------------------*/

//Specify affected tags. Add or remove from list:
var arrTags = new Array('table','span','div','class','td','tr','a','p');

//Specify spectrum of different font sizes:
var arrSizes = new Array( '8pt','10pt','12pt','14pt','16pt' );

// global cookie name
var strCookieName = new String("SIZEPREF");

/*********  begin functions section *********/

// this function just calls the regular one without any fuss, did this so can customize load event later
function setDefault(){
	setFont('body',intDefaultSize);
}

// this function is going to translate the increment to determine the font
function ts( trgt, inc ) {
	// if inc is 0, that implies user has requested a "reset" to no preference
	if(inc == 0 ){
		// remove cookie and reload page
		RemoveCookie(strCookieName);
		window.location.reload();
		return;
	} // else, continue
	
	// translate the increment to a position on the font array + 1 (0 is reserved for resetting things)
	// need to know the starting point on the array (default font)
	// this will come from the cookie if present
	var sz = intDefaultSize; // the global default size to use on the page
	sz += inc; // increment the size as requested
	
	// now, call the function that will set the font size
	setFont(trgt, sz);
	return;
}

function setFont( trgt, intStartFont ) { 
	// exit if can't get elements by ID
	if (!document.getElementById) return
	// set variables for function
	var d = document; // the document object
	var cEl = null; // the element (trgt)
	var sz = (intStartFont - 1); // set a local variable to this size
	var i; // used for iterating through the arrTags array
	var j; // used for iterating through elements on the page
	var cTags;

	// do nothing if it is not a valid number
	if(isNaN(sz)){
		return;
	}

	// keep size within contraints
	if ( sz < 0 ) sz = 0;
	if ( sz > (arrSizes.length - 1)) sz = (arrSizes.length - 1);

	intDefaultSize = sz + 1;// reset global start size
	
	// if can't get by elementid, try by tagname, first element
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	// set font size based on array position
	cEl.style.fontSize = arrSizes[ sz ];

	for ( i = 0 ; i < arrTags.length ; i++ ) {
		cTags = cEl.getElementsByTagName( arrTags[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = arrSizes[ sz ];
	}
	// set a cookie to the array position of the selected size
	SetCookie(intDefaultSize);
}

/*---------------------------------
	cookie management added by State of NH, Web Services Division
---------------------------------*/

// this function retreives the cookie font-size preference, if it is available
function GetCookie(){
	// pattern in which to find cookie
	var regExp = new RegExp(strCookieName + "=([^;]*)");
	// execute pattern against available cookies, returning what we need
	var sizePref = regExp.exec(document.cookie);
	// return the contents if present, else flag as "no cookie"
	sizePref = (sizePref) ? unescape(sizePref[1]) : "no cookie";

	// make sure we know that we're getting something valid
	if(!sizePref){
		sizePref = "no cookie";  // sizePref is undefined 
	}else{
		if(sizePref === null){
			sizePref = "no cookie";  // sizePref is null
		}
	}
	
	// attempt to convert it to a number
	sizePref = Number(sizePref);

	// make sure it converted, if not, flag as no cookie
	if(isNaN(sizePref)){
		sizePref = "no cookie";
	}
	return sizePref;
}

// this function sets the cookie for the user font size preference
function SetCookie(intSize){
	if(arguments.length < 1){
		return false;
	}
	// build cookie, we are not using expiration, domain, or secure for this 
	var strCookieString = escape(strCookieName) + "=" + escape(intSize) + "; path=/";

	// set the cookie
	document.cookie = strCookieString;
	return true;
}

function RemoveCookie(strCookieName){
	document.cookie = strCookieName += "=; path=/";
}

// this implements the default behavior for the specified event
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return true; 
	} else { 
		return false; 
	} 
}

/********* end functions section **********/

// get stored size preference; sets to "no cookie" if it is not available
var startSz = GetCookie();
var intDefaultSize = 3;

if(!isNaN(startSz)){
	//Specify the default size (position in the 0-based array of arrSizes+1)
	intDefaultSize = startSz;
	addEvent(window, 'load', setDefault);
}

