/*
** Katelynn O'Brien
** Adapted from
** http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements
*/

jQuery.fn.customToolTip = function(e) {
    $(this).live('mouseover', function(e) {
        clearTimeout($('#livetip').data('timeoutId'));

        // get vertical scroll depending on browser
        // borrowed from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
        function f_scrollTop() {
            return f_filterResults (
            window.pageYOffset ? window.pageYOffset : 0,
            document.documentElement ? document.documentElement.scrollTop : 0,
            document.body ? document.body.scrollTop : 0
        );
        }
        function f_filterResults(n_win, n_docel, n_body) {
            var n_result = n_win ? n_win : 0;
            if (n_docel && (!n_result || (n_result > n_docel)))
                n_result = n_docel;
            return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
        }
        var scrollTop = f_scrollTop();
        var posx = 0;
        var posy = 0;
        tipTitle = this.title;
        // hide browser tooltip
        this.title = '';
        // flip if tooltip will get cut off by left edge of browser
        if (e.clientX > 514) {
            posx = e.clientX - 480;
            direction = 'right';
        } else {
            posx = e.clientX - 30;
            direction = 'left';
        }
        posy = e.clientY + scrollTop + 5;
        $('#livetip').css({
            left: posx,
            top: posy
        }).html('<div class="tooltip_top_' + direction + '"></div><div class="tooltip_middle"><a href="javascript:;" class="close"><img src="' + pathToMain + 'images/new/white_close.png" alt="Close" /></a><div class="tooltip_content">' + tipTitle + '</div></div><div class="tooltip_bottom"></div>').show();
    }).live('mouseout', function(e) {
        // put browser tooltip back
        this.title = tipTitle;
    });

    $("#livetip").mouseenter(function() {
        clearTimeout($('#livetip').data('timeoutId'));
        $('#livetip').show();
    }).mouseleave(function() {
        if (jQuery.browser.msie) {
            var timeoutId = setTimeout(function() {
                $('#livetip').hide();
            }, 2000);
        } else {
            var timeoutId = setTimeout(function() {
                $('#livetip').fadeOut('slow');
            }, 2000);
        }
        $('#livetip').data('timeoutId', timeoutId);
    });

    $('#livetip a.close').live('click', function() {
        $('#livetip').hide();
    });
}

