Vertis.Values = {
  albumWidth: 137,
  albumsWidth: 138,
  albumsHeight: 180,
  albumThreshold: 170
}

Vertis.Loader = function (obj, eventname, parentt, wrapperr) {

  var wrapper = $(wrapperr),  
      scrollParent = $(parentt);

  var that = this;
  that.delay = false;
  that.blankImage = "/images/blank.gif";
  that.threshold = Vertis.Values.albumThreshold;   // tells about the number of pixels after scroll event is taken into account
  that.albumWidth = Vertis.Values.albumWidth;  // width of album calculated (more or less corrected) 
  that.obj = $(obj);
  that.images = that.obj.find('img[src*="' + that.blankImage  + '"]');
  that.imagesNr = that.images.length;
  that.src = {};
  that.lastScroll = 0;
  that.additionalRows = 3; // how many rows should be rendered after each scroll
  that.nrOfPhotosInRow = that.additionalRows * Math.ceil(wrapper.width() / that.albumWidth); // calculate number of photos in one row

  that.fullsize = {
    height: wrapper.height(),
    width: wrapper.width()
  };
  that.viewport = {
    height: that.obj.height(),
    width:  that.obj.width()
  };

/* can be used when photos are not rendered out of the box in modules/albums/templates/viewSuccess
  for (var i = 0; i < that.nrOfPhotosInRow; i++) {
    var img = that.images.eq(i);
    that.src[i] = img.attr('src');
    img.attr('src', img.attr('src2'));
  }
*/
 
 // console.log("#album height: " + that.fullsize.height + " width: " + that.fullsize.width );

  scrollParent.bind(eventname, function (e) {
    that.eventHandler(e);
  });
   /* interesting but not used
   if (that.delay) {
     return; 
   }
   that.delay = true;
   setTimeout(function () {
     that.eventHandler(e);
     that.delay = false;
   }, 200);
  
      });
  that.checkVisible();
  */
};

Vertis.Loader.prototype = {
  eventHandler: function (e) {
    var currScroll = $('#body').scrollTop();
    var diffScroll = currScroll - this.lastScroll;
    var images2 = this.obj.find('img[src*="' + this.blankImage + '"]');
    //console.log("do wczytania: " + images2.length);
    //console.log("Scroll: " + $('#body').scrollTop() +  ' height: ' +  this.fullsize.height + " ile " + this.imagesNr + " diff " + diffScroll);

    if(diffScroll > this.threshold) {
        // in case user wanted to drag the scroller very fast in his browser
        var howManyTimes = parseInt(diffScroll / this.threshold);
        var additionalRows = this.additionalRows * howManyTimes;
        var nrOfPhotosInRow = additionalRows * Math.ceil(this.fullsize.width / 136);

        for (var i = 0; i < nrOfPhotosInRow; i++) {
          var img = images2.eq(i);
          img.attr('src', img.attr('src2'));
        }
        this.lastScroll = currScroll;
        // this.checkVisible();
    }
  }


  /* interesting but not used
  checkVisible: function () {
    var showed = [];
    for (var i = 0; i < this.imagesNr; i++) {
      var img = this.images.eq(i);
      if (!this.isVisible(img)) {
        if (showed.length > 0) {
          break;
        } else {
          continue;
        }
      }

      var id = img.attr('id').match(/cache_(\d+)/)[1];
      img.attr('src', this.src[id]);
      delete this.src[id];
      showed.push('#cache_' + id);
    }

    this.imagesNr -= showed.length;
    showed = showed.join(', ');
    this.images = this.images.not(showed);
    $(showed).removeAttr('id');
  },

  isVisible: function (elem) {
    elem = elem.parent();
    var pos  = elem.position(),
        size = {
          height: elem.height(),
          width: elem.width()
        },
        scroll = {
          top: this.obj.scrollTop()
        };
    if ((pos.top > -200 && pos.top < this.viewport.height + 200) ||
       ((pos.top + size.height) > -200 && (pos.top + size.height) < this.viewport.height + 200)) {
      return true;
    }

    return false;
  }
  */ 
};
