function OverDiv()
{
    // How many pixels to the right/left of the cursor to show the popup
    // Values between 3 and 12 are best
    this.offsetx = 0;
    // How many pixels to the below the cursor to show the popup
    // Values between 3 and 12 are best
    this.offsety = 10;

    this.ns4 = (document.layers)? true:false
    this.ie4 = (document.all)? true:false

    // Microsoft Stupidity Check
    if (this.ie4) {
        if (navigator.userAgent.indexOf('MSIE 5') > 0) {
            this.ie5 = true;
        } else {
            this.ie5 = false;
        }
    } else {
        this.ie5 = false;
    }

    this.x = 0;
    this.y = 0;
    this.snow = 0;
    this.sw = 0;
    this.cnt = 0;
    this.dir = 1;
    this.currentWidth = 100;
    
    this.hiddenElements = new Array();
    this.hiddenElements.length = 0;
    
    this.mouseMove = function(e)
    {
        if (overDiv.ns4) { overDiv.x = e.pageX; overDiv.y = e.pageY; }else
        if (overDiv.ie4) { overDiv.x = event.x; overDiv.y = event.y; }else
        if (overDiv.ie5) { overDiv.x = event.x + document.body.scrollLeft; y = event.y + document.body.scrollTop; }
        else { overDiv.x = e.pageX; overDiv.y = e.pageY; }
    }
  document.onmousemove = this.mouseMove;    
    this.getPopupDiv = function()
    {
        var objDiv = document.getElementById( 'overDiv' );
        
        if ( objDiv != null && objDiv != 'undefined' )
            return objDiv;
        else
        {
            objDiv = document.createElement('div');
            objDiv.id = 'overDiv';
            objDiv.style.visibility = 'hidden';
            objDiv.style.position = 'absolute';
            document.body.appendChild(objDiv);
            return objDiv;
        }
    }
    
    this.GetAbsolutePos = function(el)
    {
        var SL = 0, ST = 0;
        var is_div = /^div$/i.test(el.tagName);
        if ( is_div && el.scrollLeft )
            SL = el.scrollLeft;
        if ( is_div && el.scrollTop )
            ST = el.scrollTop;
        var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
        if ( el.offsetParent )
        {
            var tmp = this.GetAbsolutePos(el.offsetParent);
            r.x += tmp.x;
            r.y += tmp.y;
        }
        return r;
    } 

/*
    if ((this.ns4) || (this.ie4))
    {
        document.onmousemove = this.mouseMove;
        if (this.ns4) document.captureEvents(Event.MOUSEMOVE);
    }
*/
}

var overDiv = new OverDiv();

// Public functions to be used on pages.

OverDiv.selectVariant = function( idToStore, textToStore )
{
    document.getElementById( idToStore ).value = textToStore;
    OverDiv.killEvent();
    OverDiv.hidePopupDiv();
}

// Caption popup
//OverDiv.showPopupDiv = function(idToLink, width, idText)
OverDiv.ShowPopupDiv = function(idToLink, width, idToText, position)
{
    OverDiv.hidePopupDiv();

    var controlToLink = document.getElementById( idToLink );
    var controlToText = document.getElementById( idToText );
    
    overDiv.dir = position;
    
    if (position == 1) // right
        overDiv.offsety = controlToLink.offsetHeight;
        
    //overDiv.offsety = 5;
    var text = controlToText.innerHTML;
//    txt = "<table onclick='OverDiv.killEvent();' width='"+width+"' border='0' cellpadding='2' cellspacing='1' bgcolor='#999966'><tr><td>"+text+"</td></tr></table>";
    txt = "<table onclick='OverDiv.killEvent();' border='0' cellpadding='1' cellspacing='0' bgcolor='#999966'><tr><td>"+text+"</td></tr></table>";
    OverDiv.layerWrite(txt);
  
    OverDiv.disp( controlToLink );
    OverDiv.killEvent();
    OverDiv.addEvent( document, 'click', OverDiv.hidePopupDiv );
    OverDiv.HideShowCovered( overDiv.getPopupDiv(), 'hidden' );
}

OverDiv.mailToEscape = function( mailTo )
{
    location.href = 'mailto:' + mailTo;
    OverDiv.killEvent();
}

OverDiv.killEvent = function()
{
    if ( window.event )
    {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    } 
}

