/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		iWidth = 480;
		iHeight = 320;
		
		// 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).attr("loadurl") +"' alt='Image preview' />"+ c +"</p>");
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		var imgs = new Image();
 		imgs.src = $(this).attr("loadurl");
 		var rate = (iWidth / imgs.width < iHeight / imgs.height) ? iWidth / imgs.width : iHeight / imgs.height;
 		if (rate <= 1) {
             iiWidth = imgs.width * rate;
             iiHeight =  imgs.height * rate;
         } else {
         	iiWidth = imgs.width ;
            iiHeight =  imgs.height;
         }
         $("#preview").children('img').css('width',iiWidth)
			.css('height',iiHeight);
		var xOff;var yOff;
		
		if(e.pageX + yOffset + iiWidth  > window.screen.availWidth){
			xOff = (e.pageX + yOffset - iiWidth);
		}else{
			xOff = e.pageX + yOffset;
			
		}
		
		if(e.pageY - xOffset + iiHeight  > window.screen.availHeight){
			yOff = (e.pageY - xOffset - iiHeight);
		}else{
			yOff = e.pageY - xOffset;
			
		}
		
		$("#preview")
			.css("top",(yOff) + "px")
			.css("left",(xOff) + "px")
			.css('width',iiWidth)
			.css('height',iiHeight)
			.fadeIn("fast");				
	});			
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});
