/*
tooltip.js    
*/

if (typeof tooltipWidget != "object") { var tooltipWidget = {}; }

//tooltip object
tooltipWidget.tooltip = function(id)
{
    this.id = id;
    
    strText = "Lorem ipsum dolor sit amet.";
       
    defaultStyle = "border: 1px solid black; padding: 1px; position: absolute; cursor: default; background-color: #FFFFCC; font-family: arial,helvetica; font-size: 9pt; display: block; z-index: 10000; height: auto; width: 205px; left: 711px; top: 80px";

    var tooltipField = document.getElementById(id);    
        
    // Register event handlers
    // onblur
    //var oldEventCode = (tooltipElement.onblur) ? tooltipElement.onblur : function () {};
    //tooltipElement.onblur = function () {oldEventCode(); _tooltipObj.hide(id)};  
    tooltipField.onblur = function () {_tooltipObj.hide(id)};  

    this.show = function(type)
    {
        if(!_tooltipElement)    /* If not yet created */
        {        
            _tooltipElement = document.createElement('DIV');
            //_tooltipElement.style.cssText = defaultStyle;
            _tooltipElement.id = '_tooltipElement';     
            document.body.appendChild(_tooltipElement);


        }        
        //What type of tooltip it is
      
        /* LDT: The boss says we don't need this no more
            if (type == 0)
            {
                _tooltipElement.className = '_tooltipElement_info';     
            }
            else
            {
                _tooltipElement.className = '_tooltipElement_error';     
            }
        */

        //Show tooltip
        _tooltipElement.style.display = "block";

        //Show Tooltip Text
        _note = tooltipField.getAttribute("note");
        //IE return an empty value instead of null
        if (_note != null) //&& _element_id != '')
        {        
           _tooltipElement.innerHTML = "<div class='tooltip-pointer'>&nbsp;</div><div class='tooltip_note'>" + _note + "</div>";
        }
        
        //Position tooltip
        this.positionTooltip(tooltipField);
    }
    
    this.hide = function(id)
    {
        _tooltipElement.style.display = "none";
    }
    
    this.positionTooltip = function(element)
    {
        //var leftPos = (this.getLeftPos(element));// + element.offsetWidth);
        var leftPos = (this.getLeftPos(element) + element.offsetWidth + 12); // LDT: 3px space between the element and the tooltip
        //var topPos = (this.getTopPos(element) + element.offsetHeight);
        
        //var heighthalfway = Math.ceil(element.offsetHeight / 2) // LDT: half the height of the element
        //var topPos = (this.getTopPos(element)  + heighthalfway); 
        var topPos = (this.getTopPos(element)  - 7)

        _tooltipElement.style.left = leftPos + 'px';
        _tooltipElement.style.top = topPos + 'px';  

    }
    
    this.getLeftPos= function(element)
    {
        var returnValue = element.offsetLeft;
        //var returnValue = element.offsetWidth;
        while((element = element.offsetParent) != null)
        {
            if(element.tagName!='HTML') returnValue += element.offsetLeft;
        }
        return returnValue;
    }    

    this.getTopPos= function(element)
    {       
        var returnValue = element.offsetTop;
        //var returnValue = element.offsetHeight;
        while((element = element.offsetParent) != null)
        {
            if(element.tagName!='HTML')returnValue += element.offsetTop;
        }
    return returnValue;
    }
}

var _tooltipElement = false;

function init_tooltip(element, type)
{
    _element_id = element.getAttribute("id");
    //IE return an empty value instead of null
    if (_element_id != null && _element_id != '')
    {
        _tooltipObj = new tooltipWidget.tooltip(_element_id);
        _tooltipObj.show(type);
    }
    else
    {
        alert('ToolTip Error: Element is missing an "id" attribute');
    }
}