// ---------------------------------------------------
// this script was created for Motiv Site (www.ycc.ru)
// by Pavel Shouryguin (psion@mail.ru)
// 06.11.2003-12.11.2003
// you can use it without any limitations
//
// a lot of thanks to Peter-Paul Koch,
// http://www.xs4all.nl/~ppk/js/doctype_on.html
//
// ---------------------------------------------------
// additionally was modified by Paul Yanchenko
// (paul_y@mail.ru, ICQ: 122870822) 15/01/2004:
// * cleaning script (it seemes that original script
//   was oriented to show images but was hurriedly
//   modified to show text messages instead)
// * readable improvements (i hope that)
// * fixed 1 bug: when hint message shows it may cause
//   window horizontal scroll and clipping hint message
//   if it is near right corner of the browser window.
//   Most often it takes a place at the first time
//   after page (re)load.
// * width of hint message now is automatically assigned
//   for proper wiew, but not more than 300 pixels.
// * added support for delay and timeout.
// * other cosmetical changes
//
// original script has been taken from
//          http://www.ycc.ru/userfiles/map/enlarge.js
// ---------------------------------------------------
// usage:
// 1. include this script in <HEAD> of html-file:
//      <SCRIPT src="/hintmsg.js" type="text/javascript"></SCRIPT>
// 2. place into <BODY> of html-file:
//      <DIV id="hintmessage"></DIV>
// 3. each html-element with hint message should contain
//    following attributes:
//      onmouseover="showhint('Hello, world!', event, 500, 5000);"
//      onmouseout="hidehint();"
//
//     (you may use other events)
// ---------------------------------------------------

document.write("<style type=\"text/css\">");
document.write("#hintmessage { position:absolute; visibility:hidden; border: 1px solid black; font-family: Arial; font-size:8pt; background-color: #FFFFE7; padding: 2px 5px; margin: 0; text-align: left }");
document.write("</style>");

var crossobj=false;
var timerId = 0;
var pos_X = 0;
var pos_Y = 0;


/**
 * Show the hint message
 *
 * @param   string      Text of hint message (html, backslash-escaped)
 * @param   object      event
 * @param   int         delay showing the hint for xxxx msec, 0 - immediately
 * @param   int         force hiding hint after xxxx msec, 0 - do not force
 * @access  public
 */

function showhint(text, e, delay, timeout) {
    var crossobj=document.getElementById? document.getElementById("hintmessage") : document.all.hintmessage;

    if (typeof delay == 'undefined')
        delay = 0;
    if (typeof timeout == 'undefined')
        timeout = 0;

    if ( typeof e == "object" ) {
        if ( typeof(e.pageX) == 'number' ) {
            pos_X = e.pageX;
            pos_Y = e.pageY;
        }
        else if ( typeof(e.clientX) == 'number' ) {
            pos_X = e.clientX;
            pos_Y = e.clientY;
            if ( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' )) {
                pos_X += document.body.scrollLeft;
                pos_Y += document.body.scrollTop;
            }
            else if ( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
                pos_X += document.documentElement.scrollLeft;
                pos_Y += document.documentElement.scrollTop;
            }
        } else {
            pos_X = -1;
            pos_Y = -1;
        }

        if ((pos_X >= 0) && (pos_Y >= 0)) {

            var scroll_X = 0;
            var scroll_Y = 0;

            if ( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' )) {
                scroll_X = document.body.scrollLeft;
                scroll_Y = document.body.scrollTop;
            }
            else if ( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' )) {
                scroll_X = document.documentElement.scrollLeft;
                scroll_Y = document.documentElement.scrollTop;
            }

            var win_size_X = 0;
            var win_size_Y = 0;

            var win_size_X = 0;
            var win_size_Y = 0;

            if (window.innerWidth && window.innerHeight) {
                win_size_X = window.innerWidth;
                win_size_Y = window.innerHeight;
            }
            else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
                win_size_X = document.documentElement.clientWidth;
                win_size_Y = document.documentElement.clientHeight;
            }
            else if (document.body && document.body.clientWidth && document.body.clientHeight) {
                win_size_X = document.body.clientWidth;
                win_size_Y = document.body.clientHeight;
            }

            pos_X += 15;
            pos_Y += 15;
        }
    }

    crossobj.innerHTML=text;

    if (crossobj.offsetWidth > 300) {
        crossobj.style.width = 300;
    }

    if (crossobj.offsetWidth && crossobj.offsetHeight) {

        if (pos_X - scroll_X + crossobj.offsetWidth + 5 > win_size_X)
            pos_X -= (crossobj.offsetWidth + 15);
        if (pos_Y - scroll_Y + crossobj.offsetHeight + 5 > win_size_Y)
            pos_Y -= (crossobj.offsetHeight + 15);
    }

    crossobj.style.left = pos_X + "px";
    crossobj.style.top = pos_Y + "px";

    if (delay > 0) {
        if (timerId == 0)
            timerId = window.setTimeout("showhint('"+text+"', 0, 0, "+timeout+")", delay);
    } else {
        if (timerId != 0) {
            window.clearTimeout(timerId);
            timerId = 0;
        }
        crossobj.style.visibility = "visible";
        if (timeout > 0)
            timerId = window.setTimeout('hidehint()', timeout);
    }
}

/**
 * Hide hint message
 *
 * @access  public
 */

function hidehint()
{
    if (!crossobj)
        crossobj=document.getElementById? document.getElementById("hintmessage") : document.all.hintmessage;
    crossobj.innerHTML='';
    crossobj.style.visibility="hidden";
    crossobj = false;
    window.clearTimeout(timerId);
    timerId = 0;
}

