this.imagePreview = function() {
	$(".preview").hover(function() {
		var gap = 5;

		$("#preview").remove();

		if ($(this).is('img'))
			var img = $(this);
		else
			var img = $(this).find("img");

		this.temptitle = this.title;
		this.title = "";

		var link = img.attr("src");
		link = link.replace("thumbsd", "previews");
		link = link.replace("thumbs", "previews");
		var output = "<div id='preview'><img src='" + link + "' alt='" + img.attr("alt") + "'>";
		if (this.temptitle)
			output += "<div class=\"caption\">" + this.temptitle + "</div>";
		output += "</div>"
		$("body").append(output);

		var imgpos = img.offset();

		if (((imgpos.left - $(window).scrollLeft()) * 2) < ($(window).width() - img.outerWidth())) {
			var x = imgpos.left + img.outerWidth() + gap;
			var xalign = "left";
		} else {
			var x = $(window).width() - imgpos.left + gap;
			var xalign = "right";
		}
		if (((imgpos.top - $(window).scrollTop()) * 2) < ($(window).height() - img.outerHeight())) {
			var y = imgpos.top;
			var yalign = "top";
		} else {
			var y = $(window).height() - imgpos.top - img.outerHeight();
			var yalign = "bottom";
		}

		$("#preview")
			.css(xalign, x + "px")
			.css(yalign, y + "px")
			.fadeIn("fast");
    },
	function() {
		this.title = this.temptitle;
		$("#preview").remove();
    });
	$("a.preview").click(function() {
		this.title = this.temptitle;
		$("#preview").remove();
    });
};

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

