/*
 * Url preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * added to by Darin Ronne on 8-13-2010
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.screenshotPreview = function(){	
	/* CONFIG */
		
		xOffset = -12;
		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.screenshot").hover(function(e){        
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='' />"+ c +"</p>");
		
		MouseY = 	e.pageY;	//Mouse position relative to the Document
		WindowPos = $(window).scrollTop(); //Space remaining above the Window/Viewport
		WindowHeight = $(window).height(); //Height of the Window
		WindowMouseY = MouseY - WindowPos; // Subtract the space above the Viewport from the Mouse position to find the Mouse position relative to the Window
		
		RemainingSpace = WindowHeight - WindowMouseY; //The space between the mouse and the bottom of the Window
		var img = new Image();
    //img.src = this.rel;      
    img.onload = function() { 
      ImageHeight = img.height;
        
  		if (RemainingSpace > ImageHeight){ // If there is enough room for the image, place it below the link... 	                                    
    		$("#screenshot")
  				.css("top",(e.pageY - xOffset) + "px")
  				.css("left",(e.pageX + yOffset) + "px")
    			.fadeIn("fast");
  		} 
      else { // ...or else place it above the link
  			$("#screenshot")
  				.css("top",(e.pageY - (ImageHeight+22)) + "px")
  				.css("left",(e.pageX + yOffset) + "px")
    			.fadeIn("fast");
  		}
    };
    img.src = this.rel; //Down here for IE's sake
  },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
  });	
	
	$("a.screenshot").mousemove(function(e){
			updateImagePosition(e);
	});
	
	function updateImagePosition(e){
		MouseY = 	e.pageY;	//Mouse position relative to the Document
		WindowPos = $(window).scrollTop(); //Space remaining above the Window/Viewport
		WindowHeight = $(window).height(); //Height of the Window
		WindowMouseY = MouseY - WindowPos; // Subtract the space above the Viewport from the Mouse position to find the Mouse position relative to the Window
		
		RemainingSpace = WindowHeight - WindowMouseY; //The space between the mouse and the bottom of the Window
		//ImageHeight = 300; 
		ImageHeight = $("#screenshot img").height();
		if (RemainingSpace > ImageHeight){ // If there is enough room for the image, place it below the link...
			$("#screenshot")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		} else { // ...or else place it above the link
			$("#screenshot")
				//.css("top",(e.pageY - ImageHeight) + "px")
				.css("top",(e.pageY - (ImageHeight+22)) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		}
	};
	
};

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