var banheiromesmo = {};

var Gallery = function (wrapperSelector, dataContainer, autoplay) {  

  this.wrapper = false;
  this.slider = false;
  this.children = false;
  this.numChildren = false;
  this.currMarginLeft = 0;
  this.currIndex = 0;
  this.increment = 0;
  this.dataContainer = false;
  this.buttonLeft = false;
  this.buttonRight = false;
  this.intervalId = null;
  this.elementMargin = 6;
  
  this.init = function (wrapperSelector, dataContainer) {
    var that = this;
    
    this.wrapper = $(wrapperSelector + ":first")
    this.slider = this.wrapper.find('ul.slider:first');
    this.children = this.slider.children('li');
    this.numChildren = this.children.length;
  
    this.increment = this.wrapper.width();
    
    this.children.css('width', this.increment);
    this.slider.css('width', parseInt(this.numChildren * this.wrapper.outerWidth(true), 10));
  
    this.bindDirectionEvents();
    this.dataContainer = dataContainer;
  
    if (this.dataContainer !== false) {
      $("#" + this.dataContainer).html($(this.children[0]).find('img').attr('data-' + this.dataContainer));
    }
  
    this.buttonLeft = this.wrapper.parent().find('.button-left');
    this.buttonRight = this.wrapper.parent().find('.button-right');
    
    if (this.numChildren > 1) {
      this.buttonRight.css('visibility', 'visible');
    }

    if (autoplay === true) {
      this.startSlide();
      
      var that = this;
      this.children.find("img").click(function () {
        that.stopSlide();
      });
    }
    
    if (wrapperSelector === 'div.pictograms') {
      this.increment = 498;

      this.activateProduct = function (index) {
        $(this.slider).addClass('with-active');
        $(this.slider.find('div')[index]).addClass('active');
      }
    }
    return this;
  };

  this.goLeft = function () {
    if (this.currIndex <= 0) { return; }

    if (this.currIndex <= 1) {
      this.buttonLeft.addClass('inactive');
    } else {
      this.buttonLeft.removeClass('inactive');      
    }
    this.buttonRight.removeClass('inactive');      
    
    --this.currIndex;
    this.currMarginLeft = this.currMarginLeft + this.increment;
    this.slider.stop().animate({'margin-left': this.currMarginLeft}, 1000);

    if (this.dataContainer !== false) {
      $("#" + this.dataContainer).html($(this.children[this.currIndex]).find('img').attr('data-' + this.dataContainer));
    }
  };
  this.goRight = function () {
    if (this.currIndex >= (this.numChildren - 1)) { return; }
    
    if (this.currIndex >= (this.numChildren - 2)) {
      this.buttonRight.addClass('inactive');
    } else {
      this.buttonRight.removeClass('inactive');      
    }
    this.buttonLeft.removeClass('inactive');

    ++this.currIndex;
    this.currMarginLeft = this.currMarginLeft - this.increment;
    this.slider.stop().animate({'margin-left': this.currMarginLeft}, 1000);

    if (this.dataContainer !== false) {
      $("#" + this.dataContainer).html($(this.children[this.currIndex]).find('img').attr('data-' + this.dataContainer));
    }
  };
  this.bindDirectionEvents = function () {
    var that = this;
    
    this.wrapper.parent().find(".button-right").click(function () {
      that.stopSlide();
      that.goRight();
    });
    
    this.wrapper.parent().find(".button-left").click(function () {
      that.stopSlide();
      that.goLeft();
    });

    $(document).keydown(function (e) {
      if (e.which === 37) { that.stopSlide(); that.goLeft(); }
      if (e.which === 39) { that.stopSlide(); that.goRight(); }
    });
  };
  this.startSlide = function(){
    var that = this;

    this.intervalId = setInterval(function(){
      if (that.currIndex >= (that.numChildren - 1)) { 
        that.goToFirst();
        return; 
      }

      that.goRight();
    }, 3000);
  };
  this.stopSlide = function () {
    if(this.intervalId) { clearInterval(this.intervalId); }
  };
  this.goToFirst = function() {
    this.currIndex = 0;
    this.currMarginLeft = 0;
    this.slider.stop().animate({'margin-left': 0}, 1000);
    this.buttonRight.removeClass('inactive');
    this.buttonLeft.addClass('inactive');
  };

  this.init(wrapperSelector, dataContainer);

};
