(function() {
  /*
  Photo Galleria
  http://samuylova.ru/
  Copyright 2011, Dmitriy Kubyshkin (http://kubyshkin.ru)
  */  var Galleria;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  Galleria = (function() {
    function Galleria() {
      this.strip = $('#galleria');
      this.wrapper = $('#galleria-wrapper');
      this.thumbs = this.strip.find('a');
      this.photos = [];
      this.photoContainer = $('#galleria-photo-wrapper');
      this.photo = $('<img alt="" />').appendTo(this.photoContainer);
      this.overlay = $('#galleria-photo-overlay');
      this.overlay.click(__bind(function() {
        var next;
        next = this.thumbs.eq(this.next());
        return next.click();
      }, this));
      this.speed = 300;
      this.faded = 0.5;
      this.current = (document.location + '').split('#');
      if (this.current.length < 2) {
        this.current = 0;
      } else {
        this.current = parseInt(this.current[1]) - 1;
      }
      this.loadImages();
    }
    Galleria.prototype.loadImages = function(initial) {
      var body;
      body = $('body');
      return this.thumbs.each(__bind(function(i, link) {
        var photo, thumb;
        link = $(link);
        link.click(__bind(function() {
          return this.changeImage(i);
        }, this));
        thumb = link.children('img');
        photo = $(document.createElement('img'));
        photo.attr('src', this.src(thumb));
        photo.css({
          position: 'absolute',
          top: '-2000px'
        });
        photo.bind('load', __bind(function() {
          photo.addClass('loaded');
          return thumb.fadeTo(this.speed, this.faded);
        }, this));
        this.photos.push(photo);
        photo.appendTo(body);
        if (i !== this.current) {
          return thumb.fadeTo(1, this.faded);
        } else {
          return this.changeImage(i);
        }
      }, this));
    };
    Galleria.prototype.changeImage = function(toNumber) {
      var current, leftOffset, to;
      to = this.thumbs.eq(toNumber);
      if (this.current !== toNumber) {
        current = this.thumbs.eq(this.current);
        current.find('img').stop().fadeTo(this.speed, this.faded);
      }
      this.overlay.stop().fadeTo(this.speed, 1, __bind(function() {
        this.current = toNumber;
        if (this.photos[toNumber].hasClass('loaded')) {
          this.overlay.css('background-position', '-100% -100%');
          to.find('img').stop().fadeTo(this.speed, 1);
          this.photo.attr('src', this.photos[toNumber].attr('src'));
          this.overlay.fadeTo(this.speed, 0);
          return this.overlay.attr('href', this.thumbs.eq(this.next()).attr('href'));
        } else {
          this.overlay.css('background-position', 'center center');
          this.photos[this.current].unbind('load.ci');
          return this.photos[toNumber].bind('load.ci', __bind(function() {
            return this.changeImage(toNumber);
          }, this));
        }
      }, this));
      leftOffset = to.offset().left - this.strip.offset().left;
      if ((leftOffset < this.wrapper.scrollLeft()) || (leftOffset > this.wrapper.scrollLeft() + this.wrapper.width())) {
        return this.wrapper.scrollTo(to, 300);
      }
    };
    Galleria.prototype.src = function(thumb) {
      return '/uploads' + thumb.attr('src').split('=')[1];
    };
    Galleria.prototype.next = function() {
      var next;
      next = this.current + 1;
      if (next === this.thumbs.length) {
        return 0;
      } else {
        return next;
      }
    };
    Galleria.prototype.prev = function() {
      var prev;
      prev = this.current - 1;
      if (prev === 0) {
        return this.thumbs.length - 1;
      } else {
        return prev;
      }
    };
    return Galleria;
  })();
  $(function() {
    return new Galleria;
  });
}).call(this);

