/*
 * 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.
 */

//on page load (as soon as its ready) call JT_init
$(document).ready(JT_init);

function JT_init(){
	$("a.jtip").hover(function(){
	  	JT_show(this)
	},function(){
		$('#JT').remove();
		if (jQuery.browser.msie && navigator.userAgent.toLowerCase().indexOf('6')>-1)$('select').show();
	}).click(function(){
 		return false;
  	});	
  	
  	$("a.help").attr("title", function() {
  		this.tip = this.title;
  		return "";
  	}).hover(function(){
  		JT_show(this)
  	},function(){
  		$('#JT').remove();
  		if (jQuery.browser.msie && navigator.userAgent.toLowerCase().indexOf('6')>-1)$('select').show();
  	}).click(function(){
  		return false;
  	});
}

function JT_show(elem){
	if(elem.rel == false ) { 
    rel="&nbsp;";
  } else {
    rel = elem.rel;
  }

	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || $("body").clientWidth;
	var clickElementy = getAbsoluteTop(elem) - 3; //set y position

  if($("#"+elem.id+" + label").get() != "") {
    var title = $("#"+elem.id+" + label").html().replace(":", "");
  } else if($(elem).get() != "") {
    var title = $(elem).html().replace(/<(.*)>/, "");
    title = $.trim(title);
  } else {
    var title = "";
  }

	//$("body").append("<div id='JT' style='width:250px'><div id='JT_arrow_left'></div><div id='JT_copy'>"+rel+"</div></div>");//right side
	//$("body").append("<div id='JT' style='width:250px'><div class='bor'><div id='JT_title'>"+title+"</div><div id='JT_copy'>"+rel+"</div></div><div class='shade'><div id='JT_title'>"+title+"</div><div id='JT_copy'>"+rel+"</div></div></div>");//right side
	
  if (elem.tip){
    $("body").append("<div id='JT' style='width:250px'><div class='tipbor'><div id='JT_copy'>"+elem.tip+"</div></div></div>");//right side
  } else {
    $("body").append("<div id='JT' style='width:250px'><div class='bor'><div id='JT_title'>"+title+"</div><div id='JT_copy'>"+rel+"</div></div></div>");//right side
	}
  
	var arrowOffset = getElementWidth(elem) + 11;
	var clickElementx = getAbsoluteLeft(elem) + arrowOffset+10; //set x position
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
	if (jQuery.browser.msie && navigator.userAgent.toLowerCase().indexOf('6')>-1)$('select').hide();
}

function getElementWidth(o) {
	return o.offsetWidth;
}

function getAbsoluteLeft(o) {
	// Get an object left position from the upper left viewport corner
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(o) {
	// Get an object top position from the upper left viewport corner
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function blockEvents(evt) {
  if(evt.target){
    evt.preventDefault();
  } else {
    evt.returnValue = false;
  }
}
