/*************************************************************************
  dw_tooltip.js   requires: dw_event.js and dw_viewport.js
  version date: May 21, 2005 moved init call to body onload
  (March 14, 2005: minor changes in position algorithm and timer mechanism)
  
  This code is from Dynamic Web Coding at dyn-web.com
  Copyright 2003-5 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

var Tooltip = {

             followMouse: false,
             offX: 8,
             offY: 12,
             tipID: "tipDiv",
             arrID: "arrDiv",
             tmpID: "tmpDiv",
             showDelay: 100,
             hideDelay: 200,
             ready:false,
             timer:null,
             tip:null,
             arr:null,
             list_bool:null,
             list_html:null,
             ttready:true,
             sender:null,
             last_sender:null,
             init:function(){
                if(document.createElement&&document.body&&typeof document.body.appendChild!="undefined"){
                   if(!document.getElementById(this.tipID)){
                      var el1=document.createElement("DIV");
                      el1.id=this.tipID;
                      el1.style.position = "absolute";
                      document.body.appendChild(el1);
                   }
                   if(!document.getElementById(this.arrID)){
                      var el2=document.createElement("DIV");
                      el2.id=this.arrID;
                      el2.style.position = "absolute";
                      document.body.appendChild(el2);
                   }
                   if(!document.getElementById(this.tmpID)){
                      var el3=document.createElement("DIV");
                      el3.id=this.tmpID;
                      el3.style.position = "absolute";
                      el3.style.display = "none";
                      document.body.appendChild(el3);
                   }

                   this.ready=true;
                   this.list_bool = new Array();
                   this.list_html = new Array();
                }
             },
             show:function(e,msg,step){
                if(this.timer){
                   clearTimeout(this.timer);
                   this.timer=0;
                }
                if (!this.ttready)  return;
                var sender = document.getElementById(msg);
                if (sender == this.sender) return;

                if (sender){
                  eval("Tooltip.toggleVis('"+this.tipID+"', 'hidden'); Tooltip.toggleVis('"+this.arrID+"', 'hidden');");
                }

                if (step == 1) {
                  this.timer=setTimeout("Tooltip.show('0','" + msg +"', 2)", 500);
                  return ;
                }
              
                this.tip=document.getElementById(this.tipID);
                this.arr=document.getElementById(this.arrID);
                this.sender=sender;
                this.sender_id = this.sender.id.substring(0,this.sender.id.indexOf('_'));
                this.last_sender=sender;
                if (this.writeTip(e, msg)) {
                 this.positionTip(e, msg);
                 eval("Tooltip.toggleVis('"+this.tipID+"', 'visible');Tooltip.toggleVis('"+this.arrID+"', 'visible')");
                } 
             }
             ,writeTip:function(e,msg){
               if (this.sender.tagName == 'A'){
               }
               if (this.list_bool[msg.substring(0,msg.indexOf('_'))]) {
                if(this.tip&&typeof this.tip.innerHTML!="undefined")
                  this.tip.innerHTML=this.list_html[msg.substring(0,msg.indexOf('_'))];
                  return true;
               } else {
                  //alert(this.sender.getAttribute("show_image"));
                  xajax_get_game_description(msg, this.sender.getAttribute("show_image"));
                  return false;
               }
             },
             ajaxResponse:function(msg){
                    this.list_bool[msg.substring(0,msg.indexOf('_'))] = true;
                    this.list_html[msg.substring(0,msg.indexOf('_'))] = document.getElementById(this.tmpID).innerHTML;
                    if (this.tip && this.last_sender && this.last_sender.id == msg) {
                      this.positionTip(null, msg);
                      eval("Tooltip.toggleVis('"+this.tipID+"', 'visible');Tooltip.toggleVis('"+this.arrID+"', 'visible')");
                    }
             },
             positionTip:function(e, msg){
                if(this.tip&&this.tip.style){
                   
                   this.tip.innerHTML = this.list_html[msg.substring(0,msg.indexOf('_'))];
                  
                   position_scr.init(msg, this.tipID);
                   if (this.sender.tagName == 'A') {
                     position_scr.compareA();
                   } else {
                     position_scr.compare();
                   }
                   this.arr.innerHTML = "<img src='images/" + position_scr.image_name + "'>";
                   this.arr.style.left = position_scr.arrX+"px";
                   this.arr.style.top =  position_scr.arrY+"px";
                   this.tip.style.left = position_scr.tipX +"px";
                   this.tip.style.top =  position_scr.tipY +"px";
                }
             },
             hide:function(step){
                if(this.timer){
                   clearTimeout(this.timer);
                   this.timer=0;
                }
                
                this.timer=setTimeout("Tooltip.toggleVis('"+this.tipID+"', 'hidden'); Tooltip.toggleVis('"+this.arrID+"', 'hidden');",100);
                this.tip=null;
                this.arr=null;
                this.sender=null;
             }
             ,toggleVis:function(id,vis){
                var el=document.getElementById(id);
                if(el)el.style.visibility=vis;
             }
             ,trackMouse:function(e){
                e=dw_event.DOMit(e);
                Tooltip.positionTip(e);
             }
};

function doTooltip(e, msg) {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.clearTimer();
  var tip = document.getElementById? document.getElementById(Tooltip.tipID): null;
  if ( tip && tip.onmouseout == null ) {
      tip.onmouseout = Tooltip.tipOutCheck;
      tip.onmouseover = Tooltip.clearTimer;
  }
  var arr = document.getElementById? document.getElementById(Tooltip.arrID): null;
  if ( arr && arr.onmouseout == null ) {
      arr.onmouseout = Tooltip.tipOutCheck;
      arr.onmouseover = Tooltip.clearTimer;
  }
  
  Tooltip.show(e, msg, 1);
}

function hideTip() {
  Tooltip.hide(1);
}

Tooltip.tipOutCheck = function(e) {
  e = dw_event.DOMit(e);
  // is element moused into contained by tooltip?
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
  if (this != toEl && !contained(toEl, this) && Tooltip.sender != toEl) {
    Tooltip.hide(); 
  } else {
//       dw_event.remove(Tooltip.sender)
  }
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
  return false;
}

Tooltip.timerId = 0;
Tooltip.clearTimer = function() {
  if (Tooltip.timerId) { clearTimeout(Tooltip.timerId); Tooltip.timerId = 0; }
}

Tooltip.unHookHover = function () {
    var tip = document.getElementById? document.getElementById(Tooltip.tipID): null;
    if (tip) {
        tip.onmouseover = null; 
        tip.onmouseout = null;
        tip = null;
    }
}
dw_event.add(window, "unload", Tooltip.unHookHover, true);

