/*
 * Url 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.screenshotPreview = function(){	

		/* CONFIG */
			
			xOffset = 10;
			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){
										 
										 
			if( $("#screenshot") ) {
				$("#screenshot").remove();
			}
						
			var ourProperty = $(this).find(".thumb");
			
			
			var filename = ourProperty.attr("src");
			var dot = filename.lastIndexOf(".");
			var ext = filename.substr(dot,filename.length).toLowerCase(); 
			
			if( ext == ".jpg" || ext == ".gif" || ext == ".png" || ext == ".jpeg" ) {

				this.t = ourProperty.attr("title");
				ourProperty.attr("title","");	
				var c = (this.t != "") ? "<br/>" + this.t : "";
				$("body").append("<p id='screenshot'><img src='"+ filename +"' alt='url preview' /><span>"+ c +"</span></p>");	


				$("#screenshot")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX + yOffset) + "px")
					.fadeIn("fast");
			};
		},
		function(){
			var ourProperty = $(this).find(".thumb");
			ourProperty.attr("title", this.t);	
			$("#screenshot").remove();
		});	
		
		$("a.screenshot").mousemove(function(e){
											 
			var posY;
			
			if (e.pageY - $(window).scrollTop() + $('#screenshot').height() >= $(window).height() ) {
				posY = $(window).height() + $(window).scrollTop() - $('#screenshot').height() - 15 ;
			} else {
				posY = e.pageY - 15;
			};
			
			$("#screenshot").css("top",(posY) + "px").css("left",(e.pageX + 15) + "px");
 
		
											 

				
		});	
	
};

// starting the script on page load
$(document).ready(function(){
	$(".thumb").parent().addClass("screenshot");
	screenshotPreview();
});