/*
 * 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(){
	
	// on mouse hover	
	$("a.lightbox").hover(
		function(e){

			var parent_position = $("img",$(this)).offset();
			var parent_width = $("img",$(this)).outerWidth();
			var parent_height = $("img",$(this)).outerHeight();
			
			var attr_id = $(this).attr("data-preview-id");
			var attr_src = $(this).attr("data-preview-src");
			var attr_icon = $(this).attr("data-icon");
			var title = $(this).attr("title");;
			var attr_preview_width = $(this).attr("data-preview-width");
			var attr_preview_height = $(this).attr("data-preview-height");
			
			$("body").append("<div id='hover-image-preview-"+attr_id+"' class='hover-image-preview grey-9'><img width='"+attr_preview_width+"' height='"+attr_preview_height+"' src='"+attr_src+"' title='"+title+"' alt='"+title+"' /></div>");
			
			var image_container = $("#hover-image-preview-"+attr_id);	
			image_container.css("z-index", "1000");
						
			var initial_preview_width = image_container.outerWidth();
			if (initial_preview_width > 16) {
				initial_preview_width -= 16;
			}
			
			var c = '<div class="caption" style="width: '+initial_preview_width+'px;">';
			if (attr_icon && attr_icon != "") {
				c += '<div class="' + attr_icon + '"></div>';
			}
			if  (title && title != "") {
				c += '<div class="title white">' + title + '</div>';
			}
			c += '<div class="clearboth"></div>';
			c += '</div>';
			
			image_container.append(c);
			// console.log(c);
			// var c = (this.title != "") ? "<br/>" + this.title : "";
						
			var body_center_width = ($(window).width()/2);
			var body_center_height = ($(window).height()/2);
			var preview_width = image_container.outerWidth();
			var preview_height = image_container.outerHeight();
			
			// get hidden pixels above viewport			
			var hidden_height = $("html").scrollTop();
			
			// left or right position
			if (e.pageX < body_center_width) {
				image_container.css("left",(parent_position.left + parent_width + 8) + "px");
			}
			else {
				image_container.css("left",(parent_position.left - preview_width - 8) + "px");						
			}
			
			// top or bottom position
			if ( (e.pageY - hidden_height) < body_center_height ) {
				image_container.css("top",(parent_position.top) + "px");	
			}
			else {
				image_container.css("top",(parent_position.top + parent_height - preview_height) + "px");	
			}
			
			image_container.show();
			
//			$('#hover-image-preview').css({visibility: "visible"})
//			image_container.fadeIn("fast");						
			
//			console.log(e.pageY - hidden_height);
//			console.log(body_center_height);
//			console.log(preview_height);
//			console.log(e.pageY);
//			console.log(hidden_height);			
	    },
		function(){
	    	var attr_id = $(this).attr("data-preview-id");
	    	var image_container = $("#hover-image-preview-"+attr_id);
			image_container.remove();
	    }
	);
	
	// on mouse move 
	$("a.lightbox").mousemove(function(e){
		
	});	
	
};
