var stsz = 0;
var szs = new Array( '1em','1.1em','1.2em','1.3em');

function setFont(inc) {
 document.body.style.fontSize = szs[inc];
 if (parent.frames.azlist ? true : false) parent.azlist.document.body.style.fontSize=szs[inc];
 createCookie("fontsize", inc, 365);
}

function setDef() {
 stsz = 0; setFont(stsz);
}

function setFs(inc) {
 var sz = stsz; sz += inc; if ( sz < 0 ) sz = 0; if ( sz > 3 ) sz = 3; stsz = sz; setFont(stsz);
}

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;
}

function loadFS() {
  var fs = readCookie("fontsize");
  stsz = fs ? fs : 0;
  stsz = parseInt(stsz);
  setFont(stsz);
}

function popUp(win,child,w,h,sb) {
 var t = (screen.availHeight-h)/2;
 var l = (screen.availWidth-w)/2;
 var newWindow = window.open(win,child, "toolbar=no,location=no,status=no,menubar=no,top=" +t+ ",left=" +l+ ",width=" +w+ ",height=" +h+ ",scrollbars=" +sb+ ",resizable=no");
 if (parseInt(navigator.appVersion) >= 4) { newWindow.window.focus(); }
}

function toggleSummaries(el) {
 var all_items = document.getElementsByTagName('span');
 for (var i=0; i < all_items.length; i++) {
  if (all_items[i].className == 'summary') all_items[i].style.display = (all_items[i].style.display == 'none') ? 'block' : 'none';
 }
}

function copyText() {
 if ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) {
  var article = document.title + '\r' + document.getElementById('author').innerHTML + '\r\n';
  if (document.getElementById('synopsis')) article += document.getElementById('synopsis').innerHTML + '\n\n';
  article += document.getElementById('article').innerHTML + '\n\n';
  if (document.getElementById('iconography')) article += document.getElementById('iconography').innerHTML + '\n\n';
  if (document.getElementById('footnote')) article += document.getElementById('footnote').innerHTML + '\n\n';
  article += document.getElementById('details').innerHTML + '\n\n';
  article += 'Copyright (c) Encyclopedia Mythica. All rights reserved.';
  article = article.replace("Article details:",""); article = article.replace(/(<([^>]+)>)/ig,"");
  document.getElementById('copyart').innerText = article;
  article = document.getElementById('copyart').createTextRange(); article.execCommand("Copy");
  alert("The article has been copied to the clipboard.");
 } else {
  alert("Sorry, " + navigator.appName + " does not support this function.");
 }
}

function setTarget(chk) {
 var lnk;
 if (chk)
  var ttarget = "_blank";
 else
  var ttarget = "_top";
 for (var i=0; i<=(document.links.length-1); i++) {
  lnk = document.links[i].id;
  if (lnk.substring(0,6) == 'extlnk') {
    document.links[i].target = ttarget;
  }
 }
}

function hkKeyword(n,w) {
 if (n.hasChildNodes) {
  var hi;
  for (hi=0;hi<n.childNodes.length;hi++) {
   hkKeyword(n.childNodes[hi],w);
  }
 }
 if (n.nodeType == 3) {
  tnVal = n.nodeValue;twVal = w;
  if (tnVal.indexOf(twVal) != -1) {
   pn = n.parentNode;
   if (pn.className != "hilite") {
    nv = n.nodeValue;ni = tnVal.indexOf(twVal);
    b = document.createTextNode(nv.substr(0,ni));
    dwVal = nv.substr(ni,w.length);
    a = document.createTextNode(nv.substr(ni+w.length));
    hiwordtext = document.createTextNode(dwVal);
    hw = document.createElement("span");hw.className = "hilite";
    hw.appendChild(hiwordtext);
    pn.insertBefore(b,n);pn.insertBefore(hw,n);pn.insertBefore(a,n);
    pn.removeChild(n);
   }
  }
 }
}

function hilite() {
 if (!document.createElement) return;
 var ref = document.URL;
 if (ref.indexOf('?') == -1) return;
 qs = ref.substr(ref.indexOf('?')+1);qsa = qs.split('&');
 for (i=0;i<qsa.length;i++) {
  qsip = qsa[i].split('=');
  if (qsip.length == 1) continue;
  if (qsip[0] == 'hl') {
   words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
   words = unescape(qsip[1].replace('#','')).split(/\s+/);
   for (w=0;w<words.length;w++) {
    hkKeyword(document.getElementById("article"),words[w]);
   }
  }
 }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
  window.onload = func;
  } else {
  window.onload = function() {
  oldonload();
  func();
  }
  }
}

addLoadEvent(loadFS);
addLoadEvent(hilite);
