// A global variable to keep track of the font size (1,2,3)
var size = 2;

function initFontSize() {
	var cookie = readCookie('govfontsizer');

	var smallerLink = document.getElementById("smaller");
	var resetLink = document.getElementById("reset");	
	var biggerLink = document.getElementById("bigger");
	
	var mainContentElements = getContentElementsByClass(); 

	if (cookie == 1) {	
		size = 1;
		for (i=0;i<mainContentElements.length;i++){
			mainContentElements[i].style.fontSize = "0.8em";
		}
		// Disable the "Decrease Text Size" icon link
		//smallerLink.removeAttribute('href');
		// Set the "Decrease Text Size" icon to grey	
		smallerLink.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_small_active.jpg";
		smallerLink.nextSibling.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_default.jpg";
		smallerLink.nextSibling.nextSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_large.jpg";		
	}	
	if (cookie == 2) {	
		size = 2;
		for (i=0;i<mainContentElements.length;i++){
			mainContentElements[i].style.fontSize = "1.0em";
		}
		// Disable the "Reset Text Size" icon link
		//resetLink.removeAttribute('href');
		// Set the "Decrease Text Size" icon to grey
		resetLink.previousSibling.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_small.jpg";
		resetLink.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_default_active.jpg";
		resetLink.nextSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_large.jpg";	
		
	}	
	if (cookie == 3) {	
		size = 3;
		for (i=0;i<mainContentElements.length;i++){
			mainContentElements[i].style.fontSize = "1.2em";
		}
		// Disable the "Decrease Text Size" icon link
		//biggerLink.removeAttribute('href');
		// Set the "Decrease Text Size" icon to grey
		biggerLink.previousSibling.previousSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_small.jpg";
		biggerLink.previousSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_default.jpg";		
		biggerLink.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_large_active.jpg";
		
	}

	// only call the equalHeight function if there is an element with class .equal on the page
	if($('.equal').length > 0) {
		equalHeight();
	}
}

function textSize(obj)
{	
	// Preload the greyed out images
	if (document.images) {
    img1 = new Image();
    img1.src = "http://www2.gov.bc.ca/common/images/img_text_small_active.jpg";
	img2 = new Image();
    img2.src = "http://www2.gov.bc.ca/common/images/img_text_default_active.jpg";	
	img3 = new Image();
    img3.src = "http://www2.gov.bc.ca/common/images/img_text_large_active.jpg";
	}
	
	// Declare the content areas
	
	var mainContentElements = getContentElementsByClass();


	// Declare the incremation values

	var cc_increment = 0.2;

    var cc_currentSize = new Array();

	// Get (or set) the font size	
	
	for (i=0;i<mainContentElements.length;i++){
		if(mainContentElements[i].style.fontSize == ""){
			mainContentElements[i].style.fontSize="1.0em";
		}
		cc_currentSize[i] = parseFloat(mainContentElements[i].style.fontSize); 
	}	


	if (!cc_currentSize) {
		cc_currentSize[0] = 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 -1
		//if (size = -1) {
			// Decrease the size of text			
			
			for (i=0;i<mainContentElements.length;i++){
					mainContentElements[i].style.fontSize = 0.8 + "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.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_small_active.jpg";
			obj.nextSibling.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_default.jpg";
			obj.nextSibling.nextSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_large.jpg";
			//alert(size);
			// Set the size in a cookie
			createCookie('govfontsizer',size,'1');
			// If we have reaced the lower size limit of -1
			//if (size = -1 ) {
				// Disable the "Decrease Text Size" icon link
				//obj.removeAttribute('href');
				// Set the "Decrease Text Size" icon to grey
				obj.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_small_active.jpg";
			//}
		//}
	}
	// If the "Reset Text Size" icon is clicked
	else if (obj.id == "reset")
	{
		// If we haven't reached the upper size limit of 2
		//if (size = 0) {
			// Reset the size of the text			

			for (i=0;i<mainContentElements.length;i++){
				mainContentElements[i].style.fontSize = 1.0 + "em"; 
			}			
			// Incriment the size tracker
			size = 2;
			// 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 = "http://www2.gov.bc.ca/common/images/img_text_small.jpg";
			obj.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_default_active.jpg";
			obj.nextSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_large.jpg";			
			//alert(size);
			// Set the size in a cookie
			createCookie('govfontsizer',size,'1');
			// If we have reached the upper size limit of 1
			//if (size = 0) {
				// Disable the "Increase Text Size" icon link
				//obj.removeAttribute('href');
				// Set the "Increase Text Size" icon to grey
				obj.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_default_active.jpg";
			//}
		//}
	}	
	// If the "Increase Text Size" icon is clicked
	else if (obj.id == "bigger")
	{
		// If we haven't reached the upper size limit of 1
		//if (size = 1) {
			// Increase the size of the text
		

			for (i=0;i<mainContentElements.length;i++){
				mainContentElements[i].style.fontSize = 1.2 + "em"; 
			}			
			// Incriment the size tracker
			size = 3;
			// 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.previousSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_small.jpg";
			obj.previousSibling.lastChild.src = "http://www2.gov.bc.ca/common/images/img_text_default.jpg";
			obj.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_large_active.jpg";
			//alert(size);
			// Set the size in a cookie
			createCookie('govfontsizer',size,'1');
			// If we have reached the upper size limit of 1
			//if (size = 1) {
				// Disable the "Increase Text Size" icon link
				//obj.removeAttribute('href');
				// Set the "Increase Text Size" icon to grey
				obj.firstChild.src = "http://www2.gov.bc.ca/common/images/img_text_large_active.jpg";
			//}
		//}
	}

	// only call the equalHeight function if there is an element with class .equal on the page
	if($('.equal').length > 0) {
		equalHeight();
	}
	return true;

}

function getContentElementsByClass() {

    var objArray = new Array();
	var contentClass = "content";
	var rightcolClass = "equal";
	var rightcolsearchClass = "right_column_search";
	var i; 
	var elements = document.getElementsByTagName('div');
	for (i=0;i<elements.length;i++){ 
		curId = elements[i].className;
		if(curId == contentClass || curId == rightcolClass || curId == rightcolsearchClass){
		    objArray.push( elements[i] );
                    
        }
	} 

   return objArray;

}

function getElementByClassName(objClassName) {

    var thisObj;
	var i; 
	var elements = document.getElementsByTagName('div');
	for (i=0;i<elements.length;i++){ 
		curId = elements[i].className;
		if( curId == objClassName ){
        		thisObj=elements[i];
	          	break;
        	}
	} 

   return thisObj;

}

function getElementsByClassName(objClassName,fieldClassName) {

    var objs=new Array();
	var i; 
	var elements = document.getElementsByTagName('div');
	for (i=0;i<elements.length;i++){ 
		curId = elements[i].className;
		if( curId == objClassName || curId == fieldClassName){
	             objs.push(elements[i]);
             
        	}
	} 

   return objs; 

}
