/*
 * Based on Sweet Titles by Dustin Diaz.
 * edited by: Jakub Kulhan
 */

var ProtoTip =
{
    xCord : 0,
    yCord : 0,
    obj : null,
    tooltipTagNames: ["a", "acronym", "abbr"],

    attachTooltipBehavior: function()
    {
        if (   !document.getElementById
            || !document.createElement
            || !document.getElementsByTagName)
        {
            return;
        }

        Event.observe(document, "mousemove", ProtoTip.updateXY, false);
        if (document.captureEvents)
        {
            document.captureEvents(Event.MOUSEMOVE);
        }

        for (var i = 0, tagName; tagName = ProtoTip.tooltipTagNames[i]; i++)
        {
            var elements = document.getElementsByTagName(tagName);
            for (var j = 0, element; element = elements[j]; j++)
            {
                if (!element.getAttribute("tip")) // Safely allow multiple calls
                {
                    Event.observe(element, "mouseover", ProtoTip.tipOver, false);
                    Event.observe(element, "mouseout", ProtoTip.tipOut, false);
                    element.setAttribute("tip", element.title);
                    element.removeAttribute("title");
                }
            }
        }
    },

    updateXY: function(e)
    {
        ProtoTip.xCord = Event.pointerX(e);
        ProtoTip.yCord = Event.pointerY(e);
    },

    tipOut: function(e)
    {
        if (window.tID)
        {
            clearTimeout(tID);
        }
        if (window.opacityID)
        {
            clearTimeout(opacityID);
        }

        var div = $("toolTip");
        if (div != null)
        {
            div.parentNode.removeChild(div);
        }
    },

    checkNode: function(obj)
    {
        for (var i = 0, tagName; tagName = ProtoTip.tooltipTagNames[i]; i++)
        {
            if (obj.nodeName.toLowerCase() == tagName)
            {
                return obj;
            }
        }
        return obj.parentNode;
    },

    tipOver: function(e)
    {
        ProtoTip.obj = Event.element(e);
        tID = setTimeout("ProtoTip.tipShow()", 200)
    },

    tipShow: function()
    {
        var element = ProtoTip.checkNode(ProtoTip.obj);
		if (element.getAttribute("tip") == "") return;
        //~ var extraInfo = "";
        //~ var accessKey = "";
        //~ if (element.nodeName.toLowerCase() == "a")
        //~ {
            //~ extraInfo = (element.href.length > 25 ? element.href.toString().substring(0,25) + "..." : element.href);
            //~ accessKey = (element.accessKey ? " <span>[" + element.accessKey + "]</span> " : "");
        //~ }
        //~ else
        //~ {
            //~ extraInfo = element.firstChild.nodeValue;
        //~ }
		//for me useless :)

        var tooltip = document.createElement("div");
        tooltip.id = "toolTip";
		// Setting coordinates, 
		// must be before appending element because 
		// when it's not div will flash on the top of screen (in Firefox)
        var top = ProtoTip.yCord + 15;
        var left = ProtoTip.xCord + 10;
        // Prevent horizontal hiding
        if (ProtoTip.willOverflowHorizontal(tooltip.offsetWidth + left)) {
            tooltip.style.right = "10px";
        } else {
            tooltip.style.left = left + "px";
        }

        // Prevent vertical hiding
        if (ProtoTip.willOverflowVertical(tooltip.offsetHeight + top)) {
            tooltip.style.top = (ProtoTip.yCord - tooltip.offsetHeight - 10) + "px";
        } else {
            tooltip.style.top = top + "px";
        }
        document.body.appendChild(tooltip);
        //~ tooltip.innerHTML = "<p>" + element.getAttribute("tip") + "<em>" + accessKey + extraInfo + "</em></p>";
		tooltip.innerHTML = "<p>" + element.getAttribute("tip") + "</p>";

        tooltip.style.opacity = ".1";
        tooltip.style.filter = "alpha(opacity:10)";
        ProtoTip.tipFade("toolTip", 10);
    },

    willOverflowHorizontal: function(tooltipRight)
    {
        var x;
        if (self.innerHeight) // all except Explorer
        {
            x = self.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
            // Explorer 6 Strict
        {
            x = document.documentElement.clientWidth;
        }
        else if (document.body) // other Explorers
        {
            x = document.body.clientWidth;
        }

        var scrollX;
        if (self.pageYOffset) // all except Explorer
        {
            scrollX = self.pageXOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
            // Explorer 6 Strict
        {
            scrollX = document.documentElement.scrollLeft;
        }
        else if (document.body) // all other Explorers
        {
            scrollX = document.body.scrollLeft;
        }

        return (tooltipRight > x + scrollX);
    },

    willOverflowVertical: function(tooltipBottom)
    {
        var y;
        if (self.innerHeight) // all except Explorer
        {
            y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
            // Explorer 6 Strict Mode
        {
            y = document.documentElement.clientHeight;
        }
        else if (document.body) // other Explorers
        {
            y = document.body.clientHeight;
        }

        var scrollY;
        if (self.pageYOffset) // all except Explorer
        {
            scrollY = self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
            // Explorer 6 Strict
        {
            scrollY = document.documentElement.scrollTop;
        }
        else if (document.body) // all other Explorers
        {
            scrollY = document.body.scrollTop;
        }

        return (tooltipBottom > y + scrollY);
    },

    tipFade: function(element, opac)
    {
        var tooltip = $(element);
        var newOpacity = opac + 10;
        if (newOpacity <= 90)
        {
            tooltip.style.opacity = "." + newOpacity;
            tooltip.style.filter = "alpha(opacity:" + newOpacity + ")";
            opacityID = setTimeout("ProtoTip.tipFade('toolTip', " + newOpacity + ")", 20);
        }
        else
        {
            tooltip.style.opacity = "0.99999";
            tooltip.style.filter = "alpha(opacity:99.99)";
        }
    }
};

Event.observe(window, "load", ProtoTip.attachTooltipBehavior, false);