jQuery Placement help

I want to show a div relative to a link. For this i want to also calculate the area on right and bottom and if not enough space then move the div on the left/right (default is right) side of the click or move it little upwards so that entire width and height of the div is visible.

Simply putting the div next to the click

var offset = $(this).offset();



var offsetCurrentObj     = $currentObj.offsetCurrentObj();

Should be:

var offsetCurrentObj     = $currentObj.offset();

Here is my first stab at it… There is still a lot of room to improve the code, as soon as that is done will repost.

//current object  
    var $currentObj         = $(this);
    var offsetCurrentObj     = $currentObj.offsetCurrentObj();
    //get the current objects top and left position
      var top                 = offsetCurrentObj.top;
      var left                 = offsetCurrentObj.left;

    //message object
    var $messageObj         = $("#div");
    //message object width and height
    var totalMessageHeight     = $messageObj.outerHeight();
      var totalMessagewidth     = $messageObj.outerWidth();        
    //get the screen width and screen bottom position
    var screenWidth         = $(window).width();
    var screenBottom         = $(window).scrollTop() + $(window).height();

    //check the right side of the screen
    if (left + $currentObj.width() + totalMessagewidth > screenWidth) 
        left = left - totalMessagewidth;
        left = left + $currentObj.width();
    // check the bottom of the screen
    if (top + $currentObj.height() + totalMessageHeight > screenBottom)
        top = top - $messageObj.outerHeight() + $currentObj.height(); 
    // apply top and left and show the message object
                    "top": top,
                    "left": left