/**
*	functionsJS 
*	Useful client side javaScript functions
*	@author Mark Brook
*	03-Jan-2008
*/

	var elementArray = new Array('P','H1', 'H2', 'H3','H4', 'H5', 'H6','LI', 'UL', 'A','INPUT','LABEL','TEXTAREA', 'IMG', 'TD', 'SPAN' ); //array of elements to colour on mouse events
	
	
	var fntAdjustmentInPx; //current font adjustment
	var b; //reference to the body element
	
	//zoomtimer vars
	var zoomSecs;
	var zoomTimerID = null;
	var zoomTimerRunning = false;
	var zoomDelay = 500;
	
	

/*=================== Zoom functions =================*/
/*
* this function starts the zooming process
*	@param string direction - in or out
*/
function startZoom(direction){
	zoomSecs = 1
	StopTheZoomClock()
	StartTheZoomTimer(direction);

}// end startZoom

function stopZoom(){
	StopTheZoomClock();
}

function StartTheZoomTimer(direction){
	
	//every half a second, zoom in the required direction
	if (zoomSecs == 0){
		if (direction == 1){
			zoomIn();

		}else{
			zoomOut();
	
		}//end if
		
	}else{
		zoomSecs -= 1;
	
	}//end if
		zoomTimerRunning = true
		zoomTimerID = self.setTimeout("StartTheZoomTimer(" + direction + ")", zoomDelay)
	
}//end startTheZoomTimer

function StopTheZoomClock()
{
    if(zoomTimerRunning)
        clearTimeout(zoomTimerID)
    zoomTimerRunning = false
}


/*
*	zoomIn is used to increase the size of all text in the body
* of an html page.
*/
function zoomIn(){
	
	var fs; //retrieved font size

	//+ 1px to the size
	fntAdjustmentInPx += 1;
		
	for (i = 0; i <= elementArray.length; i++){
		e = document.getElementsByTagName(elementArray[i])
		
		for (j = 0; j <= e.length; j ++){
				
			try{
				//get the current font size for this element
				fs = retrieveComputedStyle(e[j], 'fontSize');
				
				/*
				*	some browser return px, some em and some%
				*	http://sureshjain.wordpress.com/2007/07/06/53/
				*	tells us that 1 em = 100% = 12pt = 16px
				*/
				if (fs.indexOf('%') >= 0){
					//remove the % from the fs
					fs = (parseInt(fs.replace("%", ""))) / 100 * 16;
					
				}else if (fs.indexOf('em') >= 0){
					//remove the em from the fs
					fs = (parseInt(fs.replace("em", ""))) * 16;
					
				}else{
					//remove the px from the fs
					fs = parseInt(fs.replace("px", ""));
				}//end if
				
				
				
				//+ 1px to the size
				
				fs += 1;
				//put the px back at the end
				e[j].style.fontSize = fs + 'px';
								
			}catch(err){
				//if an element does not exist, an error will
				//be thrown. Don't display it
			}
			
		}//end for
		
	}//end for
	
	setSessionCookie('fontAdjustmentInPx', fntAdjustmentInPx);

	
}//end zoomIn

/*
*	zoomIn is used to increase the size of all text in the body
* of an html page.
*/
function zoomOut(){
	var fs; //retrieved font size
	//- 1px to the size
	fntAdjustmentInPx = parseInt(fntAdjustmentInPx) - 1;
	
	for (i = 0; i <= elementArray.length; i++){
		e = document.getElementsByTagName(elementArray[i])
		
		for (j = 0; j <= e.length; j ++){
				
			try{
				//get the current font size for this element
				fs = retrieveComputedStyle(e[j], 'fontSize');

				/*
				*	some browser return px, some em and some%
				*	http://sureshjain.wordpress.com/2007/07/06/53/
				*	tells us that 1 em = 100% = 12pt = 16px
				*/
				if (fs.indexOf('%') >= 0){
					//remove the % from the fs
					fs = (parseInt(fs.replace("%", ""))) / 100 * 16;
					
				}else if (fs.indexOf('em') >= 0){
					//remove the em from the fs
					fs = (parseInt(fs.replace("em", ""))) * 16;
					
				}else{
					//remove the px from the fs
					fs = parseInt(fs.replace("px", ""));
				}//end if

				fs -= 1;
				
				//put the px back at the end
				e[j].style.fontSize = fs + 'px';
								
			}catch(err){
				//if an element does not exist, an error will
				//be thrown. Don't display it
			}
			
		}//end for
		
	}//end for
	
	setSessionCookie('fontAdjustmentInPx', fntAdjustmentInPx);
	
}//end zoomOut

