(function($){ 
	$.fn.zoomer = function(options) {
		
		var defaults = {
			width: "100",
			height: "100"
		}; 
		var options = $.extend(defaults, options);
		
		return this.each( function() {
			
			var img_p = $(this).children("img:first");
			var img_g = $("<img />")
							.attr({ "src": img_p.attr("rel") })
							.css({ 	"cursor": "crosshair", "background": "white", "position": "absolute" });
			var lens = $("<div></div>")
						.attr({ "id": "zoomer_lente", "class": "zoomer_lente" })
						.css({ 	"position": "absolute", "height": options.height + "px", "width": options.width + "px", 
								"overflow": "hidden" })
						.append(img_g);
							
			
			$(this).css({ "cursor": "crosshair" });
			
			$(this).mouseover( function() {
				$("body").append(lens);
			});
			
			$(this).mousemove( function(e) {	
			
				var offsetP = img_p.offset();
				var leftP = ((e.pageX - offsetP.left) * 100) / img_p.width();
				var topP = ((e.pageY - offsetP.top) * 100) / img_p.height();
				
				var offsetG = img_g.offset();
				var leftG = (img_g.width() * leftP) / 100;
				var topG = (img_g.height() * topP) / 100;
				
				leftG = (leftG * -1) + (lens.width() / 2);
				topG = (topG * -1) + (lens.height() / 2);
				
				if (leftG > 0)
					leftG = 0;
				else if ((leftG - lens.width()) < (img_g.width() * -1))
					leftG = (img_g.width() - lens.width()) * -1;
					
				if (topG > 0)
					topG = 0;
				else if ((topG - lens.height()) < (img_g.height() * -1))
					topG = (img_g.height() - lens.height()) * -1;
			
				$(lens).css({ "left": e.pageX + 10 + "px", "top": e.pageY + 10 + "px" });
				$(img_g).css({ "left": leftG + "px", "top": topG + "px" });
			});
			
			$(this).mouseout( function() {
				$(".zoomer_lente").remove();
			});
			
		});
	}
})(jQuery); 
