/*-------------------------------------------------------------------------------
A Better jQuery Tooltip
Version 1.0
By Jon Cazier
jon@3nhanced.com
01.22.08
-------------------------------------------------------------------------------*/

$.fn.tipGreen = function(options) {

    /* Setup the options for the tooltip that can be 
    accessed from outside the plugin              */
    var defaults = {
        speed: 200,
        delay: 300
    };

    var options = $.extend(defaults, options);

    /* Create a function that builds the tooltip 
    markup. Then, prepend the tooltip to the body */
    getTipgreen = function() {
        var tTipgreen =
			"<div class='tipgreen'>" +
				"<div class='tipMidgreen'>" +
				"</div>" +
				"<div class='tipBtmgreen'></div>" +
			"</div>";
        return tTipgreen;
    }
    $("body").prepend(getTipgreen());

    /* Give each item with the class associated with 
    the plugin the ability to call the tooltip    */
    $(this).each(function() {

        var $this = $(this);
        var tipgreen = $('.tipgreen');
        var tipInner = $('.tipgreen .tipMidgreen');

        var tTitle = (this.title);
        this.title = "";

        var offset = $(this).offset();
        var tLeft = offset.left;
        var tTop = offset.top;
        var tWidth = $this.width();
        var tHeight = $this.height();

        /* Mouse over and out functions*/
        $this.hover(
			function(e) {
			    tipInner.html(tTitle);
			    var topOffset = tipgreen.height();
			    var xTip = (e.pageX - 70) + "px";
			    var yTip = (e.pageY - topOffset - 20) + "px";
			    tipgreen.css({ 'top': yTip, 'left': xTip });
			    tipgreen.show();
			    //setTipgreen(tTop, tLeft);
			    //setTimergreen();
			},
			function() {
			    //stopTimergreen();
			    tipgreen.hide();
			}
		);

        /* Delay the fade-in animation of the tooltip */
        setTimergreen = function() {
            $this.showTipTimer = setInterval("showTipgreen()", defaults.delay);
        }

        stopTimergreen = function() {
            clearInterval($this.showTipTimer);
        }

        /* Position the tooltip relative to the class 
        associated with the tooltip                
        setTipgreen = function(top, left){
        var topOffset = tipgreen.height();
        var xTip = (tLeft-30)+"px";
        var yTip = (e.pageY-topOffset-60)+"px";
        tipgreen.css({'top' : yTip, 'left' : xTip});
        }*/

        /* This function stops the timer and creates the
        fade-in animation                          */
        showTipgreen = function() {
            stopTimergreen();
            tipgreen.animate({ "top": "+=0px", "opacity": "toggle" }, defaults.speed);
        }
    });
};
