/***************************************************************************
 Jupiter Portal @ Jupiterportal.com
 Copyright (C) 2006 Cosmin Flavius (highstrike@gmail.com)

 Attribution-NonCommercial-ShareAlike 2.5 of Creative Commons
 (http://creativecommons.org)

 You are free:

    * to copy, distribute, display, and perform the work
    * to make derivative works

 Under the following conditions:

 Attribution - You must attribute the work in the manner specified
 by the author or licensor.

 Noncommercial - You may not use this work for commercial purposes.

 Share Alike - If you alter, transform, or build upon this work, you may
 distribute the resulting work only under a license identical to this one.

    * For any reuse or distribution, you must make
	  clear to others the license terms of this work.
    * Any of these conditions can be waived if you get
      permission from the copyright holder.

 Your fair use and other rights are in no way affected by the above.

 This is a human-readable summary of the Legal Code (the full license).
 (http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode)

****************************************************************************/

//-----------------------------------------------------------------
//  Take care of vars
//-----------------------------------------------------------------

var JavaScriptTooltips_fadeInSpeed = 10;
var JavaScriptTooltips_fadeOutSpeed = 20;
var JavaScriptTooltips_fadeOutDelay = 0;
var JavaScriptTooltips_fadeInDelay = 1000;
var JavaScriptTooltips_maximumOpacity = 90;
var JavaScriptTooltips_offsetX = 12;
var JavaScriptTooltips_offsetY = -23;
var tooltipData = new Array();

////////////////////////////////////////////////////////
// Function:         dataArray
// Description: Add this. in the values

function dataArray(opacity, timer, context)
{
	this.opacity = opacity;
	this.timer = timer;
	this.context = context;
}

////////////////////////////////////////////////////////
// Function:         show
// Description: Showes the tooltip

function show(context, content, event)
{
	if (!context.id)
	{
		var HTMLelID = '';
		var letters = 'abcdefghijklmnopqrstuvwxyz';
		var numbers = '0123456789';
		for (i = 0; i < 12; i++) { HTMLelID += Math.floor(Math.random() * 2) == 0 ? letters.charAt(Math.floor(Math.random() * 26)) : numbers.charAt(Math.floor(Math.random() * 10)); }

		context.id = HTMLelID;
	}

	if (!document.getElementById(context.id + "_tooltip"))
	{
		tooltipID = context.id + "_tooltip";
		tooltipData[tooltipID] = new dataArray(0, false, context);

		if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1)
		{
			var newIFrame = document.createElement("iframe");

			newIFrame.id = tooltipID + "_iframe";
			newIFrame.src = "about:blank";
			newIFrame.frameBorder = "0";
			newIFrame.scrolling = "no";
			newIFrame.style.position = "absolute";
			newIFrame.style.display = "block";
			newIFrame.top = 0;
			newIFrame.left = 0;
			newIFrame.height = 0;
			newIFrame.width = 0;

			document.body.appendChild(newIFrame);
		}

		var newTooltip = document.createElement("div");

		newTooltip.id = tooltipID;
		newTooltip.style.position = "absolute";
		newTooltip.style.display = "block";
		newTooltip.style.left = 0;
		newTooltip.style.top = 0;
		newTooltip.style.width = "auto";
		newTooltip.style.height = "auto";

		document.body.appendChild(newTooltip);
		context.onmouseout = function() { hide(tooltipID) }

		document.getElementById(tooltipID).onmouseover = function () { _fadeIn(tooltipID); }
		document.getElementById(tooltipID).onmouseout = function () { hide(tooltipID); }
		document.getElementById(tooltipID).onclick = function () { hide(tooltipID, true); }

		if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1)
		{
			document.getElementById(tooltipID + "_iframe").onmouseover = function () { _fadeIn(tooltipID); }
			document.getElementById(tooltipID + "_iframe").onmouseout = function () { hide(tooltipID); }
			document.getElementById(tooltipID + "_iframe").onclick = function () { hide(tooltipID, true); }
		}

		styledcontent = '<div class=tooltip>&raquo; ' + content + '</div>';
		document.getElementById(tooltipID).innerHTML = styledcontent;
	}

	tooltipID = context.id + "_tooltip";
	document.getElementById(tooltipID).style.display = 'block';

	var posx = 0;
	var posy = 0;

	if (event.pageX || event.pageY)
	{
		posx = event.pageX;
		posy = event.pageY;

	}
	else if (event.clientX || event.clientY)
	{
		posx = event.clientX + document.body.scrollLeft;
		posy = event.clientY + document.body.scrollTop;

	}

	with (document.getElementById(tooltipID).style) {

		left = posx + JavaScriptTooltips_offsetX;
		top  = posy - JavaScriptTooltips_offsetY;

	}

	leftPos = eval(document.getElementById(tooltipID).style.left.replace(/px/g, ''));
	topPos = eval(document.getElementById(tooltipID).style.top.replace(/px/g, ''));

	width = document.getElementById(tooltipID).offsetWidth;
	height = document.getElementById(tooltipID).offsetHeight;

	if (typeof( window.innerWidth ) == 'number')
	{
		screenWidth = window.innerWidth;
		screenHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		screenWidth = document.documentElement.clientWidth;
		screenHeight = document.documentElement.clientHeight;
	}
	else if(document.body && ( document.body.clientWidth || document.body.clientHeight))
	{
		screenWidth = document.body.clientWidth;
		screenHeight = document.body.clientHeight;
	}

	if (typeof(window.pageYOffset) == 'number')
	{
		scrollY = window.pageYOffset;
		scrollX = window.pageXOffset;
	}
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		scrollY = document.documentElement.scrollTop;
		scrollX = document.documentElement.scrollLeft;
	}
	else
	{
		scrollY = document.body.scrollTop;
		scrollX = document.body.scrollLeft;
	}

	if (leftPos + width > screenWidth + scrollX) { leftPos = posx - width - JavaScriptTooltips_offsetX; }
	if (leftPos < scrollX) { leftPos = scrollX; }
	if (topPos < scrollY) { topPos = posy + JavaScriptTooltips_offsetY; }
	if (topPos + height > screenHeight + scrollY) { topPos = screenHeight + scrollY - height; }

	document.getElementById(tooltipID).style.left = leftPos + "px";
	document.getElementById(tooltipID).style.top = topPos + "px";

	if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1 && document.getElementById(tooltipID + "_iframe").style.display != 'block')
	{
		with (document.getElementById(tooltipID + "_iframe").style)
		{
			left = document.getElementById(tooltipID).style.left;
			top = document.getElementById(tooltipID).style.top;
			width = document.getElementById(tooltipID).offsetWidth;
			height = document.getElementById(tooltipID).offsetHeight;
			visibility = 'visible';
		}
	}

	_fadeIn(tooltipID);
}

