//============================================================================
// Copyright (c) 2006 Pelco. All rights reserved.
//
// This file contains trade secrets of Pelco.  No part may be reproduced or
// transmitted in any form by any means or for any purpose without the express
// written permission of Pelco.
//
// $File: tooltip.js$
// $Revision: 1$
// $Date: 01/16/2007 08:46:49 AM$
// $Author: bkimbell$
//============================================================================

gTooltips = new Array();

////////////////////////////////////////////////////////////////////////////////
// creates a new dynamic tooltip using the specified objects
function Tooltip(divId, tipId) {
  this.init(divId, tipId);
}

////////////////////////////////////////////////////////////////////////////////
Tooltip.prototype.init = function(divId, tipId) {
  // behavior defaults
  this.align = "left";
  this.valign = "top";
  this.where = "right";
  this.hideDelayMs = 200;
  this.showDelayMs = 200;

  // load the active object
  this.divId = divId;
  this.div = document.getElementById(divId);

  // load the tip object
  if (tipId == null) {
    this.tipId = Math.round(10000*Math.random()) + "-tooltip";
  } else {
    this.tipId = tipId;
  }

  // find or create the tooltip div
  this.tip = document.getElementById(this.tipId);
  if (this.tip == null) {
    document.write("<div id=\"" + this.tipId + "\"></div>\n");
    this.tip = document.getElementById(this.tipId);
  }

  // associate the tip with our display function
  this.div.onmouseover = new Function("tooltipShow('" + this.tipId + "', -1)");
  this.div.onmouseout = new Function("tooltipHide('" + this.tipId + "', -1)");
  this.tip.onmouseover = new Function("tooltipShow('" + this.tipId + "', -1)");
  this.tip.onmouseout = new Function("tooltipHide('" + this.tipId + "', -1)");

  // set the correct style for the tooltip
  this.tip.className = "tooltip";
  this.tip.style.display = "inline";
  this.tip.style.position = "absolute";
  this.tip.style.visibility = "hidden";

  // track the tooltip globally
  gTooltips[this.tipId] = this;
}

////////////////////////////////////////////////////////////////////////////////
Tooltip.prototype.show = function () {
  // transfer object properties to the tooltip div
  if (this.width != null) { this.tip.style.width = this.width; }
  if (this.height != null) { this.tip.style.height = this.height; }
  if (this.content != null) { this.tip.innerHTML = this.content; }

  // figure out the position of the tooltip
  var pos = findPosition(this.div);
  var x = pos[0];
  var y = pos[1];

  // adjust the position based on the style
  var divSize = findSize(this.div);
  var tipSize = findSize(this.tip);

  //calc the tips location
  if (this.where      == 'above') {y -= (tipSize[1] + 4)} 
  else if (this.where == 'below') {y += (divSize[1] + 4);} 
  else if (this.where == 'left')  {x -= (tipSize[0] + 4);}
  if (this.where == 'right')      {x += (divSize[0] + 4);}

  if (x == pos[0]) {
    if(this.align      == 'right' ) {x += divSize[0] - tipSize[0];}
    else if(this.align == 'center') {x += (divSize[0] - tipSize[0])/2;}
  }

  if (y == pos[1]) {
    if(this.valign      == 'bottom' ) {y += divSize[1]  - tipSize[1];}
    else if(this.valign == 'center')  {y += (divSize[1] - tipSize[1])/2;}
  }

  // show the tooltip in the right place
  this.tip.style.top = y;
  this.tip.style.left = x;
  this.tip.style.visibility = "visible";
}

////////////////////////////////////////////////////////////////////////////////
Tooltip.prototype.hide = function () {
  this.tip.style.visibility = "hidden";
}

////////////////////////////////////////////////////////////////////////////////
function tooltipShow(id, delayMs) {
  var tip = gTooltips[id];
  clearTimeout(tip.timer);

  if (delayMs == 0) {
    tip.show();
  } else {
    tip.timer = setTimeout("tooltipShow('" + id + "', 0)", tip.showDelayMs);
  }
}

////////////////////////////////////////////////////////////////////////////////
function tooltipHide(id, delayMs) {
  var tip = gTooltips[id];
  clearTimeout(tip.timer);

  if (delayMs == 0) {
    tip.hide();
  } else {
    tip.timer = setTimeout("tooltipHide('" + id + "', 0)", tip.hideDelayMs);
  }
}


