﻿
;(function($) {
    var tempID = 0;
    var version = '1.0';
    
    $.fn.jTooltip = function(options){
        var o = { s: this.selector, c: this.context };
       
        var settings = { 'message':'Default tooltip', 'xOffset':'15', 'yOffset':'15', 'width':'500' };
        var myTitle
        $.extend(settings, options);
                
        if ($("#jTooltip").length <= 0)
            createTooltip();       
         
        $(o.s).hover(function(e){
            this.t =  $(this).attr("tip");
           
		   // this.title = "";		    
        });
        
        
        $(o.s).mouseover(function(e){
            //set the message
            $("#jTooltip").html(this.t);            
            
            //check for our width
            if (settings.width != '0')
                $("#jTooltip").css("width", settings.width.replace("px", "") + 'px');
                       
            $("#jTooltip").css("top",(e.pageY + parseInt(settings.yOffset)) + "px")
                .css("left",(e.pageX + parseInt(settings.xOffset)) + "px").show();
                
            doBoundryCheck(settings, e);
        });    
        
        $(o.s).mouseout(function(){
           // this.title = this.t;
            $("#jTooltip").hide();
        });      

        function createTooltip(){
            var t = '<div class="jTooltip noShow" id="jTooltip"></div>';
            $("body").append(t);
        }
                
        function removeTooltip(){
            $("#jTooltip").remove();
        }
                
        function doBoundryCheck(settings, e){
            //get the current left, top of the tooltip 
            var left = parseInt($("#jTooltip").css("left")) + parseInt(settings.xOffset) + 20;
            var top = parseInt($("#jTooltip").css("top")) + parseInt(settings.yOffset) + 20;            
            //get the current height, width of the tooltip
            var height = parseInt($("#jTooltip").outerHeight());
            var width = parseInt($("#jTooltip").outerWidth());
            
            //get the current WINDOW height, width
            var winHeight = jQuery(window).height() + jQuery(window).scrollTop();
            var winWidth = jQuery(window).width() + jQuery(window).scrollLeft();
            
            var absoluteX = left + width;
            var absoluteY = top + height;
                                   
            if (absoluteX > winWidth){
                //we need to move the tooptip over                
                $("#jTooltip").css("left",(e.pageX - width - settings.xOffset) + "px");
            }
            
            if (absoluteY > winHeight){                            
                //we need to move the tooptip over
                $("#jTooltip").css("top",(e.pageY - height - settings.yOffset) + "px");                
            }                        
        }

        //Return from the main jTooltip class
        return this;
    };
                      
})(jQuery);




