/*
 * Image preview script 
 * powered by $ (http://www.$.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * edited by Jan Chodura (http://www.creactive.cz)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-$
 *
 */

this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
	
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.previewImage").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='previewImage'><img src='"+ this.rel +"' alt='Image preview' />"+ c +"</p>");								 
		
		$("#previewImage")
			.fadeIn("fast");					
    },
	function(){
		this.title = this.t;	
		$("#previewImage").remove();
    }).mousemove(function(e){		
		var ttw = $("#previewImage").width();
		var tth = $("#previewImage").height();
		var wscrY = $(window).scrollTop();
		var wscrX = $(window).scrollLeft();
		var curX = (document.all) ? event.clientX + wscrX : e.pageX;
		var curY = (document.all) ? event.clientY + wscrY : e.pageY;
		var winX = $(window).width();
		var winY = $(window).height();
				
		var realCurY = curY-wscrY;
		var realCurX = curX-wscrX;
		var tttop = wscrY;
		var ttleft = wscrX+realCurX+xOffset;

		if (realCurY+tth > winY) {
			tttop += realCurY - (realCurY+tth-winY+(2*yOffset));
		} else {
			tttop += realCurY-(2*yOffset);
			if (wscrY >= tttop) {
				tttop += (wscrY - tttop + yOffset);
			} else if (tttop-wscrY < yOffset) {
				tttop += yOffset - (tttop-wscrY);
			}
		}
				
		if (winX / (ttw+xOffset) > 2) {
			if (realCurX + (2*xOffset) + ttw >= winX) {
				ttleft -= (xOffset+ttw+xOffset)+yOffset;
			}
		}
			
		$("#previewImage").css('top', tttop + 'px').css('left', ttleft + 'px');
	});
		
	$("div.previewImage").bind("mouseenter",function(e){
		this.t = this.title;
		this.title = "";
		$("div.previewImage a").each(function(){
			this.aTitle = this.title;
			this.title = '';
		});
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='previewImage'><img src='"+ this.lang +"' alt='Image preview' />"+ c +"</p>");								 
		
		$("#previewImage")
			.fadeIn("fast");
	}).bind("mouseleave",function(){
		this.title = this.t;
		$("div.previewImage a").each(function(){
			this.title = this.aTitle;
		});		
		$("#previewImage").remove();
	}).mousemove(function(e){
		var ttw = $("#previewImage").width();
		var tth = $("#previewImage").height();
		var wscrY = $(window).scrollTop();
		var wscrX = $(window).scrollLeft();
		var curX = (document.all) ? event.clientX + wscrX : e.pageX;
		var curY = (document.all) ? event.clientY + wscrY : e.pageY;
		var winX = $(window).width();
		var winY = $(window).height();
				
		var realCurY = curY-wscrY;
		var realCurX = curX-wscrX;
		var tttop = wscrY;
		var ttleft = wscrX+realCurX+xOffset;

		if (realCurY+tth > winY) {
			tttop += realCurY - (realCurY+tth-winY+(2*yOffset));
		} else {
			tttop += realCurY-(2*yOffset);
			if (wscrY >= tttop) {
				tttop += (wscrY - tttop + yOffset);
			} else if (tttop-wscrY < yOffset) {
				tttop += yOffset - (tttop-wscrY);
			}
		}
				
		if (winX / (ttw+xOffset) > 2) {
			if (realCurX + (2*xOffset) + ttw >= winX) {
				ttleft -= (xOffset+ttw+xOffset)+yOffset;
			}
		}
			
		$("#previewImage").css('top', tttop + 'px').css('left', ttleft + 'px');
	});				
};

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