/* Usefull method to debug object property */
function LogObject(obj,console_on)
{

  var props = new Array();
  for (var i in obj)
  {
    props.push(i);
  }

  props.sort();
  if(console_on)
  {
    console.log(props.join("\n"));
  }
  else
  {
    return props.join("\n");
  }
}

/* Helper variable to store and retrieve the tooltip easily */
var ToolTipRegistrar = {
  marker:new Array()
};

/* Javascript object which hold all the method and variable to handle the tooltip */
function ToolTip
(
  parentContainer,
  sourceElement,
  baseClassName,
  currentClassName,
  product_id,
  width,
  max_height,
  options
)
{
  var id = "";
  var hide_timeout = -1;
  this.hidden = true;
  var leftOffset = 0;

  this.MoveTo = function
  (
    event,
    parent
  )
  {

    var top,left;

    /* compute absolute top and left position (relative to the body element) where the tooltip should be displayed */
    top =
      $('product_' + id ).cumulativeOffset()[1] +
      Math.round($('product_' + id).offsetHeight * 0.75);

    left =
      $('product_' + id ).cumulativeOffset()[0] +
      leftOffset;

    $('product_' + id + '_tool_tip')
      .setStyle
      ({
        left: left + 'px',
        top: top + 'px'
      });
  };

  this.Show = function(event, parent)
  {
    /* clear timeout to prevent tooltip to be hidden */
    if(-1 != hide_timeout)
    {
      window.clearTimeout(hide_timeout);
      hide_timeout = -1;
    }

    /* force display of the tooltip */
    $('product_' + id + '_tool_tip').setStyle({display: "block"});
    $('product_' + id + '_tool_tip').setStyle({visibility: "visible"});

    /* change state of the tooltip and move it if it was previously hidden */
    if (this.hidden)
    {
      this.hidden = false;
      this.MoveTo(event, parent);
    }
  };

  /* private function which really hide the tooltip */
  function HideToolTip()
  {
    this.hidden = true;
    $('product_' + id + '_tool_tip').setStyle({display: "none"});
    hide_timeout = -1;
  }

  /*
    function which set a timeout after which the tooltip should be hidden if the
    pointer does not enter on one of the tootip "shower" element.
  */
  this.Hide = function()
  {
    hide_timeout = window.setTimeout(
      function (){HideToolTip()},
      600
    );
  };

  /*
    function which register the tooltip and assign the correct dimension to the
    element which compose the tooltip
  */
  this.Build = function
  (
    parentContainer,
    sourceElement,
    baseClassName,
    currentClassName,
    product_id,
    current_width,
    max_height,
    options
  )
  {
    if('undefined' != typeof options.leftOffset)
    {
      leftOffset = options.leftOffset;
    }
    /* Set up private and public id width the product passed as an argument */
    id = this.id = product_id;

    /*Register the tool_tip */
    ToolTipRegistrar.marker[product_id]= this;

    /* Set the width of the tool_tip if was passed as a number argument */
    if('Number' == typeof current_width)
    {
      $('product_' + product_id + '_tool_tip')
        .setStyle
        ({
          width: current_width + "px"
        });
      $('product_' + product_id + '_tool_tip')
        .select('.content_wrapper')[0]
        .setStyle
        ({
          width: current_width + "px"
        });
    }

    var parent_height =
      sourceElement
        .parent('.container_box')
        .offsetHeight;

    var height = $('product_' + product_id + '_tool_tip')
      .select('.content_wrapper')[0]
      .offsetHeight - 8;
    var width = $('product_' + product_id + '_tool_tip')
      .select('.content_wrapper')[0]
      .offsetWidth - 18;

    var top =
      sourceElement.parent('tr').offsetTop +
      Math.round
      (
        sourceElement.parent('tr').offsetHeight
        /
        2
      );

    $('product_' + this.id + '_tool_tip')
      .hover
      (
        this.Show,
        this.Hide,
        {
          enterDelay:300,
          leaveDelay:0
        }
      )

    $('product_' + this.id + '_tool_tip')
      .select('.select_hidder')[0]
      .setStyle
      ({
        height: height + "px",
        width: width + "px"
      });

  };
  this.Build
  (
    parentContainer,
    sourceElement,
    baseClassName,
    currentClassName,
    product_id,
    width,
    max_height,
    options
  );
}
