//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// Released on Dec 05 2007
// mail: renzi.mrc@gmail.com
// *** meiking edited it on Apr 24 2008 at www.bjcl.net.
// *** add some cool function, rework many place.
// *** if exist bugs, please tell me.
// *** mail: meiking@gmail.com
//**************************************************************
(function($){

		$.fn.jqueryzoom = function(options){

		var settings = {
				xzoom: 400,		//zoomed width default width
				yzoom: 400,		//zoomed div default width
				offset: 1000,		//zoomed div default offset
				position: "left"  //zoomed div default position,offset position is to the right of the image
			};

			if(options) {
				$.extend(settings, options);
			}

			var top = getOffsetTop($("img.jqzoom").get(0));
			var left = getOffsetLeft($("img.jqzoom").get(0));

            var noalt ='';

		$("#zoom_div").hover(function(){
			var img = new GetXYWH($("img.jqzoom").get(0));

		    var imageLeft = img.left;
		    var imageRight = $("img.jqzoom").get(0).offsetRight;
		    var imageTop =  img.top;
		    var imageWidth = img.width;
		    var imageHeight = img.height;

		    $("#zoom_div").width(imageWidth);
		    $("#zoom_div").height(imageHeight);

		    var bigimage = $("img.jqzoom").attr("lowalt");                

		    if($("div.zoomdiv").get().length == 0){
				$("#zoom_div").after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
		    }

			if($("#view").get().length == 0) {
				$("#zoom_div").append("<div id='view'></div>");
				$("#zoom_div").append("<div id='lim_top' class='use_filter'></div>");
				$("#zoom_div").append("<div id='lim_left' class='use_filter'></div>");
				$("#zoom_div").append("<div id='lim_right' class='use_filter'></div>");
				$("#zoom_div").append("<div id='lim_bottom' class='use_filter'></div>");
			}

		    if(settings.position == "right"){
				leftpos = imageLeft + imageWidth + settings.offset;

		    }else{
				leftpos = imageLeft - settings.xzoom - settings.offset;
		    }

		    $("div.zoomdiv").css({"top": imageTop,"left": leftpos ,"width": settings.xzoom,"height": settings.yzoom});
		    $("div.zoomdiv").show();


					$(document.body).mousemove(function(e){

				    var bigwidth = $(".bigimg").get(0).offsetWidth;

				    var bigheight = $(".bigimg").get(0).offsetHeight;

				    var scaley ='x';

				    var scalex= 'y';


				    if(isNaN(scalex)|isNaN(scaley)){

				    var scalex = Math.round(bigwidth/imageWidth) ;

				    var scaley = Math.round(bigheight/imageHeight);

				    }

					mouse = new MouseEvent(e);


					scrolly = mouse.y - imageTop - ($("div.zoomdiv").height()*1/scaley)/2 ;
					$("div.zoomdiv").get(0).scrollTop = scrolly * scaley  ;

					$("#y").get(0).innerHTML = scrolly + " | " + scrolly * scaley;
					$("#imgy").get(0).innerHTML = imageTop + " | " + mouse.y;

				    scrollx = mouse.x - imageLeft - ($("div.zoomdiv").width()*1/scalex)/2 ;
					$("div.zoomdiv").get(0).scrollLeft = scrollx * scalex ;

					$("#x").get(0).innerHTML = scrollx + " | " + scrollx * scalex;
					$("#imgx").get(0).innerHTML = imageLeft + " | " + mouse.x;

					$("#isie").get(0).innerHTML = ie;
					
					var view_width = imageWidth/scalex;
					var view_height = imageHeight/scaley;

					$("#view").css({"display":"block", "width":view_width, "height":view_height, "z-index":"99"});
					
					$("#lim_top").get(0).style.height = imageHeight;
					$("#lim_top").get(0).style.width = imageWidth;
					$("#lim_left").get(0).style.height = imageHeight;
					$("#lim_left").get(0).style.width = imageWidth;
					$("#lim_bottom").get(0).style.height = imageHeight;
					$("#lim_bottom").get(0).style.width = imageWidth;
					$("#lim_right").get(0).style.height = imageHeight;
					$("#lim_right").get(0).style.width = imageWidth;
	
					/* mouse top */
					if (mouse.y-view_height/2 <= imageTop) {
						$("#view").get(0).style.top = $("img.jqzoom").get(0).offsetTop;
						$("#lim_top").height(1);
						$("#lim_bottom").height(imageHeight-view_height);
						$("#lim_bottom").get(0).style.top	= imageTop+view_height+1;
						$("#lim_left").get(0).style.top		= imageTop+1;
						$("#lim_right").get(0).style.top	= imageTop+1;
					}
					/* mouse buttom */
					else if(mouse.y+view_height/2 >= imageTop+imageHeight) {
						$("#view").get(0).style.top = $("img.jqzoom").get(0).offsetTop+imageHeight-view_height-1;
						$("#lim_top").height(imageHeight-view_height);
						$("#lim_bottom").height(1);
						$("#lim_bottom").get(0).style.top	= imageTop+imageHeight-1;
						$("#lim_left").get(0).style.top		= imageTop+imageHeight-(view_height);
						$("#lim_right").get(0).style.top	= imageTop+imageHeight-(view_height);
					}
					else {
						$("#view").get(0).style.top = mouse.y-view_height/2;
						$("#lim_top").height(mouse.y-imageTop-view_height/2);
						$("#lim_bottom").height(imageTop+imageHeight-mouse.y-view_height/2);
						$("#lim_bottom").get(0).style.top	= mouse.y+view_height/2;
						$("#lim_left").get(0).style.top		= mouse.y-view_height/2;
						$("#lim_right").get(0).style.top	= mouse.y-view_height/2;						
					}

					/* mouse right */
					if(mouse.x+view_width/2 >= imageLeft+imageWidth) {
						$("#view").get(0).style.left = $("img.jqzoom").get(0).offsetLeft+imageWidth-view_width-1;
						$("#lim_left").width(imageWidth-view_width);
						$("#lim_right").width(1);
						$("#lim_right").get(0).style.left = imageLeft+imageWidth-1;
					}
					/* mouse left */
					else if (mouse.x-view_width/2 <= $("img.jqzoom").get(0).offsetLeft) {
						$("#view").get(0).style.left = $("img.jqzoom").get(0).offsetLeft;
						$("#lim_left").width(1);
						$("#lim_right").width(imageWidth-view_width);
						$("#lim_right").get(0).style.left = imageLeft+view_width;
					}
					else {
						$("#view").get(0).style.left = mouse.x-view_width/2;
						$("#lim_left").width(mouse.x-imageLeft-view_width/2);
						$("#lim_right").width(imageWidth-(mouse.x-imageLeft+view_width/2));
						$("#lim_right").get(0).style.left = mouse.x+view_width/2;						
					}

					$("#lim_left").get(0).style.left = imageLeft;
					$("#lim_right").height(view_height);
					$("#lim_left").height(view_height);
					$("#lim_top").get(0).style.top = imageTop;
					$("#lim_top").get(0).style.left = imageLeft;
					$("#lim_bottom").get(0).style.left = imageLeft;	

				    });
										

		    },function(){
               $("img.jqzoom").attr("alt",noalt);
		       $("div.zoomdiv").hide();
		       $(document.body).unbind("mousemove");
		       $("div.zoomdiv").remove();
			   $("#view").remove();
			   $("#lim_top").remove();
			   $("#lim_left").remove();
			   $("#lim_right").remove();
			   $("#lim_bottom").remove();
		    });

		}


})(jQuery);