OverDiv.HideShowCovered = function( el, vis )
{
    var tags = new Array("applet", "iframe", "select");

    p = overDiv.GetAbsolutePos(el);
    var EX1 = p.x;
    var EX2 = el.offsetWidth + EX1;
    var EY1 = p.y;
    var EY2 = el.offsetHeight + EY1;
    
    for (var i=0;i<overDiv.hiddenElements.length;i++)
    {
        var cc = overDiv.hiddenElements[i];
            p = overDiv.GetAbsolutePos(cc);
            var CX1 = p.x;
            var CX2 = cc.offsetWidth + CX1;
            var CY1 = p.y;
            var CY2 = cc.offsetHeight + CY1;
            if (((CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)))
                cc.style.visibility = 'visible';
    }
    overDiv.hiddenElements.length = 0;
    for (var k = tags.length; k > 0; )
    {
        var ar = document.getElementsByTagName(tags[--k]);
        var cc = null;

        for (var i = ar.length; i > 0;)
        {
            cc = ar[--i];

            p = overDiv.GetAbsolutePos(cc);
            var CX1 = p.x;
            var CX2 = cc.offsetWidth + CX1;
            var CY1 = p.y;
            var CY2 = cc.offsetHeight + CY1;
            if (!((CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)))
            {
                if (vis == 'hidden')
                    overDiv.hiddenElements[overDiv.hiddenElements.length] = cc;
                cc.style.visibility = vis;
            }
        }
    }
        
}

// Clears popups if appropriate
OverDiv.hidePopupDiv = function(){
    if (overDiv.cnt >= 1) { overDiv.sw = 0 };
     {
        if (overDiv.sw == 0) {
            overDiv.snow = 0;
            OverDiv.hideObject(overDiv.getPopupDiv().style);
        } else {
            overDiv.cnt++;
        }
    }
    OverDiv.removeEvent( document, 'click', OverDiv.hidePopupDiv );
    OverDiv.HideShowCovered( overDiv.getPopupDiv(), 'visible' );
    
    // show all items 
    for (var i=0;i<overDiv.hiddenElements.length;i++)
    {
        var cc = overDiv.hiddenElements[i];
        cc.style.visibility = 'visible';
    }
    
}

OverDiv.addEvent = function( el, evname, func )
{
    if (el.attachEvent) { // IE
        el.attachEvent("on" + evname, func);
    } else if (el.addEventListener) { // Gecko / W3C
        el.addEventListener(evname, func, true);
    } else { // Opera (or old browsers)
        el["on" + evname] = func;
    }
}

OverDiv.removeEvent = function( el, evname, func )
{
    if (el.detachEvent) { // IE
        el.detachEvent("on" + evname, func);
    } else if (el.removeEventListener) { // Gecko / W3C
        el.removeEventListener(evname, func, true);
    } else { // Opera (or old browsers)
        el["on" + evname] = null;
    }
}

// Non public functions. These are called by other functions etc.
/*
ComboBox.createTable = function( variants )
{
    var result = "<table width='100%' border='1' style=\"border-collapse:collapse;\">";
    for( var i = 0; i < variants.length; i++ )
    {
        result += "<tr style=\"cursor:hand;\" onclick=\"ComboBox.selectVariant( '" + variants[i].IdToStore + "', '" + variants[i].Value + "' );\">";
        result += "<td>" + variants[i].Text + "</td></tr>";
    }
    result += "</table>";
    return result;
}
*/

// Common calls

OverDiv.disp = function ( controlToLink ) {
    {
        if (overDiv.snow == 0)  {
      
            if (overDiv.dir == 1) { // Right
                var position = overDiv.GetAbsolutePos( controlToLink );
                OverDiv.moveTo( overDiv.getPopupDiv().style, position.x+overDiv.offsetx, position.y+overDiv.offsety );
            }
            if (overDiv.dir == 0) { // Moving
                OverDiv.moveTo( overDiv.getPopupDiv().style, overDiv.x+overDiv.offsetx+document.body.scrollLeft, overDiv.y+overDiv.offsety+document.body.scrollTop + 20);
            }
      
            OverDiv.showObject(overDiv.getPopupDiv().style);
            overDiv.snow = 1;
        }
    }
// Here you can make the text goto the statusbar.
}

OverDiv.move = function ( controlToLinkID ) {
    overDiv.snow = 0;
    OverDiv.disp(document.getElementById(controlToLinkID));
    OverDiv.HideShowCovered( overDiv.getPopupDiv(), 'hidden' );
}

// Writes to a layer
OverDiv.layerWrite = function (txt) {
    if (overDiv.ns4) {
        var lyr = overDiv.getPopupDiv().document;
        lyr.write(txt);
        lyr.close();
    } else {
        overDiv.getPopupDiv().innerHTML = txt;
    }
}

// Make an object visible
OverDiv.showObject = function (obj) {
    if (overDiv.ns4) obj.visibility = "show";
    else if (overDiv.ie4) obj.visibility = "visible";
        else obj.visibility = "visible";
    }
// Hides an object
    OverDiv.hideObject = function (obj) {
    if (overDiv.ns4) obj.visibility = "hide";
    else if (overDiv.ie4) obj.visibility = "hidden";
        else obj.visibility = "hidden";
}

// Move a layer
OverDiv.moveTo = function (obj, xL, yL) {
    obj.left = xL;
    obj.top = yL;
}
