/*
 * 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
 *
 *
 * 24.10.2010 by bUraK
 *
 * -- Fix --
 * If you move mouse over an element near the bottom of the page, the preview
 * window does not position in the visible area. Now everything works fine :)
 *
 * -- Changed --
 * The target image is stored in rel attribute now. Since I use href property
 * to link somewhere else :)
 *
 * http://www.burakg.com
 *
 */

this.imagePreview = function(){
	/* CONFIG */

		xOffset = 30;
		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.rel +"' alt='Image preview' />"+ c +"</p>");

                var top = (e.pageY - yOffset);
                if( $("#preview").height() + e.clientY > $(window).height() )
                    top = ($(document).scrollTop()+$(window).height()) - $("#preview").height()-20;

		$("#preview")
			.css("top", top+ "px")
			.css("left",(e.pageX + xOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });

	$("a.preview").mousemove(function(e){

                var top = (e.pageY - yOffset);
                if( $("#preview").height() + e.clientY > $(window).height() )
                    top = ($(document).scrollTop()+$(window).height()) - $("#preview").height()-20;
		$("#preview")
			.css("top",top + "px")
			.css("left",(e.pageX + xOffset) + "px");
	});

};


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