// BIG BUG FOUND IN SAFARI: innerHTML in Safari does not reliable put correct quotes on the results of innerHTML
// Sometimes, double quote are put around double-quoted items.  Sometimes, we thought we saw inconsistent single
// and double-quoting.  As a result, we have stopped using innerHTML for the timeticks and instead just redraw
// them from a stored string.

var bOnIcon = false;
var bOnPopUp = false;

// NOTE THAT ANTYHING USING DOCUMENT.ALL WON'T WORK ON NAVIGATOR (AND MAYBE SAFARI)
function showPopUp(iWindow)
{ if (iWindow != 0) {
	var id1 = eval('document.all.XY' + iWindow);
	// alert(id1.offsetLeft + "," + id1.offsetTop);
	
	
    var iX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
    var iY = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	
	   // set the object to the left, or to the right if necessary
	// var iCandidate = event.x; // parseInt(id1.offsetLeft,10)+5;
	// alert(iX + '+' + document.all.popup.offsetWidth + '>' + document.body.scrollWidth);
	if (iX + document.all.popup.offsetWidth > document.body.scrollWidth)
	   iX -= document.all.popup.offsetWidth;
	document.all.popup.style.left = iX + 'px';
	
	   // set the object to the bottom, or to the top if necessary
	// iCandidate = event.y; // parseInt(id1.offsetLeft,10)+5;
	// alert(iY + '+' + document.all.popup.offsetHeight + '>' + document.body.scrollHeight);
	if (iY + document.all.popup.offsetHeight > document.body.scrollHeight)
	   iY -= document.all.popup.offsetHeight;
	document.all.popup.style.top = iY + 'px'; // parseInt(id1.offsetTop,10)+20;

	// alert(document.all.bottomtext.offsetHeight + ";" + document.all.bottomwhite.height);
	// document.all.bottomwhite.height = document.all.bottomtext.offsetHeight - 13;
	bOnIcon = true;
	}
  else bOnPopUp = true; 
  document.all.popup.style.visibility = "visible";
}

function hidePopUp(iWindow)
{  if (iWindow == 0) bOnPopUp = false; else bOnIcon = false;
	if (!bOnPopUp && !bOnIcon) document.all.popup.style.visibility = "hidden";
}

function linkOn(strID)
{  eval('document.all.' + strID).color = '#FFFFFF';
}

function linkOff(strID)
{  eval('document.all.' + strID).color = '#FFFF00';
}

function searchFor(strGoal)
{	window.opener.document.all.T1.value = (window.opener.document.all.T1.value != "" ? window.opener.document.all.T1.value + " " : "") + "Era:" + strGoal;
	window.close();
}

// constants that define the timeline
var iRowHeight = 12;	// height of each row
var iRowTop = 0;		// y coordinate of upper left of timeline
var iTimeFirst = 0;   	// x coordinate of upper left of timeline built in to the strings generated
var iTickXOffset = 10;	// x-offset to add to ticks - was 13 but moved back to 10
var iTickYOffset = 1 + (sfri ? 3 : 0);	// y-offset to add to ticks (Safari is 3 pixels more)
var iRowTypeOffset = -2;// pixels to move type higher
var iSegmentWidth = 49; // width of each segment in pixels NOT USED, HOWEVER
var iTickHeight = 5;
var iTickWidth = 9;		// an odd number; midpoint is the center pixel
var strGray = "60";		// how to mute colors; expressed as hex string

// IE has a terrible problem with relative elements: http://www.positioniseverything.net/explorer/threepxtest.html: it adds 3 pixels to the dimensions of floating boxes
// The solution is a bizarre hack that sets the outer left and right margins to a negative 3 pixels but extends the image's by a compensating 3 pixels

var iTick = 0;			// number of ticks
function timelineCreateTickMenuOld(iRow,iTime,strMenu)
{   
    document.writeln("<span id='XY" + iTick++ + "' style='position: absolute; left: " + (iTime - (iTickWidth - 1)/2 + iTimeFirst + iTickXOffset) + "px; top: " + (iRow*iRowHeight + iRowTop + iTickYOffset) + "px;'>" +
	           "<img width=" + iTickWidth + " height=" + iTickHeight + " src='/images/timeline_tick.gif' onmouseover='popup(\"" + strMenu + "\",1)' onmouseout='popdown()'></span>");
}

