// fs.js - JavaScript unit to set font size in Web pages
// Copyright J. A. Wrotniak, 2008-2009; free for anyone to use or modify
// 1.00 08/03/12 Original
// 1.01 08/03/13 Added font face setting
// 1.02 08/03/14 Added cookie removal
// 1.03 08/03/19 Added larger fonts for w>1280
// 1.04 09/02/25 Part used in fs.html now separared as fs-fs.js
// 1.05 09/03/01 Added _in_set
// 1.06 09/12/13 _ss_faces and _s_faces now contain multiple options
//----------------------------------------------------------------------
// For details how this works, see the source of
// http://www.wrotniak.net/fs.html
// and also the header part of the source of any other page there.
//----------------------------------------------------------------------

// Function fs_reset() will be executed automatically upon loading of
// any page which refers to this file (fs.js).

window.onload = fs_reset;
window.onresize = fs_reset;

//----------------------------------------------------------------------

var _ss_faces = "Candara, Verdana, sans-serif";
var _s_faces  = "Constantia, Georgia, serif";
var _in_set = 0;

//----------------------------------------------------------------------
// These are called from any page within the whole site:
//----------------------------------------------------------------------

// Helper function; modified after Peter-Paul Koch; original at
// http://www.quirksmode.org/js/cookies.html

function read_cookie(nam) {
   var s = nam + "=";
   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(s) == 0) return c.substring(s.length,c.length);
      }
   return null;
   }

//----------------------------------------------------------------------

// Sets the base font size for the page. All others will be properly
// rescaled if expressed as % in HTML or CSS, not absolute!

function fs_set_size(val) {
   if (val!=null) {
      document.body.style.fontSize = val;
      }
   }
   
//----------------------------------------------------------------------

// Sets the font face to serif or sans ('serif' or 'sans') or to a given name.
// Called from fs_reset() or from fs_face().

function fs_set_face(val) {
   if (val=='sans') {
      document.body.style.fontFamily = _ss_faces;
      }
   else if (val=='serif') {
      document.body.style.fontFamily = _s_faces;
      }
   else if (val!=null){
      document.body.style.fontFamily = val;
      }
   }

//----------------------------------------------------------------------

// Called on loading or refreshing of any page at wrotniak.net.
// This is done by placing fs.js reference in the HTML header.
// Sets font size/face from cookies, if found; if not, then by screen size.

function fs_reset() {
   var fs = read_cookie("fs");
   var ff = read_cookie("ff");
   var w = document.body.clientWidth;
   if (ff!=null||fs!=null) {
      // found cookies, do nothing
      } 
   else if (w<360) {
      fs = "12pt";
      ff = 'sans';
      }
   else if (w<560) {
      fs = "7pt";
      ff = 'sans';
      }
   else if (w<600) {
      fs = "8pt";
      ff = 'sans';
      }
   else if (w<680) {
      fs = "9pt";
      ff = 'sans';
      }
   else if (w<760) {
      fs = "10pt";
      ff = 'sans';
      }
   else if (w<800) {
      fs = "11pt";
      ff = 'sans';
      }
   else if (w<960) {
      fs = "12pt";
      ff = 'sans';
      }
   else if (w<1024) {
      fs = "13pt";
      ff = 'serif';
      }
   else if (w<1280) {
      fs = "14pt";
      ff = 'serif';
      }
   else {
      fs = "15pt";
      ff = 'serif';
      }
   fs_set_face(ff);
   fs_set_size(fs);
   if (_in_set) { // only when called from fs-set.js
      fs_show();
      }
   }

//--- END --------------------------------------------------------------

