/*
Javascript Tooltips
Version 1.0
Copyright (C) 2005-2006 Greg Methvin (greg@methvin.net)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.


This script provides functions to create stylable tooltips
on links within specified elements of a page.

*/

// Constants

// The CSS for the tooltip
var TooltipCss = "tooltip.css";
// The ID of the tooltip element
var TooltipId = "tooltip";
// The class name of the title section
var TooltipTitleClass = "title";
// The class name of the URL section
var TooltipUrlClass = "url";
// The opacity of the tooltip
var TooltipOpacity = 0.95;
// The maximum length of the URL text
var HrefMaxLength = 50;
// The pixels [x, y] to offset the tooltip from the location of the mouse pointer
var TooltipOffset = [0, 20];
// The title to show if there is none on the link (e.g. "link:", set to null to hide)
var DefaultTitle = null;

/*
Enable tooltips on links within the specified element

void enableTooltips(Object el)

Parameters:
    * el: A String specifying the ID of the element or an Element object.

*/
function enableTooltips(el)
{
    if(!document.getElementById || !document.getElementsByTagName)
        return;
    // add CSS
    if (TooltipCss)
    {
        var css = document.createElement("link");
        css.setAttribute("type", "text/css");
        css.setAttribute("rel", "stylesheet");
        css.setAttribute("href", TooltipCss);
        css.setAttribute("media", "screen");
        document.getElementsByTagName("head")[0].appendChild(css);
    }
    // get the links in the element
    if (typeof el == "string")
        el = document.getElementById(el);
    el = el || document;
    var moveTooltip = function(e)
    {
        var tt = document.getElementById(TooltipId);
        if (!tt)
            return;
        e = e || window.event;
        var x = e.pageX || e.clientX
            && (document.documentElement.scrollLeft && e.clientX + document.documentElement.scrollLeft
            || e.clientX + document.body.scrollLeft);
        var y = e.pageY || e.clientY
            && (document.documentElement.scrollTop && e.clientY + document.documentElement.scrollTop
            || e.clientY + document.body.scrollTop);
        var offX = TooltipOffset[0];
        var offY = TooltipOffset[1];
        x += offX;
        y += offY;
        tt.style.position = "absolute";
        tt.style.top = y + "px";
        tt.style.left = x + "px";
        var maxX = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) - 20 - tt.offsetWidth;
        var maxY = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - tt.offsetHeight;
        if (y > maxY)
        {
            var scrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
            var newY = y - 2*offY - tt.offsetHeight;
            if (newY < scrollY && scrollY < newY + offY)
                newY = scrollY;
            tt.style.top = newY + "px";
        }
        if (x > maxX)
        {
            var scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
            var newX = maxX;
            if (newX < scrollX)
                newX = scrollX;
            tt.style.left = newX + "px";
        }
    };
    var els = el.getElementsByTagName("a");
    // prepare the tooltips
    for (var i = 0; i < els.length; i++)
    {
        var el = els[i];
        var title = el.getAttribute("title") || DefaultTitle;
        el.removeAttribute("title");
        var tooltip = document.createElement("span");
        tooltip.style.display = "block";
        if (title != null)
        {
            var titleEl = document.createElement("span");
            titleEl.className = TooltipTitleClass;
            titleEl.appendChild(document.createTextNode(title));
            tooltip.appendChild(titleEl);
            titleEl.style.display = "block";
        }
        var href = el.getAttribute("href");
        if (href != null)
        {
            var urlEl = document.createElement("strong");
            urlEl.className = TooltipUrlClass;
            urlEl.style.display = "block";
            if (href.length > HrefMaxLength)
                href = href.substr(0, HrefMaxLength - 3) + "...";
            urlEl.appendChild(document.createTextNode(href));
            tooltip.appendChild(urlEl);
        }
        // set opacity
        setOpacity(tooltip, TooltipOpacity);
        // apply event listeners
        el.tooltip = tooltip;
        el.onmouseover = function(e)
        {
            var tt = this.tooltip;
            if (!tt)
                return;
            tt.id = TooltipId;
            tt.setAttribute("id",TooltipId);
            document.getElementsByTagName("body")[0].appendChild(tt);
            moveTooltip(e);
        };
        el.onmouseout = function(e)
        {
            var tt = document.getElementById(TooltipId);
            if (!tt)
                return;
            document.getElementsByTagName("body")[0].removeChild(tt);
            tt.id = null;
            tt.setAttribute("id", null);
        };
        el.onmousemove = moveTooltip;
    }
}

/*
Disable tooltips on links within the specified element

void disableTooltips(Object el)

Parameters:
    * el: A String specifying the ID of the element or an Element object.

*/
function disableTooltips(el)
{
    // get the links in the element
    if (typeof el == "string")
        el = document.getElementById(el);
    el = el || document;
    var els = el.getElementsByTagName("a");
    // disable the tooltips
    for (var i = 0; i < els.length; i++)
    {
        var el = els[i];
        // remove the tooltip
        el.tooltip = null;
        // kill the event listeners
        el.onmouseover = el.onmouseout = el.onmousemove = null;
    }
}
/**
Set the opacity the specified element

void setOpacity(Element el, Number opacity)

Parameters:
    * el: The Element on which to set the opacity.
    * opacity: A Number greater than or equal to 0 and less than or equal to 1, specifying the opacity.

**/
function setOpacity(el, opacity)
{
    el.style.filter = "alpha(opacity:" + parseInt(opacity * 100) + ")";
    el.style.KHTMLOpacity = opacity;
    el.style.MozOpacity = opacity;
    el.style.opacity = opacity;
}