var tooltipContainer  = "summaryTooltip";
var tooltipContent    = "summaryTooltipContent";

var loadTimer;
var loadDelay = 100;

var closeTimer;
var closeDelay = 100;

var onTooltip = false;
var onLink    = false;

var adsColumnWidth = 157;

//--------------------------------------------------
// Handles when the user mouses over a headline
//
function overLink(articleID)
{
	// -- If we are on the tooltip, and the tooltip is over another link, then that counts as being on
	//  - the tooltip only, since the tooltip renders over the link.  Browser events dont catch this,
	//  - so we have to do it manually.
	if (onTooltip)
		return;

	// -- Clear any old delay timers
	clearInterval(loadTimer);
	clearInterval(closeTimer);

	// -- Remember that we are over a link
	onLink = true;

	// -- Set the timer for the tooltip delay
	loadTimer = setTimeout("showTooltip(" + articleID + ")", loadDelay);
}

//--------------------------------------------------
// Handles when the user mouses off of a headline
//
function offLink()
{
	// -- Remember that we are no longer over a link
	onLink = false;

	// -- Clear the opening delay
	clearInterval(loadTimer);

	// -- Close the tooltip, unless we are over the link's tooltip instead of over its link
	if (!onTooltip)
        closeTimer = setTimeout("closeTooltip()", closeDelay);
}

//--------------------------------------------------
// Remembers when the user's mouse is over a
// tooltip, so we don't close it
//
function setOnTooltip()
{
	// -- Clear the closing timeout, if it was timing down to closing
	clearInterval(closeTimer);

	// -- Remember that we are over the tooltip
	onTooltip = true;
}

//--------------------------------------------------
// Allows up to close the tooltip, now that we
// aren't using it
//
function setOffTooltip()
{
	// -- Remember that we are no longer over the tooltip
	onTooltip = false;

	// -- Start the countdown to closing the tooltip, unless we are on the link that triggered it instead
	if (!onLink)
		closeTimer = setTimeout("closeTooltip()", closeDelay);
}

//--------------------------------------------------
// Displays the tooltip, moves it to the correct
// position, and then finds, formats, and adds
// the content.
//
function showTooltip(articleID)
{
	var currentLink = "headline" + articleID;

	// -- If the tooltip from the other side is open, this will ensure that everything is reset before opening
	closeTooltip();

	// -- Find the center of the content area by finding the center of the screen, then shifting by the ads column width
	var windowCenter = 125;

	if (navigator.appName == "Netscape")
		windowCenter  = window.innerWidth / 2;
	else
		windowCenter  = document.body.offsetWidth / 2;

	windowCenter -= adsColumnWidth;

	// -- Tooltips for the left side go to the right of the content
    if(findPosX(document.getElementById(currentLink)) < windowCenter)
    {
	    var linkX = findPosX(document.getElementById(currentLink)) + document.getElementById(currentLink).offsetWidth/2.5;
	    var linkY = findPosY(document.getElementById(currentLink)) + document.getElementById(currentLink).offsetHeight;
	}

	// -- Tooltips for the right side go to the left of the content
	else
	{
	    var linkX = findPosX(document.getElementById(currentLink)) + document.getElementById(tooltipContainer).offsetWidth/3;
	    var linkY = findPosY(document.getElementById(currentLink)) + document.getElementById(currentLink).offsetHeight;
	}

	// -- Set the tooltip position, and show it
	document.getElementById(tooltipContainer).style.top = linkY + "px";
	document.getElementById(tooltipContainer).style.left = linkX + "px";

	document.getElementById(tooltipContainer).style.visibility = "visible";

	// -- Load the article summary content
	document.getElementById(tooltipContent).innerHTML = getSummary(articleID);
}

//--------------------------------------------------
// Gets the stored summary information for an
// article, formats it, and returns it
//
function getSummary(articleID)
{
	var html = "";
	var currentContent = document.getElementById("details" + articleID).innerHTML.split('+++++');

	var id           = currentContent[0];
	var headline     = currentContent[1];
	var subtitle     = currentContent[2];
	var pubDate      = currentContent[3];
	var authorName   = currentContent[4];
	var authorEmail  = currentContent[5];
	var summaryImage = currentContent[6];
	var summary      = currentContent[7];

	html += "<a href=\"/article/" + id + "/\" class=\"SecondStoryHeadline\">" + headline + "</a><br />";

	if (subtitle != null && subtitle != "")
		html += "<b>" + subtitle + "</b><br />";

	html += "<span class=\"authorname\">" + pubDate;

	if (authorName != null && authorName != "" && authorName != "_None")
	{
		html += " - ";

		if (authorEmail != null && authorEmail != "")
			html += "<a href=\"mailto:" + authorEmail + "\">";

		html += authorName;

		if (authorEmail != null && authorEmail != "")
			html += "</a>";
	}

	html += "</span><br /><br />";

	if (summaryImage != null && summaryImage != "")
	{
		html += "<img align=\"left\" class=\"newsimage\" style=\"margin-right:6px;\" src=\"" + summaryImage + "\" alt=\"" + headline + "\" />";
	}

	if (summary != null && summary != "")
		html += summary;

	html += "<a href=\"/article/" + id + "/\">";

	if (summary == null || summary == "")
		html += "Read the complete article";

	html += " <img src='/images/layout/more.gif' height='11' width='20' alt='More...' align='absmiddle'></a>";

	return html;
}

//--------------------------------------------------
// Hides the tooltip
//
function closeTooltip()
{
    document.getElementById(tooltipContainer).style.visibility  = "hidden";
}