//called onLoad
function doInitialisation() {
	initStyle();
	initFontSize();
}

/***********************
 * Start Font Size Section *
 ***********************/
//chosen font style. There are 7 available sizes. 0 is smallest, 3 is middle and 6 is the largest
var g_iFontSize = 3;

//Site font size specifications
var gc_strFontSize0 = "80%";
// var gc_strFontSize1 = "95%";
var gc_strFontSize1 = "100%";
var gc_strFontSize2 = "110%";
var gc_strFontSize3 = "140%";
var gc_strFontSize4 = "160%";
var gc_strFontSize5 = "180%";
var gc_strFontSize6 = "200%";
 
 //user actions
var gc_iDecrease = 0;
var gc_iIncrease = 1;


//read from the cookie and see if we need to change the font size
function initFontSize() {
	//if css is off set the font size to 4 (100%) and exit
	if ( g_bStyleDisabled ) {	
		setPageFont ( 4 );
		return; 
	}	

	//if cookie fontsize value exists then set it
	var iStrCookieVal = readCookie( "g_iFontSize" );
	if ( iStrCookieVal != null ) 
		setPageFont ( iStrCookieVal );
}


//changes the font size for the entire page
function resizeText( iDirectgc_iOn ) {
	( iDirectgc_iOn == gc_iIncrease ) ? g_iFontSize++: g_iFontSize--;

	switch ( g_iFontSize ) {
		//in expected range so set it
		case 0: case 1: case 2: case 3: case 4: case 5: case 6:
			setPageFont ( g_iFontSize ); 		
			break;
			
		//would be below bounds so set to smallest
		case -1:
			setPageFont ( 0 ); 
			alert ( "Minimum font size reached" );
			break;
			
		//would be above bounds so set to the largest
		case 7:
			setPageFont ( 6 );
			alert ( "Maximum font size reached" );
			break;
			
		//if this happens the code is wrong
		default:
			alert ( "Unexpected error manipulating font size" );
			return;
	}
}

//when the font change we need to set the css value, the global and the cookie
function setPageFont( iFontSize ) {
	document.body.style.fontSize =	eval ( "gc_strFontSize" + iFontSize );
	g_iFontSize = iFontSize;
	createCookie( "g_iFontSize", iFontSize , 7 ) ;
}
/***********************
 * End Font Size Section *
 ***********************/

 
/**************************
 * Start Style Change Section *
 **************************/
 //Boolean specifies all stylesheets should be enabled/disabled
var g_bStyleDisabled = false;

 //actions when manipulating styles
var gc_iOff = 0;
var gc_iOn = 1;
var gc_iToggle = 2;


//read from the cookie and see if the style need disabling
function initStyle() {
	var iStrCookieVal = readCookie( "g_bStyleDisabled" );
	
	if ( iStrCookieVal != null ) {
		( iStrCookieVal == "true" ) ? ( g_bStyleDisabled = true ) : ( g_bStyleDisabled = false );
		
		 if( g_bStyleDisabled == true )
			manipulateStyle( gc_iOff );
	}
}


// change the style, turn it on/off or toggle it
function manipulateStyle( iAction ) {
	switch ( iAction ) {
		case gc_iOff:
			g_bStyleDisabled = true;
			break;
			
		case gc_iOn:
			g_bStyleDisabled = false;
			break;
			
		case gc_iToggle:
			g_bStyleDisabled = !g_bStyleDisabled;
			break;
	}

	( g_bStyleDisabled ) ?setPageFont ( 4 ) : setPageFont ( 3 );
	
	var iNumSheets = document.styleSheets.length;
	var strLinkText = "Styling ";
	
	for ( var i=0; i < iNumSheets; i++ ) 
		document.styleSheets[ i ].disabled = g_bStyleDisabled;
	
	g_bStyleDisabled ? ( strLinkText += "On" ) : ( strLinkText += "Off" ) ;
	document.getElementById( 'style_toggle' ).innerHTML = strLinkText;

	createCookie( "g_bStyleDisabled", g_bStyleDisabled , 7 ) ;
}
/*************************
 * End Style Change Section *
 *************************/

 
/************************** 
 * Start Search Focus Section *
 **************************/
 function enterSearch() {	
	document.getElementById( 'query' ).select();
 } 
/*************************
 * End Search Focus Section *
 **************************/
 
 
/****************************************
 * Start Cookie Handling Section                        *
*  http://www.quirksmode.org/js/cookies.html *
 *****************************************/
 
 //name and value represent the cookie, days the cookies expiry
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 = "";
		
	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 eraseCookie( name ) {
	createCookie( name, "", -1 );
}
/***************************
 * End Cookie Handling Section *
 ****************************/
