/**
 * @author Huy Dinh
 * @date Sept 2008
 * @version 0.1
 *
 * A plug in to show a magnify effect on images
 */
(function ($) {
	var zoomImageContainerCentreX,
		zoomImageContainerCentreY,
		zoomImageContainerHeight,
		zoomImageContainerWidth,
		thumbImageOffsetX,
		thumbImageOffsetY,
		sensorFade,
		sensorWidth,
		sensorHeight,
		zoomImageWidth,
		zoomImageHeight,
		zoomImage = $("<img style='position:absolute;'/>"),
		zoomImageContainer = $("<div id='huy-magnifier' style='overflow:hidden;position:absolute;display:none;'></div>").append(zoomImage),
		sensor = $("<div style='position:absolute;top:0;left:0;background-color:#fff;opacity:0;filter:Alpha(Opacity=0);cursor:crosshair;z-index:2000;'></div>").mousemove(function(e) {
		 	  zoomImageContainer.css({
				left:e.pageX - zoomImageContainerCentreX,
				top:e.pageY - zoomImageContainerCentreY
			});
			zoomImage.css({
				left:((e.pageX - thumbImageOffsetX) / sensorWidth) * -(zoomImageWidth - zoomImageContainerWidth),
				top:((e.pageY - thumbImageOffsetY) / sensorHeight) * -(zoomImageHeight - zoomImageContainerHeight)
			});
		}).bind("mouseleave", function() {
			zoomImageContainer.hide();
			sensor.hide();
		});

	$(function() {
		$("body").append(zoomImageContainer).append(sensor);
		zoomImageContainerWidth = zoomImageContainer.width();
		zoomImageContainerHeight = zoomImageContainer.height();
		zoomImageContainerCentreX = zoomImageContainerWidth * .5;
		zoomImageContainerCentreY = zoomImageContainerHeight * .5;
	});

	$.fn.magnify = function(options) {
		var thumbImage = options.image,
				imgTesting = new Image(),
				fullHeight,
				fullWidth,
				that = this;

		function CreateDelegate(contextObject, delegateMethod) {
				return function() {
						return delegateMethod.apply(contextObject, arguments);
				}
		}

		function imgTesting_onload() {
				fullWidth = this.width;
				fullHeight = this.height;
				loadTheRest();
		}

		imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
		imgTesting.src = thumbImage[0].src;
		if(imgTesting.complete) $(imgTesting).trigger("load");

		function loadTheRest() {
				if( options.hasOwnProperty('lightbox') ) {
						sensor.click(function(){
								thumbImage.addClass('lightboxOpen');
								zoomImageContainer.hide();
								sensor.hide();
								options.lightbox.open({
										triggerElement: options.image,
										closeCallback: function() {
												thumbImage.removeClass('lightboxOpen');
										},
										width: fullWidth,
										height: fullHeight
								})
						});
				}

				var thumbContainer = that.parent().css({
						position: "relative"
				});

				that.attr("alt","").bind("mouseenter", function(){
						if( !thumbImage.hasClass('lightboxOpen') ) {
								sensorWidth = thumbImage.width();
								sensorHeight = thumbImage.height();
								thumbImageOffsetX = thumbImage.offset().left;
								thumbImageOffsetY = thumbImage.offset().top;
								zoomImage.attr("src", thumbImage.attr("src"));
								zoomImageWidth = fullWidth;
								zoomImageHeight = fullHeight;
								sensor.css({
										width:  sensorWidth + "px",
										height: sensorHeight + "px",
										left:   thumbImageOffsetX + "px",
										top:    thumbImageOffsetY + "px"
								}).show();
								zoomImageContainer.show();
						}
				});

				if (that.width() > that.height()) {
						that.height(that.height() / (that.width() / thumbContainer.width())).width(thumbContainer.width()).css({
								bottom:0
						});
				} else {
						var thumbImageWidth = that.width() / (that.height() / thumbContainer.height());
						that.width(thumbImageWidth).height(thumbContainer.height()).css({
								marginLeft:((thumbContainer.width() - thumbImageWidth) * 0.5) + "px"
						});
				}
				return this;
		}

	}
})(jQuery);

