/**
 * @name Lazyload
 * @description Lazyloading for assets, etc
 * @author Jeffrey Nellissen
 */

var jQuery = jQuery || {};
var bm = bm || {};

bm.lazyload = (function ($) {

  var Lazyload = function (element, options) {
    this.$parent = $(element);
    this.$window = $(window);
    this.options = $.extend({}, Lazyload.DEFAULTS, options);

    this.init();
  };

  /**
   *
   * @type {{$window: (*|HTMLElement), threshold: number, itemSelector: string}}
   */
  Lazyload.DEFAULTS = {
    threshold: 0,
    itemSelector: '.js-lazyload'
  };

  /**
   *
   * @returns {Lazyload}
   */
  Lazyload.prototype.init = function () {
    this.$items = this.$parent.find(this.options.itemSelector);
    this.$window.on('reset.unveil', $.proxy(this.reset, this));
    this.$window.on('scroll.unveil resize.unveil lookup.unveil', $.proxy(this.unveil, this));
    $.proxy(this.unveil, this);
    return this;
  };

  Lazyload.prototype.reset = function(){
      this.$items = this.$parent.find(this.options.itemSelector);
      $.proxy(this.unveil, this);
  }

  /**
   * @function _unveil
   * @returns void
   * @private
   */
  Lazyload.prototype.unveil = function () {

    if (this.$items.length <= 0) {
      return;
    }
    var $window = this.$window;
    var threshold = this.options.threshold;

    function isInView() {
      var $item = $(this);
      if (!$item.hasClass('js-slideIn') && $item.is(':hidden')) {
        return;
      }
      var windowTop = $window.scrollTop(),
        windowBottom = windowTop + $window.height(),
        itemTop = $item.offset().top,
        itemBottom = itemTop + $item.height();

        return itemTop <= windowBottom + threshold;
        //return itemBottom >= windowTop - threshold && itemTop <= windowBottom + threshold;
    }

    var inview = this.$items.filter(isInView);
    var loaded = inview.addClass('js-lazyloaded').trigger('unveil');
    this.$items = this.$items.not(loaded);
  };


  Lazyload.prototype.addItems = function(items){
    var self = this;
    $.each(items, function(){
      self.$items.push(this);
    });
    this.unveil();
  };
  /**
   * @function getItems
   * @returns {*|Lazyload.$items}
   */
  Lazyload.prototype.items = function () {
    return this.$items;
  };

  Lazyload.prototype.updateItemlist = function(){
    //console.log('updateITemList', this.$parent,this.options.itemSelector, this.$parent.find(this.options.itemSelector));
    this.$items = this.$parent.find(this.options.itemSelector);
  };



  // ContentSwitch PLUGIN DEFINITION
  // ==========================

  function Plugin(option, args) {
    return this.each(function () {
      var $this = $(this);
      var data = $this.data('bm.lazyload');
      var options = $.extend({}, Lazyload.DEFAULTS, $this.data(), typeof option === 'object' && option);

      if (!data) {
        $this.data('bm.lazyload', (data = new Lazyload(this, options)));
      }
      if (typeof option === 'string') {
        data[option](args);
      }
    });
  }

  var old = $.fn.lazyload;

  $.fn.lazyload = Plugin;
  $.fn.lazyload.Constructor = Lazyload;


  // ContentSwitch NO CONFLICT
  // ====================

  $.fn.lazyload.noConflict = function () {
    $.fn.lazyload = old;
    return this;
  };


}(jQuery));


