$.fn.extend({actual:function(a,j){var b,c,g,f,e,i,d,h;if(!this[a]){throw'$.actual => The jQuery method "'+a+'" you called is not exist';}g=$.extend({absolute:false,clone:false},j);c=this;d=g.clone===true?function(){c=c.filter(":first").clone().css({position:"absolute",top:-1000}).appendTo("body");}:function(){b=c.parents().andSelf().filter(":hidden");f=g.absolute===true?{position:"absolute",visibility:"hidden",display:"block"}:{visibility:"hidden",display:"block"};e=[];b.each(function(){var l={},k;for(k in f){l[k]=this.style[k];this.style[k]=f[k];}e.push(l);});};h=g.clone===true?function(){c.remove();}:function(){b.each(function(l){var m=e[l],k;for(k in f){this.style[k]=m[k];}});};d();i=c[a]();h();return i;}});


 
this.imagePreview = function(){	

	/* CONFIG */
		
		xOffset = -20;
		yOffset = 10;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
		yz=e.pageY - xOffset;
		xz=e.pageX + yOffset;
		if (xz+520>$(window).width()) xz=$(window).width()-520;
		if (xz<=0) xz=0;
		if (window.innerHeight/2<=e.screenY)
		{ 
			// $("#preview").css({'position':'absolute','visibility':'hidden','display':'block'});
			var hgh=$("#preview").actual('height');
			// $("#preview").css({'position':'static','visibility':'visible','display':'none'});
			yz= e.pageY - hgh + xOffset;		 
		}
		$("#preview")
			.css("top",(yz) + "px")
			.css("left",(xz) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		yz=e.pageY - xOffset;
		xz=e.pageX + yOffset;
		if (xz+520>$(window).width()) xz=$(window).width()-520;
		if (xz<=0) xz=0;
		if (window.innerHeight/2<=e.screenY) 
			yz= e.pageY - 350 + xOffset;
		$("#preview")
			.css("top",yz + "px")
			.css("left",xz + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});


