/*
 * 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 = 150;
        xOffset = 50;
		yOffset = 20;

		// 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 : "";

		// Get thumb source and replace it with the source of the preview...
        var preImg = $(this).children('img').attr('src');
		preImg = preImg.replace(/.jpg/,'pre.jpg');

		$("body").append("<p id='preview'><img src='"+ preImg +"' alt='Image preview' />"+ c +"</p>");


        if(e.clientY - xOffset < 0)
            xOffset = 100;
        else
            xOffset = 50

        if(e.clientY + 200 > $(window).height())
            xOffset = 350;
        else
            xOffset = 150;

        if(e.clientX + 450 > $(window).width())
            yOffset = -500;
        else
            yOffset = 20;

		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");

    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });
	$("a.preview").mousemove(function(e){


        if(e.clientY + xOffset > $(window).height())
            xOffset = -50;
        else
            xOffset = 50;

         if(e.clientY + 200 > $(window).height())
            xOffset = 350;
        else
            xOffset = 150;

        if(e.clientX + 450 > $(window).width())
            yOffset = -450;
        else
            yOffset = 20;

		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");

	});
};

