// A global variable to keep track of the font size (-2,-1,0,1,2)
var size = 0;

function initFontSize() {
	var cookie = readCookie('bcfontsize');
	
	var mainContent = document.getElementById("mainColumn");
	var wrapperContent = document.getElementById("wrapper");
	var headerContent = document.getElementById("header");
	var smallerLink = document.getElementById("smaller");
	var biggerLink = document.getElementById("bigger");
	
	if (cookie == -2) {	
		size = -2;
		mainContent.style.fontSize = "0.5em";
		wrapperContent.style.fontSize = "0.8em";
		headerContent.style.fontSize = "1.06em";
		// Disable the "Decrease Text Size" icon link
		smallerLink.removeAttribute('href');
		// Set the "Decrease Text Size" icon to grey
		smallerLink.firstChild.src = "images/textSizer/minusGrey.gif";
	}	
	if (cookie == -1) {	
		size = -1;
		mainContent.style.fontSize = "0.8em";
		wrapperContent.style.fontSize = "0.9em";
		headerContent.style.fontSize = "1.03em";
	}	
	if (cookie == 1) {	
		size = 1;
		mainContent.style.fontSize = "1.4em";
		wrapperContent.style.fontSize = "1.1em";
		headerContent.style.fontSize = "0.97em";
	}	
	if (cookie == 2) {	
		size = 2;
		mainContent.style.fontSize = "1.7em";
		wrapperContent.style.fontSize = "1.2em";
		headerContent.style.fontSize = "0.94em";
		// Disable the "Decrease Text Size" icon link
		biggerLink.removeAttribute('href');
		// Set the "Decrease Text Size" icon to grey
		biggerLink.firstChild.src = "images/textSizer/plusGrey.gif";
	}	
}
	



function textSize(obj)
{
	// Preload the greyed out images
	if (document.images) {
    img1 = new Image();
    img1.src = "images/textSizer/minusGrey.gif";
		img2 = new Image();
    img2.src = "images/textSizer/plusGrey.gif";
	}
	
	// Declare the content areas
	var mainContent = document.getElementById("mainColumn");
	var wrapperContent = document.getElementById("wrapper");
	var headerContent = document.getElementById("header");
	// Declare the incremation values
	var mc_increment = 0.3;
	var wc_increment = 0.1;
	var hc_increment = -0.03; // negative to slightly counteract increasing the font size in the wrappper.
	
	
	// Get (or set) the font size
	var mc_currentSize = parseFloat(mainContent.style.fontSize);
	var wc_currentSize = parseFloat(wrapperContent.style.fontSize);
	var hc_currentSize = parseFloat(headerContent.style.fontSize);

	if (!mc_currentSize) {
		mc_currentSize = 1.1; // same as the size declare in fonts.css
	}
	if (!wc_currentSize) {
		wc_currentSize = 1;
	}
	if (!hc_currentSize) {
		hc_currentSize = 1;
	}


	//Handle text sizing
	// If the "Decrease Text Size" icon is clicked
	if (obj.id == "smaller")
	{
		// If we haven't reached the lower size limit of -2
		if (size > -2) {
			// Decrease the size of text
			mainContent.style.fontSize = (mc_currentSize - mc_increment) + "em";
			wrapperContent.style.fontSize = (wc_currentSize - wc_increment) + "em";
			headerContent.style.fontSize = (hc_currentSize - hc_increment) + "em";
			// Decriment the size tracker
			size -= 1;
			// Make sure the "Increase Text Size" icon link is active
			obj.nextSibling.setAttribute('href', '#');
			// and make sure the "Increase Text Size" icon is blue
			obj.nextSibling.firstChild.src = "images/textSizer/plus.gif";
			//alert(size);
			// Set the size in a cookie
			createCookie('bcfontsize',size,'1');
			// If we have reaced the lower size limit of -2
			if (size == -2 ) {
				// Disable the "Decrease Text Size" icon link
				obj.removeAttribute('href');
				// Set the "Decrease Text Size" icon to grey
				obj.firstChild.src = "images/textSizer/minusGrey.gif";
			}
		}
	}
	// If the "Increase Text Size" icon is clicked
	else if (obj.id == "bigger")
	{
		// If we haven't reached the upper size limit of 2
		if (size < 2) {
			// Increase the size of the text
			mainContent.style.fontSize = (mc_currentSize + mc_increment) + "em";
			wrapperContent.style.fontSize = (wc_currentSize + wc_increment) + "em";
			headerContent.style.fontSize = (hc_currentSize + hc_increment) + "em";
			// Incriment the size tracker
			size += 1;
			// Make sure the "Decrease Text Size" icon link is active
			obj.previousSibling.setAttribute('href', '#');
			// and make sure the "Decrease Text Size" icon is blue
			obj.previousSibling.firstChild.src = "images/textSizer/minus.gif";
			//alert(size);
			// Set the size in a cookie
			createCookie('bcfontsize',size,'1');
			// If we have reached the upper size limit if 2
			if (size == 2) {
				// Disable the "Increase Text Size" icon link
				obj.removeAttribute('href');
				// Set the "Increase Text Size" icon to grey
				obj.firstChild.src = "images/textSizer/plusGrey.gif";
			}
		}
	}
	return true;
}