/*
jQuery Version:				jQuery 1.3.2
Plugin Name:				aToolTip V 1.0
License:					aToolTip is licensed under a Creative Commons Attribution 3.0 Unported License
*/
(function ($) {

    $.fn.aToolTipRemove = function (options) {
        var obj = $(this);
        obj.unbind('hover');
        //$('body').find('.' + obj.selector).remove();
    };

    $.fn.aToolTip = function (options) {

        // setup default settings
        var defaults = {
            clickIt: false,
            closeTipBtn: 'aToolTipCloseBtn',
            fixed: false,
            inSpeed: 400,
            outSpeed: 100,
            tipContent: '',
            toolTipClass: 'aToolTip',
            xOffset: 5,
            yOffset: 5,
            innerFrame: '#NscFatturaWebSlideIFrame'
        },

        // This makes it so the users custom options overrides the default ones
    	settings = $.extend({}, defaults, options);

        return this.each(function () {

            var obj = $(this);
            var _document = (parent && parent.document ? parent.document : document);
            var _window = (window.parent ? window.parent : window);
            var $body = $("body", _document);
            var $innerFrame = ($body.find(settings.innerFrame) || $body);

            // Decide weather to use a title attr as the tooltip content
            if (settings.tipContent && settings.tipContent != undefined) {
                var tipContent = settings.tipContent;
            }
            else {
                if (obj.attr('title')) {
                    // set the tooltip content/text to be the obj title attribute
                    var tipContent = obj.attr('title');
                } else {
                    // if no title attribute set it to the tipContent option in settings
                    var tipContent = settings.tipContent;
                } 
            }

            // check if obj has a title attribute and if click feature is off
            if (tipContent && !settings.clickIt) {
                // Activate on hover	
                obj.hover(function (el) {
                    obj.attr({ title: '' });
                    $innerFrame.find('.' + settings.toolTipClass).remove();
                    $body.find('.' + settings.toolTipClass).remove();
                    $body.append("<div class='" + settings.toolTipClass + "'><p class='aToolTipContent'>" + tipContent + "</p></div>");
                    $body.find('.' + settings.toolTipClass).css({
                        position: 'absolute',
                        display: 'none',
                        zIndex: '50000',
                        top: ((obj.offset().top + ($innerFrame.offset() ? $innerFrame.offset().top : 0)) - $body.find('.' + settings.toolTipClass).height() - settings.yOffset) + 'px',
                        left: ((obj.offset().left + ($innerFrame.offset() ? $innerFrame.offset().left : 0)) + obj.outerWidth() + settings.xOffset) + 'px'
                    })
					.stop().fadeIn(settings.inSpeed);
                },
				function () {
				    // Fade out
				    //$body.find('.' + settings.toolTipClass).stop().fadeOut(settings.outSpeed, function() { $(this).remove(); });
				    $body.find('.' + settings.toolTipClass).stop().remove();
				});
            }

            // Follow mouse if fixed is false and click is false
            if (!settings.fixed && !settings.clickIt) {
                obj.mousemove(function (el) {
                    var __left = (el.pageX + ($innerFrame.offset() ? $innerFrame.offset().left : 0) + settings.xOffset);
                    __left = ((__left + 200) > $(_window).width() ? __left - ($body.find('.aToolTipContent').width() + 20) : __left);
                    $body.find('.' + settings.toolTipClass).css({
                        top: ((el.pageY + ($innerFrame.offset() ? $innerFrame.offset().top : 0)) - $body.find('.' + settings.toolTipClass).outerHeight() - settings.yOffset),
                        left: __left
                    })
                });
            }

            // check if click feature is enabled
            if (tipContent && settings.clickIt) {
                // Activate on click	
                obj.click(function (el) {
                    obj.attr({ title: '' });
                    $body.find('.' + settings.toolTipClass).remove();
                    $innerFrame.find('.' + settings.toolTipClass).remove();
                    $body.append("<div class='" + settings.toolTipClass + "'><p class='aToolTipContent'>" + tipContent + "</p></div>");
                    $body.find('.' + settings.toolTipClass).append("<a class='" + settings.closeTipBtn + "' href='#' alt='close'>close</a>");
                    $body.find('.' + settings.toolTipClass).css({
                        position: 'absolute',
                        display: 'none',
                        zIndex: '50000',
                        top: ((obj.offset().top + ($innerFrame.offset() ? $innerFrame.offset().top : 0)) - $body.find('.' + settings.toolTipClass).height() - settings.yOffset) + 'px',
                        left: ((obj.offset().left + ($innerFrame.offset() ? $innerFrame.offset().left : 0)) + obj.outerWidth() + settings.xOffset) + 'px'
                    })
					.fadeIn(settings.inSpeed);
                    // Click to close tooltip
                    $body.find('.' + settings.closeTipBtn).click(function () {
                        $body.find('.' + settings.toolTipClass).fadeOut(settings.outSpeed, function () { $(this).remove(); });
                        return false;
                    });
                    return false;
                });
            }

        }); // END: return this

        // returns the jQuery object to allow for chainability.  
        return this;
    };
})(jQuery);
