var xfade = {
  prepareXFade: function(container, navId, delay) {
    // Test DOM
    if (!api.testDOM() ||
        !document.getElementById(container)) {
      return;
    }

    // Set properties
    this.container = document.getElementById(container);
    this.navId = navId;
    this.delay = delay;
    this.items = api.getElementNodes(this.container.firstChild);
    this.nums = new Array();
    this.current = 0;
    this.timer = null;

    // Make numbered nav
    this.makeNav();
  },

  // Make numbered nav
  makeNav: function() {
    // Make nav container
    var div = this.container.parentNode.appendChild(document.createElement("div"));
    div.setAttribute("id", this.navId);

    // Add nav items
    var p = this;
    api.map(function(obj){p.addNavItems(); obj.xOpacity = 0;}, p.items);

    // Start fade cycle
    this.showCurrent(this.items[0]);
  },

  // Helper: Make nav items
  addNavItems: function() {
    var div = this.nums[this.nums.length] =
      document.getElementById(this.navId).appendChild(document.createElement("div"));
    div.setAttribute("id", this.navId+"-"+this.nums.length);

    var p = this;
    div.onclick = function() {
      clearTimeout(p.timer); // stop cross fade
      api.map(function(obj){ p.reset(obj); }, p.items); // reset opacities
      p.showCurrent(p.items[p.current = api.countPreviousSiblings(this)]); // show current element
    }
  },

  // Hilite nav item
  hiliteNav: function(obj, here) {
    obj.className = api.removeLastClassName(obj);
    if (here) { obj.className = "on"; }
  },

  // Show current fade item
  showCurrent: function(obj) {
    obj.xOpacity = .99;
    obj.style.display = "block";
    this.setOpacity(obj);

    this.hiliteNav(this.nums[this.current], true);

    var p = this;
    this.timer = setTimeout(function() { p.so_xfade() }, this.delay);
  },

  // Reset opacities
  reset: function(obj) {
    obj.xOpacity = 0;
    obj.style.display = "none";
    this.setOpacity(obj);

    this.hiliteNav(this.nums[api.countPreviousSiblings(obj)], false);
  },

  // Image Cross Fade Redux | Version 1.0
  // Last revision: 02.15.2006 | steve@slayeroffice.com
  so_xfade: function() {
    var p = xfade; // added - to set object

    var cOpacity = p.items[p.current].xOpacity;
    var nIndex = (p.items[p.current+1]) ? p.current+1 : 0;

    var nOpacity = p.items[nIndex].xOpacity;

    cOpacity -= .05;
    nOpacity += .05;

    p.items[nIndex].style.display = "block";
    p.items[p.current].xOpacity = cOpacity;
    p.items[nIndex].xOpacity = nOpacity;

    p.setOpacity(p.items[p.current]);
    p.setOpacity(p.items[nIndex]);

    if(cOpacity <= 0) {
      p.items[p.current].style.display = "none";
      p.current = nIndex;
      p.timer = setTimeout(function() { p.so_xfade() }, p.delay);
    } else {
      // Added for numbered nav
      if (p.nums[p.current].className == "on") {
        p.hiliteNav(p.nums[p.current], false);
        p.hiliteNav(p.nums[nIndex], true);
      }

      p.timer = setTimeout(function() { p.so_xfade() }, 50);
    }
  },
  setOpacity: function(obj) {
    if (obj.xOpacity > .99) {
      obj.xOpacity = .99;
      return;
    }
    obj.style.opacity = obj.xOpacity;
    obj.style.MozOpacity = obj.xOpacity;
    obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
  }
}

api.addEvent(window, "load", function() {
    xfade.prepareXFade("quote-messaging", "numbernav", 4000);
  });