/*!
 * jQuery miniZoomPan 1.0
 * 2009 Gian Carlo Mingati
 * Version: 1.0 (18-JUNE-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Requires:
 * jQuery v1.3.2 or later
 *
 * Modified by dirtondirt to work better
 *
 */
jQuery.fn.miniZoomPan = function(settings) {

	settings = jQuery.extend({
	sW: 10, // small image width
	sH: 10, // small image height
	lW: 20, // large image width
	lH: 20 // large image height
	}, settings);

	return this.each(function(){

		var div =  jQuery(this);
		div.css({overflow: "hidden", width: settings.sW, height: settings.sH}).addClass("minizoompan");
		var ig = div.children();
		ig.css({position: "relative"});
		jQuery(window).bind("load", function() {
			ig.width(settings.sW);
			ig.height(settings.sH);
		});

		div.mousemove(function(e){
			var divWidth = div.width();
			var divHeight = div.height();
			var igW = ig.width();
			var igH = ig.height();
			var dOs = div.offset();
			var mode = this.getAttribute("mode");
			if(mode != "l"){
				this.setAttribute("mode", "l");
				ig.css({ width: settings.lW, height: settings.lH});
				var hiSrc = ig.attr("src").replace(/s.gif/, 'l.gif');
				swapImage(ig, hiSrc);
			}
			var leftPan = (e.pageX - dOs.left) * (divWidth - igW) / (divWidth+1*2);
			var topPan = (e.pageY - dOs.top) * (divHeight - igH) / (divHeight+1*2);
			ig.css({left: leftPan, top: topPan});
		});

		div.hover(
			function () {
				this.setAttribute("mode", "l");
				ig.css({ width: settings.lW, height: settings.lH});
				var hiSrc = ig.attr("src").replace(/s.gif/, 'l.gif');
				swapImage(ig, hiSrc);
			},
			function () {
				this.setAttribute("mode", "s");
				ig.css({left: "0", top: "0", width: settings.sW, height: settings.sH});
				var loSrc = ig.attr("src").replace(/l.gif/, 's.gif');
				swapImage(ig, loSrc);
			}
		);

		function swapImage(param, uri){
			param.load(function () {
				div.children("span.loader").fadeOut(250);
			}).error(function (){
			alert("Image does not exist or its SRC is not correct.");
			}).attr('src', uri);
		}



	});

};