function timelineCreateTickMenu(iRow,iTime,strMenu)
{   
    return "<span id='XY" + iTick++ + "' style='position: absolute; left: " + (iTime - (iTickWidth - 1)/2 + iTimeFirst + iTickXOffset) + "px; top: " + (iRow*iRowHeight + iRowTop + iTickYOffset) + "px;'>" +
	       "<img width=" + iTickWidth + " height=" + iTickHeight + " src='/images/timeline_tick.gif' onmouseover='popup(\"" + strMenu + "\",1)' onmouseout='popdown()'></span>";
}

var strTicksContents;

function timelineCreate()
{
	// write everything up to where the actual timeline background piece goes
	// document.writeln('<span id=timelinecontents><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td bgcolor="#666666"><img src="/images/spacer.gif" width="10" height="1"></td></tr><tr><td height="15" bgcolor="#D3D3BA"><div align="left"><img src="images/aras_timeline.gif" width="80" height="15" hspace="10" align="absmiddle"><span class="timelineText">Results by Time and Culture. Click on any <img src="/images/timeline_tick.gif" width="9" height="5" align="absmiddle"></span></div></td></tr><tr><td bgcolor="#787B6A"><img src="images/spacer.gif" width="10" height="1"></td></tr><tr><td><img src="images/spacer.gif" width="10" height="1"></td></tr><tr><td id="timeline" bgcolor="#E6E6CE"><table border="0" cellspacing="0" cellpadding="0" align="center" id=foo><tr><td>');
	document.writeln('<span id=timelinecontents><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td bgcolor="#666666"><img src="/images/spacer.gif" width="10" height="1"></td></tr>');
	document.writeln('<tr><td height="15" bgcolor="#D3D3BA"><div align="left"><img src="images/aras_timeline.gif" width="80" height="15" hspace="10" align="absmiddle"><span class="timelineText">');
	document.writeln('Results by Time and Culture. Click on any <img src="/images/timeline_tick.gif" width="9" height="5" align="absmiddle"></span></div></td></tr><tr><td bgcolor="#787B6A"><img src="images/spacer.gif" width="10" height="1"></td></tr>');
	document.writeln('<tr><td><img src="images/spacer.gif" width="10" height="1"></td></tr><tr><td id="timeline" bgcolor="#E6E6CE"><table border="0" cellspacing="0" cellpadding="0" align="center" id=foo><tr><td>');
/* TBD test version
	document.writeln('<span id=timelinecontents><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td bgcolor="#666666"><img src="/images/spacer.gif" width="10" height="1"></td></tr>');
	document.writeln('<tr><td height="15" bgcolor="#D3D3BA"><div align="left"><img src="images/aras_timeline.gif" width="80" height="15" hspace="10" align="absmiddle"><span class="timelineText">');
	document.writeln('Results by Time and Culture. Click on any <img src="/images/timeline_tick.gif" width="9" height="5" align="absmiddle"></span></div></td></tr><tr><td bgcolor="#787B6A"><img src="images/spacer.gif" width="10" height="1"></td></tr>');
	document.writeln('<tr><td><img src="images/spacer.gif" width="10" height="1"></td></tr><tr><td id="timelineX" bgcolor="#E6E6CE"><table border="0" cellspacing="0" cellpadding="0" align="center" id=foo><tr><td>');
	*/
		
	// finish up by writing the timeline itself
	document.writeln('<p height=210><img id="timeline_image" src="/images/timeline_base.gif" width="610" height="210"></p></td></tr></table></td></tr><tr><td bgcolor="#8E8F86"><img src="/images/spacer.gif" width="10" height="1"></td></tr></table></span>');

	// find out where the timeline graphic is	
	iRowTop = getOffsetTop(document.getElementById("timeline_image"));
	iTimeFirst = getOffsetLeft(document.getElementById("timeline_image"));
	// alert(iRowTop + "," + iTimeFirst);
	
	// get a string with all the ticks in it
	strTicksContents = eval(strCreateTickMenus);
	
	// instantiate the menus
	drawMenus();

	// if the window is resized, move the ticks to accomodate
	strOnResize += "moveTicks();"; // window.onresize = moveTicks;
	
	// IE Windows doesn't need this, but FireFox (et. al.) need to move the ticks after the window is loaded - perhaps
	// because the scrollbar gets drawn after the timeline and effectively moves things?
	strOnLoad += "moveTicksAndSetFocus();"; // window.onload = moveTicksAndSetFocus;
}

