/*
 * 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 = 20;
		yOffset = 10;
		taskBarPad = 50;
		
		// 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 : "";
		
		$("body").append("<p id='preview'><img id='img_preview' src='"+ this.href +"' alt='Loading Image... ' />"+ c +"</p>");								 
		$('#img_preview').jScale({ls:'500px'});
		$("body").css("overflow:hidden");


		var x = e.pageX + xOffset;
		var page_width = $(document).width();
		var image_width = $("#preview").width()
		if(x+image_width>page_width){
			x=x-image_width-(xOffset*2);
		}

		var y = e.pageY + yOffset;
		var page_height = $(window).height()-taskBarPad;
		var image_height = $("#img_preview").height();
		var image_bot = image_height + y;
		if(image_bot>page_height) y=page_height-image_height;

		$("#preview")
			.css("top",(y) + "px")
			.css("left",(x) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
		$("body").css("overflow:");
    });	
	$("a.preview").mousemove(function(e){
		var x = e.pageX + xOffset;
		var page_width = $(window).width();
		var image_width = $("#img_preview").width()
		if(x+image_width+xOffset>page_width){
			x=x-image_width-(xOffset*2);
		}
		
		var y = e.pageY + yOffset;
		var page_height = $(window).height()-taskBarPad;
		var image_height = $("#img_preview").height();
		var image_bot = image_height + y;
		if(image_bot>page_height) y=page_height-image_height;

		/*
		$("#message").html(
			'<div style="position:absolute;left:0">'+
			"doc_height:"+$(document).height()+"<br/>win_height:"+$(window).height()+"<br/>image_height:"+image_height+"<br/>POS:"+y+"<br/>mouseY:"+e.pageY+
			//"<br/>document_width:"+page_width+"<br/>image_width:"+image_width+"<br/>X POS:"+x+"<br/>pageX:"+e.pageX+
			"</div>"
		);
		*/
		//$("#message").html("document_width:"+page_width+"<br/>image_width:"+image_width+"<br/>x:"+x+"<br/>pageX:"+e.pageX);
		$("#preview")
			.css("top",(y) + "px")
			.css("left",(x) + "px");
	});
	$("a.preview").click(function(e){
		e.preventDefault();				  
	});
};


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