(function($) {
	
	$.fn.exposeAndEnlarge = function(options) {
		
		var largeImg = null;
		var popupStatus = 0;
		
		// Build options
		var opts = $.extend({}, $.fn.exposeAndEnlarge.defaults, options);
		
		return this.each(function() {
			var self = this;
			
			$(self).bind('click', function(e) {
				
				e.preventDefault();
				e.stopPropagation();
				
				// Grab the image id from the 'rel' attribute
				var rel = $(self).attr('rel');
				largeImg = $(rel);
				
				// Center and load the popup
				centerPopup(largeImg);
				loadPopup(largeImg);
				
				return false;
			});
			
      // // Fade out on hover
      // if($(opts.enlargeLink)) {
      //   $(opts.enlargeLink).hover( 
      //    function() {
      //      $(self).fadeTo(.2, .8)
      //    }
      //   );
      // }
      
			// Fade out and place the enlargeLink
			$(self).hover(
				function() {
					$(self).fadeTo(.2, .8);
					position = $(self).offset();
					if(opts.enlargeLink) {
  					$(opts.enlargeLink)
  						.css('top', (position.top + 10) + $(this).height())
  						.css('left', (position.left - 145) + $(this).width())
  						.css('display', 'block');
					}
				},
				function() {
					$(self).fadeTo(.2, 1);
					if(opts.enlargeLink) {
  					$(opts.enlargeLink).css('display', 'none');
					}
				}
			);

			// Click on any of the following elements to disable the popup
			$(opts.overlay+', '+opts.preloads).click(function() {
				disablePopup();
			});

			// Hit 'esc' to close
			$(document).keypress(function(e) {
				if(e.keyCode==27 && popupStatus==1) {
					disablePopup();
				}
			});

			$(window).resize(function() {
				if(largeImg)
					centerPopup(largeImg);
			});
			
		});
					
		// Popup Functionality 
		// From http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/ (Modified)

		function loadPopup(largeImg) {

			if(popupStatus==0) {
				$(opts.overlay).css('opacity', 0.7);
				$(opts.overlay).fadeIn('slow');
				largeImg.fadeIn('slow');
				popupStatus = 1;
			}

			// Keep body from scrolling
			$('body').css('overflow', 'hidden');
		}

		function disablePopup() {
			if(popupStatus==1) {
				$(opts.overlay).fadeOut("slow");
				$(opts.preloads).fadeOut("slow");
				popupStatus = 0;
			}

			// re-enable body scrolling
			$('body').css('overflow', 'auto');
		}

		function centerPopup(largeImg) {

			var windowWidth = $(window).width();
			var windowHeight = $(window).height();

			if(largeImg.height() > windowHeight)
				largeImg.css('height', windowHeight-10);

			var popupHeight = largeImg.height();
			var popupWidth = largeImg.width();

			var offset = 0;

			largeImg.css({
				'top'				: (windowHeight/2-popupHeight/2)-offset,
				'left'			: (windowWidth/2-popupWidth/2)-offset
			});

			// Load the close image
			$(opts.closeLink)
				.css('top', largeImg.offset().top + 5)
				.css('left', largeImg.offset().left + 5)
				.fadeIn('slow');
				
			// Load overlay
			$(opts.overlay).css('height', windowHeight);

		}
	};
	
	$.fn.exposeAndEnlarge.defaults = {
		enlargeLink : '#magnifying_glass',
		preloads		: '#image_preloads img',
		overlay			: '#overlay',
		closeLink		: '#close_image'
	};
})(jQuery);
