/*
 * 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(){
  $(".jtip").hover(function(){JT_show(this.id,this.alt)},function(){$('#JT').remove()}).click(function(){return false});
}

function JT_show(linkId,title){
  if(title == false)title="&nbsp;";
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var hasArea = w - getAbsoluteLeft(linkId);
  var clickElementy = getAbsoluteTop(linkId) - 3; //set y position

  //$('#' + linkId).css('cursor','pointer');

  var wwidth = 250;
  if(hasArea>(wwidth)){
    $("body").append("<div id='JT'><div id='JT_close'>"+title+"</div></div>");//right side
    var arrowOffset = getElementWidth(linkId) + 11;
    var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
  }else{
    $("body").append("<div id='JT'><div id='JT_close'>"+title+"</div></div>");//left side
    var clickElementx = getAbsoluteLeft(linkId) - (wwidth * 0.6); //set x position
  }

  $('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
  $('#JT').show();

}

function getElementWidth(objectId) {
  x = document.getElementById(objectId);
  return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
  o = document.getElementById(objectId);
  oLeft = o.offsetLeft;
  while(o.offsetParent!=null) {
    oParent = o.offsetParent;
    oLeft += oParent.offsetLeft;
    o = oParent;
  }
  return oLeft;
}

function getAbsoluteTop(objectId) {
  o = document.getElementById(objectId);
  oTop = o.offsetTop;
  while(o.offsetParent!=null) {
    oParent = o.offsetParent;
    oTop += oParent.offsetTop;
    o = oParent;
  }
  return oTop;
}