function MouseEvent(e) {
	this.x = e.pageX
	this.y = e.pageY
}

function getOffsetTop(obj) {
	var top = obj.offsetTop;
	while(1) {
		if(obj.offsetParent != null) {
			top+= obj.offsetParent.offsetTop;
			obj = obj.offsetParent;
		}
		else
			break;
	}
	return top;
}
function getOffsetLeft(obj) {
	var top = obj.offsetLeft;
	while(1) {
		if(obj.offsetParent != null) {
			top+= obj.offsetParent.offsetLeft;
			obj = obj.offsetParent;
		}
		else
			break;
	}
	return top;
}

var w3c=(document.getElementById)? true:false;
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var ie5=(w3c && ie)? true : false;
var ns6=(w3c && (navigator.appName=="Netscape"))? true: false;
var op8=(navigator.userAgent.toLowerCase().indexOf("opera")==-1)? false:true;

function Obj(o){
	return document.getElementById(o)?document.getElementById(o):o;
}

function GetXYWH(o){
	var nLt=0;
	var nTp=0;
	var offsetParent = o;
	while (offsetParent!=null && offsetParent!=document.body) {
		nLt+=offsetParent.offsetLeft;
		nTp+=offsetParent.offsetTop;
		if(!ns6){
			parseInt(offsetParent.currentStyle.borderLeftWidth)>0?nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
			parseInt(offsetParent.currentStyle.borderTopWidth)>0?nTp+=parseInt(offsetParent.currentStyle.borderTopWidth):"";
		}
		offsetParent=offsetParent.offsetParent;
		//alert(offsetParent.tagName);
	}
	this.left = nLt;
	this.top = nTp;
	this.width = o.offsetWidth;
	this.height = o.offsetHeight;
}