﻿// JScript File

var fontSize = {
  // sizeUnit and defaultSize for body.style.fontSize
  sizeUnit: "px",
  defaultSize: 12,
  // numbers (same unit as sizeUnit)
  maxSize:     36,
  minSize:     6,

  init: function() {
    if ( !document.body || !document.getElementById ) return;
    var size = window.location.search? window.location.search.slice(1): getCookie("fontSize");
    size = !isNaN( parseFloat(size) )? parseFloat(size): this.defaultSize;
    // in case default unit changed or size passed in url out of range
    if ( size > this.maxSize || size < this.minSize ) size = this.defaultSize;
    var sizerEl = document.getElementById('sizer');
    if (sizerEl) sizerEl.style.display = "block";
    document.body.style.fontSize = size + this.sizeUnit;
  },
  
  adjust: function(inc) {
    var size = parseFloat( document.body.style.fontSize );
    size += inc;
    // Test against max and min sizes 
    if (inc > 0) size = Math.min(size, this.maxSize);
    else size = Math.max(size, this.minSize);
    setCookie( "fontSize", size, 180, "/" );
    document.body.style.fontSize = size + this.sizeUnit;
  },

  reset: function() {
    document.body.style.fontSize = this.defaultSize + this.sizeUnit;
    deleteCookie("fontSize", "/");
  }
}

function setCookie(name,value,days,path,domain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-99 00:00:01 GMT";
  }
}

fontSize.init();

