function zoomSetup() {
	jQuery.fn.zoom = function(bigWidth, bigHeight, bigURL) {
		return this.each(function() {
			var small = jQuery(this);
			small.css("position", "relative");
			small.attr("big", bigURL);
			jQuery(this).wrap('<div style="width: ' + small.width() + 'px; height: ' + small.height() + 'px; overflow: hidden; position: relative; display:block; margin:auto"></div>');
			var div = small.parent();
			var big = small.clone();
			var zoom = small.clone();
			var lense = jQuery('<img src="images/zoom_icon.png" style="position: absolute; z-index: 100; display: none" />');
			lense.load(function(e) {
				var lense = jQuery(this);
				var small = lense.prev("img");
				lense.css("left", (small.width() - lense.width())/2 + "px");
				lense.css("top", (small.height() - lense.height())/2 + "px");
			});
			lense.appendTo(div);
			var reset = jQuery('<div style="cursor: pointer; position: absolute; z-index: 100; background: black; color:#fff; font-size:14px; text-align:center; width:73px; height:25px; padding-top:10px; -moz-border-radius: 7px; -webkit-border-radius: 7px; border-radius: 7px; display: none">Reset</div>');
			small.css("cursor", 'url("zoom-in.cur"), -moz-zoom-in');
			lense.css("cursor", 'url("zoom-in.cur"), -moz-zoom-in');
			reset.appendTo(div);
			reset.css("right", "10px");
			reset.css("bottom", "10px");
			zoom.hide();
			zoom.prependTo(div);
			big.width(bigWidth);
			big.height(bigHeight);
			big.css("cursor", "move");
			big.hide();
			big.prependTo(div);
			var preloader = jQuery('<img src="images/preloader.gif" style="position: absolute; z-index: 100; display: none" />');
			preloader.load(function(e) {
				var preloader = jQuery(this);
				var small = preloader.siblings("img").last().prev("img");
				preloader.css("left", (small.width() - preloader.width())/2 + "px");
				preloader.css("top", (small.height() - preloader.height())/2 + "px");
			});
			preloader.prependTo(div);
			big.draggable({drag: function(e, ui) {
				var target = jQuery(e.target);
				if (ui.position.left > 0) {
					ui.position.left = 0;
				} else if (ui.position.left < target.parent().width() - target.width()) {
					ui.position.left = target.parent().width() - target.width();
				}
				if (ui.position.top > 0) {
					ui.position.top = 0;
				} else if (ui.position.top < target.parent().height() - target.height()) {
					ui.position.top = target.parent().height() - target.height();
				}
			}});
			small.hover(function(e) {
				var lense = jQuery(this).next("img");
				lense.show();
			}, function(e) {
				var lense = jQuery(this).next("img");
				lense.hide();
			});
			lense.hover(function(e) {
				var small = jQuery(this).prev("img");
				if (small.is(":visible")) {
					small.trigger("mouseover");
				}
			});
			div.click(function(e) {
				var div = jQuery(this);
				var lense = div.children("img").last();
				var small = lense.prev("img");
				var zoom = small.prev("img");
				var big = zoom.prev("img");
				var preloader = big.prev("img");
				if (small.is(":hidden")) {
					return true;
				}
				var x = (e.pageX - small.offset().left);
				var y = (e.pageY - small.offset().top);
				var bigLeft = x - x * big.width() / small.width();
				var bigTop = y - y * big.height() / small.height();
				big.attr("src", small.attr("src"));
				if (typeof this.preload == 'undefined') {
					this.preload = new Image();
					this.preload.src = small.attr("big");
				}
				big.css("left", bigLeft + "px").css("top", bigTop + "px");
				small.hide();
				lense.hide();
				zoom.show();
				zoom.animate({width: big.width() + "px", height: big.height() + "px", left: bigLeft + "px", top: bigTop + "px"}, "slow", function() {
					var small = jQuery(this).next("img");
					var big = jQuery(this).prev("img");
					var preloader = big.prev("img");
					preloader.show();
					big.siblings("div").show();
					big.load(function() {
						var big = jQuery(this);
						big.show().unbind("load")
						big.siblings("img").hide();
					});
					big.attr("src", small.attr("big"));
				});
			});
			reset.click(function(e) {
				var reset = jQuery(this);
				var small = reset.siblings("img").last().prev("img");
				var zoom = small.prev("img");
				var big = zoom.prev("img");
				var preloader = big.prev("img");
				reset.hide();
				zoom.show();
				big.hide();
				zoom.animate({width: small.width() + "px", height: small.height() + "px", left: "0px", top: "0px"}, "slow", function() {
					var small = jQuery(this).siblings("img").last().prev("img");
					small.show().siblings("img").hide();
				});
			});
			big.dblclick(function(e) {
				jQuery(this).siblings("div").trigger("click");
			});
		});
	}
}

if (jQuery) {
	zoomSetup();
} else if (google.setOnLoadCallback) {
	google.setOnLoadCallback(function() {
		zoomSetup();
	});
}

