/*-------------------------------------------------------------------------------
	A Better jQuery Tooltip
	Version 1.0
	By Jon Cazier
	jon@3nhanced.com
	01.22.08

added funtions by Alvin Casco 03 Mar 10
-------------------------------------------------------------------------------*/
(function($) {
    $.fn.betterTooltip = function(options){
	
	/* Setup the options for the tooltip that can be 
	   accessed from outside the plugin              */
	var defaults = {
		speed: 200,
		delay: 300
	};
	
	options = $.extend(defaults, options);
	
	/* Create a function that builds the tooltip 
	   markup. Then, prepend the tooltip to the body */
	var getTip = function() {
		var tTip = 
			"<div class='tip'>" +
				"<div class='tipMid'>"	+
				"</div>" +
				"<div class='tipBtm'></div>" +
			"</div>";
		return tTip;
	}
	$("body").prepend(getTip());
	
	/* Give each item with the class associated with 
	   the plugin the ability to call the tooltip    */
	$(this).each(function(){
		
		var $this = $(this);
		var tip = $('.tip');
		var tipInner = $('.tip .tipMid');
		
		var tTitle = (this.title);
		this.title = "";
		/*removed the following
		var offset = $(this).offset();
		var tLeft = offset.left;
		var tTop = offset.top;
		//var tWidth = $this.width();//not referenced
		//var tHeight = $this.height();//not referenced
		*/
                

		/* Mouse over and out functions*/
		$this.hover(
                    function() {
                        var IE = document.all?true:false;
                        if (!IE) document.captureEvents(Event.MOUSEMOVE)
                            document.onmousemove = getMouseXY;
                            var tempX = 0;
                            var tempY = 0;
                            function getMouseXY(e) {
                                if (IE) { // grab the x-y pos.s if browser is IE
                                    //tempX = event.clientX + document.body.scrollLeft;
                                    //tempY = event.clientY + document.body.scrollTop;

                                    var scrollPos = getScroll();
                                    tempX = event.clientX + scrollPos['scrollLeft'];
                                    tempY = event.clientY + scrollPos['scrollTop'];
                                }
                                else {  // grab the x-y pos.s if browser is NS
                                    tempX = e.pageX;
                                    tempY = e.pageY;
                                }
                                if (tempX < 0){tempX = 0;}
                                if (tempY < 0){tempY = 0;}
                                 setTip(tempY,tempX);
                            }

                        //var offset = $(this).offset();
                        tipInner.html(tTitle);
                        //setTip(tTop,tLeft);
                        setTimer();
                    },
                    function() {
                        stopTimer();
                        tip.hide();
                    }
		);

                /*added this function for IE*/
                getScroll = function(){
                    if (self.pageYOffset) {
                        return {scrollTop:self.pageYOffset,scrollLeft:self.pageXOffset};
                    } 
                    else if (document.documentElement && document.documentElement.scrollTop) {
                        // Explorer 6 Strict
                        return {scrollTop:document.documentElement.scrollTop,scrollLeft:document.documentElement.scrollLeft};
                    } 
                    else{
                        return {scrollTop:document.body.scrollTop,scrollLeft:document.body.scrollLeft};
                    }
                }

		/* Delay the fade-in animation of the tooltip */
		setTimer = function() {
                    $this.showTipTimer = setInterval("showTip()", defaults.delay);
                    //$this.showTipTimer = setInterval(showTip(), defaults.delay);
		}
		
		stopTimer = function() {
                    clearInterval($this.showTipTimer);
		}
		
		/* Position the tooltip relative to the class 
		   associated with the tooltip                */
		setTip = function(top, left){
                    var topOffset = tip.height();
                    //var xTip = (left-30)+"px";
                    var xTip = (left-60)+"px";
                    //var yTip = (top-topOffset-60)+"px";
                    var yTip = (top-topOffset-45)+"px";
                    tip.css({'top' : yTip, 'left' : xTip});
		}
		
		/* This function stops the timer and creates the
		   fade-in animation                          */
		showTip = function(){
                    stopTimer();

                    //tip.animate({"top": "+=20px", "opacity": "toggle"}, defaults.speed);
                    tip.animate({"opacity": "toggle"}, defaults.speed);
		}
	});
        return this;
    };
})(jQuery);