var ns4=document.layers?1:0
var ie4=document.all?1:0
var ns6=document.getElementById&&!document.all?1:0

function getWindowOffSet() {
  var offX = 0, offY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {    //for Netscape 4,6
    offY = window.pageYOffset;
    offX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {    //for DOM compatible IE
    offY = document.body.scrollTop;
    offX = document.body.scrollLeft;
  } else if( document.documentElement &&
      ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {    //for IE6 standard(s) mode
    offY = document.documentElement.scrollTop;
    offX = document.documentElement.scrollLeft;
  }
  return [offX, offY];
}

function getWindowSize() {
  var w = 0, h = 0;
  if( typeof( window.innerWidth ) == 'number' ) { //for Netscape 4,6
    w = window.innerWidth;
    h = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {//for IE6 standard(s) mode
    //IE 6+ in 'standards compliant mode'
    w = document.documentElement.clientWidth;
    h = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {//for IE4
    w = document.body.clientWidth;
    h = document.body.clientHeight;
  }
  return [w, h];
}

// tooltip install
function installTip()
{
	if (document.layers) return; // ns4 = not supported

/* This is commented in order to avoid mouse move event activations
	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 jspalatte 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; width: 225; height:"50px"></div>' );
}

function jspalatte(id, delay)
{
	this.newWidth = "225px";
	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, width)
	{
		var idObj = eval("document.all."+this.id+"");
		var newWidth = width || "225px";
		idObj.style.width = newWidth;
		if( document.layers || window.opera ) return;
		this.updateToolTip = true;
		var x = ((getWindowSize()[0]/2)-(300/2)) + getWindowOffSet()[0];
		var y = (getWindowSize()[1]/4) + getWindowOffSet()[1];
		this.mouseX = x;
		this.mouseY = y;

		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);
	}
	
	// update coordinates of the layer based on mouse coords;
	this.update = function()
	{
		this.obj.style.left = (this.getMouseX() + 1) + "px";
		this.obj.style.top = (this.getMouseY() + ( document.all? 20: 15 )) + "px";
	}
	
	// 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, "");
	}

	// 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
			var idObj = eval("document.all."+this.id+"");
			mouseX = event.clientX;
			mouseX +=  document.body.scrollLeft - (idObj.style.width.replace(/px/g,'')/2);	
			mouseY = event.clientY;
			mouseY += document.body.scrollTop ;
		}
		else
		{
			// Netscape and Mozilla
			mouseX = e.pageX;
			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 ) ); }

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 jspalatte installation
if (!window.autoInstall || window.autoInstall == true) installTip();
