var tooltip=function(){
 var id = 'ttip';
 var top = 65;
 var left = 13;
 var maxw = 180;
 var currw = maxw;
 var speed = 15;
 var timer = 30;
 var endalpha = 95;
 var alpha = 0;
 var topConst = 0;
 var ttip,t,c,b,h;
 var righty = 0;
 var ie = document.all ? true : false;
 
 return{
  show:function(v,r,w){
   if(ttip == null){
	// Create the div the first time through. 
    ttip = document.createElement('div');
    ttip.setAttribute('id',id);
    t = document.createElement('div');
    t.setAttribute('id',id + 'top');
    c = document.createElement('div');
    c.setAttribute('id',id + 'cont');
    b = document.createElement('div');
    b.setAttribute('id',id + 'bot');
    ttip.appendChild(t);
    ttip.appendChild(c);
    ttip.appendChild(b);
    document.body.appendChild(ttip);
    ttip.style.opacity = 0;
    ttip.style.filter = 'alpha(opacity=0)';
    document.onmousemove = this.pos;	
   }
   righty = r ? 1 : 0;

   // Set up to display it.  
   ttip.style.display = 'block';
   c.innerHTML = v;
   ttip.style.width = w ? w + 'px' : 'auto';
   if(!w && ie){
    t.style.display = 'none';
    b.style.display = 'none';
    ttip.style.width = ttip.offsetWidth;
    t.style.display = 'block';
    b.style.display = 'block';
   }
  if(ttip.offsetWidth > maxw){ttip.style.width = maxw + 'px'}
  //h = parseInt(ttip.offsetHeight);
  //h = parseInt(ttip.offsetHeight) + top;

  if (topConst == 0) {h = parseInt(ttip.offsetHeight) + top;}
  clearInterval(ttip.timer);
  ttip.timer = setInterval(function(){tooltip.fade(1)},timer);
  },
  
  pos:function(e){
   //alert ("element scrollLeft: "+document.documentElement.scrollLeft+", body scrollLeft: "+document.body.scrollLeft);

   var screenW = 0;
   if (self.innerHeight) {
     screenW = self.innerWidth;
   }
   else if (document.documentElement && document.documentElement.clientHeight) {
     screenW = document.documentElement.clientWidth;	 
   }
   else if (document.body) {
      screenW = document.body.clientWidth;
   }
   var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
   //var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
   if (ie) {
	   l=event.clientX + document.documentElement.scrollLeft;
   }
   else {
	   l=e.pageX;
   }
   var margin = Math.round((screenW-document.body.offsetWidth)/2);
   //ttip.style.top = (u - h) + 'px';
   ttip.style.top = u+'px';
   if (margin > 0) {
	    l=l-margin;
   } 
   var ww = ie ? parseInt(ttip.style.width) : ttip.style.width;
   var totalw = ww+l+margin+15;  
   if (righty && (totalw >= screenW-1)) {
     //alert ("this width: "+ww+", l: "+l+", margin: "+margin+", totalw: "+totalw+"; screenWidth: "+screenW);
	 l=screenW-maxw-margin-20;
   }
   //ttip.style.left=l+'px';
   ttip.style.left = (l + left) + 'px';
  
  },
  
  fade:function(d){
   var a = alpha;
   // if you're not at the beginning and fading, OR you're not at the end but hiding:
   if((a != endalpha && d == 1) || (a != 0 && d == -1)) {
     var i = speed;
     if (endalpha - a < speed && d == 1){
        i = endalpha - a;
     }
	 else if (alpha < speed && d == -1) {
            i = a;
     }
     alpha = a + (i * d);
     ttip.style.opacity = alpha * .01;
     ttip.style.filter = 'alpha(opacity=' + alpha + ')';
   }
   else {
       clearInterval(ttip.timer);
       if(d == -1){ttip.style.display = 'none'}
   }
 },
 hide:function(){
  clearInterval(ttip.timer);
   ttip.timer = setInterval(function(){tooltip.fade(-1)},timer);
   topConst=0;
  }
 };
}();
