/*-------------------------------------------------------------------------------
	A Better jQuery Tooltip
	Version 1.0
	By Jon Cazier
	jon@3nhanced.com
	01.22.08
	
	Version 2.0
	bottom oriented and touch ready
	Drew Greenwell
	12.17.11
-------------------------------------------------------------------------------*/

$.fn.betterTooltip = function (options) {

    /* Setup the options for the tooltip that can be 
    accessed from outside the plugin              */
    var defaults = {
        speed: 200,
        delay: 300,
        offsetContainer: null
    };

    var options = $.extend(defaults, options);
    var $activeTip = null;
    /* Create a function that builds the tooltip 
    markup. Then, prepend the tooltip to the body */
    getTip = function () {
        var tTip =
			"<div class='tip'>" +
                "<div class='tipPoint'></div>" +
        //"<div class='tipTop'></div>" +
				"<div class='tipMid'>" +
				"</div>" +
        //"<div class='tipBtm'></div>" +
			"</div>";
        return tTip;
    }
    $("body").prepend(getTip());
    //dismiss the tip if escape is pressed
    $(document).keyup(function (event) {
        if (event.keyCode == 27) {
            $activeTip = null;
            $(".tip").hide();
        }
    });
    $("body").click(function (event) {
        if (!$(event.target).hasClass("openExternal") && $(event.target).closest('.tTip').length == 0) {
            $activeTip = null;
            $(".tip").hide();
        }
    });



    /* 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 point = $('.tipPoint');
        var tipInner = $('.tip .tipMid');

        var tTitle = this.title;
        if ($(this).attr("href") !== undefined) {
            var href = this.href;
            tTitle += '<a class="openExternal" href="' + href + '" target="_blank">View this page</a>';
            $(this).data(tip, 'tiplink', href);
            this.href = "javascript:;";
            this.target = "_self";
        }
        this.title = "";


        /* Mouse over and out functions*/
        $this.click(
			function () {
			    if ($this != $activeTip) {
			        var tWidth = $this.width();
			        var tHeight = $this.height();
			        var tOffset = $(this).offset();
			        tPointHeight = point.height();
			        var offset = (options.offsetContainer == null) ? tOffset : options.offsetContainer.offset();
			        var tLeft = offset.left;
			        var tTop = offset.top + tHeight + 5;
			        var tXPoint = (tOffset.left - offset.left) + (tWidth - 10) + "px";
			        var tYPoint = -(tPointHeight * 0.6) + "px";
			        stopTimer();
			        tip.hide();
			        tipInner.html(tTitle);
			        setTip(tTop, tLeft, tYPoint, tXPoint);
			        setTimer();
			        $activeTip = $this;
			    } else {
			        $activeTip = null;
			        stopTimer();
			        tip.hide();
			    }
			}
		);

        /* Delay the fade-in animation of the tooltip */
        setTimer = function () {
            $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, pTop, pLeft) {

            var topOffset = tip.height();
            var xTip = (left - 15) + "px";
            var yTip = top + "px";

            tip.css({ 'top': yTip, 'left': xTip });
            point.css({ 'top': pTop, 'left': pLeft });
        }

        /* This function stops the timer and creates the
        fade-in animation                          */
        showTip = function () {
            stopTimer();
            tip.animate({ "top": "+=20px", "opacity": "toggle" }, defaults.speed);
        }
    });
};
