// Web page typography helper v1.1 (c) 2011,2012 Silas S. Brown.
// License: GPL

// Purpose: Adds typographical characters to your Web pages ONLY IF the
// browser supports them
// (so they'll still work in basic/mobile browsers with ASCII only)

// Usage: At the END of the page (before </body>) do:
// <script language="javascript" src="typography.js"></script>

if(document.getElementsByTagName) {
  var b=document.getElementsByTagName("BODY")[0],
      d=document.createElement("DIV"),s=document.createElement("SPAN");
  d.appendChild(s);
  s.innerHTML = "\u2014";
  b.appendChild(d); var emWidth = s.offsetWidth; b.removeChild(d);
  s.innerHTML = "\u2013";
  b.appendChild(d); var enWidth = s.offsetWidth; b.removeChild(d);
  s.innerHTML = "\ufb01";
  b.appendChild(d); var fiWidth = s.offsetWidth; b.removeChild(d);
  s.innerHTML = "\ufb03";
  b.appendChild(d); var ffiWidth = s.offsetWidth; b.removeChild(d);
  var supports_dashes = emWidth > enWidth,
      supports_ligatures = ffiWidth > fiWidth;
  if (supports_dashes || supports_ligatures) {
  function treewalk(n) {
   var c=n.firstChild;
   while(c) {
    switch (c.nodeType) {
    case 1: 
        if (c.nodeName) {
        var cc = c.nodeName.toLowerCase();
        if (cc!="script" && cc!="code" && cc!="pre" && cc!="tt" && cc!="kbd" && cc!="textarea" && cc!="style" && cc!="samp" && cc!="var") treewalk(c);
        }
        break;
    case 3:
        if (supports_dashes) c.nodeValue=c.nodeValue.replace(/'neath/g,"\u2019neath").replace(/---/g,"\u2014").replace(/[ \n]'/g," \u2018").replace(/``/g,"\u201C").replace(/`/g,"\u2018").replace(/^''/,"\u201D").replace(/^'/,"\u2018").replace(/''/g,"\u201D").replace(/'/g,"\u2019").replace(/[ \n]"/g," \u201C").replace(/^"/,"\u201C").replace(/"/g,"\u201D");
      // (ought to be able to say \s instead of [ \n], but it doesn't seem to work on all browsers)
      if (supports_ligatures) c.nodeValue=c.nodeValue.replace(/fi/g,"\ufb01").replace(/fl/g,"\ufb02");
      // .replace(/ff/g,"\ufb00"); - doesn't always work so well (might be a different font)
      // also took out .replace(/ffl/g,"\ufb04") before fl, and .replace(/ffi/g,"\ufb03") before fi
      // (TODO how do we check fi and fl use the same font?  should do most of the time)
    }
    c=c.nextSibling;
   }
  }
  treewalk(document); // (NB this doesn't reach the text that's been hidden by hide0)
  }
}

// Another thing: Some sighties have fonts so small that mobile-friendly
// large-print-friendly sites end up with far too much blank space and the
// users complain that the site is too basic.  Try to detect that situation.
var h1=window.innerHeight,h2=0,h3=document.body.clientHeight;
if(document.documentElement) h2=document.documentElement.clientHeight;
if(!h1){h1=h2;if(!h1)h1=h3;} if(!h2 || h2==h1) h2=h3;
// Now h1 is window height, h2 is hopefully document height
// (however chrome/Webkit increases h2 to match h1 for small docs. Firefox 4 is OK)
// if we increase font size, h2 should increase by at most its square (as it'll take more lines AND the lines are higher). Sometimes will increase by less than that though (TODO could re-check after a delay and change the written style element)
if(h1 && h2 && h2<h1) { var s=(Math.floor(Math.sqrt(h1/h2)*10)/10); if(s>1) document.write("<style>body{font-size:"+s+"em;}<"+"/style>"); }

