/*
 stToolTip v 1.1
 Tool Tip display object.
 Brandon Harris (bharris@gaijin.com / http://www.gaijin.com)
 Requires jQuery.
 *   
 Usage:
    Define an html element for your tooltips, thus:
    
    <div id="helpbox"><div id="helpdata"></div></div>

    Put it at the bottom.
    
    You may apply styles to the inner div all you want but the outer div 
    CANNOT have any style.

 In your initialization script:
 
  var tt = new stToolTip();
  tt.initialize();
  
  There are three main methods to use.  Each does something different:
  
  tt.l(event, elementID);
    - shows the tooltip.  Replaces the value of the innerdiv with the contents
      of elementID.
      
  tt.ld(event, "text to display");
    - shows the tooltip.  Replaces the value of the innerdiv with the second
      argument, a string.
      
  tt.hide();
    - hides the tooltip.
    
  So:
    <a href="foo.html" onmouseover="tt.l(event, 'fooHelp')" onmouseout="tt.hide()">bah</a>

    <div id="fooHelp" style="display:none">
      <div class="title">This is a title</div>
      <div class="helptext">This is a bunch of help text</div>
    </div>

  Or:
    <a href="foo.html" onmouseover="tt.ld(event, 'Click here to go to foo.html!')" onmouseout="tt.hide()">bah</a>
  
    
*/
stToolTip = function() {
  this.ie = false;
  this.moz = false;
  this.opera = false;
  this.safari = false;
  
  var self;
  this.effect;
  this.hideTimer;
  this.waitTimer;
  
  this.defaults = {
    tipID: "helpbox",
    tipData: "helpdata",
    delay: true,
    offset: 25,
    minLeftDistance: 5,
    minRightDistance: 205,
    minBottomDistance: 60,
    minTopDistance: 60,
    displayTime: 10000,
    waitTime: 1500,
    showTips: true
  }
  //this.options = this.defaults;
  
  this.toggleTips = function() {
    this.options.showTips = !this.options.showTips;
  }
  
  this.initialize = function(opts) {
    self = this;
    if (document.all) {
      this.ie = true;
    } else if (document.implementation.hasFeature("Events", "2.0")) {
      if (navigator.userAgent.indexOf("Opera") != -1) {
        this.opera = true;
      } else if (navigator.userAgent.indexOf("Safari") != -1) {
        this.safari = true;
      } else {
        this.moz = true;
      }
    }
    // Now apply defaults for things that don't exist.
    if (opts) {
      this.options = opts;
    }
    if (this.options.showTips == null) { this.options.showTips = this.defaults.showTips; }
    if (!this.options.tipID) { this.options.tipID = this.defaults.tipID; }
    if (!this.options.tipData) { this.options.tipData = this.defaults.tipData; }
    if (!this.options.delay) { this.options.delay = this.defaults.delay; }
    if (!this.options.offset) { this.options.offset = this.defaults.offset; }
    if (!this.options.minLeftDistance) { this.options.minLeftDistance = this.defaults.minLeftDistance; }
    if (!this.options.minRightDistance) { this.options.minRightDistance = this.defaults.minRightDistance; }
    if (!this.options.minBottomDistance) { this.options.minBottomDistance = this.defaults.minBottomDistance; }
    if (!this.options.minTopDistance) { this.options.minTopDistance = this.defaults.minTopDistance; }
    if (!this.options.displayTime) { this.options.displayTime = this.defaults.displayTime; }
    if (!this.options.waitTime) { this.options.waitTime = this.defaults.waitTime; }
    
  };
  this.position = function(e) {
    if (this.opera) return;
    if (!this.options.showTips) return;
    var theX = 0;
    var theY = 0;
    if (this.ie) {
      theX = event.clientX + document.documentElement.scrollLeft;
      theY = event.clientY + document.documentElement.scrollTop;
    } else if (this.moz) {
      theX = e.clientX + window.pageXOffset;
      theY = e.clientY + window.pageYOffset;
    }
    var placeX = theX + this.options.offset;
    //var placeX = theX - this.options.offset - document.getElementById(this.options.tipID).offsetWidth;
    var placeY = theY - this.options.offset - document.getElementById(this.options.tipID).offsetHeight;
    // if we're too high or too left
    if (this.ie) {
      if (placeY <= document.documentElement.scrollTop) { placeY = document.documentElement.scrollTop + 5; }
      if (placeX <= document.documentElement.scrollLeft) { placeX = document.documentElement.scrollLeft + 5; }
      document.all[this.options.tipID].style.left = placeX + "px";
      document.all[this.options.tipID].style.top = placeY + "px";   
    } else if (this.moz) {
      if (placeY <= window.pageYOffset) { placeY = window.pageYOffset + 5; }
      if (placeX <= window.pageXOffset) { placeX = window.pageXOffset + 5; }
      document.getElementById(this.options.tipID).style.left = placeX + "px";
      document.getElementById(this.options.tipID).style.top = placeY + "px";
    }
  };
  this.ld = function(e, data) {
    if (this.opera) return;
    if (!this.options.showTips) return;
    // clear timeouts.
    //this.effect.clearTimer();
    clearInterval(this.waitTimer);
    clearInterval(this.hideTimer);
    // set content
    document.getElementById(this.options.tipData).innerHTML = data;
    this.position(e);
    if (this.options.delay) {
      this.waitTimer = setInterval(sTT, this.options.waitTime); // wrap visibility
    } else {
      this.waitTimer = setInterval(sTT, 1); // wrap visibility
    }
    return true;
  };
  this.l = function(e, helpID) {
    if (this.opera) return;
    if (!this.options.showTips) return;
    // clear timeouts.
    clearInterval(this.waitTimer);
    clearInterval(this.hideTimer);
    // set content
    document.getElementById(this.options.tipData).innerHTML = document.getElementById(helpID).innerHTML;
    this.position(e);
    if (this.options.delay) {
      this.waitTimer = setInterval(sTT, this.options.waitTime); // wrap visibility
    } else {
      this.waitTimer = setInterval(sTT, 1); // wrap visibility
    }
    return;
  };
  this.hide = function() {
    if (this.opera) return;
    if (!this.options.showTips) return;
    clearInterval(this.waitTimer);
    //clearInterval(this.hideTimer);
    //if (document.getElementById(this.options.tipID).style.visibility == "visible") {
      $("#" + this.options.tipID).fadeOut("slow");
    //}
  };
  function sTT() {
    if (self.opera) return;
    clearInterval(self.waitTimer);
    clearInterval(self.hideTimer);
    //if (document.getElementById(self.options.tipID).style.visibility != "visible") {
      $("#" + self.options.tipID).fadeIn("slow");
      self.hideTimer = setInterval(hTT, self.options.displayTime);
    //}
  }
  function hTT() {
    if (self.opera) return;
    if (!self.options.showTips) return;
    clearInterval(self.waitTimer);
    //clearInterval(self.hideTimer);
    //if (document.getElementById(self.options.tipID).style.visibility == "visible") {
    $("#" + self.options.tipID).fadeOut("slow");
    //}
  }
}
