// Author: Alex littlejohn

(function($){
    $.fn.extend({
        tooltip: function(options) {
            
            var defaults = {
                offset: 10,
                beforeShowTooltip: function(content){},
                showTooltip: function(content){
                    content.show();
                },
                hideTooltip: function(content){
                    content.hide();
                },
                contentElement: "#tooltipcontent"
            };
            
            var defaultElement = "thetooltip";
            
            var tooltip;
            
            var options =  $.extend(defaults, options);
            var o = options;            
            
            createTooltip(defaultElement, o.contentElement);
            
            //Iterate over the current set of matched elements
            return this.each(function() {
                var obj = $(this);
                obj.hover(function() {
                    o.beforeShowTooltip(obj);
                    positionTooltip(obj);
                }, function() {
                    o.hideTooltip(tooltip);
                });
            });
            
            // creates the tooltip element
            function createTooltip(container, content) {
                
                if($("#"+container).length >= 1) {
                    
                } else {
                    $("body").append("<div id="+container+"></div>")
                }
                $("#"+container).append($(content));
                tooltip = $("#"+container);
            }
            
            function positionTooltip(element) {
                var element = element;
                
                var tooltipDimentions = getDimentions(tooltip);
                var elementDimentions = getDimentions(element);
                
                var dimentions = $.fn.positioningRight(tooltipDimentions, o.offset, elementDimentions);
                
                tooltip.css('top', dimentions['top']);
                tooltip.css('left', dimentions['left']);
                
                o.showTooltip(tooltip);
            }
            
            function getDimentions(element) {
                var height = element.outerHeight(true);
                var width = element.outerWidth(true);
                var top = $(element).offset().top;
                var left = $(element).offset().left;
                var info = new Array();
                
                // Set dimensions
                info['height'] = height;
                info['width'] = width;
                
                // Set position
                info['top'] = top;
                info['left'] = left;
                
                return info;
            }
        }
    });
})(jQuery);

(function($){
    //positioning the tooltip above center and static to the object
    $.fn.positioningTop = function(contentInfo, offset, targetInfo) {
        contentInfo['top'] = targetInfo['top'] - offset - contentInfo['height'];
        contentInfo['left'] = (targetInfo['left'] + (targetInfo['width'] / 2)) - (contentInfo['width'] / 2);
        
        return contentInfo;
    };
    
    //positioning the tooltip left center and static to the object
    $.fn.positioningLeft = function(contentInfo, offset, targetInfo) {
        contentInfo['top'] = (targetInfo['top'] + (targetInfo['height'] / 2)) - (contentInfo['height'] / 2);
        contentInfo['left'] = targetInfo['left'] - contentInfo['width'] - offset;
        
        return contentInfo;
    };
    //positioning the tooltip left center and static to the object
    $.fn.positioningRight = function(contentInfo, offset, targetInfo) {
        contentInfo['top'] = (targetInfo['top'] + (targetInfo['height'] / 2)) - (contentInfo['height'] / 2);
        contentInfo['left'] = targetInfo['left'] + targetInfo['width'] + offset;
        
        return contentInfo;
    };
})(jQuery);
