//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
//
// jqZoom version 2.1
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// First Release on Dec 05 2007
// i'm searching for a job,pick me up!!!
// mail: renzi.mrc@gmail.com
//**************************************************************

(function($){

	$.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: "right" ,//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) {
		$.extend(settings, options);
	}

	var noalt='';
	$(this).hover(
		function(){
			var myClick = false;
			$(this).click(function(e){
				if (myClick)
				{
					return false;
				}
				myClick = true;
				var imageInfo = {
					left	: this.offsetLeft,
					right	: this.offsetRight,
					top		: $(this).get(0).offsetTop,
					width	: $(this).children('img').get(0).offsetWidth,
					height	: $(this).children('img').get(0).offsetHeight
				}

				
				noalt= $(this).children("img").attr("alt");
				var bigimage = $(this).children("img").attr("jqimg");
				$(this).children("img").attr("alt",'');
				if($("div.zoomdiv").get().length == 0){
					$(this).after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
					$(this).append("<div class='jqZoomPup'>&nbsp;</div>");
				}
				var bigwidth = 800;
				var bigheight = 960;
				if(settings.position == "right"){
					if(imageInfo.left + imageInfo.width + settings.offset + settings.xzoom > screen.width){

						leftpos = imageInfo.left  - settings.offset - settings.xzoom;

					}else{
					leftpos = imageInfo.left + imageInfo.width + settings.offset;
				}
				}else{
					leftpos = imageInfo.left - settings.xzoom - settings.offset;
					if(leftpos < 0){
						leftpos = imageInfo.left + imageInfo.width  + settings.offset;
					}
				}
				doZoom(e,imageInfo,bigwidth,bigheight);
				$("div.zoomdiv").css({ top: imageInfo.top,left: leftpos });
				$("div.zoomdiv").width(settings.xzoom);
				$("div.zoomdiv").height(settings.yzoom);
				$("div.zoomdiv").show();
				if(!settings.lens){
				  $(this).css('cursor','crosshair');
				}

				$(this).mousemove(function(e){
					doZoom(e,imageInfo,bigwidth,bigheight);
				});
			});
		},
		function(){
			$(this).children("img").attr("alt",noalt);
			$(this).unbind("mousemove");
			$(this).unbind("click");
			if(settings.lens){
				$("div.jqZoomPup").remove();
			}
			$("div.zoomdiv").remove();
		});

		count = 0;

		if(settings.preload){
			$('body').append("<div style='display:none;' class='jqPreload"+count+"'>sdsdssdsd</div>");
			$(this).each(function(){
				var imagetopreload= $(this).children("img").attr("jqimg");
				var content = jQuery('div.jqPreload'+count+'').html();
				jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');
			});

		}

		function doZoom(e,imageInfo,bigwidth,bigheight){
					var scaley ='x';
					var scalex= 'y';
					if(isNaN(scalex)|isNaN(scaley)){
						var scalex = (bigwidth/imageInfo.width);
						var scaley = (bigheight/imageInfo.height);
						$("div.jqZoomPup").width((settings.xzoom)/scalex );
						$("div.jqZoomPup").height((settings.yzoom)/scaley);
						if(settings.lens)
							$("div.jqZoomPup").css('visibility','visible');
					}
					xpos = e.pageX - $("div.jqZoomPup").width()/2 - imageInfo.left;
					ypos = e.pageY - $("div.jqZoomPup").height()/2 - imageInfo.top ;
					if(settings.lens){
						xpos = (e.pageX - $("div.jqZoomPup").width()/2 < imageInfo.left ) ? 0 : (e.pageX + $("div.jqZoomPup").width()/2 > imageInfo.width + imageInfo.left ) ?  (imageInfo.width -$("div.jqZoomPup").width() -2)  : xpos;
						ypos = (e.pageY - $("div.jqZoomPup").height()/2 < imageInfo.top ) ? 0 : (e.pageY + $("div.jqZoomPup").height()/2  > imageInfo.height + imageInfo.top ) ?  (imageInfo.height - $("div.jqZoomPup").height() -2 ) : ypos;
						$("div.jqZoomPup").css({ top: ypos,left: xpos });
					}
					scrolly = ypos;
					$("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
					scrollx = xpos;
					$("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex ;
		}

	}
})(jQuery);

function MouseEvent(e) {
	this.x = e.pageX
	this.y = e.pageY
}
var a = "";
function debug(str){
	a = a + "-" + str.toString();
//	$("#recommend-zone").html(a);
}

