'use strict';
var bidiFun = new function() {
  var logotr, logortl;
  //, houseltr, housertl
  return {
    add: function() {
      var btn = document.createElement('button'),
      btnTxt = document.createTextNode('Fun!');
      btn.setAttribute('id', 'bidi-fun-button');
      btn.setAttribute('style', 'position: fixed; left: 0; bottom: 0;');
      btn.appendChild(btnTxt);
      eventListener.add(btn, 'click', bidiFun.rtl, false);
      document.documentElement.lastChild.appendChild(btn);
      bidiFun.logoltr = document.getElementsByTagName('h1')[0].firstChild;
      bidiFun.logortl = bidiFun.logoltr.cloneNode(false);
      bidiFun.logortl.src = '/images/logo-rtl.png';
      //bidiFun.houseltr = document.getElementById('chibi-house');
      //bidiFun.housertl = bidiFun.houseltr.cloneNode(false);
      //bidiFun.housertl.src = 'images/chibi_house-rtl.png';
    },
    
    rtl: function() {
      document.documentElement.setAttribute('dir', 'rtl');
      var btn = document.getElementById('bidi-fun-button'),
      btnTxt = document.createTextNode('Less Fun!');
      btn.replaceChild(btnTxt, btn.firstChild);
      eventListener.replaceF(btn, 'click', bidiFun.rtl, bidiFun.ltr, false);
      bidiFun.logoltr.parentNode.replaceChild(bidiFun.logortl, bidiFun.logoltr);
      //bidiFun.houseltr.parentNode.replaceChild(bidiFun.housertl, bidiFun.houseltr);
    },
  
    ltr: function() {
      document.documentElement.setAttribute('dir', 'ltr');
      var btn = document.getElementById('bidi-fun-button'),
      btnTxt = document.createTextNode('Fun!');
      btn.replaceChild(btnTxt, btn.firstChild);
      eventListener.replaceF(btn, 'click', bidiFun.ltr, bidiFun.rtl, false);
      bidiFun.logortl.parentNode.replaceChild(bidiFun.logoltr, bidiFun.logortl);
      //bidiFun.housertl.parentNode.replaceChild(bidiFun.houseltr, bidiFun.housertl);
    }
  };
}

eventListener.add(window, 'load', bidiFun.add, false);

var konami = new function() {
  // var store = '';
  var curPos = 0;
  var code = new Array('38', '38', '40', '40', '37', '39', '37', '39', '66', '65');
  return {
    // check: function(aKey) {
      // store += aKey;
      // if (!store.match(/^38(38(40(40(37(39(37(39(66(65)?)?)?)?)?)?)?)?)?$/)) {
        // store = '';
      // }
      // else if (store.length==20) {
        // store = '';
        // alert('Konami!!');
      // }
    // }
    check: function(aKey) {
      if (code[curPos] == aKey) {
        if (++curPos == 10) {
          curPos = 0;
          alert('Konami!');
        }
      }
      else {
        curPos = 0;
      }
    }
  };
}

eventListener.add(document, 'keyup', function(e) { konami.check(e.keyCode); }, false);
