/**
 * Title2MouseOver
 *
 * Convert all element title tags to javascript mouseover
 *
 */

function findPos(oObj) // Find absolute position of any DOM object.
{
    var curleft = curtop = 0;
    if (oObj.offsetParent)
    {
        do
        {
            curleft += oObj.offsetLeft;
            curtop += oObj.offsetTop;
        } while (oObj = oObj.offsetParent);

        return [curleft,curtop];
    }
    else return false;
}

function addLoadEvent(func) // Add new onLoad event safely without destroying any exisiting event
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }

}

function getElementsWithAttribute(oElm, strAttributeName)
{
    var arrElements = (document.all) ? document.all : oElm.getElementsByTagName("*");
    var arrReturnElements = new Array();
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++)
    {
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0)
        {
            arrReturnElements.push(oCurrent);
        }
    }
    return arrReturnElements;
}


t2m_mmovehander = function(evt) // MouseMove handler for t2m
{
    if (!evt) var evt = window.event;

    if (evt.pageX || evt.pageY)
    {
        posx = evt.pageX;
        posy = evt.pageY;
    }
    else if (evt.clientX || evt.clientY)
    {
        posx = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    var pos=[posx,posy];

    if (window.innerWidth || window.innerHeight) var docwidth = window.innerWidth;
    if (document.body.clientWidth || document.body.clientHeight) var docwidth = document.body.clientWidth;

    var overhalf = pos[0]>(docwidth/2);

    var t2mpopupbox = document.getElementById('t2mpopupbox');
    if(t2mpopupbox)
    {
        if(overhalf) pos[0]-= (t2mpopupbox.offsetWidth+20);

        t2mpopupbox.style.position = 'absolute';
        t2mpopupbox.style.left = (pos[0]+15)+'px';
        t2mpopupbox.style.top = (pos[1]+10)+'px';
    }
}

t2m_moverhander = function(that, evt) // MouseOver handler for t2m
{
    if (!evt) var evt = window.event;

    if (evt.pageX || evt.pageY)
    {
        posx = evt.pageX;
        posy = evt.pageY;
    }
    else if (evt.clientX || evt.clientY)
    {
        posx = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }

    var pos=[posx,posy];

    if (window.innerWidth || window.innerHeight) var docwidth = window.innerWidth;
    if (document.body.clientWidth || document.body.clientHeight) var docwidth = document.body.clientWidth;

    var overhalf = pos[0]>(docwidth/2);

    var t2mpopupbox = document.getElementById('t2mpopupbox');
    if(!t2mpopupbox)
    {
        t2mpopupbox = document.createElement('div');
        t2mpopupbox.setAttribute('id', 't2mpopupbox');

        document.body.appendChild(t2mpopupbox);
    }

    t2mpopupbox.style.display = 'none';

    t2mpopupbox.innerHTML = '<div class="title_popup_box"><div class="title_popup_outer"><div class="title_popup_inner">' + that.title + '</div></div></div>';

    t2mpopupbox.style.textAlign = 'left';
    t2mpopupbox.style.position = 'absolute';

    if(overhalf) pos[0]-= (t2mpopupbox.offsetWidth+20);

    t2mpopupbox.style.left = (pos[0]+15)+'px';
    t2mpopupbox.style.top = (pos[1]+10)+'px';

    if(that.title)
    {
        that.origtitle = that.title;
        that.title = '';
    }

    t2mpopupbox.style.display = 'block';

    document.onmousemove = function(e) { t2m_mmovehander(e); }
}


t2m_mouthander = function(that, evt) //MouseOut handler for t2m
{
    if (!evt) var evt = window.event;

    var t2mpopupbox = document.getElementById('t2mpopupbox');
    if(t2mpopupbox) document.body.removeChild(t2mpopupbox);

    if(that.origtitle) that.title = that.origtitle;

    document.onmousemove = null;
}

t2m_init = function() // Initialize t2m
{
    elems = getElementsWithAttribute(document, 'title');

    for(i=0; i<elems.length; i++)
    {
        elems[i].onmouseover = function(e) { if (!e) var e = window.event; t2m_moverhander(this,e); }
        elems[i].onmouseout  = function(e) { if (!e) var e = window.event; t2m_mouthander(this,e); }
    }
}

addLoadEvent(t2m_init);

