
  // Еще Одна Ебучая Галерея

  var JAFG = function (opts) {
    this.opts = opts;
    this.directionLtr = true;
    this.mo = false;
    var jafg = this;
    if ($(this.opts.items).size () == 0) {
      this.hideLeftTrigger ();
      this.hideRightTrigger ();
    } else {
      this.itemWidth = parseInt ($(this.opts.items).css ('width'));
      this.itemsCount = $(this.opts.items).size ();
      this.currentItem = 0;
      $(this.opts.leftTrigger).bind ('click', function () {
        jafg.back ();
        return false;
      });
      $(this.opts.rightTrigger).bind ('click', function () {
        jafg.forward ();
        return false;
      });
      $(this.opts.rightTrigger + ', ' + this.opts.leftTrigger + ', ' + this.opts.items).bind ('mouseover', function () {
        jafg.mo = true;
      });
      $(this.opts.rightTrigger + ', ' + this.opts.leftTrigger + ', ' + this.opts.items).bind ('mouseout', function () {
        jafg.mo = false;
      });
      this.updateTriggers ();
    }
  }
  
  JAFG.prototype.forward = function () {
    if (!this.isLastItem ()) {
      $(this.opts.floating).stop (false, true).animate ({
        marginLeft: (parseInt ($(this.opts.floating).css ('margin-left')) - this.itemWidth) + 'px'
      }, 400);
      this.currentItem++;
      this.updateTriggers ();
      this.onChange ();
    }
  }
  
  JAFG.prototype.back = function () {
    if (this.currentItem != 0) {
      $(this.opts.floating).stop (false, true).animate ({
        marginLeft: (parseInt ($(this.opts.floating).css ('margin-left')) + this.itemWidth) + 'px'
      }, 400);
      this.currentItem--;
      this.updateTriggers ();
      this.onChange ();
    }
  }
  
  JAFG.prototype.forwardOrBack = function () {
    if (!this.mo) {
      if (this.directionLtr) {
        if (this.isLastItem ()) {
          this.directionLtr = false;
          this.back ();
        } else {
          this.forward ();
        }
      } else {
        if (this.currentItem == 0) {
          this.directionLtr = true;
          this.forward ();
        } else {
          this.back ();
        }
      }
    }
  }
  
  JAFG.prototype.startForwardOrBack = function () {
    var self = this;
    setInterval (function () {
      self.forwardOrBack ();
    }, 2800);
  }
  
  JAFG.prototype.updateTriggers = function () {
    if (this.currentItem == 0) {
      this.hideLeftTrigger ();
    } else {
      this.showLeftTrigger ();
    }
    if (this.isLastItem ()) {
      this.hideRightTrigger ();
    } else {
      this.showRightTrigger ();
    }
  }
  
  JAFG.prototype.isLastItem = function (item) {
    if (typeof item == 'undefined')
      item = this.currentItem;
    return item == this.itemsCount - 1;
  }
  
  JAFG.prototype.hideLeftTrigger = function () {
    $(this.opts.leftTrigger).css ('visibility', 'hidden');
  }
  
  JAFG.prototype.showLeftTrigger = function () {
    $(this.opts.leftTrigger).css ('visibility', 'visible');
  }

  JAFG.prototype.hideRightTrigger = function () {
    $(this.opts.rightTrigger).css ('visibility', 'hidden');
  }
  
  JAFG.prototype.showRightTrigger = function () {
    $(this.opts.rightTrigger).css ('visibility', 'visible');
  }
  
  JAFG.prototype.onChange = function () {};

