// <![CDATA[
var DataHead = '<div class="spacer">&nbsp;</div>';
var DataTail = '<div class="spacer">&nbsp;</div>';
var Footer = $_('footer');
var Lang = 'en';
var ImgPath = 'Images/QBL/';
var InfoPath = 'Apps/getQblInfo';
var PrevFootnote;
var SiteMap;
var WindowWidth;
var WindowHeight;
addEvent(window, 'load', doInitialSetup);
function doInitialSetup() {
  var pg1 = setStartPage('greeting','p');
  $_('info-container').innerHTML = 'Please wait ... loading page.';
  setWindowSizes();
  addEvent(window, 'resize', setWindowSizes);
  setQueryURL('query-url');
  setInfoBlock('info-container');
  setInfoTitle('info-title');
  setNoteBlock('note-container');
  setNoteTitle('note-title');
  setNoteBox('explanation');
  setDataCallback(formatDataBlock);
  setTitleCallback(formatTitleBlock);
  setInitCallback(showBusyWindow);
  setChangeCallback(qblOnChange);
  setArrowBwd('back-arrow');
  setArrowFwd('fwd-arrow');
  qInfo(pg1);
  var foot = $_('footer');  // Set up footer to fill the space below
  if (foot) {               // the tree image rollover area.
    setFooterSize();
    addEvent(foot, 'mouseover', moveFooter);
    addEvent(foot, 'mouseout',  moveFooter);
  }
}
function moveFooter() {
  var z = Footer.style.zIndex == 3 ? 1 : 3;
  Footer.style.zIndex = z;
}
function setFooterSize() {
  var ti = $_('tree-image');
  if (ti) {
    var tih = ti.offsetHeight + ti.offsetTop + 2;
    var ht  = WindowHeight - tih;
    if (ht >= 120) {
      Footer.style.top = tih + 'px';
      ht -= 60;
      var x = $_('query-url');
      x.style.height = ht + 'px';
      x.style.maxHeight = ht + 'px';
      x = $_('query-links');
      x.style.position = 'absolute';
      x.style.bottom = '2px';
    }
  }
}
function setWindowSizes() {
  var o = getWindowSize();
  WindowWidth = o.width;
  WindowHeight = o.height;
  if (o.width && o.height) {
    var ww = o.width - (250 + 20 + 40);
    var www = ww - 50;
    doSetWindowSizes('tree-detail','info-container',ww,www);
    doSetWindowSizes('explanation','note-container',ww,www);
    setFooterSize();
  }
}
function doSetWindowSizes(boxName,textName,ww,www) {
  var d = $_(boxName);
  if (d) {
    d.style.width  = ww + 'px';
    var x = ww - 40;
    var c = $_(boxName + '-top-c');
    if (c) c.style.width = x + 'px';
    c = $_(boxName + '-btm-c');
    if (c) c.style.width = x + 'px';
    c = $_(textName);
    if (c) {
      c.style.width = www + 'px';
    }
  }
}
function cursOn() {
  document.body.style.cursor = 'pointer';
  var ln = arguments.length;
  if (ln < 2) return;
  if (ln == 2) {
    var img = ImgPath + arguments[1];
    var o;
    if (o = $_(arguments[0]))
      o.src = img;
  }
  else {
    var img = ImgPath + arguments[ln - 1] + '-';
    while (--ln > 0) {
      if (o = $_(arguments[ln - 1]))
        o.src = img + ln;
    }
  }
}
function cursOff() {
  document.body.style.cursor = 'default';
  var img = ImgPath + 'blank';
  var o;
  for (var ln = 0; ln < arguments.length; ln++) {
    if (o = $_(arguments[ln]))
      o.src = img;
  }
}
function formatDataBlock(d) {
  return DataHead + d + DataTail;
}
function formatTitleBlock(d,ix,max) {
  document.title = 'Thelemistas QBL | ' + d;
  var ttl = $_('qbl-pg-title');
  if (ttl) ttl.innerHTML = '<span>' + d + '</span>';
  var t = '<span>';
  if (ix > 0)
    t = t + '<a onclick="qBwd()" title="Backward"><img src="Images/Icons/bwd" height="20" width="20" alt="-"/></a>';
  t = t + d;
  if (ix < max)
    t = t + '<a onclick="qFwd()" title="Forward"><img src="Images/Icons/fwd" height="20" width="20" alt="+"/></a>';
  return t + '</span>';
  
}
function showFootnote(name) {
  var box = $_('fntext');
  if (box) {
    if ((box.style.visibility == 'visible') && (name == PrevFootnote))
      box.style.visibility = 'hidden';
    else {
      var f = $_(name);
      if (f) {
        var note = $_('fncontent');
        if (note) {
          note.innerHTML = f.innerHTML;
          box.style.visibility = 'visible';
          PrevFootnote = name;
        }
      }
    }
  }
  return false;
}
function showBusyWindow() {
  var w = $_('busy-window');
  if (w) w.style.visibility = 'visible';
  return true;
}
function hideBusyWindow() { return hideWindow('busy-window'); }
function hideFootnote()   { return hideWindow('fntext'); }
function hideWindow(name) {
  var w = $_(name);
  if (w) w.style.visibility = 'hidden';
  return false;
}
function qblOnChange() {
  hideBusyWindow();
  hideFootnote();
  return;
}
function showSiteMap() {
  if (SiteMap)
    $_('site-map').style.visibility = 'visible';
  else
    doFetch(':map:', loadSiteMap, 0);
}
function goToPage(page,fn) {
  hideWindow('site-map');
  fn(page);
}
function loadSiteMap() {
  if ((xo = DataFetchObj)
   && (xo.readyState == 4)) {
    SiteMap = xo.responseText.split('\n');
    abortFetch();
    hideBusyWindow();
    if (SiteMap) {
      $_('site-list').innerHTML = formatSiteMap();
      $_('site-map').style.visibility = 'visible';
    }
  }
  return false;
}
function formatSiteMap() {
  var s = '';
  var end = Math.floor((SiteMap.length + 2) / 3);
  for (var ix = 0; ix < end; ix++) {
    s = s + formatThis(SiteMap[ix])
          + formatThis(SiteMap[ix + end])
          + formatThis(SiteMap[ix + end + end]);
  }
  return s;
}
function formatThis(a) {
  if (!a) return '';
  a = a.split('=');
  var fn = (a[0].match(/Tarot/)) ? 'qNote' : 'qInfo';
  return '<a class="map-block" onclick="goToPage(' + "'" + a[0] + "',"
       + fn + ')" title="' + a[0] + '">' + a[1] + '</a>';
}
// ]]>

