////////////
// [ubertooltip.js] v1.6
// 2002-07-23
// Author: chrisken
// URL: http://www.cs.utexas.edu/users/chrisken/ubertooltip.html
// Supports: alphaAPI.js
////

// wrapper to automate the tooltip install
function installTip() {
	if( document.layers ) return; // ns4 = bad
	function mouseMove(e) { if( window.tip ) tip.mouseMove( e ); } // update tooltip coordinates if the object exists
	document.onmousemove = mouseMove; // capture mouse movement in IE/Opera
	if( document.addEventListener ) document.addEventListener( "mousemove", mouseMove, true ); // capture in Mozilla/NS6
	writeTip( 'tipDiv' );
	
	// Create abstract functions until the page has loaded and a full ubertooltip can be created
	tip = new Object();	
	tip.show = function( x ) { };
	tip.hide = function( x ) { };
	tip.mouseMove = function( x ) { };
	tip.loaded = false;
}

// outputs the tooltip <div> with the minimum amount of css
function writeTip( divName ) {
	document.write( '<div id="' + divName + '" style="position: absolute; visibility: hidden; top: 0px; left: 0px; ' );
	document.write( 'filter: alpha(opacity=100); -moz-opacity: 0; z-index: 37"></div>' );
}

function uberToolTip( id, delay ) {
	this.id = id || "tipDiv";
	this.mouseX = 0;
	this.mouseY = 0;
	this.updateToolTip = false;
	this.obj = null;
	this.fader = null;
	this.delay = delay || 40;
	
	this.getMouseX = function() { return this.mouseX; }
	this.getMouseY = function() { return this.mouseY; }
	
	// display (fade in, if supported) the tooltip and update coords when the mouse moves
	this.show = function( html ) {
		if( document.layers || window.opera ) return;
		this.updateToolTip = true;
		if( this.fader ) { // alphaAPI was included and the browser supports fading
			this.fader.setTrans( 0 );
			this.fader.fadeIn();
		}
		this.update();
		writeLayerObj( this.obj, html );
		showLayerObj( this.obj );
		
		SetControlVisibility(this, true);
	}
	
	// update coordinates of the layer based on mouse coords;
	this.update = function() {
		this.obj.style.left = this.getMouseX();
		this.obj.style.top = this.getMouseY() + ( document.all? 20: 15 );
	}
	
	// hide layer and stop updating with mouse
	this.hide = function() {
		if( document.layers || window.opera ) return;
		hideLayerObj( this.obj );
		this.updateToolTip = false;
		if( this.fader ) this.fader.setTrans( 0 );
		this.obj.style.left = this.obj.style.top = 0;
		writeLayerObj( this.obj, "" );
		
		SetControlVisibility(this, false);
	}

	// grab a reference to the <div> and initialize the fader
	this.init = function() {
		if( document.layers || window.opera ) return;
		this.obj = document.getLayer( this.id );
		if( !this.obj ) {
			alert( 'Error: tooltip layer initialized to "' + this.id + '",\nbut a <div> with that id does not exist.' );
			return;
		}
		this.loaded = true;
		if( window.alphaAPI && !document.layers && ( document.all || ( document.getElementById && document.addEventListener ) ) ) {
			// only allow IE and Mozilla
			this.fader = new alphaAPI( this.id, this.delay, null, 100, 0, null, 10 );
			this.fader.setTrans( 0 );
			this.fader.norepeat( );
		}	
	}
	
	this.mouseMove = function( e ) {
		var mouseX, mouseY;
		if( window.event && !document.layers ) {
			// Internet Explorer & Opera mouse detection
			mouseX = event.clientX;
			mouseX +=  document.body.scrollLeft;
			mouseX -= 100;
			if (mouseX < 5) mouseX = 5;
			if (mouseX + 230 > screen.width) mouseX = screen.width - 230;
			mouseY = event.clientY;
			mouseY += document.body.scrollTop;
		} else {
			// Netscape and Mozilla
			mouseX = e.pageX - 100;
			if (mouseX < 5) mouseX = 5;
			if (mouseX + 230 > screen.width) mouseX = screen.width - 230;
			mouseY = e.pageY + 10;
		}
		this.mouseX = mouseX;
		this.mouseY = mouseY;
		if( this.obj && this.updateToolTip ) this.update();
	}
	
	return this;
}

///////
// Mini-dhtml library with NS4 support removed
////

// Use getLayer so that IE 4 works the same as IE 5+ & Mozilla
if( document.getElementById ) document.getLayer = document.getElementById;
else if( document.all ) document.getLayer = function( id ){ return document.all[ id ]; }

// wrapper to showLayerObj
function showLayer( id ) { showLayerObj( document.getLayer( id ) ); }

// show a layer object
function showLayerObj( layer ) { layer.style.visibility = "visible"; }

// wrapper to hideLayerObj
function hideLayer( id ) { hideLayerObj( document.getLayer( id ) ); }

// hide a layer object
function hideLayerObj( layer ) { layer.style.visibility = "hidden"; }

// wrapper to writeLayerObj
function writeLayer( id, html ) { writeLayerObj( document.getLayer( id ), html ); }

// write html to a layer
function writeLayerObj( layer, html ) { // pass in the layer object
	if( typeof( layer.innerHTML ) == "undefined" ) {
		if( !document.layers ) { // Opera
		} else { // Netscape
		}
	} else { // IE and Mozilla are cool
		html = html.replace(/&/,"&amp;");
		layer.innerHTML = html;
	}
}

// set autoInstall to false if you want to customize your uberTooltip installation
if( !window.autoInstall && window.autoInstall != false) installTip();

