var offsetfromcursorX=20; //Customize x offset of tooltip
var offsetfromcursorY=20; //Customize y offset of tooltip

var offsetdivfrompointerX=10; //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14; //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

var tooltip_id = "dhtmltooltip";

function create_tooltip_object () {
    var divelement = document.createElement('div');
    divelement.setAttribute('id', tooltip_id);
    document.body.appendChild(divelement);
    
    return divelement;
}

function ddrivetip(thetext, thewidth, thecolor){
    var tooltip_obj = document.getElementById("dhtmltooltip");
    if ( !tooltip_obj ) tooltip_obj = create_tooltip_object();
    
    if ( thewidth ) tooltip_obj.style.width = thewidth+"px";
    if ( thecolor ) tooltip_obj.style.backgroundColor = thecolor;
    if ( thetext  ) tooltip_obj.innerHTML = thetext;

    document.observe( "mousemove", positiontip );
    
    return;
}

var positiontip = function(e){
    var tooltip_obj = document.getElementById("dhtmltooltip");
    
    var curX = e.pointerX();
    var curY = e.pointerY();
    
    //Find out how close the mouse is to the corner of the window
    var winwidth=getWindowWidth();
    var winheight=getWindowHeight();
    
    var rightedge = winwidth-e.clientX-offsetfromcursorX;
    var bottomedge = winheight-e.clientY-offsetfromcursorY;
    
    var leftedge = -1000;
    if ( offsetfromcursorX < 0 ) leftedge = offsetfromcursorX * -1;
    
    var new_x_loc = 0;
    var new_y_loc = 0;
    //if the horizontal distance isn't enough to accomodate the width of the context menu
    if ( rightedge < tooltip_obj.offsetWidth ){
        //move the horizontal position of the menu to the left by it's width
        new_x_loc = curX - tooltip_obj.offsetWidth;
    }
    else if ( curX < leftedge ) {
        new_x_loc = 5;
    }
    else{
        //position the horizontal position of the menu where the mouse is positioned
        new_x_loc = curX + offsetfromcursorX - offsetdivfrompointerX;
    }
    
    //same concept with the vertical position
    if ( bottomedge < tooltip_obj.offsetHeight ){
        new_y_loc = curY - tooltip_obj.offsetHeight - offsetfromcursorY;
    }
    else {
        new_y_loc = curY + offsetfromcursorY + offsetdivfrompointerY;
    }
    
    tooltip_obj.style.left = new_x_loc + "px";
    tooltip_obj.style.top = new_y_loc + "px";
    tooltip_obj.style.visibility = "visible";
    
    return;
}

function hideddrivetip(){
    var tooltip_obj = document.getElementById("dhtmltooltip");
    
    tooltip_obj.style.visibility="hidden";
    tooltip_obj.style.left="-1000px";
    tooltip_obj.style.backgroundColor='';
    tooltip_obj.style.width='';
    
    document.stopObserving( "mousemove", positiontip );
    
    return;
}



function getWindowHeight(){
    var innerHeight;
	if (navigator.appVersion.indexOf('MSIE')>0) {
		innerHeight = document.body.clientHeight;
    } else {
		innerHeight = window.innerHeight;
    }
    return innerHeight;	
}
 
function getWindowWidth(){
    var innerWidth;
	if (navigator.appVersion.indexOf('MSIE')>0) {
		innerWidth = document.body.clientWidth;
    } else {
		innerWidth = window.innerWidth;
    }
    return innerWidth;	
}
