(function(){
  B = {
    defaults: {
      arrow: "\u2191",
      container: "content",
      destination: "container",
      tags: "h3",
      linkClass: "backToTop",
      linkTxt: "back to top"
    },
    
    $: function(obj) {
      return document.getElementById(obj);
    },
    
    getOptions: function() {
      for(key in B.defaults) {
        B[key] = B.defaults[key];
        if(B.options && B.options[key]) B[key] = B.options[key];
      }
    },
    
    linkP: function() {
      t = B.linkTxt + " " + B.arrow;
      tn = document.createTextNode(t);
      a = document.createElement("a");
      a.setAttribute("href", "#" + B.destination);
      a.appendChild(tn);
      p = document.createElement("p");
      p.setAttribute("class", B.linkClass);
      p.appendChild(a);
      return p;
    },
    
    initB: function() {
      B.getOptions();
      var myContainer = B.$(B.container);
      if(myContainer) {
        var myTags = myContainer.getElementsByTagName(B.tags);
        for(var x=1, myTag; myTag = myTags[x]; x++) {
          myContainer.insertBefore(B.linkP(),myTag);
        }
        if(myTags.length) myContainer.appendChild(B.linkP());
      }
    },
    
    init: function () {
      if (window.addEventListener) window.addEventListener("load", this.initB, false);
      else if (window.attachEvent) window.attachEvent("onload", this.initB);
    }
  }
  
  window['BackToTop'] = B;
})();

BackToTop.init();
