/* xToolbar Demo
   Cross-Browser.com
*/

var alphaVisible = true;
var betaVisible = true;

// Register an onload listener which checks browser support then initializes the TB.
xAddEventListener(window, 'load',
  function() {
    var e = xGetElementById('navBeta'); // any ID known to exist in the document
    if (e && e.style && document.createElement && e.appendChild && e.setAttribute) {
      window.xtb = new xToolbar('xToolbar', 'xButton', 'xButtonHover', 'navBeta',
        [['^', btnIni, topClk], ['&lt;', btnIni, leftClk], ['&gt;', btnIni, rightClk]]
      );
    }
  }, false
);

// Button Listeners

function btnIni(ele, idx)
{
  var s = '';
  switch (idx) {
    case 0:
      s = 'Top of Page';
      break;
    case 1:
      s = 'Toggle Left Column';
      break;
    case 2:
      s = 'Toggle Right Column';
      break;
  }
  ele.title = s;
}
function topClk()
{
  if (window.scrollTo) window.scrollTo(0,0);
  else if (window.scroll) window.scroll(0,0);
  else location.href = '#topofpg';
  return false;
}
function leftClk()
{
  var d, m, e, a, i;
  if (alphaVisible) {
    d = 'none';
    m = '0px';
  }
  else {
    d = 'block';
    m = '170px';
  }
  e = xGetElementById('navAlpha');
  e.style.display = d;
  a = xGetElementsByClassName('content');
  for (i = 0; i < a.length; ++i) {
    a[i].style.marginLeft = m;
  }
  alphaVisible = !alphaVisible;
}
function rightClk()
{
  var d, m, e, a, i;
  if (betaVisible) {
    d = 'none';
    m = '0px';
  }
  else {
    d = 'block';
    m = '210px';
  }
  e = xGetElementById('navBeta');
  e.style.display = d;
  a = xGetElementsByClassName('content');
  for (i = 0; i < a.length; ++i) {
    a[i].style.marginRight = m;
  }
  betaVisible = !betaVisible;
}

/* xToolbar Object Prototype

  clsTb - the class applied to the toolbar (button container).
  clsBtn - the default class applied to all buttons.
  clsHvr - the class applied to a button when hovered.
  idRef - the toolbar will be aligned with the top of this element.
  oBtns - an array of arrays:
    oBtns = [
      [character, function(ele, idx), onclick],
      ...
    ];
*/

function xToolbar(clsTb, clsBtn, clsHvr, idRef, oBtns)
{
  var tb, btns = [], inst = ++xToolbar.instances;
  // Guess if the browser supports fixed positioning ;-).
  var fixed = typeof window.XMLHttpRequest != 'undefined';
  // Create toolbar element.
  tb = newDiv(document.body, inst, clsTb);
  if (fixed) {
    tb.style.position = 'fixed';
  }
  tb.style.visibility = 'visible';
  // Create button elements.
  for (var i = 0; i < oBtns.length; ++i) {
    btns[i] = newDiv(tb, inst + '' + i, clsBtn, oBtns[i][0], oBtns[i][2], onOver, onOut);
    oBtns[i][1](btns[i], i);
  }
  // Set initial position.
  onResize();
  // Register window listeners.
  xAddEventListener(window, 'unload', onUnload, false);
  xAddEventListener(window, 'resize', onResize, false);
  if (!fixed) {
    xAddEventListener(window, 'scroll', onScroll, false);
  }

  // Button event listeners.
  function onOver()
  {
    this.className = clsHvr;
  }
  function onOut()
  {
    this.className = clsBtn;
  }
  // Resize/scroll listeners.
  function onScroll()
  {
    xSlideTo(tb, xPageX(tb), xScrollTop() + tb.xtbFloatOffset, 800);
  }
  function onResize()
  {
    var y = xPageY(idRef); // the top of the TB will be aligned with this ID
    xMoveTo(tb, xClientWidth() - xWidth(tb) - 2, y);
    if (!fixed) {
      tb.xtbFloatOffset = y;
      onScroll();
    }
  }
  // Window unload listener.
  function onUnload()
  {
    tb = null;
    for (var i = 0; i < btns.length; ++i) {
      btns[i].onclick = null;
      btns[i].onmouseover = null;
      btns[i].onmouseout = null;
      btns[i] = null;
    }
  }
  // Create a new element and initialize it's properties.
  function newDiv(p, n, cls, h, clk, movr, mout)
  {
    var e = document.createElement('div');
    if (e && p) {
      e.id = cls + n;
      e.className = cls;
      if (h) e.innerHTML = h;
      p.appendChild(e);
      if (clk) e.onclick = clk;
      if (movr) e.onmouseover = movr;
      if (mout) e.onmouseout = mout;
    }
    return e;
  }
}

xToolbar.instances = 0; // static property