// move ticks to adjust for the scrollbar appearance AND call the searchbox's setFocus function
function moveTicksAndSetFocus()
{	moveTicks();
	setFocus();
}

var iTimeFirstPrevious = -1;

function moveTicks()
{

	// do nothing if ticks aren't showing
	if (document.getElementById("timelinecontents").innerHTML == "") return;

	// find out the new location of the timeline graphic
	var iRowTopNew = getOffsetTop(document.getElementById("timeline_image"));
	var iTimeFirstNew = getOffsetLeft(document.getElementById("timeline_image"));
	
	// if the graphic hasn't moved, move nothing
	if (iRowTop == iRowTopNew && iTimeFirstNew == iTimeFirstPrevious) return;
	
	// first, reset the ticks to their iTimeFirst settings
	document.getElementById("tickscontents").innerHTML = strTicksContents;
	
	// iterate over all the ticks to reset them relative to the new location
	var i;
	for (i = 0; i < iTick; i++)
		document.getElementById("XY" + i).style.left = parseInt(document.getElementById("XY" + i).style.left) + iTimeFirstNew - iTimeFirst;
		
	// remember the new location for the next move
	iRowTop = iRowTopNew;
	iTimeFirstPrevious = iTimeFirstNew;
}

function getOffsetLeft (el) {
  var ol = el.offsetLeft;
  while ((el = el.offsetParent) != null)
    ol += el.offsetLeft;
  return ol;
}
function getOffsetTop (el) {
  var ot = el.offsetTop;
  while((el = el.offsetParent) != null)
   ot += el.offsetTop;
  return ot;
}

// returns the value of the cookie of the given name
function getCookie(strName)
{		// get the original cookie
	var strCombined = document.cookie;
	var iStart = strCombined.indexOf(strName + "=");
	if (iStart == -1) return "";
	else {
			// if the cookie is found, look for the ending semicolon
		var iEnd = strCombined.indexOf(";",iStart);
		if (iEnd == -1) iEnd = strCombined.length;
		return unescape(strCombined.substring(iStart + strName.length + 1,iEnd));
		}
}

// sets the cookie of the given name
function setCookie(strName,strValue)
{
	var dateNextYear = new Date();
	dateNextYear.setFullYear(dateNextYear.getFullYear() + 1);
	document.cookie = strName + "=" + escape(strValue) + "; expires= " + dateNextYear.toGMTString();
}

// create timeline
timelineCreate();

// display checkbox area
establishCheckBox();

// get the contents of the timeline and ticks, so can remove them easily
var strTimelineContents = document.getElementById("timelinecontents").innerHTML;

// if no cookie set for this, set to 1 (show timeline)
var iTimelineState = (getCookie("showtimeline") != "") ? parseInt(getCookie("showtimeline")) : 1;
updateTimeline();

// shows/hides the timeline; updates the cookie; updates the checkbox state; updates the screen background for now TBD
function updateTimeline()
{
	// show/hide the timeline and its ticks
	document.getElementById("timelinecontents").innerHTML = (iTimelineState % 2) ? strTimelineContents : "";
	document.getElementById("tickscontents").innerHTML = (iTimelineState % 2) ? strTicksContents : "";
	
	// remember that the ticks are at the original setting
	iTimeFirstPrevious = iTimeFirst;

	// if the ticks are visible, move them into place
	moveTicks();
	
	// set the checkbox
	document.getElementById("timelinecheckbox").status = (iTimelineState % 2) == 1;
	
	// set the cookie
	setCookie("showtimeline",iTimelineState);
	
	// update the screen background
	if (document.getElementById("searchresultstable") != null) {
		if (iTimelineState % 2) document.getElementById("searchresultstable").style.backgroundImage = 
			"url(" + ((iTimelineState >= 2) ? "/images/spacer.gif" : "/images/results_starfield.gif") + ")";
		}
		
	// TBD show the status number
	// window.status = "Click box status " + iTimelineState;
}

// called when the checkbox is clicked to advance the state
function checkboxClick()
{
	// advance to the next state
	iTimelineState = (iTimelineState + 1) % 4;
	updateTimeline();
	eval(strOnResize);
}

function showImageCoordinates (image) {
  var x = getOffsetLeft(image);
  var y = getOffsetTop(image);
  alert(x + ':' + y);
}


