/* Image preview script
 * powered by jQuery (http://www.jquery.com)
 *
 * written by Alen Grakalic (http://cssglobe.com)
 * extended by Urs Weiss
 *
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

this.imagePreview = function(){
	xOffset = 10;
	yOffset = 30;

	$("a.tx-rwdexhibitions-preview").hover(function(e){
		$this = $(this);
		$this.href = this.href;
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";

		$this.children("img").spinner();

		$.loadImages([$this.href], function() {
			$this.children("img").spinner('remove');
			$("body").append("<p id=\"tx-rwdexhibitions-preview\"><img src=\""+ $this.href +"\" alt=\"Image preview\" />"+ c +"</p>");
			$("#tx-rwdexhibitions-preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");

			// Get with of preview image and set preview container width
			$("#tx-rwdexhibitions-preview").children("img").load(function () {
				setTimeout(function() {
					var imgWidth = $("#tx-rwdexhibitions-preview").children("img").width();
					$("#tx-rwdexhibitions-preview").css("width", imgWidth + "px");
				}, 0);
			});
		})

   	},

	function(){
		this.title = this.t;
		$("#tx-rwdexhibitions-preview").remove();
	});


	$("a.tx-rwdexhibitions-preview").mousemove(function(e){
		var windowSize = getWindowSize();
		var popupSize = getPopupSize();

		if (windowSize.width + windowSize.scrollLeft < e.pageX + popupSize.width + xOffset){
			$("#tx-rwdexhibitions-preview").css("left", e.pageX - popupSize.width - xOffset);
		} else {
			$("#tx-rwdexhibitions-preview").css("left", e.pageX + xOffset);
		}
		if (windowSize.height + windowSize.scrollTop < e.pageY + popupSize.height + yOffset){
			$("#tx-rwdexhibitions-preview").css("top", e.pageY - popupSize.height - yOffset);
		} else {
			$("#tx-rwdexhibitions-preview").css("top", e.pageY + yOffset);
		}

	});

	function getPopupSize() {
		return {
			width: $("#tx-rwdexhibitions-preview").width(),
			height: $("#tx-rwdexhibitions-preview").height()
		};
	}

	function getWindowSize() {
		return {
			scrollLeft: $(window).scrollLeft(),
			scrollTop: $(window).scrollTop(),
			width: $(window).width(),
			height: $(window).height()
		};
	}

};

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

