// modified to expand width of height of obj
// won't fit in remainder of window
//

var debug = 0
var defWidth = 500;

var prevTooltip;
function getWindowWidth() {
    
    if(window.innerWidth) {
        return window.innerWidth;
    } 
    
    return document.body.clientWidth
}

//trw, added 2.23.09
function getWindowHeight() {
    
    if(window.innerHeight) {
	return window.innerHeight;
    } 
    
    return document.body.clientHeight
}

function mouseX(e) {

    if(e.pageX) {
        return e.pageX;
    }
    
    return e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}

function mouseY(e) {
   
   if(e.pageY) {
        return e.pageY;
    } 
    
    return e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);    
}

function tooltip(e, o) {

    var windowWidth = getWindowWidth();
    var windowHeight = getWindowHeight();

if( 0 )
 alert("Window width=" + windowWidth + " height=" + windowHeight);

    o = document.getElementById(o);
    
    if(prevTooltip && prevTooltip != o) {
        prevTooltip.style.visibility = 'hidden';
    }

    if(o.style.visibility == 'visible') {
	o.style.width = defWidth; // trw, restore width, font
	o.style.fontSize = '11px';
        o.style.visibility = 'hidden';
    } else {
    
        if(o.offsetWidth) {
            ew = o.offsetWidth;
        } else if(o.clip.width) {
            ew = o.clip.width;
        }
    
	// trw, added height
	if(o.offsetHeight) {
	    eh = o.offsetHeight;
	} else if(o.clip.height) {
	    eh = o.clip.height;
        }

        y = mouseY(e) + 16;
        x = mouseX(e) - (ew / 4);

//alert("orig x=" + x + ", y=" + y);

        if (x < 2) {
            x = 2;
        } else if(x + ew > windowWidth) {
	    //x = windowWidth - ew - 4;
	    x = windowWidth - ew - 20;
        }


if(0)
alert("mouseY=" + y + ", eh=" + eh +
", windowHeight=" + windowHeight + ", e.pageY=" + e.pageY +
", e.clientY=" + e.clientY +
", document.documentElement.clientHeight=" + document.documentElement.clientHeight +
", window.pageYOffset=" + window.pageYOffset + "x=" + x +
", document.documentElement.scrollTop=" + document.documentElement.scrollTop +
", document.body.scrollTop " + document.body.scrollTop
 );

if( 1 ) {
 if( e.clientY + eh > windowHeight ) {
  //y1 = y;
  //y =  windowHeight - eh - 4;
  //y = document.documentElement.clientHeight - eh - 4;
  // y = document.documentElement.clientHeight - eh - 4;
  o.style.width = windowWidth - 40;
  o.style.fontSize = '10px';
  x = 8;
  //alert("x is now " + y + ", was " + y1);
  //alert("x is now " + x + ", o.style.width=" + o.style.width);
 }
}
        
        o.style.left = x + 'px';
        o.style.top = y + 'px';    
        
        o.style.visibility = 'visible';
        
        prevTooltip = o;
    }
}
