/*
    A generic way of applying tooltips to links on a page   
    Uses the rel attribute of each link trigger as the id of the tooltip to show    
*/

Site.Controllers.Tooltips = Class.create
(
    Site.Base,
    {
        initialize: function(triggers, options)
        {
            
            this.tooltips = new Array();
        
            triggers.each
            (
                function(trigger)
                {
                    var tooltip = this.getTooltip(trigger);
                    
                    if (tooltip)
                    {
                        this.tooltips.push(new Site.Widgets.Tooltip(tooltip, trigger, options));
                    }
                    
                    tooltip = null;
                },
                this
            );
        },
        
        hideAll: function()
        {
            this.tooltips.invoke("hide");
        },
        
        getTooltip: function(trigger)
        {
            // first, try the rel attribute (links)
            if (trigger.rel)
            {
                if (tooltip = $(trigger.rel))
                {
                    return tooltip;
                }
            }
            
            // next, assume the trigger ID to the hash value in href 
            if (trigger.href)
            {
                var parts = trigger.href.split("#");
                
                if (parts.length > 1)
                {
                    if (tooltip = $(parts[1]))
                    {
                        return tooltip;
                    }
                }
            }
            
            // next look for the className "trigger-[tooltipId]" (this is for non-<a> tag triggers)
            
            var classId = $A(trigger.className.split(" ")).find
            (
                function(className)
                {
                    if (className.substr(0, 8) == "trigger-")
                    {
                        return true;
                    }
                }
            );
            
            if (classId)
            {
                return $(classId.substr(8));
            }
        },
        
        destroy: function()
        {
            this.tooltips.invoke("destroy");
        }
    }
);
