
  /*
   * ЗАВИСИМОСТИ: jQuery, SameDistance (samedistance.js)
   * 
   * containerSelector
   * floatingBlockSelector
   * itemsSelector
   * leftTrigger
   * rightTrigger
   * currentItem // -1 - поставить по середке
   * 
   */

  GalleryBox = function (options) {
    var self = this;
    this.options = options;
    this.itemsCount = $(options.itemsSelector).size ();
    SameDistance (options.containerSelector, options.itemsSelector, true, function (itemsPerRow, space, outerWidth, isFirstTime) {
      self.itemsSpace = space;
      self.itemsOuterWidth = outerWidth;
      self.visibleItems = itemsPerRow;
      if (itemsPerRow >= self.itemsCount) {
        self.setCurrentItem (0, false);
      }
      if (isFirstTime && itemsPerRow < self.itemsCount) {
        if (self.options.currentItem == -1)
          self.setCurrentItem (Math.round (self.itemsCount / 2) - Math.round (itemsPerRow / 2), false);
        else
          self.setCurrentItem (self.options.currentItem, false);
      } else {
        self.setCurrentItem (self.currentItem, true);
      }
      self.setTriggersVisibility ();
    });
    $(options.leftTrigger).click (function () {
      self.slideLeft ();
      return false;
    });
    $(options.rightTrigger).click (function () {
      self.slideRight ();
      return false;
    });
  }
    
  GalleryBox.prototype.slideLeft = function () {
    if (this.currentItem != 0) {
      var flb = $(this.options.floatingBlockSelector);
      flb.animate ({
        marginLeft: '+=' + (this.itemsOuterWidth + this.itemsSpace) + 'px'
      }, 200);
      this.currentItem--;
    }
    this.setTriggersVisibility ();
  }

  GalleryBox.prototype.slideRight = function () {
    if ((this.currentItem + this.visibleItems) < this.itemsCount) {
      var flb = $(this.options.floatingBlockSelector);
      flb.animate ({
        marginLeft: '-=' + (this.itemsOuterWidth + this.itemsSpace) + 'px'
      }, 200);
      this.currentItem++;
    }
    this.setTriggersVisibility ();
  }
  
  GalleryBox.prototype.setCurrentItem = function (index, animate) {
    this.currentItem = index;
    var marginLeft;
    if (index == 0)
      marginLeft = 0;
    else {
      var spaces = index * this.itemsSpace;
      var widths = index * this.itemsOuterWidth;
      marginLeft = -(spaces + widths);
    }
    var flb = $(this.options.floatingBlockSelector);
    if (animate) {
      flb.animate ({
        marginLeft: marginLeft + 'px'
      }, 200);
    } else {
      flb.css ('margin-left', marginLeft + 'px');
    }
  }
  
  GalleryBox.prototype.setLeftTriggerVisibility = function (visibility) {
    $(this.options.leftTrigger).css ('visibility', visibility ? 'visible' : 'hidden');
  }

  GalleryBox.prototype.setRightTriggerVisibility = function (visibility) {
    $(this.options.rightTrigger).css ('visibility', visibility ? 'visible' : 'hidden');
  }
  
  GalleryBox.prototype.setTriggersVisibility = function () {
    //document.title = this.currentItem + '/' + this.visibleItems + '/' + this.itemsCount;
    if (this.visibleItems >= this.itemsCount) {
      this.setLeftTriggerVisibility (false);
      this.setRightTriggerVisibility (false);
    } else {
      this.setLeftTriggerVisibility (this.currentItem != 0);
      this.setRightTriggerVisibility ((this.currentItem + this.visibleItems) < this.itemsCount);
    }
    if (this.itemsCount == 0) {
      this.setLeftTriggerVisibility (false);
      this.setRightTriggerVisibility (false);
    }
  }

