/*
 * xTooltip - plugin de jQuery para fabricar tooltips
 * author: Xavier Lesa <xavierlesa@gmail.com>
 *
 */
/*
 *  +---+--------+---+
 *  |TL |   TOP  |TR |
 *  +---+--------+---+
 *  | L | object | R |
 *  +---+--------+---+
 *  |BL | BOTTOM |BL |
 *  +---+--------+---+
 *
 */

(function($, undefined){
    $.fn.xTooltip = function(settings){
        var self = this;
        var conf = jQuery.extend({
            defclass:'tooltip-over', 
            defData:[], 
            defSource:'title', 
            usehtml:true, 
            topMargin:10, 
            leftMargin:0, 
            delay:0, 
            fadeInSpeed:'normal',
            fadeOutSpeed:'fast',
            defPosition:'top',
            css: {
                'margin':'0px',
                'padding':'5px',
                'background-color':'#CCC',
                'border-radius': '3px 3px 3px 3px',
                '-webkit-border-radius': '3px',
                '-moz-border-radius': '3px',
                'border': 'solid 1px #777',
                'box-shadow': '3px 3px 5px #777',
                '-webkit-box-shadow': '3px 3px 5px #777',
                '-moz-box-shadow': '3px 3px 5px #777',
                'color': '#EEE',
                'min-width': '200px',
                'overflow': 'hidden',
                'position': 'absolute',
                'transition': 'opacity .15s linear',
                '-moz-transition': 'opacity .15s linear',
                '-ms-transition': 'opacity .15s linear',
                '-o-transition': 'opacity .15s linear',
                '-webkit-transition': 'opacity .15s linear',
                'z-index': '1200'
            }
        }, settings);
        var tpl = "<div class=\""+conf.defclass+"\" id=\"tooltip\"></div>";
        var b = $(this).parents('body');

        // loop
        this.each(function(iter, obj){
            var e = $(obj);
            var p = e.offset();
            var t = $(tpl).attr('id', 'tooltip-' + iter);

            t.css(conf.css).css({'left':p.left,'top':p.top,'display':'none'});

            if(conf.defData.length){ src = conf.defData[iter] } else { src = e.attr(conf.defSource) };
            if(conf.usehtml){ t.html(src) } else { t.text(src) };
 
            self.show = function(){
                var p = $(this).offset();

                var _t = (function(){
                    r = p.top;
                    switch(conf.defPosition){
                        case 'top': r = conf.topMargin ? p.top - (conf.topMargin + t.height()) : p.top - t.height(); break;
                        case 'tl': r = conf.topMargin ? p.top - (conf.topMargin + t.height()) : p.top - t.height(); break;
                        case 'tr': r = conf.topMargin ? p.top - (conf.topMargin + t.height()) : p.top - t.height(); break;
                        case 'bottom': r = conf.topMargin ? p.top - conf.topMargin : p.top; break;
                        case 'bl': r = conf.topMargin ? p.top - conf.topMargin : p.top; break;
                        case 'br': r = conf.topMargin ? p.top - conf.topMargin : p.top; break;
                    }
                    return r
                })();

                var _l = (function(){ 
                    r = (($('body').width() - p.left) >= t.width()) ? p.left : p.left - t.width();
                    switch(conf.defPosition){
                        case 'l': r = conf.leftMargin ? p.left - (conf.leftMargin + t.width()) : p.left - t.width();
                        case 'r': r = conf.leftMargin ? p.left + (conf.leftMargin + t.width()) : p.left + t.width();
                    }
                    return r; 
                })();

                t.css({'top':_t,'left':_l});
                t.clearQueue();
                t.fadeIn(conf.fadeInSpeed); 
            };
            self.hide = function(){ if(conf.delay){ t.clearQueue(); t.delay(conf.delay).fadeOut(conf.fadeOutSpeed); } else { t.fadeOut(conf.fadeOutSpeed); } };

            e.hover(self.show, self.hide);
            // attach tooltip
            b.append(t);
        });
        return this
    }
})(jQuery);

