/*
  loosely based on:
  http://orderedlist.com/blog/articles/fancyzoom-meet-jquery/
  http://static.railstips.org/orderedlist/demos/fancy-zoom-jquery/
 */

(function($){
$.fn.fancyZoom = function(opts){

  var options   = opts || {};
  var zooming   = false;

  var zoom = $('#zoom');
  var zoom_close;
  var zoom_content;

  if (zoom.length != 0) {
    zoom_close = $('#zoom_close');
    zoom = $('#zoom');
    zoom_content  = $('#zoom_content');
  } else {
    var html = '<div id="zoom" style="display:none;z-index:5;position:absolute;overflow:visible;padding:20px;background:transparent;">\n' +
               '  <div id="zoom_content"></div>\n' +
               '  <a href="#" title="Close" id="zoom_close" style="position:absolute;top:10px;right:10px;overflow:visible;z-index:6;">\n' +
               '    <img width="30" height="30" src="/img/close-button.gif" alt="Close" style="border:none; margin:0; padding:0;" />\n' +
               '  </a>\n' +
               '</div>\n';
    $('body').append(html);
    zoom = $('#zoom');

    $('html').click(function(e){if($(e.target).parents('#zoom:visible').length == 0) hide();});
    $(document).keyup(function(event){
        if (event.keyCode == 27 && $('#zoom:visible').length > 0) hide();
    });

    zoom_close = $('#zoom_close');
    zoom_close.click(hide);
    zoom_content  = $('#zoom_content');
    zoom_content.css({
      'background-color': 'silver',
      'border': '1px #e0e1e2 solid',
      'padding': '6px',
      '-ms-border-radius': '8px 8px 8px 8px', '-moz-border-radius': '8px', '-webkit-border-radius': '8px', 'border-radius': '8px',
      '-webkit-box-shadow': '0 0 9px 9px rgba(0,0,0,0.2)', '-moz-box-shadow': '0 0 9px 9px rgba(0,0,0,0.2)', 'box-shadow': '0 0  9px 9px rgba(0,0,0,0.2)'
    });
  }


  this.each(function() {
    $($(this).attr('href')).hide();
    $(this).click(show);
  });

  return this;

  function show(e) {
    if (zooming) return false;
		zooming         = true;
		var content_div = $($(this).attr('href'));
  	var zoom_width  = options.width;
		var zoom_height = options.height;

		var w = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
  	var h = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
  	var wy = window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop);

    var width  = zoom_width  || content_div.width();
    var height = zoom_height || content_div.height();
    var startWidth  = width / 4;
    var startHeight = height / 4;

    // ensure that newTop is at least 25 so it doesn't cut the top of close button
    var newLeft = (w - width) / 2;
		var newTop  = Math.max(wy + (h - height) / 2, 25);

    zoom_content.html(content_div.html());
    $('#zoom_content img').css('width', '100%');
    if (options.closeOnClick) {
      zoom.click(hide);
    }
    zoom_close.css({display : 'none'});

    var curLeft = e.pageX - startWidth / 2;
    var curTop  = e.pageY - startHeight / 2;
    zoom_close.attr('curTop', curTop);
    zoom_close.attr('curLeft', curLeft);

    zoom.css({
      display   : 'block',
      opacity   : 0,
      left      : curLeft + 'px',
      top       : curTop + 'px',
			width     : startWidth + 'px',
			height    : startHeight / 4 + 'px'
		});
    zoom.attr("left", curLeft);
    zoom.attr("top", curTop);
    zoom.animate({
      top    : newTop + 'px',
      left   : newLeft + 'px',
      width  : width + 'px',
      height : height + 'px',
      opacity: 1
    }, 750, 'swing', function() {
      zoom_close.show();
      zooming = false;
    });
    return false;
  }

  function hide() {
    if (zooming) return false;
		zooming = true;
	  zoom.unbind('click');
		zoom_close.hide();
		zoom.animate({
      top     : zoom_close.attr('curTop') + 'px',
      left    : zoom_close.attr('curLeft') + 'px',
      opacity : "hide",
      width   : '1px',
      height  : '1px'
    }, 500, 'swing', function() {
      zoom_content.html('');
			zooming = false;
    });
    return false;
  }

}
})(jQuery);
