
(function($)
{
    $.fn.jqzoom = function(options)
    {
        var settings = {
            zoomWidth: 157,	
            zoomHeight: 118,	
			preloadPosition : 'center'
			
        };
		options = options || {};
        $.extend(settings, options);
		
		return this.each(function()
		{
			var a = $(this);
			var aTitle = a.attr('title'); 
			$(a).removeAttr('title');
			$(a).css('outline-style','none');
			var img = $("img", this);
			var imageTitle = img.attr('title');
			img.removeAttr('title');	
			var smallimage = new Smallimage( img );
			var smallimagedata = {};
			var btop = 0;
			var bleft = 0;
			var largeimage = new Largeimage( a[0].href );
			var lens = new Lens();
			var lensdata = {};
			var largeimageloaded = false;
			var scale = {};
			var stage = null;
			var running = false;
			var mousepos = {};
			var firstime = 0;
			var preloadshow = false;
			var isMouseDown = false;
			var dragstatus = false
			smallimage.loadimage();
			$(this).click(function(){return false;});
			$(this).hover(function(e)
			{
				mousepos.x = e.pageX;
				mousepos.y	= e.pageY;
				activate();
			},function()
			{
				//deactivate();
			});



/* --------------------------------------------- activate --------------------------------------------- */
			function activate()
			{

				if ( !running ) {
					$('div.open').attr("innerHTML","");
					running = true;				
					imageTitle = img.attr('title');
					img.removeAttr('title');
					aTitle = a.attr('title');
					$(a).removeAttr('title');
					if (!largeimage || $.browser.safari) { largeimage = new Largeimage( a[0].href ); }
					if(!largeimageloaded || $.browser.safari)
					{
						largeimage.loadimage();
					}else
					{
						stage = new Stage();
						stage.activate();
						lens = new Lens;
						lens.activate();
					}
					a[0].blur();
					return false;
				}
			}

/* --------------------------------------------- Smallimage --------------------------------------------- */
		function Smallimage( image )
		{
			this.node = image[0];

			this.loadimage = function() {
				this.node.src = image[0].src;
			};

			this.node.onload = function()
			{
				a.css({'cursor':'crosshair','display':'block'});
				if(a.parent().css('position') != 'absolute') { a.parent().css('position','relative'); }
			//	if($.browser.safari || $.browser.opera) { $(img).css({position:'absolute',top:'0px',left:'0px'}); }
				smallimagedata.w = $( this ).width();
				smallimagedata.h = $( this ).height();
				smallimagedata.h = $( this ).height();
				smallimagedata.pos = $( this ).offset();
				smallimagedata.pos.l = $( this ).offset().left;
				smallimagedata.pos.t = $( this ).offset().top;
				smallimagedata.pos.r = smallimagedata.w + smallimagedata.pos.l;
				smallimagedata.pos.b = smallimagedata.h + smallimagedata.pos.t;
				a.height(smallimagedata.h);
				a.width(smallimagedata.w);
			};



			return this;
		};


/* --------------------------------------------- LENS --------------------------------------------- */
		function Lens()
		{

			this.node = document.createElement("div");
			$(this.node).addClass('jqZoomPup');
			this.node.onerror = function() {
				$( lens.node ).remove();
				lens = new Lens();
				lens.activate() ;
			};
			this.loadlens = function()
			{
				lensdata.w = (settings.zoomWidth)/scale.x;
				lensdata.h = (settings.zoomHeight)/scale.y;
			$( this.node ).css({
					width: lensdata.w + 'px',
					height: lensdata.h + 'px',
					position: 'absolute',
					display: 'none',
					borderWidth: 1+'px'
				});
			a.append(this.node);
			}
			return this;
		};
		
		
/* --------------------------------------------- LENS activate --------------------------------------------- */		
		Lens.prototype.activate = function()
		{
			this.loadlens();
			lens.setposition(null);
			$(a).bind( 'mousemove', function(e)
			{
				mousepos.x = e.pageX;
				mousepos.y = e.pageY;
				lens.setposition( e );
			});
			return this;
		};


/* --------------------------------------------- LENS setposition --------------------------------------------- */		
		Lens.prototype.setposition = function( e)
		{

			if(e)
			{
				mousepos.x = e.pageX;
				mousepos.y	= e.pageY;
			}

			if(firstime == 0)
			{
			 	var lensleft = (smallimagedata.w)/2 - (lensdata.w)/2 ;
			 	var lenstop = (smallimagedata.h)/2 - (lensdata.h)/2 ;
				$('div.jqZoomPup').show()
				if(settings.lens) {this.node.style.visibility = 'visible';}
				else {this.node.style.visibility = 'hidden';$('div.jqZoomPup').hide();}
				firstime = 1;
			}else
			{
				var lensleft = mousepos.x - smallimagedata.pos.l - (lensdata.w)/2 ;
				var lenstop = mousepos.y - smallimagedata.pos.t -(lensdata.h)/2 ;
			}

				if(overleft()) {lensleft = 0  + bleft;}
				else if(overright())
				{
					if($.browser.msie)
					{
					lensleft = smallimagedata.w - lensdata.w  + bleft + 1  ;
					}else
					{
					lensleft = smallimagedata.w - lensdata.w  + bleft - 1  ;
					}
				}

				//in alto
				if(overtop())
				{
					lenstop = 0 + btop ;
				}else
				//sotto
				if(overbottom())
				{

					if($.browser.msie){
					lenstop = smallimagedata.h - lensdata.h  + btop + 1 ;
					}else
					{
					lenstop = smallimagedata.h - lensdata.h - 1 + btop  ;
					}

				}
				lensleft = parseInt(lensleft);
				lenstop = parseInt(lenstop);
				$('div.jqZoomPup',a).css({top: lenstop,left: lensleft });
				this.node.style.left = lensleft + 'px';
				this.node.style.top = lenstop + 'px';
				largeimage.setposition();
				function overleft() { return mousepos.x - (lensdata.w +2*1)/2  - bleft < smallimagedata.pos.l; }
				function overright() { return mousepos.x + (lensdata.w + 2* 1)/2  > smallimagedata.pos.r + bleft ; }
				function overtop() { return mousepos.y - (lensdata.h + 2* 1)/2  - btop < smallimagedata.pos.t; }
				function overbottom() { return mousepos.y + (lensdata.h + 2* 1)/2    > smallimagedata.pos.b + btop; }
			return this;
		};

/* --------------------------------------------- LENS getoffset --------------------------------------------- */
		Lens.prototype.getoffset = function() {
			var o = {};
			o.left = parseInt(this.node.style.left) ;
			o.top =  parseInt(this.node.style.top) ;
			return o;
		};

/* --------------------------------------------- Largeimage --------------------------------------------- */
		function Largeimage( url )
		{
			this.url = url;
			this.node = new Image();
			this.loadimage = function()
			{
				if(!this.node)
				this.node = new Image();
				this.node.style.position = 'absolute';
				this.node.style.display = 'none';
				this.node.style.left = '-5000px';
				this.node.style.top = '10px';
				document.body.appendChild( this.node );
				this.node.src = this.url;
			}
			this.node.onload = function()
			{
				this.style.display = 'block';
				var w = Math.round($(this).width());
				var	h = Math.round($(this).height());
				this.style.display = 'none';
				scale.x = (w / smallimagedata.w);
				scale.y = (h / smallimagedata.h);
				largeimageloaded = true;
				if(running)
				{
					stage = new Stage();
					stage.activate();
				}
				if(running)
				{
					lens = new Lens();
					lens.activate() ;
				}
			}
			return this;
		}


		Largeimage.prototype.setposition = function()
		{
          	this.node.style.left = Math.ceil( - scale.x * parseInt(lens.getoffset().left) + bleft) + 'px';
			this.node.style.top = Math.ceil( - scale.y * parseInt(lens.getoffset().top) +btop) + 'px';
		};
/* --------------------------------------------- Stage --------------------------------------------- */
		function Stage()
		{

			var leftpos = smallimagedata.pos.l;
			var toppos = smallimagedata.pos.t;
			this.node = document.createElement("div");
			$( this.node )
				.css({
					position: 'absolute',
					width: Math.round(settings.zoomWidth) + 'px',
					height: Math.round(settings.zoomHeight) + 'px',
					overflow: 'hidden',
					display: 'none',
					zIndex: 2
				});
			return this;
		}

		Stage.prototype.activate = function()
		{

			if ( !this.node.firstChild ) this.node.appendChild( largeimage.node );
			$(this.node).show();
            if ($.browser.msie && $.browser.version < 7) {
	        this.ieframe = $('<iframe class="zoom_ieframe" frameborder="0" src="#"></iframe>')
	          .css({ position: "absolute",zIndex: 2,width:settings.zoomWidth,height:settings.zoomHeight })
	          .insertBefore(this.node);
			};
			
			document.getElementById('open').appendChild( this.node );

			
			largeimage.node.style.display = 'block';
		}
		Stage.prototype.remove = function() { }
		});
	}
})(jQuery);

	function trim(stringa)
	{
	    while (stringa.substring(0,1) == ' ') {stringa = stringa.substring(1, stringa.length);}
	    while (stringa.substring(stringa.length-1, stringa.length) == ' ') {stringa = stringa.substring(0,stringa.length-1);}
	    return stringa;
	}