// JavaScript Document


dojo.require("dijit._base.place");


//  archivePage (2009-11-23)
//  this object computing and adding paging of archive and e-shop page


function archivePage() {
  var self = this;
  this.imgWidth = 156;
  this.maxWidth = (this.imgWidth*2+12*2);
  this.tableElem = null;
  //
  this.reset = function() {
    this.colPosition = 0;
    this.colShow = 0;
    this.colMax = 0;
  };
  //
  this.init = function() {
    self.tableElem = dojo.byId('archivetable');
    if (!self.tableElem) return false;
    self.tableElem.className = self.tableElem.className.replace('archive-nojs','archive-js');
    self.reset();
    self.transform();
    dojo.connect(window,"onresize",function() {
      self.reset();
      self.transform();
    });
  };
  //
  this.makeArrows = function(parentElem) {
    var linkR = document.createElement('a');
    var linkL = document.createElement('a');
    linkR.href = '#';
    linkL.href = '#';
    linkR.className = 'global-arrow-right';
    linkL.className = 'global-arrow-left';
    parentElem.appendChild(linkR);
    parentElem.appendChild(linkL);
    dojo.connect(linkR,'onclick',self.changePosition);
    dojo.connect(linkL,'onclick',self.changePosition);
    if (self.colPosition>=(self.colMax-self.colShow)) {
      dojo.addClass(linkR,'invisible');
    } else {
      dojo.removeClass(linkR,'invisible');
    }
    if (self.colPosition<=0) {
      dojo.addClass(linkL,'invisible');
    } else {
      dojo.removeClass(linkL,'invisible');
    }
  };
  //
  this.transform = function() {
    var viewport = dijit.getViewport();
    self.colShow = Math.floor((viewport.w-(2*13))/(self.imgWidth+(2*12)));
    var isHeads = 0;
    if (dojo.query('td.head',self.tableElem).length>0) {
      isHeads = 1;
    }
    var rows = self.tableElem.getElementsByTagName('tr');
    dojo.forEach(rows,function(node0,index0){
      var cols = node0.getElementsByTagName('td');
      if (cols[0].className=='archive-arrows') {
        cols[0].innerHTML = '';
        self.makeArrows(cols[0]);
      } else {
        self.colMax = cols.length;
        dojo.forEach(cols,function(node,index){
          if (node.className=='head') {
          } else {
            if (index==(self.colShow-1+self.colPosition)) {
              node.className = 'rightpart';
            } else {
              node.className = '';
            }
            if (index<(self.colPosition+isHeads) || index>=(self.colShow+self.colPosition)) {
              node.style.display = 'none';
            } else {
              node.style.display = '';
            }
          }
        });
      }
    });
  };
  //
  this.changePosition = function(evt,thisElem) {
    if (!thisElem) thisElem = this;
    if (thisElem.className.indexOf('left')>0) {
      self.colPosition--;
    }
    if (thisElem.className.indexOf('right')>0) {
      self.colPosition++;
    }
    self.transform();
    if (evt) dojo.stopEvent(evt);
  };
  //
  return self.init();
}


// end of document