/*
*	resetZoom is used to go through all page elements and adjust
* them to default size i.e. 0 adjustment
*/
function resetZoom(){
	fntAdjustmentInPx = "0";
	setSessionCookie('fontAdjustmentInPx', fntAdjustmentInPx);
	location.reload(true);
}//end resetZoom


/*
*	setZoom is used to go through all page elements and adjust
* them according to the fontAdjusmentInPx value which is stored
*	in the sites cookie
*/
function setZoom(){
	//find out if a cookie exists with a font size for this page
	fntAdjustmentInPx = getCookie('fontAdjustmentInPx');
	
	if (fntAdjustmentInPx == false){
		//if no cookie exists, set fntAdjustmentInPx to 0
		fntAdjustmentInPx = 0;
	}else{
		fntAdjustmentInPx = parseInt(fntAdjustmentInPx);
	}
	
	var fs; //retrieved font size
	
	for (i = 0; i <= elementArray.length; i++){
		e = document.getElementsByTagName(elementArray[i])
		
		for (j = 0; j <= e.length; j ++){
				
			try{
				//get the current font size for this element
				fs = retrieveComputedStyle(e[j], 'fontSize');
				//remove the px from the fs
					/*
				*	some browser return px, some em and some%
				*	http://sureshjain.wordpress.com/2007/07/06/53/
				*	tells us that 1 em = 100% = 12pt = 16px
				*/
				if (fs.indexOf('%') >= 0){
					//remove the % from the fs
					fs = (parseInt(fs.replace("%", ""))) / 100 * 16;
					
				}else if (fs.indexOf('em') >= 0){
					//remove the em from the fs
					fs = (parseInt(fs.replace("em", ""))) * 16;
					
				}else{
					//remove the px from the fs
					fs = parseInt(fs.replace("px", ""));
				}//end if

				
				//adjust the size according to fontAdjustment
				fs += fntAdjustmentInPx;
				//put the px back at the end
				e[j].style.fontSize = fs + 'px';
				//alert(e[j].nodeName + ' ' + fs + 'px');

		}catch(err){
				//if an element does not exist, an error will
				//be thrown. Don't display it
		}
			
		}//end for
		
	}//end for
}//end set zoom

/**
* used to calculate the computed style of an element
* taking into account the various css rules
* 
*	I took this function from (James & Adams, 2006, pp. 205-206)
*
*	James, E. & Adams, C. (2006) JavaScript Anthology 101 Essential Tips, Tricks & Hacks. SitePoint Pty. Ltd.
*
*	@param element - the element whos style is to be read
*	@param styleProerty	- property to retrieve
*/
function retrieveComputedStyle(element, styleProperty)
{
  var computedStyle = null;

  if (typeof element.currentStyle != "undefined")
  {
    computedStyle = element.currentStyle;
  }
  else
  {
    computedStyle = document.defaultView.getComputedStyle(element, null);
  }

  return computedStyle[styleProperty];
}//end retrieveComputedStyle


/*=================End zoom functions =================*/


/*================= Cookie functions ===========================*/
/*
*	setCookie 
* @param string name - name of the cookie
* @param string value - the value of the cookie
*/
function setCookie(name, value){
	var cookieName =name;
	var cookieValue = value;
	
	var exdate=new Date();
	
	//get the current date and add 365 days to it
	exdate.setDate(exdate.getDate() + 365);

	var theCookie = cookieName + "=" + cookieValue + ';expires=' + exdate.toGMTString();
	
	document.cookie = theCookie;
	
}//end setCookie

/*
*	setSessionCookie 
* @param string name - name of the cookie
* @param string value - the value of the cookie
*/
function setSessionCookie(name, value){
	
	document.cookie=name+ "=" + value;
	
}//end setSessionCookie


/*
* getCookie function will retrieve a cookie valeu
* @param string searchName
*
* @return string - the cookie value
* 
*/
function getCookie(searchName){
	var cookies = document.cookie.split(";");

	for (var i = 0; i < cookies.length; i++)	{
		var cookieCrumbs = cookies[i].split("=");
		var cookieName = cookieCrumbs[0];
		
		//if there is a leading space, remove it
		if(cookieName.substr(0,1) == " "){
			cookieName = cookieName.substr(1, cookieName.length - 1);
		}//end if
		
		var cookieValue = cookieCrumbs[1];
		
		if (cookieName == searchName){
			return cookieValue;
		}//end if
	
	}//end if
	
	return false;

}//end getCookie

/*========================= End cookie functions =============*/



/*=================================================
									Onload 
===================================================*/


window.onload = function(e) {
	setZoom();
}

window.onunload = function(e) {
}