////////////////////////////////////////////////////////
// Function:         hide
// Description: Hides a tooltip

function hide(tooltipID, instantly)
{
	if (tooltipData[tooltipID].opacity >= JavaScriptTooltips_maximumOpacity && !instantly) { tooltipData[tooltipID].timer = setTimeout(fadeOut(tooltipID), JavaScriptTooltips_fadeOutDelay); }
	else { tooltipData[tooltipID].timer = setTimeout(fadeOut(tooltipID), 1); }
}

////////////////////////////////////////////////////////
// Function:         fadeIn
// Description: Enabler function for the _fadeIn method

function fadeIn(tooltipID)
{
	clearTimeout(tooltipData[tooltipID].timer);
	return '_fadeIn(\'' + tooltipID + '\')';
}

////////////////////////////////////////////////////////
// Function:         _fadeIn
// Description: Fades in a tooltip

function _fadeIn(tooltipID)
{
	if (tooltipData[tooltipID].opacity <= JavaScriptTooltips_maximumOpacity)
	{

		if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1) { setOpacity(tooltipID + "_iframe", tooltipData[tooltipID].opacity); }

		setOpacity(tooltipID, tooltipData[tooltipID].opacity);
		tooltipData[tooltipID].opacity += JavaScriptTooltips_fadeInSpeed;
		tooltipData[tooltipID].timer = setTimeout(fadeIn(tooltipID), 1);

	}
	else
	{
		if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1){ setOpacity(tooltipID + "_iframe", JavaScriptTooltips_maximumOpacity); }

		setOpacity(tooltipID, JavaScriptTooltips_maximumOpacity);
		clearTimeout(tooltipData[tooltipID].timer);
	}

	for (var i in tooltipData) { document.getElementById(i).style.zIndex -= 1; }
	document.getElementById(tooltipID).style.zIndex = 9999;
}

////////////////////////////////////////////////////////
// Function:         fadeOut
// Description: Enabler function for the _fadeOut method

function fadeOut(tooltipID)
{
	clearTimeout(tooltipData[tooltipID].timer);
	return '_fadeOut(\'' + tooltipID + '\')';
}

////////////////////////////////////////////////////////
// Function:         _fadeOut
// Description: Fades out a tooltip

function _fadeOut(tooltipID)
{
	if (tooltipData[tooltipID].opacity >= 0)
	{
		if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1) { setOpacity(tooltipID + "_iframe", tooltipData[tooltipID].opacity); }

		setOpacity(tooltipID, tooltipData[tooltipID].opacity);
		tooltipData[tooltipID].opacity -= JavaScriptTooltips_fadeOutSpeed;
		tooltipData[tooltipID].timer = setTimeout(fadeOut(tooltipID), 1);
	}
	else
	{
		clearTimeout(tooltipData[tooltipID].timer);
		document.getElementById(tooltipID).style.display = 'none';
		if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1) { document.getElementById(tooltipID + "_iframe").style.display = "none"; }
	}
}

////////////////////////////////////////////////////////
// Function:         setOpacity
// Description: Sets opacity

function setOpacity(tooltipID, value)
{
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") == -1) { document.getElementById(tooltipID).style.filter = "alpha(opacity=" + value + ")"; }
	else if (navigator.appName == "Netscape" && navigator.userAgent.indexOf("Safari") == -1) { document.getElementById(tooltipID).style.MozOpacity = value / 100; }
	else if (navigator.vendor == "KDE") { document.getElementById(tooltipID).style.KHTMLOpacity = value / 100; }
	else { document.getElementById(tooltipID).style.opacity = value / 100; }
}