var otherBlogsBrowser = function(id){
  this.id = id;
};

otherBlogsBrowser.prototype = {
 init: function() {
    thisInstance = this;
    this.count = 0;
    this.total = 5;
    this.browser = document.getElementById(this.id);
    this.leftBtn = document.getElementById('moth_reverse');
    this.rightBtn = document.getElementById('moth_forward');
    this.btns = [this.leftBtn, this.rightBtn];
    this.leftBtn.onclick = thisInstance.back;
    this.rightBtn.onclick = thisInstance.forward;
    this.items = new Array;
    this.divs = this.browser.getElementsByTagName("div");
    for(var i = 0; i < this.divs.length; i++) {
       if (/mothbox/i.test(this.divs[i].className)) {
          this.items.push(this.divs[i]);
          if (i>this.total+1) this.hide(this.divs[i]);
       }
    }
 },
 back: function() {
    var targets = thisInstance.computeTarget('b');
    thisInstance.count -= (thisInstance.count > 0) ? 1 : 0 ;
    if (thisInstance.count == thisInstance.items.length - 6) thisInstance.activateBtn(thisInstance.btns)
    if (thisInstance.count == 0 ) {thisInstance.deactivateBtn(this);}
    if (targets && targets[0] && targets[1]) {
       thisInstance.show(targets[0]);
       thisInstance.hide(targets[1])
    }
 },
 forward: function(){
    var targets = thisInstance.computeTarget('f');
    thisInstance.count += (thisInstance.count < thisInstance.items.length - thisInstance.total )? 1 : 0 ;
    if (thisInstance.count == 1) { thisInstance.activateBtn(thisInstance.btns)}
    if (thisInstance.count == thisInstance.items.length - thisInstance.total) {thisInstance.deactivateBtn(this);}
    if (targets && targets[0] && targets[1]) {
       thisInstance.hide(targets[0]);
       thisInstance.show(targets[1])
    }
 },
 hide: function(target) {
    target.style.display = 'none';
 },
 show: function(target) {
    target.style.display = 'inline';
 },
 computeTarget: function(direction){
    var targets = new Array;
    if (direction=='f') {
       var targetIndex = this.count;
       var targetIndex2 = this.count + this.total;
       //console.log(targetIndex)
       //console.log(targetIndex2)
    }
    else{
       var targetIndex = this.count-1;
       var targetIndex2 = this.count + this.total-1;
       //console.log(targetIndex)
       //console.log(targetIndex2)
    }
    if (targetIndex < (thisInstance.items.length - thisInstance.total)) {
       targets = [this.items[targetIndex],this.items[targetIndex2]];
       return targets;
    }
 },
 activateBtn: function(buttons){
    //console.log( buttons.className) 
     if (buttons.length){
         for (i = 0; i < buttons.length; i++) {
             buttons[i].className = "";
         }
     }else buttons.className = "";
     //console.log( buttons.className) 
 },
 deactivateBtn: function(button){
     button.className = "inactive";
     //console.log( button.className) 
 }
}