/*
Filename: clickzoom.js
Desc:     Zoom image when clicked, requires jQuery
Author:   Relevant Arts Enterprise, Inc. <http://www.relevantarts.com/>
			    John A. Lock <jlock@relevantarts.com>
Created:  2011-Feb-11
Modified: 

Set image dimensions before calling. Example:

var thumb_width = 132;
var thumb_height = 100;
var full_width = 524;
var full_height = 396;

*/

var horz_offset = (full_width - thumb_width) / 2;
var vert_offset = (full_height - thumb_height) / 2;

// Handle zoom/restore on images when clicked
$(function() {
  $('div.click_zoom div').click(function() {
    if ($(this).find('img').width() == thumb_width) {
      zoom($(this));
    }
    else {
      restore($(this));
    }
  });
});

// Zoom image to full size
function zoom(imgobj) {
  imgobj.css('z-index', '10');
  var pos = imgobj.offset();
  var diff = pos.top - vert_offset;
  // Make sure the image does not bleed off the top of the browser window
  if (diff < 0) {
    var newTop = '-' + ((vert_offset + diff) - 5) + 'px';
  }
  else {
    var newTop = '-' + vert_offset + 'px';
  }
  diff = pos.left - horz_offset;
  // Make sure the image does not bleed off the left side of the browser window
  if (diff < 0) {
    var newLeft = '-' + ((horz_offset + diff) - 5) + 'px';
  }
  else {
    var newLeft = '-' + horz_offset + 'px';
  }
  // Make sure the image does not bleed off the right side of the browser window
  var winWidth = $('body').width();
  diff = winWidth - ((pos.left - horz_offset) + full_width);
  if (diff < 0) {
    newLeft = '-' + ((horz_offset - diff) + 10) + 'px';
  }
  // Zoom to the requested dimensions
  imgobj.find('img').stop().animate({
      marginTop: newTop,
      marginLeft: newLeft,
      width: full_width+'px',
      height: full_height+'px'
  }, 200);
}

// Restore original thumbnail size
function restore(imgobj) {
  imgobj.css('z-index', '0');
  imgobj.find('img').stop().animate({
      marginTop: '0px',
      marginLeft: '0px',
      width: thumb_width+'px',
      height: thumb_height+'px'
  }, 200);
}

