
function agSpotlightImageLoaded()
{
  var $galleryInst = this;

  function showSpotlight( e, fullWidth, fullHeight )
  {
    var $curImg = $galleryInst.current_image;

    var $origWidth = $curImg.outerWidth();
    var $origHeight = $curImg.outerHeight();
    var $offset = $curImg.offset();
    $offset.top -= $(window).scrollTop();
    $offset.left -= $(window).scrollLeft();

    var $origCss = {
      left: $offset.left,
      top: $offset.top,
      width: $origWidth,
      height: $origHeight,
      opacity: 0.0,
      visibility: 'visible',
      display: 'block',
      position: 'fixed',
    };

    var $bigWidth = fullWidth;
    var $bigHeight = fullHeight;
    var $bigImgCss = {
      opacity: 1,
      left: ( $(window).width() - $bigWidth ) / 2,
      top: ( $(window).height() - $bigHeight ) / 2,
      width: $bigWidth,
      height: $bigHeight,
    };

    var $clone = $galleryInst.current_image.find( 'img' ).clone();
    $clone.stop();
    $clone.css( $origCss );

    var $container = jQuery( '<div/>' ).appendTo( document.body );
    $container.css( {
      zIndex: 200,
      display: 'block',
      visibility: 'visible',
      position: 'fixed',
      width: '100%',
      height: '100%',
      top: 0,
      letf: 0,
      cursor: 'pointer'
    } );
    $container.css( 'background-image', 'url(/static/fadebg.png)' );
    $container.appendTo( document.body );
    $clone.appendTo( $container );

    $clone.animate(
      $bigImgCss,
      250,
      function() {}
    );

    function closeBigImg( e )
    {
      var $origWidth = $curImg.outerWidth();
      var $origHeight = $curImg.outerHeight();
      var $offset = $curImg.offset();
      $offset.top -= $(window).scrollTop();
      $offset.left -= $(window).scrollLeft();

      var $normalImgCss = {
        opacity: 0.0,
        top: $offset.top,
        left: $offset.left,
        width: $origWidth,
        height: $origHeight,
      };

      $clone.animate(
        $normalImgCss,
        250,
        function() {
          $container.remove();
        }
      );
    }

    $clone.click( closeBigImg );
    $container.click( closeBigImg );
  }

  function foo( e )
  {
    var $i = new Image();
    $i.onload = function() {

      var maxSizeMultiplier = 0.9;
      var maxWidth = maxSizeMultiplier * $(window).width();
      var maxHeight = maxSizeMultiplier * $(window).height();

      var width = this.width;
      var height = this.height;

      if( width > maxWidth )
      {
        height = height * ( maxWidth / width );
        width = maxWidth;
      }
      if( height > maxHeight )
      {
        width = width * ( maxHeight / height );
        height = maxHeight;
      }

      showSpotlight( e, width, height );
    }
    $i.src = $galleryInst.current_image.find( 'img' ).attr( 'src' );
  }

  this.current_image.click( foo );
}



