/*
 * JQZoom Behind Evolution 1.0.1 - Javascript Image magnifier
 *
 * Copyright (c) Engineer Renzi Marco(www.mind-projects.it)
 * Behind Evolution Manipulated and engineered by David Wilson
 * 
 * Date: 7-10-2009
 *
 * ChangeLog:  For Behind Evolution, all code pertaining to objects not needed were stripped. (e.g. Title code, other zoom styles, etc.
 * Additionally, code was optimized for speed, reducing the number of DOM crawls through jquery in order to shave milliseconds.
 *  
 * $License : GPL,so any change to the code you should copy and paste this section,and would be nice to report this to me(renzi.mrc@gmail.com).
 */
(function($)
{
	$.fn.jqzoom = function(options)
	{
  var settings = {
      zoomWidth: 200,		//zoomed width default width
      zoomHeight: 200,		//zoomed div default width
			showEffect: 'show',
			hideEffect: 'hide',
			fadeinSpeed: 'fast',
			fadeoutSpeed: 'slow',
			showPreload: true,
			preloadText : 'Loading zoom',
			preloadPosition : 'center'   //bycss
  };

	//extending options
	options = options || {};
    	$.extend(settings, options);


	return this.each(function()
	{
			var a = $(this);
			var img = $("img", this);
			var hdnInput = $("input", this);
			a.removeAttr('title');
			
			var smallimage = null; 
			var smallimagedata = {};

			//imageborder
			var btop = 0;
			var bleft = 0;
			var loader = null;

			var largeimage=null;
			var lens = null; 
			var lensdata = {};
			var scale = {}; 
			var stage = null; 
			var running = false;
			var mousepos = {};
			var firstime = 0;
			var preloadshow = false;
			var isMouseDown = false;
			var dragstatus = false

			a.hover(function(e)
			{
				mousepos.x = e.pageX;
				mousepos.y = e.pageY;
				activate();
			},function()
			{
				deactivate();
			});

			function activate()
			{
				var actObj = a[0];
				var actImg = img[0];
				var actSrc = hdnInput[0];
				var bNoZoom = actObj.className;
				if(bNoZoom=='nozoom') { return;	} 
				
				if(!smallimage) {
					smallimage = new Smallimage( actImg );
					smallimage.node.onload();
				}

				img.css({'background-position' : '1000px 1000px'});

				if ( !running ) {

					//finding border
					smallimage.findborder();
					running = true;

					if (!largeimage) {
						largeimage = new Largeimage( actSrc.value );
						loader = new Loader();
						largeimage.url = actSrc.value;
						if(settings.showPreload) loader.show();
						largeimage.loadimage();
					} else if (!largeimage.node || largeimage.url != actSrc.value) {
						if(largeimage.url != actSrc.value) {
							largeimage = new Largeimage( actSrc.value );
							stage = null;
							lens = null;
						}
						//largeimage.url = actSrc.value;
						if(settings.showPreload) loader.show();
						largeimage.loadimage();
					} else {
						if(settings.showPreload) loader.show();
						largeimage.setstage();
					}
					actObj.blur();
					return false;
				}
			}

			function deactivate()
			{
				img.css({'background-position' : '50% 50%'});

				//resetting parameters
				running = false;
				if(largeimage)largeimage.isloaded = false;

				if(stage)stage.remove();

				$().unbind();
				a.unbind('mousemove');
				firstime = 0;

			};

			//smallimage
			function Smallimage( image )
			{
				this.url = image.src;
				this.node = image;

				this.findborder = function()
				{
					var bordertop = '';
					bordertop = $(img).css('border-top-width');
					btop = '';
					var borderleft = '';
					borderleft = $(img).css('border-left-width');
					bleft = '';

					if(bordertop) {
						for(i=0;i<3;i++) {
							var x = [];
							x = bordertop.substr(i,1);

							if(isNaN(x) == false) {
								btop = btop +''+ bordertop.substr(i,1);
							} else {
								break;
							}
						}
					}

					if(borderleft) {
						for(i=0;i<3;i++) {
							if(!isNaN(borderleft.substr(i,1)))
							{
								bleft = bleft + borderleft.substr(i,1)
							}else
							{
								break;
							}
						}
					}
					btop = (btop.length > 0) ? eval(btop) : 0;
					bleft = (bleft.length > 0) ? eval(bleft) : 0;
				}
				
				this.node.onload = function()
				{
					var thisObj = $( this );
					if(settings.zoomWidth==0)settings.zoomWidth=thisObj.width();
					if(settings.zoomHeight==0)settings.zoomHeight=thisObj.height();
					smallimagedata.w = settings.zoomWidth;
					smallimagedata.h = settings.zoomHeight;
					smallimagedata.pos = thisObj.offset();
					smallimagedata.pos.l = smallimagedata.pos.left;
					smallimagedata.pos.t = smallimagedata.pos.top;
					smallimagedata.pos.r = smallimagedata.w + smallimagedata.pos.l;
					smallimagedata.pos.b = smallimagedata.h + smallimagedata.pos.t;
				};
				return this;
			};


			//Lens
			function Lens()
			{

				this.loadlens = function()
				{
					lensdata.w = (settings.zoomWidth)/scale.x;
					lensdata.h = (settings.zoomHeight)/scale.y;
				}
				return this;
			};

			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.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 ;
					//ADDED
					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;
						}
				}
				
				if(overtop()) {
						lenstop = 0 + btop ;
				} else if(overbottom()) {
					if($.browser.msie) {
						lenstop = smallimagedata.h - lensdata.h  + btop + 1 ;
					} else {
						lenstop = smallimagedata.h - lensdata.h - 1 + btop  ;
					}
				}
				
				lensdata.l = parseInt(lensleft);
				lensdata.t = parseInt(lenstop);
				
				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;
			};

			//LARGEIMAGE
			function Largeimage( url )
			{
				this.url = url;
				this.node = new Image();
				this.isloaded = false;
				
				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; // fires off async
				}

				this.setstage = function() {
					if(running){
						if(!stage)stage = new Stage();
						stage.activate();
						if(!lens)lens = new Lens();
						lens.activate() ;
						loader.hide();
					}
				}
				
				this.node.onload = function()
				{
					if(largeimage.url=="")return;
					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);
					largeimage.isloaded = true;
					largeimage.setstage();
				}
				return this;
			}

			Largeimage.prototype.setposition = function()
			{
				this.node.style.left = Math.ceil( - scale.x * parseInt(lensdata.l) + bleft) + 'px';
				this.node.style.top = Math.ceil( - scale.y * parseInt(lensdata.t) +btop) + 'px';
			};

			//STAGE
			function Stage()
			{
				var leftpos = smallimagedata.pos.l;
				var toppos = smallimagedata.pos.t;
				this.node = document.createElement("div");
				$(this.node).addClass('jqZoomWindow');

				$( this.node )
					.css({
						position: 'absolute',
						width: Math.round(settings.zoomWidth) + 'px',
						height: Math.round(settings.zoomHeight) + 'px',
						left: '0px',
						top: '0px',
						display: 'none',
						zIndex: 200,
						overflow: 'hidden'
				});

				leftpos = 0
				toppos = 0
				return this;
			}


			Stage.prototype.activate = function()
			{
				if (!this.node.firstChild ) { this.node.appendChild( largeimage.node ); }

				a.parent().append(this.node);

				switch(settings.showEffect) {
					case 'show':
						$(this.node).show();
					break;
					case 'fadein':
						$(this.node).fadeIn(settings.fadeinSpeed);
					break;
					default:
						$(this.node).show();
					break;
				}

				$(this.node).show();

				largeimage.node.style.display = 'block';
			}

			Stage.prototype.remove = function() {
				$(this.node).hide();
			}

			function Loader()
			{
				this.node = document.createElement("div");
				$(this.node).addClass('preload');
				$(this.node).html(settings.preloadText);
				$(this.node)
					.appendTo("body")
					.css('visibility','hidden');


				this.show = function()
				{
					switch(settings.preloadPosition) {
						case 'center':
							loadertop =  smallimagedata.pos.t + (smallimagedata.h - $(this.node ).height())/2;
							loaderleft = smallimagedata.pos.l + (smallimagedata.w - $(this.node ).width())/2;
						break;
						default:
							var loaderoffset = this.getoffset();
							loadertop = !isNaN(loaderoffset.top) ? smallimagedata.pos.t + loaderoffset.top : smallimagedata.pos.t + 0;
							loaderleft = !isNaN(loaderoffset.left) ? smallimagedata.pos.l + loaderoffset.left : smallimagedata.pos.l + 0;
						break;
					}

					$(this.node).css({
						top: loadertop  ,
						left: loaderleft ,
						position: 'absolute',
						visibility:'visible'
					});
				}
				this.hide = function()
				{
					$(this.node).css('visibility','hidden');
				}
				return this;
			}

			Loader.prototype.getoffset = function()
			{
				var o = {};
				o.left = this.node.offsetLeft;
				o.top = this.node.offsetTop;
				return o;
			}
		});
	}
})(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;
}
