/*
* 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.
*/
/*
* Changed by Giuliano Riboni;
* osorio@intelimen.com.br
* Removed the "title";
* Changed the starter position of the div;
*/
 
//on page load (as soon as its ready) call JT_init
$(document).ready(JT_init);

function JT_init(){
 $("img[helpButton=true]").hover(
   function(){
     JT_show($(this).attr('helpForm'), $(this).attr('helpField') , $(this).attr('id'), $(this).attr('helpMessage'), $(this).attr('helpWidth'))
   }
   ,
   function(){
     $('#JT').remove()
   }
 ).click(
   function(){
     return false
   }
 );	   
}

function JT_show(formId, inputId, helpButtonId, message, textWidth){
  //Remove all divs;
  $('#JT').remove();
  //Some adjusts for the position;
  var xAdjustment   = 5;
  var yAdjustment   = 3;
  var de            = document.documentElement;
  var w             = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
  var hasArea       = w - getAbsoluteLeft( helpButtonId );
  //Set y position;
  var clickElementy = getAbsoluteTop( helpButtonId ) - 3 + yAdjustment;
  if( hasArea > ( ( textWidth * 1 ) + 75 ) ){
    //Show the tooltip in the right side;
    $("body").append("<div id='JT' style='width:" + ( textWidth * 1 )+ "px'><div id='JT_arrow_left'></div><div id='JT_copy'></div></div>");
    var arrowOffset = getElementWidth( helpButtonId ) + 11;
    //Set x position;
    var clickElementx = getAbsoluteLeft( helpButtonId ) + arrowOffset + xAdjustment;
  }else{
    //Show the tooltip in the left side;
    $("body").append("<div id='JT' style='width:" + ( textWidth * 1 )+ "px'><div id='JT_arrow_right' style='left:" + ( ( textWidth * 1 ) ) + "px'></div><div id='JT_copy'></div></div>");
     //Set x position;
    var clickElementx = getAbsoluteLeft( helpButtonId ) - ( ( textWidth * 1 ) + 15 ) + xAdjustment;
  }
  $('#JT').css( {left: clickElementx+"px", top: clickElementy+"px"} );
  $('#JT').show();
  $('#JT_copy').html( message );
}

function getElementWidth(objectId) {
  x = document.getElementById(objectId);
  return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
  // Get an object left position from the upper left viewport corner;
  o = document.getElementById(objectId);
  // Get left position from the parent object;
  oLeft = o.offsetLeft;
  // Parse the parent hierarchy up to the document element;
  while(o.offsetParent!=null) {   
    // Get parent object reference;
    oParent = o.offsetParent;
    // Add parent left position;
    oLeft += oParent.offsetLeft;
    o = oParent;
  }
  return oLeft;
}

function getAbsoluteTop(objectId) {
  // Get an object top position from the upper left viewport corner;
  o = document.getElementById(objectId);
  // Get top position from the parent object;
  oTop = o.offsetTop;
  // Parse the parent hierarchy up to the document element;
  while(o.offsetParent!=null) { 
    // Get parent object reference;
    oParent = o.offsetParent;
    // Add parent top position;
    oTop += oParent.offsetTop;
    o = oParent;
  }
  return oTop;
}

function blockEvents(evt) {
  if(evt.target){
    evt.preventDefault();
  }else{
    evt.returnValue = false;
  }
}