// function to increase or decrease styles via the buttons built in the function below
function resizeText(direction) {
	// rebuff tests
	if(!baseElem) return false;
	
	if(direction == 'default') {
		// reset to our base value
		upCount=0;
		downCount=0;
		baseElem.style.fontSize = defaultFontSize + "em";
		currentFontSize = defaultFontSize;
	} else if(direction == 'large') {
	if (upCount <=2){	
		upCount++	
		// adjust currentFontSize, ensuring rounding to 2 decimal places
		currentFontSize = roundNumber(currentFontSize + sizeInc, 2);
		// set the base element font-size
		baseElem.style.fontSize = currentFontSize + "em";}
	} else if(direction == 'small') {
	if (downCount >=-2){	
	downCount--
		// adjust currentFontSize, ensuring rounding to 2 decimal places
		currentFontSize = roundNumber(currentFontSize - sizeInc, 2);
		// set the base element font-size
		baseElem.style.fontSize = currentFontSize + "em";}
	}
	// reset the cookie
	//eraseCookie("savedFontSize");
	createCookie("savedFontSize",currentFontSize,365);
}

// basic global variables for the style switcher:
// 1. defaultSize is the base font-size for #page, 1em. This value is the start value for a new user.
// 2. currentFontSize is adjustable via cookie / button press on the controls (see initResizer)
defaultFontSize = currentFontSize = 1.0;
// 4. Set the increment value, as we're using ems it's 0.2
sizeInc = .2;
upCount=0
downCount=0

// function does the following:
// 1. Checks for 'savedFontSize' cookie and takes the value within OR builds default 'savedFontSize' cookie
// 2. Builds relevant html for the switcher controls
function initResizer() {
	// rebuff tests
	if(!document.getElementById("primary_nav")) return false;
	if(!document.createElement("span")) return false;
	
	// 3. Set the base font sizing element here so it can be used throughout
	baseElem = document.getElementById("page");
	
	// STAGE 1: The cookie testing phase
	//var fontSize_cookie = readCookie("savedFontSize");
	if (fontSize_cookie = readCookie("savedFontSize")) {
		// if we have a cookie value, update the currentFontSize global var (and make sure it's a NUMBER)
		currentFontSize = Number(fontSize_cookie);
		// reset the #page font-size value immediately
		baseElem.style.fontSize = fontSize_cookie + "em";
	}
	// if there isn't a cookie, set a cookie with the currentFontSize global var
	createCookie("savedFontSize",currentFontSize,365);
	
	// STAGE 2: Setting up the resize controls
	// set up the host html fragment
	var host_el = document.getElementById("primary_nav");
	// create the container <span>
	var sp = document.createElement("span");
	sp.setAttribute("id","text_resizer");
	var sp_text = document.createTextNode("Resize text");
	sp.appendChild(sp_text);
	
	// create magnifier img, and assemble
	var img = document.createElement("img");
	img.setAttribute("src","images/client/selfridges01/text_resize_plus.gif");
	img.setAttribute("title","Increase the text size for this site");
	img.onclick = function() {
		resizeText('large');
	}
	// append img item to span
	sp.appendChild(img);
	
	// create zero out img, and assemble
	var img_2 = document.createElement("img");
	img_2.setAttribute("src","images/client/selfridges01/text_resize_zero.gif");
	img_2.setAttribute("title","Return to the default text size for this site");
	img_2.onclick = function() {
		resizeText('default');
	}
	// append img item to span
	sp.appendChild(img_2);
	
	// create reducer img, and assemble
	var img_3 = document.createElement("img");
	img_3.setAttribute("src","images/client/selfridges01/text_resize_minus.gif");
	img_3.setAttribute("title","Decrease the text size for this site");
	img_3.onclick = function() {
		resizeText('small');
	}
	// append img item to span
	sp.appendChild(img_3);
	
	// append ul to host html
	host_el.appendChild(sp);
}

// add to the onload queue...
//addLoadEvent(initResizer);