/*
 * JTip
 * By Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 * JTip is built on top of the very light weight jquery library.
 * Adapted by Stephen Richardson to use on-page content via the rel attribute rather than ajax-ed content
 */

//on page load (as soon as its ready) call JT_init
$(document).ready(JT_init);

var createJT = true;
var title;

function JT_init() {
    $(".jTip").hover(function() {
        JT_show(this.title, this);
        title = this.title;
        this.title = "";
    },
	function() {
        this.title = title;
        $('#JT, #JT_arrow_left').stop().animate({ opacity: 0 }, { duration: 500, queue: false });
    }).click(function() {
        return false;
    });
}

function JT_show(title, item){
    var clickElementy = getAbsoluteTop(item) + 6; //set y position
    var arrowOffset = getElementWidth(item) + 44;
    var clickElementx = getAbsoluteLeft(item) + arrowOffset; //set x position

	if (createJT) {
	    $("body").append("<div id='JT'><span></span><div><p></p></div></div>"); //right side
	    $("body").append(" <div id='JT_arrow_left'></div>");
        $('#JT, #JT_arrow_left').css("opacity", 0);
 	    createJT = false;
	}

	$('#JT P').html(title);
	$('#JT_arrow_left').css({ left: clickElementx - 50 + "px", top: clickElementy + "px" });
	$('#JT').css({ left: clickElementx + "px", top: clickElementy + "px" });
	$('#JT, #JT_arrow_left').stop().animate({ opacity: 0.7 }, { duration: 500, queue: false });		
}

function getElementWidth(object) {
    return object.offsetWidth;
}

function getAbsoluteLeft(object) {
	// Get an object left position from the upper left viewport corner
    var o = object;
	var oLeft = o.offsetLeft;            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
	    var oParent = o.offsetParent;    // Get parent object reference
		oLeft += oParent.offsetLeft; // Add parent left position
		o = oParent;
	}
	return oLeft;
}

function getAbsoluteTop(object) {
	// Get an object top position from the upper left viewport corner
    var o = object;
	var oTop = o.offsetTop;            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
	    var oParent = o.offsetParent;  // Get parent object reference
	    oTop += oParent.offsetTop;  // Add parent top position
		o = oParent;
	}
	return oTop;
}