﻿var offsetxpoint=-40; //Customize x offset of tooltip
var offsetypoint=20; //Customize y offset of tooltip
var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;
var tipobj = document.getElementById("dhtmltooltip"); //document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : "";
var iTimerId = -1;
var nSrcElementLeft = -1, nSrcElementTop = -1;
var nSrcElementWidth = -1, nSrcElementHeight = -1;
var ctrlSrcElement = null;

function ietruebody()
{
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function keepSrcElementRect(ctrl)
{
    if( ctrl == null )
        return;
        
    ctrlSrcElement = ctrl;
    nSrcElementLeft = event.clientX - event.offsetX + ietruebody().scrollLeft;
    nSrcElementTop = event.clientY - event.offsetY + ietruebody().scrollTop;

    if( ctrl.offsetWidth != null )
    {
        nSrcElementWidth = ctrl.offsetWidth;
        nSrcElementHeight = ctrl.offsetHeight;
    }
    else
    {
        nSrcElementWidth = screen.width;
        nSrcElementHeight = screen.Height;
    }
}

function isPointInRect(iIntX, iIntY, iIntLeft, iIntTop, iIntWidth, iIntHeight)
{
    return ( iIntX >= iIntLeft  &&  iIntX <= (iIntLeft + iIntWidth - 1)
            && iIntY >= iIntTop  &&  iIntY <= (iIntTop + iIntHeight - 1)
            );
}

function ddrivetip(thetext)
{
//    var bSuppressToolTip = false;
//    var objDivSubmitProgress = getDotNetElementById("updp1");

    //if( event.type == "mouseover" )
    //    keepSrcElementRect(event.srcElement);

//    if( objDivSubmitProgress != null )
//        if( objDivSubmitProgress.style.display != "none"  )
//            bSuppressToolTip = true;
    tipobj = document.getElementById("dhtmltooltip");
    if( (ns6||ie))// && !bSuppressToolTip )
    {
        tipobj.style.width = "auto";
// removed because we use its own style (div)
//        if (typeof thewidth!="undefined") 
//            tipobj.style.width = thewidth + "px";
//        if (typeof thecolor!="undefined" && thecolor!="") 
//            tipobj.style.backgroundColor=thecolor;

        tipobj.innerHTML=thetext;
        if( iTimerId >= 0 )
            window.clearTimeout(iTimerId);
        enabletip=true;
        return false
    }
}

function positiontip(e)
{
    if (enabletip)
    {
        var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
        var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

        if( iTimerId >= 0 )
            window.clearTimeout(iTimerId);

        if( ctrlSrcElement != null && ctrlSrcElement.type != null)
        {
            if( ctrlSrcElement.type.substr(0, 6) == "select" )
            {
                if( isPointInRect(curX, curY
                                , nSrcElementLeft, nSrcElementTop
                                , nSrcElementWidth, nSrcElementHeight
                                )
                  )
                {
                    iTimerId = window.setTimeout("hideddrivetip();", 5000);
                }
                else
                {
                    iTimerId = window.setTimeout("hideddrivetip();", 100);
                    return;
                }
            }    
        }
        
        //Find out how close the mouse is to the corner of the window
        var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20;
        var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20;

        var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000    // original -1000
        var nXOffset = (ie? ietruebody().scrollLeft : window.pageXOffset);
        var nNewLeft;

        //if the horizontal distance isn't enough to accomodate the width of the context menu
        if (tipobj.offsetWidth > rightedge)
        {
            //move the horizontal position of the menu to the left by it's width
            if( ie )
                nNewLeft=nXOffset+event.clientX-(tipobj.offsetWidth-rightedge+40);
            else
                nNewLeft=nXOffset+e.clientX-(tipobj.offsetWidth-rightedge+40);
        }
        else if( curX < leftedge )
            nNewLeft=5;
        else
            //position the horizontal position of the menu where the mouse is positioned
            nNewLeft = curX+offsetxpoint;
        
        if( nNewLeft < nXOffset + 5 )
            nNewLeft = nXOffset + 5;
        tipobj.style.left=nNewLeft+"px"

        //same concept with the vertical position
        if (bottomedge<tipobj.offsetHeight)
            tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px";
        else
            tipobj.style.top=curY+offsetypoint+"px";
        tipobj.style.visibility = "visible";
    }
}

function hideddrivetip()
{
    if (ns6||ie)
    {
        enabletip=false;
        tipobj.style.visibility="hidden";
        tipobj.style.left="-1000px";
        tipobj.style.backgroundColor='';
        tipobj.style.width='';
    }
    iTimerId = -1;
}

document.onmousemove=positiontip

function getDotNetElementById(strServerSideId)
{
    var ctrlCollection = document.all;
    var i;
    strServerSideId = "_" + strServerSideId;
//removed    var strIdList = "";
    for( i = 0;  i < ctrlCollection.length;  i++ )
    {
//removed    strIdList += " " + ctrlCollection(i).id;
//removed  if( ctrlCollection(i).id.endsWith(strServerSideId) )
        if( stringEndsWidth(ctrlCollection(i).id, strServerSideId) )
            return ctrlCollection(i); 
    }
//removed    alert( strIdList);
    return null;
}