/////////
// [ alphaAPI.js ] v1.2
// 2002-03-04
// Author: chrisken
// URL: http://www.cs.utexas.edu/users/chrisken/alphaapi.html
/////

function alphaAPI(id, fadeInDelay, fadeOutDelay, startTrans, stopTrans, offsetTime, deltaTrans) {
	this.obj = ("alphaAPIobj" + alphaAPI._count++); // allow setTimeout references to the current object
	eval(this.obj + " = this");
	this.layerObj = document.getElementById( id );
	this.fadeInDelay = fadeInDelay || 40;
	this.fadeOutDelay = fadeOutDelay || this.fadeInDelay;
	this.startTrans = startTrans;
	this.stopTrans = stopTrans;
	this.offsetTime = (offsetTime || 0) * 1000;
	this.deltaTrans = deltaTrans || 10;
	this.timer = null;
	this.command = null;
	this.paused = false;
	this.repeat = true;
	this.started = false;
	
	this.norepeat = function() {
		this.repeat = false;
	}
	
	this.setTransBy = function(deltaTrans) {
		this.setTrans(this.getTrans() + deltaTrans);
	}
	
	this.toggle = function() {
		if( !this.started ) { // we haven't started fading yet
			this.start();
		} else if( this.paused ) {
			this.paused = false;
			this.unpause();
		} else {
			this.paused = true;
			this.pause();
		}
	}
	
	this.timeout = function(command, delay) {
		this.command = command;
		this.timer = setTimeout(command, delay);
	}
	
	this.setTrans = function(opacity) {
		if( !this.layerObj ) return;
		if( this.layerObj.filters ) {
			this.layerObj.filters.alpha.opacity = opacity;
		}
		if (!document.all && this.layerObj.style.setProperty) this.layerObj.style.setProperty("-moz-opacity", opacity / 100, "");
	}	

	this.getTrans = function() {
		if( !this.layerObj ) return 0;
		if (document.all) {
			if( typeof(this.layerObj.filters.alpha.opacity) != "undefined" )
				return this.layerObj.filters.alpha.opacity;
		} else if( this.layerObj.style.getPropertyValue )
				return this.layerObj.style.getPropertyValue("-moz-opacity") * 100;
		return 100;
	}
	
	this.start = function() {
    	if( this.layerObj ) {
    		this.started = true;
        	this.setTrans( this.startTrans );
        	if( this.startTrans > this.endTrans ) this.timeout( this.obj + ".fadeOut()", this.offsetTime );
        	else this.timeout( this.obj + ".fadeIn()", this.offsetTime );
        }
    }
    
    this.unpause = function() {
    	if( !this.started ) { // we never started, so we need to start instead of unpausing
    		this.start();
    	} else eval(this.command); // we were explicitly paused
    }
    
    this.pause = function() {
    	this.stopTimer();
    }
    
    this.stopTimer = function() {
    	clearTimeout(this.timer);
    	this.timer = null;
		this.started = false;
    }
    
    this.stop = function() {
    	this.command = null;
    	this.stopTimer();
    }

    this.fadeOut = function() {
    	this.stopTimer();
	    if (this.getTrans() > this.stopTrans){
	        this.setTransBy(-1 * this.deltaTrans);
	        this.timeout(this.obj + ".fadeOut()", this.fadeOutDelay);
	        }
	    else {
	        if (this.repeat) this.timeout(this.obj + ".fadeIn()", this.fadeInDelay);
	        }
	    }   
    
	this.fadeIn = function() {
		this.stopTimer();
	    if (this.getTrans() < this.startTrans){
	        this.setTransBy(this.deltaTrans);
	        this.timeout(this.obj + ".fadeIn()", this.fadeInDelay);
        }
	    else {
	        if (this.repeat) this.timeout(this.obj + ".fadeOut()", this.fadeOutDelay);
	    }
	}
		
	return this;	
}

alphaAPI._count = 0;

//
// On IE, controls such as SELECT, OBJECT, IFRAME (before 5.5)
// are window based controls.  So, if the tip and these
// controls overlap, tip would be hidden behind them.  Thus
// one needs to turn the visibility of these controls off when the
// tip is showing, and turn their visibility back on
// when the tip is hiding.
//
// --
// Ali Amirnezhad
// WEBILIX websolutions
// http://www.webilix.com
//
	
function GetX(obj) {
	var x = 0;

	do {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	} while (obj);
	
	return x;
}

function GetY(obj) {
	var y = 0;
	
	do {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}	while (obj);
	
	return y;
}

var overlap = new Array();
function SetControlVisibility(tip, hide) {
  if (!document.all) return;
  
  var i = 0;
  for (i=0; i<overlap.length; i++) overlap[i].style.visibility = 'visible';
  overlap = new Array();
  if (!hide) return;
  
	var x = tip.mouseX;
	var y = tip.mouseY;
	var w = document.getElementById(tip.id).offsetWidth;
	var h = document.getElementById(tip.id).offsetHeight;
	
	var i; var t;
	var tags = ['SELECT', 'OBJECT', 'IFRAME'];
	
	for (t=0; t<tags.length; t++) {
  	for (i=0; i<document.all.tags(tags[t]).length; ++i) {
  		var obj = document.all.tags(tags[t])[i];
	  	if (!obj || !obj.offsetParent) continue;

		  // check for overlap

  		var ox = GetX(obj);
	  	var oy = GetY(obj);
		  var ow = obj.offsetWidth;
  		var oh = obj.offsetHeight;

	  	if (ox > (x + w) || (ox + ow) < x) continue;
		  if (oy > (y + h) || (oy + oh) < y) continue;
		
		  overlap[overlap.length] = obj;
  		obj.style.visibility = 'hidden';
  	}
	}
}
