﻿/*
 * jQuery UI Tooltip
 *
 * Depends:
 *	    ui.core.js
 */
 
(function($) {
    $.widget("ui.tooltip", {

        init: function() {
            this.options.removed = false;
            if (this.options.mode == 'hover') {
                $(this.element).hover(hoverHandler, outHandler);

            }
            else if (this.options.mode == 'focus') {
                $(this.element).focus(hoverHandler);
                $(this.element).blur(outHandler);
            }
            else if (this.options.mode == 'manual') {
            }
        },

        show: function() {
            var o = this.options;
            if (!o.removed) {
                if (o.mode == 'manual') {
                    prepare(this.element, o);
                }

                $(o.tooltip).show();
            }
        },

        hide: function() {
            var o = this.options;
            $(o.tooltip).hide();
        },

        left: function() {
            var o = this.options;
            prepare(this.element, o);
        },

        top: function() {
            var o = this.options;
            prepare(this.element, o);
        },

        remove: function() {
            var o = this.options;
            o.removed = true;
        }
    });

    $.extend($.ui.tooltip, {
        defaults: {
            tooltip: '',
            onShow: undefined,
            onHide: undefined,
            mode: 'hover',
            // provide a speed for the animation
            speed: 1000,
            // provide a period for the popup to keep showing
            period: 2000,
            // default the animation algorithm to the basic slide
            animation: 'slide',
            left: 10,
            top: -100,
            removed: false
        },
        animations: {
            slide: function(e, options) {
                e.fadeIn('fast');
            },
            fade: function(e, options) {
                e.fadeOut('fast');
            }
        }
    });

    function hoverHandler(event) {
        //Fetch Options
        var o = $.data(this, 'tooltip').options;

        //Element who raised the event
        var $this = $(this);

        //Helper functon for Positioning and Calling Callback function
        prepare($this, o);

        //Call Show method of the tooltip Widget,
        //Show method should play on any required animations
        $.data(this, 'tooltip').show();
    };
    function outHandler(event) {
        //Fetch Options
        var o = $.data(this, 'tooltip').options;

        //Get tooptip Element
        var $tooltip = $(o.tooltip);

        //If call back method defined, initiate the call
        if ($.data(this, 'tooltip').options.onHide) {
            $.data(this, 'tooltip').options.onHide.call(this, { target: $(this) });
        }

        //Call Hide method of the tooltip Widget,
        //Hide method should play on any required animations
        $.data(this, 'tooltip').hide();
    };
    function prepare(jObj, options) {
        var $tooltip = $(options.tooltip);
        var element = jObj[0];
        var offset = jObj.offset();

        var left = options.left;
        var top = options.top;
        //var left = jObj.offset().left - options.left;
        //var top = jObj.offset().top - options.top;

        //console.log('left: ' + left + '; top: ' + top + ';option.left: ' + options.left + ';option.top: ' + options.top + ';object.left: ' + jObj.offset().left + ';object.top: ' + jObj.offset().top);

        if (options.onShow) {
            options.onShow.call(this, { target: jObj });
        }

        if ($(window).width() - offset.left <= $tooltip.width()) {
            left = offset.left - $tooltip.width();
        }
        else {
            left += 5;
        }
        $tooltip.css({ position: 'absolute', top: top + 'px', left: left + 'px' });
    };

})(jQuery);