//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
//
// jqZoom version 2.2
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// First Release on Dec 05 2007
// i'm looking for a job,pick me up!!!
// mail: renzi.mrc@gmail.com
//**************************************************************

(function(jQuery){
	jQuery.fn.jqueryzoom = function(options){

		var settings = {
			xzoom: 200,		//zoomed width default width
			yzoom: 200,		//zoomed div default width
			offset: 10,		//zoomed div default offset
			position: "" ,//zoomed div default position,offset position is to the right of the image
			lens:1, //zooming lens over the image,by default is 1;
			preload: 1
		};

		if(options) {
			jQuery.extend(settings, options);
		}

		var noalt = '';
		
		var $THIS = jQuery(this);

		jQuery(this).hover(function(){
			
			var $this = jQuery(this);
			var imageLeft = $this.offset().left;                
			var imageTop = $this.offset().top;
			
			var $children = $this.children('img');
			
			var imageWidth = $children.get(0).offsetWidth;
			var imageHeight = $children.get(0).offsetHeight;

			noalt = $children.attr("alt");

			var bigimage = $children.attr("jqimg");
			$children.attr("alt",'');
			if(jQuery("div.zoomdiv").get().length == 0){
				$this.after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
				$this.append("<div class='jqZoomPup'>&nbsp;</div>");
			}
			
			var zoomdiv = jQuery("div.zoomdiv");
			switch(settings.position) {
				case 'right': 
					if(imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width){
						leftpos = imageLeft  - settings.offset - settings.xzoom;
					}else{
						leftpos = imageLeft + imageWidth + settings.offset;
					}
					zoomdiv.css({ top: imageTop, left: leftpos });
					break;
				case 'left': 
					leftpos = imageLeft - settings.xzoom - settings.offset;
					if(leftpos < 0){
						leftpos = imageLeft + imageWidth  + settings.offset;
					}
					zoomdiv.css({ top: imageTop, left: leftpos });
					break;
				default: 
					break;
			}
			zoomdiv.width(settings.xzoom);
			zoomdiv.height(settings.yzoom);
			zoomdiv.show();
			if(!settings.lens){
				$this.css('cursor','crosshair');
			}

			jQuery(document.body).mousemove(function(e){
				mouse = new MouseEvent(e);

				/*jQuery("div.jqZoomPup").hide();*/

				var $jqZoomPup = jQuery("div.jqZoomPup");
				var zoomdiv = jQuery("div.zoomdiv").get(0);

				var bigwidth = jQuery(".bigimg").get(0).offsetWidth;
				var bigheight = jQuery(".bigimg").get(0).offsetHeight;

				var scaley ='x';
				var scalex= 'y';
				if(isNaN(scalex)|isNaN(scaley)){
					var scalex = (bigwidth/imageWidth);
					var scaley = (bigheight/imageHeight);

					$jqZoomPup.width((settings.xzoom)/scalex );
					$jqZoomPup.height((settings.yzoom)/scaley);
					if(settings.lens){
						$jqZoomPup.css('visibility','visible');
					}
				}

				xpos = mouse.x - $jqZoomPup.width()/2 - imageLeft;
				ypos = mouse.y - $jqZoomPup.height()/2 - imageTop ;
				
				var $jqZoomPup_width = $jqZoomPup.width();
				var $jqZoomPup_height = $jqZoomPup.height();

				if(settings.lens){
					xpos = (mouse.x - $jqZoomPup_width/2 < imageLeft ) ? 0 : (mouse.x + $jqZoomPup_width/2 > imageWidth + imageLeft ) ?  (imageWidth -$jqZoomPup_width -2)  : xpos;
					ypos = (mouse.y - $jqZoomPup_height/2 < imageTop ) ? 0 : (mouse.y + $jqZoomPup_height/2  > imageHeight + imageTop ) ?  (imageHeight - $jqZoomPup_height -2 ) : ypos;
				}


				if(settings.lens){
					$jqZoomPup.css({ top: ypos,left: xpos });
				}
				scrolly = ypos;
				zoomdiv.scrollTop = scrolly * scaley;
				scrollx = xpos;
				zoomdiv.scrollLeft = (scrollx) * scalex ;
			});
		},function(){
			jQuery(this).children("img").attr("alt",noalt);
			jQuery(document.body).unbind("mousemove");
			if(settings.lens){
				jQuery("div.jqZoomPup").remove();
			}
			jQuery("div.zoomdiv").remove();
		});

		count = 0;

		if(settings.preload){
			jQuery(document).ready(function() {
				jQuery('body').append("<div style='display:none;' class='jqPreload"+count+"'>sdsdssdsd</div>");
				$preload = jQuery('div.jqPreload'+count+'');
				var delay = 50;
				$THIS.each(function(){
					var $this = jQuery(this);
					window.setTimeout(function() {
						var imagetopreload = $this.children("img").attr("jqimg");
						$preload.append('<img src=\"'+imagetopreload+'\">');
					}, delay);
					delay += 50;
				});
			});
		}

	}

})(jQuery);

function MouseEvent(e) {
	this.x = e.pageX;
	this.y = e.pageY;
}


