/*
 * ===========================================================================
 * *x Software + Systeme - xS+S - Andreas Haumer
 * A-3242 TEXING, Altendorf 37 / A-1100 WIEN, Karmarschgasse 51/2/20
 * Tel: +43-1-6060114-0 / Fax: +43-1-6060114-71 / EMail: office@xss.co.at
 * Copyright (c) 2002-2006 by Andreas Haumer, xS+S. All rights reserved
 * ---------------------------------------------------------------------------
 * Filename : $RCSfile: tooltips.js,v $
 * RCSId    : $Id: tooltips.js,v 1.1 2005/02/25 12:40:15 martin Exp $
 * Purpose  : xS+S JavaScript Library - Tooltip-Funktionen
 * Author   : Martin H�ller <martin.hoeller@xss.co.at>
 * Date     : 17-Feb-2003
 * Update   : $Date: 2005/02/25 12:40:15 $
 * Comments : You can find JavaScript functions here, that provide basic
 *            support for using tooltips.
 * ---------------------------------------------------------------------------
 */

var tooltip_id       = "xss_tooltip";
var tooltip_id_title = "xss_tooltip_title";
var tooltip_id_body  = "xss_tooltip_body";

/*****************************************************************************
 * xss_breakLines - This function takes a string and returns an array of
 *                  strings. The array consists of elements each containing
 *                  one line of the original string.
 *                  '\n' and '<br>' are interpreted as new-line characters.
 **********************/

function xss_breakLines( text ) {
    var regexp = new RegExp( "<br>", "gi" );
    var textarray;

    // convert all html line breaks ('<br>') to '\n'
    textarray = text.replace( regexp, "\n  " );

    // split the text into a string array and return it
    textarray = textarray.split( "\n" );

    return textarray;
}


/*****************************************************************************
 * xss_ShowToolTip - Shows a ToolTip with the given 'title' and 'text'. If
 *                   there are just two arguments, the second argument is
 *                   interpreted as the 'text'.
 *                   The 'event' comes always first and specifies and event-
 *                   object which provides coordinates for the tooltip.
 **********************/

function xss_showToolTip( event, title, text )
{
    var tooltip;
    var e;
    var x;
    var y;

    // check if a tooltip-element already exists
    if ( tooltip = document.getElementById( tooltip_id ) ) {
        document.body.removeChild( tooltip );
    }

    // if text is omitted, the second argument is taken as the text
    if ( arguments.length == 2 ) {
        text  = title;
        title = null;
    }

    // calculate x/y-coordinates of the tooltip (upper left corner)
    if (event.pageY) {
      y = event.pageY;
      x = event.pageX;
    } 
    else if (event.clientY) {
      if (document.body.scrollTop > document.documentElement.scrollTop) {
        y = event.clientY + document.body.scrollTop;
      } 
      else {
        y = event.clientY + document.documentElement.scrollTop;
      }

      if (document.body.scrollLeft > document.documentElement.scrollLeft) {
        x = event.clientX + document.body.scrollLeft;
      }
      else {
        x = event.clientX + document.documentElement.scrollLeft;
      }
    }

    // add small offset to avoid flickering
    x = x+5;
    y = y+5;

    // create and initialize the main tooltip-element
    tooltip = document.createElement( "div" );
    tooltip.setAttribute( "id", tooltip_id );
    tooltip.style.visibility = "visible";
    tooltip.style.position   = "absolute";
    tooltip.style.left       = x + "px";
    tooltip.style.top        = y + "px";

    document.body.appendChild( tooltip );

    // convert text to array with one line per element
    if ( title ) title = xss_breakLines( title);
    if ( text  ) text  = xss_breakLines( text );

    // create and initialize the tooltip-subelements
    if ( title ) {
        e = document.createElement( "div" );
        e.setAttribute( "id", tooltip_id_title );

        tooltip.appendChild( e );
        for ( var line in title ) {
            if ( line > 0 ) e.appendChild( document.createElement( "br" ) );
            e.appendChild( document.createTextNode( title[line] ) );
        }
    }

    e = document.createElement( "div" );
    e.setAttribute( "id", tooltip_id_body );
    tooltip.appendChild( e );

    for ( var line in text ) {
        if ( line > 0 ) e.appendChild( document.createElement( "br" ) );
        e.appendChild( document.createTextNode( text[line] ) );
    }
    
}


/*****************************************************************************
 * xss_hideToolTip - Hide the tooltip that was created with xss_showToolTip()
 **********************/

function xss_hideToolTip() {
    if ( tooltip = document.getElementById( tooltip_id ) ) {
        document.body.removeChild( tooltip );
        tooltip = null;
    }
}


// end of file

