/**
* jQuery simpleTooltip plugin
*
* @author Marius ILIE (http://dev.mariusilie.net) / Improved by Aamir Afridi (http://www.aamirafridi.com - info@aamirafridi.com)
* @date 2009-08-06
* @example $("link").simpletooltip({html : 'My Tooltip', mouseOffset: {x : 10 , y : 12}})
* @example $("link").simpletooltip({ opacity : 0.8, css : { 'padding' : '8px' , 'border' : '2px solid #ccc' } })
* @desc create a tooltip effect for any html element's title attribute or any text provided
*
* @name simpletooltip
* @type jQuery
* @param String - html - custom text which will overwrite the text in element's title attribute
* @param String - errorText - text which will appear if there is no title attribute found or html provide to the plugin otherwise leaving this empty will diable the plugin
* @param String - cssClass - if provided, It will overwrite any css provided to the plugin
* @param Number - opacity - the transparency of the tooltip. From 0.0 to 1. Should work on IE but haven't tested
* @param String - fadeSpeed - the animation speed of the tooptip to fadeIn
* @param Number  - mouseOffset.x - the position of the tooltip on x-axis
* @param Number  - mouseOffset.y - the position of the tooltip on y-axis
* @param Array  - css - css properties will be applied on the tooltip
* @cat Plugin
*/
 
(function($, options) {
    $.fn.simpletooltip = function(options) {
        var o = $.extend(true, {}, $.fn.simpletooltip.defaults, options);
        return this.each(function() {
            var title = $(this).attr("title");
            $(this).attr('title', '');
            var text = (title != '') ? title : o.errorText, st;
            if (o.html == '' && text == '') return;
            $(this).hover(function(e) {
                var tipX = e.pageX + o.mouseOffset.x,
                                tipY = e.pageY + o.mouseOffset.y;
                st = $('<div></div>').appendTo('body').html(($.trim(o.html) != '') ? o.html : text)
                    .css({ 'position': 'absolute', 'z-index': 100, 'display': 'none', 'left': tipX, 'top': tipY });
                if ($.trim(o.cssClass) != '') st.addClass(o.cssClass);
                st.css(o.css).fadeTo(o.fadeSpeed, o.opacity);
            }, function() {
                st.fadeOut(o.fadeSpeed, function() { $(this).remove(); });
            })
                .mousemove(function(e) {
                    if (st) {
                        var tipX = e.pageX + o.mouseOffset.x,
                                    tipY = e.pageY + o.mouseOffset.y,
                            tipWidth = st.outerWidth(true),
                            tipHeight = st.outerHeight(true);
                        if (tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
                        if ($(window).height() + $(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
                        st.css({ 'left': tipX, 'top': tipY });
                    }
                });
        });
    }
    $.fn.simpletooltip.defaults = {
        html: '',
        errorText: '', //Leave it empty which will show nothing
        cssClass: 'ui-state-highlight',
        opacity: 1,
        fadeSpeed: 'fast',
        mouseOffset: {
            x: 10,
            y: 10
        },
        css: {
            'padding': 7,
            'width': '200px',
            '-moz-border-radius': 5,
            '-webkit-border-radius': 5
        }
    };
})(jQuery);
