/*
 * Simple JavaScript Fontsize changer,
 * providing sidewide fontsize setting by storing the current fontsize in a cookie.
 *
 * You can change the fontsize by triggering the following methods:
 * - FontSize.increase();
 * - FontSize.decrease();
 * - FontSize.reset();   
 */
if(typeof de == "undefined") var de = new Object(); 
if(typeof de.zone35 == "undefined") de.zone35 = new Object(); 
de.zone35.FontSize = function() {
	// adjust the following settings to suite your needs
	this.defaultFontSize 	= 100;
    this.maxFontSize 		= 120;
    this.minFontSize 		= 80;
    this.creaseFontSize		= 10;
	
	// do not change the values below
	this.prefsLoaded 		= false;
    this.currentFontSize 	= this.defaultFontSize;
}
de.zone35.FontSize.prototype = {
	reset: function() {
    	this.currentFontSize = this.defaultFontSize;
    	this.changeFontSize(0);
    },
	increase: function() {
    	this.changeFontSize(1);
    },
	decrease: function() {
    	this.changeFontSize(-1);
    },
    changeFontSize: function(sizeDifference) {
    	this.currentFontSize = parseInt(this.currentFontSize) + parseInt(sizeDifference * this.creaseFontSize);
    	if (this.currentFontSize > this.maxFontSize){
    		this.currentFontSize = this.maxFontSize;
    	} else if (this.currentFontSize < this.minFontSize) {
    		this.currentFontSize = this.minFontSize;
    	}
    	this.setFontSize(this.currentFontSize);
    	this.saveSettings();
    },
    setFontSize: function(fontSize) {
    	document.body.style.fontSize = fontSize + '%';
    },
    createCookie: function(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=/";
    },
    readCookie: function(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;
    },
    loadSettings: function() {
    	if(!this.prefsLoaded) {
    		cookie = this.readCookie("z35.fontSize");
    		this.currentFontSize = cookie ? cookie : this.defaultFontSize;
    		this.setFontSize(this.currentFontSize);
    		this.prefsLoaded = true;
    	}
    },
    saveSettings: function() {
      this.createCookie("z35.fontSize", this.currentFontSize, 365);
    }

}

var FontSize = new de.zone35.FontSize();

function loadFontSize() { FontSize.loadSettings(); }
function saveFontSize() { FontSize.saveSettings(); }
window.onload = loadFontSize;
window.onunload = saveFontSize;


