// Adapted from cdc.gov

// The different text sizes we support.
// var sizes = new Array("60%", "70%", "80%", "90%", "100%", "110%", "120%", "130%", "140%");
var sizes = new Array( "70%", "80%", "90%", "100%", "110%", "120%", "126%");

var defaultSize = 2; // default to 100%
// Holds the index of the current size.
var currentSize = defaultSize;
// Special variable for Mozilla because of how it handles CSS rules.
var ruleCount;
// Domain of the session cookie.
var sessionCookieDomain = ".fda.gov";
// Set the div string.
var divStr = "#content";

// Here, get the current value of the cookie, if it exists, and style the page accordingly.
if (getCookie("textSize") != "") {
   currentSize = Number(getCookie("textSize"));
	if (document.styleSheets[0].cssRules) { // Mozilla
		setStyleMozilla(divStr);
	}
	else { // IE
		setStyleIE(divStr);
	}
}

// Parses through the current cookie, if it exists, and pulls out the relevant value.
function getCookie(name) {
   if (document.cookie.length > 0) {
	   var start = document.cookie.indexOf(name + "=");
		if (start != -1) {
		   start += (name.length + 1);
			var end = document.cookie.indexOf(";", start);
			if (end == -1) {
			   end = document.cookie.length;
			}
			return unescape(document.cookie.substring(start, end));
		}
	}
	return "";
}

// Make the text size either bigger or smaller.
function changeTextSize(offset) {
	currentSize = currentSize + offset;
	if (currentSize >= sizes.length) { // don't overflow the sizes array
	   currentSize = sizes.length - 1;
	}
	if (currentSize < 0) { // don't underflow the sizes array
		currentSize = 0;
   }
   //alert("current size "+currentSize);

	if (document.styleSheets[0].cssRules) { // Mozilla
		if (document.styleSheets[0].cssRules[0]) {
			setStyleMozilla(divStr);
		}
	}
	else if (document.styleSheets[0].rules) { // IE
		setStyleIE(divStr);
	}
	else { // do nothing for unrecognized browsers
	}
	// Set the cookie
	document.cookie = 'textSize='+ currentSize + '; path=/; domain=' + sessionCookieDomain;
}

function setStyleMozilla(divStr) {
   ruleCount = document.styleSheets[0].cssRules.length;
	document.styleSheets[0].insertRule(divStr + " { font-size: " + sizes[currentSize] + "; }", ruleCount);
}

function setStyleIE(divStr) {
	//alert("setting style "+sizes[currentSize]);
	document.styleSheets[0].addRule(divStr, "{ font-size: " + sizes[currentSize] + "; }");
}


// Function to fix the right/bottom margins on rounded corner spans post-load, for IE
// This is necessary because IE is dumb and sometimes renders an extra pixel on the right/bottom
function fixMargin(obj) {
   if (document.styleSheets[0].rules) { // only want to do this for IE
      var bottomMarginSize = ((obj.offsetHeight % 2) == 1) ? "-1px" : "0px";
      var rightMarginSize = ((obj.offsetWidth % 2) == 1) ? "-1px" : "0px";
      var spans = obj.getElementsByTagName('span');
      for (var i=0; i<spans.length; i++) {
         if (spans[i].className.match(/b[l|r]_/)) {
	    spans[i].style.marginBottom = bottomMarginSize;
         }
         if (spans[i].className.match(/[t|b]r_/)) {
            spans[i].style.marginRight = rightMarginSize;
         }
      }
   }
}