// Generated by CoffeeScript 1.9.1 (function() {

'use strict';
var get_scroll, zoom;

jQuery.fn.picture_zoomer = function(speed) {
  if (speed == null) {
    speed = 400;
  }
  return $(this).on('click', function(e) {
    e.preventDefault();
    if ($(this).is('img')) {
      return zoom($(this), speed);
    } else {
      return zoom($(this).find('img'), speed);
    }
  });
};

get_scroll = function() {
  return document.documentElement.scrollTop || document.body.scrollTop;
};

zoom = function(img, speed) {
  var large, offset;
  large = $(new Image());
  large.attr('src', img.attr('data-large') || img.attr('src'));
  offset = img.offset();
  return large.on('load', function() {
    var close, height, w_height, w_width, width;
    width = large.prop('width');
    height = large.prop('height');
    w_width = $(window).width() - 25;
    w_height = $(window).height() - 25;
    if (width > w_width) {
      height = height / (width / w_width);
      width = w_width;
    }
    if (height > w_height) {
      width = width / (height / w_height);
      height = w_height;
    }
    large.css({
      width: img.width(),
      height: img.height(),
      left: offset.left,
      top: offset.top,
      position: 'absolute',
      'box-shadow': '0 0 8px rgba(0, 0, 0, .3)',
      'z-index': 5000
    });
    $(document.body).append(large);
    large.animate({
      width: width,
      height: height,
      left: ($(window).width() - width) / 2,
      top: ($(window).height() + $('#menu').height() - height) / 2 + get_scroll()
    }, {
      duration: speed
    });
    close = function() {
      return large.animate({
        width: img.width(),
        height: img.height(),
        left: offset.left,
        top: offset.top
      }, {
        duration: speed,
        complete: function() {
          return large.remove();
        }
      });
    };
    $(document.body).one('click', close);
    return $(document).on('keydown.photo_viewer', function(e) {
      if (e.keyCode !== 27) {
        return;
      }
      close();
      return $(document).off('keydown.photo_viewer');
    });
  });
};

}).call(this);