var Balloons = {

    offsetfromcursorX: 12, // x offset of tooltip
    offsetfromcursorY: 10, // y offset of tooltip

    offsetdivfrompointerX: 10, // x offset of tooltip DIV relative to pointer image
    offsetdivfrompointerY: 14, // y offset of tooltip DIV relative to pointer image

    initialize: function(marker, container, url)
        {
            document.write('<div id="dhtmltooltip"></div>');
            document.write('<img id="dhtmlpointer" >');
            Balloons.marker = marker;
            Balloons.container = container;
            Balloons.url = url;
            Balloons.balloon = $('dhtmltooltip');
            Balloons.pointer = $('dhtmlpointer');


            Event.observe(document, "click", function(e)
                {
                    var el = Event.element(e);

                    while(el.parentNode)
                    {
                        if (el.parentNode.id == 'dhtmltooltip')
                        {
                            return;
                        }
                        el = el.parentNode;
                    }

                    Balloons.hideBalloon();
                }, false);
        },

    installBalloons: function()
        {
            Balloons.hideBalloon();

            var links = $A($(Balloons.container).getElementsByTagName('a'));

            links.each(function(link){
                            if (link.getAttribute('rel') == Balloons.marker)
                            {
                                Event.observe(link, "click", Balloons.showBalloon, false); 
                            }
                        });
        },

    staticBalloon: function(e)
        {
            var msg = Event.element(e).getAttribute('msg');

            Balloons.X = Event.pointerX(e);
            Balloons.Y = Event.pointerY(e);
            Balloons.clientX = e.clientX;
            Balloons.clientY = e.clientY;
            Balloons.clientWidth = document.body.clientWidth;
            Balloons.clientHeight = document.body.clientHeight;

            Balloons.inflateBalloon({'responseText' : msg});
            Event.stop(e);
        },

    showBalloon: function(e)
        {
            var id = Event.element(e).getAttribute('eventId');

            Balloons.X = Event.pointerX(e);
            Balloons.Y = Event.pointerY(e);
            Balloons.clientX = e.clientX;
            Balloons.clientY = e.clientY;
            Balloons.clientWidth = document.body.clientWidth;
            Balloons.clientHeight = document.body.clientHeight;

            new Ajax.Request(
                        Balloons.url + '/' + id,
                        {
                            method: 'get',
                            onComplete: Balloons.inflateBalloon
                        });

            Event.stop(e);
        },

    inflateBalloon: function(originalRequest)
        {
            Element.update(Balloons.balloon, '<div style="text-align: right;"><a href="#" onClick="Balloons.hideBalloon(); return false;"><img src=\"/images/icons/delete_balloon.gif\" /></a></div><div style="font-size: 11px;">' + originalRequest.responseText + '</div>');

            var nondefaultpos = false;

            // find out how close the mouse is to the corner of the window
            var rightedge = Balloons.clientWidth - Balloons.clientX - Balloons.offsetfromcursorX;
            var bottomedge = Balloons.clientHeight - Balloons.clientY - Balloons.offsetfromcursorY;
            var leftedge = (Balloons.offsetfromcursorX < 0) ? Balloons.offsetfromcursorX * (-1) : -1000;

            // if the horizontal distance isn't enough to accomodate the width of the context menu
            if (rightedge < Balloons.balloon.offsetWidth)
            {
                // move the horizontal position of the menu to the left by it's width
                Balloons.balloon.style.left = Balloons.X - Balloons.balloon.offsetWidth + "px";
                nondefaultpos = true;
            }
            else if (Balloons.X < leftedge)
            {
                Balloons.balloon.style.left = "0px";
            }
            else
            {
                // position the horizontal position of the menu where the mouse is positioned
                Balloons.balloon.style.left = Balloons.X + Balloons.offsetfromcursorX - Balloons.offsetdivfrompointerX + "px";
                Balloons.pointer.style.left = Balloons.X + Balloons.offsetfromcursorX + "px";
            }

            // same concept with the vertical position
            if (bottomedge < Balloons.balloon.offsetHeight)
            {
                Balloons.balloon.style.top = Balloons.Y - Balloons.balloon.offsetHeight - Balloons.offsetfromcursorY + "px";
                nondefaultpos = true;
            }
            else
            {
                Balloons.balloon.style.top = Balloons.Y + Balloons.offsetfromcursorY + Balloons.offsetdivfrompointerY + "px";
                Balloons.pointer.style.top = Balloons.Y + Balloons.offsetfromcursorY + "px";
            }

            Balloons.balloon.style.visibility = "visible";

            if ( ! nondefaultpos)
            {
                Balloons.pointer.style.visibility = "visible";
            }
            else
            {
                Balloons.pointer.style.visibility = "hidden";
            }
        },

    hideBalloon: function()
        {
            Balloons.balloon.style.visibility = "hidden";
            Balloons.pointer.style.visibility = "hidden";
            Balloons.balloon.style.left = "-1000px";
            Balloons.balloon.style.backgroundColor = '';
            Balloons.balloon.style.width = '';
        },

    removeBalloons: function()
        {
            var links = $A($(Balloons.container).getElementsByTagName('a'));

            links.each(function(link){
                            if (link.getAttribute('rel') == Balloons.marker)
                            {
                                Event.stopObserving(link, "click", Balloons.showBalloon, false); 
                            }
                        });
        }
}
