/**
 * jQuery brTip plugin
 * This jQuery plugin was inspired and based on various other plugins of tooltip, but this is better =)
 * @name jquery-brtip-1.1.js
 * @author Gabriel Sobrinho - gabriel.sobrinho@gmail.com
 */

(function($){
    $.fn.tooltip = function(opts){
        opts = $.extend({
            fadeIn: '',
            fadeOut: '',
            toShow: 100,
            toHide: 500,
            opacity: 0.8,
            top: -1,
            left: 15,
            title: null,
            minLength:null,
            box: null,
            delayToShow: null,
            delayToHide: null,
            footerSeparator: '',
            txtFooter: '',
            txt: ''
        }, opts);
        function _clearTimes(){clearTimeout(opts.delayToShow); clearTimeout(opts.delayToHide);}
        function _create(){
            _clearTimes();
            if (!opts || !opts.box){
                opts.box = $('<div class="tootltip_box"><div class="tootltip_content">&nbsp;</div><div class="tootltip_footer">&nbsp;</div></div>').appendTo('body');
                opts.box.css('opacity', opts.opacity);
            };
            opts.box.find('div.tootltip_content').html(opts.txt);
            if ( opts.txtFooter != '' ) {
                opts.box.find('div.tootltip_footer').html(opts.txtFooter).show();
            } else {
                opts.box.find('div.tootltip_footer').html(opts.txtFooter).hide();
            }
            opts.delayToShow = setTimeout(function(){opts.box.fadeIn(opts.fadeIn);}, opts.toShow);}
        function _hide(){opts.delayToHide = setTimeout(function(){opts.box.fadeOut(opts.fadeOut);}, opts.toHide);}
        function _setPos(top, left){
            if (opts && opts.box){
                opts.box.css({
                    top: top + opts.top,
                    left: left + opts.left
                });
            }else{setTimeout(function(){_setPos(top, left);}, 100);}
        }
        return this.each(function(){
            var self = $(this);
            if(opts.minLength=='' || self.attr('title').length <  opts.minLength){return;}
            self
                .mouseover(function(e){
                    var tit = self.attr('title');
                    var fot = '';
                    var aux = [];
                    if ( opts.footerSeparator != '' && tit.charAt( opts.footerSeparator ) ) {
                        aux = tit.split(opts.footerSeparator);
                        tit = aux[0];
                        fot = aux.reverse()[0];
                    }
                    opts.txt = tit;
                    opts.txtFooter = fot;
                    self.attr('title', '');

                    _create();

                    var w = parseInt((screen.availWidth)/2), p = '';
                    e.pageX>w ? p = parseInt((e.pageX)-260) : p = e.pageX;
                    _setPos(e.pageY, p);

                })
                .mouseout(function(e){
                    if ( opts.footerSeparator != '' && opts.txtFooter != '' ) {
                        opts.txt = opts.txt + opts.footerSeparator + opts.txtFooter;
                    }
                    self.attr('title', opts.txt);
                    opts.txt = '';
                    opts.txtFooter = '';
                    _hide();
                })
                .mousemove(function(e){
                    var w = parseInt((screen.availWidth)/2), p = '';
                    e.pageX>w ? p = parseInt((e.pageX)-260) : p = e.pageX;
                    _setPos(e.pageY, p);
                })
        });
    };
}(jQuery));
