this.imagePreview = function(){	
	xOffset = 10;
	yOffset = 30;	
	
	imgDiv = 2;

	
  $("a.preview").hover(function(e){  	
  	var img = new Image();		
  	$("#preview-image").remove();
  	
  	$("body").append('<div id="preview-image" style="display: none; position: absolute; z-index: 20000000;"></div>');  	
  	ptop = (e.pageY - xOffset);
	  pleft = (e.pageX + yOffset);
  	var iml = this.href
  	$("#preview-image").html('');
  	$("#preview-image")  	
			.css("top", ptop + "px")
			.css("left", pleft + "px")
	
			.show();			
		$("#preview-image").addClass('loading');			
		
		$(img)		
		  .load(function(){
				$(this).hide();
				$("#preview-image")
				  .removeClass('loading')
				  .html('')
				  .append(this);
				if (img.width > $(window).width() && img.height > $(window).height()){					
					
					xDif = (img.width - $(window).width());					
					yDif = (img.width - $(window).height());
					if (xDif > yDif) {						
						img.width = ($(window).width()-($(window).width()/2));
					} else {
						
						img.height = ($(window).height() - ($(window).height()/2))
					}
				} else if (img.width > $(window).width()){					
					img.width = ($(window).width() - ($(window).width()/2))
				} else if (img.height > $(window).height()){
					
					img.height = ($(window).height() - ($(window).height()/2))
				} 
				// adjust the width and height attributes
			  
				$(this).fadeIn("fast");
				if (((ptop + $("#preview-image").height())-$(window).scrollTop()) > $(window).height()){
					// Find a placement where it won't fall below the window
		  		ptop = ($(window).height()-($("#preview-image").height())) + $(window).scrollTop();		  
				} 		
				if (ptop < $(window).scrollTop()){
					ptop = $(window).scrollTop();
				}						
				if ((pleft + $("#preview-image").width()) > $(window).width()){
					pleft = ($(window).width()-$("#preview-image").width());
					//pleft = $(window).width()-img.width;
				}		
				$("#preview-image")
					.css("top",ptop + "px")
					.css("left",pleft + "px");				
				
		 })
		 .attr('src', iml);		 
	}, function(){
		$("#preview-image").hide();
		$("#preview-image").remove();
  });
	
	$("a.preview").mousemove(function(e){
		ptop = (e.pageY - xOffset);
		pleft = (e.pageX + yOffset);		
		if (((ptop + $("#preview-image").height())-$(window).scrollTop()) > $(window).height()){
			// Find a placement where it won't fall below the window
		  ptop = ($(window).height()-($("#preview-image").height())) + $(window).scrollTop();
		  
		} 		
		if (ptop < $(window).scrollTop()){
			ptop = $(window).scrollTop();
		}		

		if ((pleft + $("#preview-image").width()) > $(window).width()){
			pleft = ($(window).width() - $("#preview-image").width());
			if ((e.pageX + xOffset) > pleft && (e.pageX + xOffset)  < (pleft + $("#preview-image").width())){
				ptop = e.pageY + yOffset;
			}			
			//pleft = $(window).width()-img.width;
		}
		$("#preview-image")
			.css("top",ptop + "px")
			.css("left",pleft + "px");
	});	
		
};

// starting the script on page load
$(document).ready(function(){
	imagePreview();
});
