var prefsLoaded = false;
var defaultFontSize = 100;
var currentFontSize = defaultFontSize;
var msgSent = false;

function revertStyles(){
	currentFontSize = defaultFontSize;
	changeFontSize(0);
}

function changeFontSize(sizeDifference){
	currentFontSize = parseInt(currentFontSize) + parseInt(sizeDifference * 10);

	if(currentFontSize > 160){
		currentFontSize = 160;
	}else if(currentFontSize < 80){
		currentFontSize = 80;
	}

	setFontSize(currentFontSize);
	createCookie("fontSize", currentFontSize, 365);
};

function setFontSize(fontSize){
	var c = (document.getElementById) ? document.getElementById('contentmain') : document.all('contentmain');
	c.style.fontSize = fontSize + '%';
};


function setUserOptions(){
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);

	if(!prefsLoaded){

		cookie = readCookie("fontSize");
		currentFontSize = cookie ? cookie : defaultFontSize;
		setFontSize(currentFontSize);
		
		prefsLoaded = true;
	}

}





function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  //if (screen.width >= 1280) { if (msgSent == false) { alert("[beta debug]:\n I've detected that you're using a large monitor ("+screen.width+"x"+screen.height+"), nice one! I'm gonna increase the width of the page for you now..."); msgSent = true; }return 'fluid'; }
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) { alert (a.getAttribute("title")); return a.getAttribute("title"); }
  }
  return null;
}

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) {
  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 = function(e) {
   // check user resolution, if 1280 or greater set fluid as default stylesheet
   //alert('window onload');
   //if (screen.width >= 1280 && !readCookie('style')) { setActiveStyleSheet('fluid'); }
   setUserOptions();
   externalLinks();
   //setFontSize(fs);
  //var cookie = readCookie("style");
  //var title = cookie ? cookie : getPreferredStyleSheet();
  //setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

/*
var nowOnload = window.onload; // Let's save the existing assignment, if any
window.onload = function () {
  // Here is your precious function
  // You can call as many functions as you want here;
  myOnloadFunction1();

  // Now we call old function which was assigned to onLoad, thus playing nice
  if(nowOnload != null && typeof(nowOnload) == 'function') {
      nowOnload();
  }
}

// Your precious function
function myOnloadFunction1() {
  alert('myOnloadFunction1');
}
*/

