$(document).ready(function(){

  
  /* CONFIG */     
  //these 2 variable determine popup's distance from the cursor
  // you might want to adjust to get the right result    
  
  yOffset = 150;
  xOffset = 30;  
  
  /* 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='url preview' />"+ c +"</p>");                                                                
                $("#screenshot")
                        .css("top",(e.pageY - xOffset) + "px")
                        .css("left",(e.pageX + yOffset) + "px")
                        .fadeIn("fast");                                               
    },
        function(){
                this.title = this.t;   
                $("#screenshot").remove();
    }); 
        /**************************** Mousemovement *********************************************/
  /********** Track the mouse to prevent loading the image out of the viewport   **********/
  
  //keep track of mouse movement in an image and move preview element
  $("a.screenshot").mousemove(function(e){
        
    //width & height of the popup with the larger image triggered on hover.
    var elementHeight = $("#screenshot").outerHeight(true);
    var elementWidth = $("#screenshot").outerWidth(true); 
       
    //Size of the viewport, meaning the area of the page, that is completely visible.
    var winHeight = $(window).height(); 
    var winWidth = $(window).width();  
    
    //If the page has been scolled, addidtional calculation must be done.
    //Scroll position is the same as the number of pixels that are hidden from view above the scrollable area. 
    //If the scroll bar is at the very top, or if the element is not scrollable, this number will be 0.    
    var scrolledPixelTop = $(window).scrollTop();
    var scrolledPixelLeft = $(window).scrollLeft();  //a site should not be vertically scrollable, but maybe...    
  
    //calculate if image would be loaded outside 
    //the width that is avaliable for displaying, could be smaller that the actual image => offscreen
    var calcHeight = winHeight - e.pageY + yOffset + scrolledPixelTop;
    var calcWidth = winWidth - e.pageX - xOffset + scrolledPixelLeft;
    
    //set initial
    var xPosition = e.pageX;
    var yPosition = e.pageY;  
//    console.log('yPosition: ' + yPosition + ' calcHeight: ' + calcHeight + ' winHeight: ' + winHeight + ' elementHeight: ' + elementHeight);
//    console.log('xPosition: ' + xPosition + ' calcWidth: ' + calcWidth + ' winWidth: ' + winWidth + ' elementWidth: ' + elementWidth);
    
    if( calcHeight < elementHeight ) {
      var yPosition = winHeight - elementHeight + yOffset + scrolledPixelTop;    
      //console.log('NEW yPosition' + yPosition);      
    }    
      
    if( calcWidth < elementWidth ) {
      var xPosition = e.pageX - elementWidth - 2*xOffset + scrolledPixelLeft;     
      //console.log('NEW xPosition' + xPosition);      
    }       
   
//    console.log(' yPosition' + yPosition);
//    console.log(' xPosition' + xPosition);
    if($("#screenshot")) {    
    $("#screenshot")
       .css("top", (yPosition - yOffset) + "px")
       .css("left",(xPosition + xOffset) + "px");
    }
  }); //end mousemove 
  
}); //main function Drupal.behaviors end
