//Changed by Ron Biggs. Modifications use jquery notation.
var prefsLoaded = false;
var defaultFontSize = 16;
var currentFontSize = defaultFontSize;
var defaultContainerWidth = 850;
var currentContainerWidth = defaultContainerWidth;
var ContainerBlock = "content_txt";

function revertStyles(){
        currentFontSize = defaultFontSize;
        changeFontSize(0);
}
function changeFontSize(sizeDifference){
	currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference);
	var oWhichButton, vText, str, vTitle
	var vSwitch = false;
	if(currentFontSize > 21){
	        currentFontSize = 21;
			oWhichButton = "#button1";
			vText = "Font size cannot go any larger";
			vSwitch = true;
	}else if(currentFontSize < 8){
	        currentFontSize = 8;
			oWhichButton = "#button2";
			vText = "Font size cannot go any smaller";
			vSwitch = true;
	}else if(currentFontSize == defaultFontSize){
	        currentFontSize = defaultFontSize;
			oWhichButton = "#button3";
			vText = "Font size is currently set at the detault size.";
			vSwitch = true;
	}else{
		$("#buttons div").removeClass("dimmed");
		$("#buttons div>a>img").each(function(){
			vTitle = $(this).attr("alt");
			$(this).attr("title",vTitle);
		});
		vSwitch = false;
	}
	if(vSwitch){
		$("#buttons div").removeClass("dimmed");
		$(oWhichButton).addClass("dimmed").find("img").attr("title",vText);
	} 
	setFontSize(currentFontSize);
};

function setFontSize(fontSize){
	if(document.getElementById(ContainerBlock) == null) return false;
	document.getElementById(ContainerBlock).style.fontSize = fontSize + "px";
};

function changeContainerWidth(newWidth) {
        var obj1 = document.getElementById('container');
        var obj2 = document.getElementById('rbox')
        currentContainerWidth = parseInt(newWidth);
        if (currentContainerWidth == 0)
        {obj1.style.width = '95.7%';
        obj2.style.width = '60.59%';}
        else {obj1.style.width = currentContainerWidth + 'px';
              obj2.style.width = (currentContainerWidth-380) + 'px';
        }
};

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 expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
};

function readCookie(name) {//Name = the variable name within the cookie.
  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;
};

window.onload = setUserOptions;

function setUserOptions(){
    if(!prefsLoaded){
        userFontSize = readCookie("fontSize");
        currentFontSize = userFontSize ? userFontSize : defaultFontSize;
        setFontSize(currentFontSize);
        prefsLoaded = true;
    	changeFontSize(0); // added to set the "state" of the buttons.
    }
}

window.onunload = saveUserOptions;

function saveUserOptions()
{
   createCookie("fontSize", currentFontSize, 30);
   createCookie("containerWidth", currentContainerWidth, 30);
}
