var PRESO = function() {
  var $ = {};
  return {
    init : function(mainList) {

      var el = [];

      var t = document.getElementsByTagName('LI');
      for (var i = 0; i < t.length; i++) {
        el[el.length] = t[i];
      }
      
      for (var i = 0; i < el.length; i++) {
        if (el[i].className == 'rot13') {
          this.rotElText(el[i]);
          el[i].onmouseover = function() { 
            PRESO.rotElText(this);
            this.onmouseover = null;
          }
        }
      }
      
      $.mainFooter = 'http://kentbrewster.com/badges';
      $.nav = {};
      $.nav.forward = ['next'];
      $.nav.backward = ['back'];
      if (mainList) {
        $.md = document.getElementById(mainList);
        var theKids = $.md.getElementsByTagName('DD');
        for (var i = 0; i < theKids.length; i++) {
          if (theKids[i].parentNode.id == mainList) {
            var p = document.createElement('p');      
            if (i > 0) {
              var a = document.createElement('a');
              a.className = 'prev';
              var r = Math.floor(Math.random() * $.nav.backward.length);
              a.innerHTML = $.nav.backward[r];
              a.onclick = function() {
                var dd = this.parentNode.parentNode;
                dd.className = '';
                var dt = PRESO.getPreviousSibling(dd);
                dt.className = '';
                var activeDD = PRESO.getPreviousSibling(dt);
                activeDD.className = 'active';
                var activeDT = PRESO.getPreviousSibling(activeDD);
                activeDT.className = 'active';
              }
              p.appendChild(a);
            }
            if (i < theKids.length -1) {
              var a = document.createElement('a');
              a.className = 'next';
              var r = Math.floor(Math.random() * $.nav.forward.length);
              a.innerHTML = $.nav.forward[r];
              a.onclick = function() {
                var dd = this.parentNode.parentNode;
                dd.className = '';
                var dt = PRESO.getPreviousSibling(dd);
                dt.className = '';                
                var activeDT = PRESO.getNextSibling(dd);
                activeDT.className = 'active';                
                var activeDD = PRESO.getNextSibling(activeDT);
                activeDD.className = 'active';

              }
              p.appendChild(a);
            }
          
            p.className = 'nav';
            
            var a = document.createElement('A');
            a.innerHTML = a.href = $.mainFooter;
            a.target = '_blank';
            p.appendChild(a);
            theKids[i].appendChild(p);
          }
        }
      }        
    },
    getNextSibling : function(el) {
      var nextSib = el.nextSibling;
      if (nextSib && nextSib.nodeType != 1) {
        nextSib = nextSib.nextSibling;
      }
      return nextSib;
    },
    getPreviousSibling : function(el) {
      if (el) {
      var prevSib = el.previousSibling;
      if (prevSib && prevSib.nodeType != 1) {
        prevSib = prevSib.previousSibling;
      }
      return prevSib;
      }
    },
    rotElText : function(el) {
      for ( var i = 0; i < el.childNodes.length; i++) {
        if (el.childNodes[i].nodeType == 3) {
          el.childNodes[i].nodeValue = this.rot13(el.childNodes[i].nodeValue);
        } else if (el.childNodes[i].nodeType == 1) {
          this.rotElText(el.childNodes[i]);
        }
      }
    },    
    rot13 : function(textIn) {
      var rot13map = [];
      var s  = "abcdefghijklmnopqrstuvwxyz";
      for (var i=0; i<s.length; i++) { rot13map[s.charAt(i)] = s.charAt((i+13)%26); }
      for (var i=0; i<s.length; i++) { rot13map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase(); }
      var textOut = "";
      for (var i=0; i<textIn.length; i++) {
        var b = textIn.charAt(i);
	textOut += (b>='A' && b<='Z' || b>='a' && b<='z' ? rot13map[b] : b);
      }
      return textOut;
    }  
  };
}();

window.onload = function() {
  PRESO.init('main');
};


