//==========================================================
// SCRIPTING BY: Andrew Gaddis (2009)
//==========================================================

//tmpspeedsetting = 1000;

//------------------------------------------------------------------------------------------------
function buildWhereIsLRO()
{
	//NOTES: This function sets up the building of where is LRO. Sets initial loading, calls
	//	AJAX, etc.
	var resultid = 'whereislrocontainer';
	var url = '/STANDARD/php/pg/whereislro.php?overrideDebug='+runSystemX;
	var par = '';
	var obj = document.getElementById(resultid);
	obj.style.display = '';
	obj.innerHTML = '<div class="ajaxloading"><img src="/EPO/IMG/ajaxloading.gif"><p>Loading...</p></div>';
	setTimeout("callAJAX('"+resultid+"','"+url+"','"+par+"')",500);
}
//------------------------------------------------------------------------------------------------
function setupWhereIsLRO()
{
	//NOTES: This function "creates" the current position, shadows, and sets LRO on its
	//	movement track. All 'config' settings (except debug) are set via the PHP
	//	script near its top. These configs are prepended with 'js_'. Actual moving
	//	is handled by a separate function. All function input is declared 'in-script'
	//	by PHP (which echo's out the PHP array in JS format).
	//Initialize
	var debug = false;
	var thecontainer = 'whereislrocontainer';
	var objcontainer = document.getElementById(thecontainer);
	var objlro = document.getElementById('whereislroloc');
	var startrow = [];
	var starttime = '';
	var startx = '';
	var starty = '';
	var startlat = '';
	var startlong = '';
	var startalt = '';
	var i = (0);
	var currhtml = '';
	var flip = false;
	var flipprev = false;
	//Determine Initial Position
	startrow = lroplots[lronowpt];
	starttime = startrow[0];
	startx = startrow[1];
	starty = startrow[2];
	startlat = startrow[3];
	startlong = startrow[4];
	startalt = startrow[5];
	startnexttime = startrow[6];
	newtime = Math.floor(Number(startnexttime)*60*1000);
	//Draw Major Lat/Long Lines
	if (wil_showquadmin)
	{
		var iii = 58.3;
		jg.setColor(wil_quadmincolor);
		//Draw Longitude Lines
		while (iii<680)
		{
			jg.drawLine(iii,0,iii,350);
			iii = (iii+58.3);
		}
		iii = 58.3;
		//Draw Latitude Lines
		while (iii<340)
		{
			jg.drawLine(0,iii,700,iii);
			iii = (iii+58.3);
		}
	}
	//Draw Quadrant Lines
	if (wil_showquad)
	{
		jg.setColor(wil_quadcolor);
		var iii = (58.3*3);
		jg.drawLine(0,iii,700,iii);
		var iii = (58.3*6);
		jg.drawLine(iii,0,iii,350);
	}
	//Place Shadows
	while(i<numentries)
	{
		//Setup Vars
		var thisid = 'shadow'+i;
		var objshadow = document.getElementById(thisid);
		var shadowrow = [];
		var shadowtime = '';
		var shadowx = '';
		var shadowy = '';
		var shadowlat = '';
		var shadowlong = '';
		var shadowalt = '';
		var dropshadow = true;
		//Get & Place Shadow
		shadowrow = lroplots[i];
		shadowtime = shadowrow[0];
		shadowx = Number(shadowrow[1]);
		shadowy = Number(shadowrow[2]);
		shadowlat = (Math.round((Number(shadowrow[3])*10))/10);
		shadowlong = (Math.round((Number(shadowrow[4])*10))/10);
		shadowalt = Number(shadowrow[5]);
		objshadow.style.left = (shadowx-3)+'px';
		objshadow.style.top = (shadowy-3)+'px';
		//Switch Shadow Color (if in past)
		if (i<lronowpt)
		{
			objshadow.src = '/EPO/IMG/dot'+wil_shadowdot+'.gif';
		}
		//Get Shadow Connecting Dot Info
		var conrow = '';
		var conx = '';
		var cony = '';
		var label = '';
		var labelx = '';
		var labely = '';
		var labelxoffset = '';
		var lavelyoffset = '';
		var labellength = 0;
		var labelalign = 'left';
		var condiff = '';
		if (i==(numentries-1)) { conrow = lroplots[(i-1)]; } else { conrow = lroplots[(i+1)]; }
		conx = Number(conrow[1]);
		cony = Number(conrow[2]);
		//Move Text Box (flip)
		if ((Math.abs(cony-shadowy)<18) && (flipprev==false))
		{ flip = true; } 
		else { flip = false; }
		//Determine Whether to Drop
		condiff = (Math.abs(shadowy-cony));
		if (condiff<3)
		{
			dropshadow = false;
		}
		if (shadowy<3) { dropshadow = false; }
		if (shadowy>347) { dropshadow = false; }
		//Draw Shadow Iteration #
		if (wil_showiter && dropshadow)
		{
			jg.setColor(wil_textcolor);
			jg.setFont("Verdana","10px",Font.PLAIN);
			jg.drawString(i,(shadowx-20),(shadowy-5));
		}
		//Display Shadow Info/etc
		if (wil_showtime) { label = label + shadowtime; labellength = (255); }
		if (wil_showll) { label = label + " ("+shadowlat+"&deg;,"+shadowlong+"&deg;)"; labellength = (255); }
		if (wil_showalt) { label = label + ' ('+shadowalt+'km)'; }
		if (wil_showpx || debug) { label = label + " ["+shadowx+","+shadowy+"]"; labellength = (255); }
		if (wil_showtime && wil_showll) { labellength = 255; }
		if (wil_showtime && wil_showll && wil_showpx) { labellength = 255; }
		if (wil_showll && wil_showpx) { labellength = 255; }
		if (wil_showtime && wil_showll && wil_showpx && wil_showalt) { labellength = 255; }
		jg.setColor(wil_textcolor);
		jg.setFont("Verdana","10px",Font.PLAIN);
		labelxoffset = Number(9);
		labelyoffset = Number(-7);
		if (shadowx>=500) { labelxoffset = (-1)*(labellength); labelalign = 'right'; }
		if (i<lronowpt) { var usebg = wil_dottextbgo; } else { var usebg = wil_dottextbgn; }
		if (i==lronowpt) { var usebg = wil_dottextbg; var useopac = 1; } else { var useopac = wil_dottextopacity; }
			//Catch flipping box side
			if (flip)
			{
				labelxoffset = ((-1)*(Number(labellength))-4);
				labelalign = 'right';
			}
		label = '<div style="position:relative;">'
				+ '<div style="width:250px;position:absolute;overflow:auto;text-align:'+labelalign+'">'
					+ '<div id="shadowbg'+i+'" style="background:'+usebg+';color:'+usebg+';float:'+labelalign+';'
						+ 'opacity:'+useopac+';filter:alpha(opacity='+Math.round(Number(useopac)*100)+')">'+label+'</div>'
				+ '</div>'
				+ '<div style="width:250px;position:absolute;overflow:auto;text-align:'+labelalign+';">'
					+ '<div style="float:'+labelalign+';">'+label+'</div>'
				+ '</div>'
			+ '</div>';
		labelx = (shadowx+Number(labelxoffset)); //+8
		labely = (shadowy+Number(labelyoffset)); //-6
		if (dropshadow) { jg.drawString(label,labelx,labely); }
		//Display Shadow Dot
		if (dropshadow)
		{
			if (wil_showdot) { objshadow.style.display = ''; } else { objshadow.style.display = 'none'; }
		}
		else { objshadow.style.display = 'none'; }
		//Reiterate While Loop
		flipprev = flip;
		i = (i+1);
	}
	//Draws Initial Shadow Connecting Lines
	drawLinesWhereIsLRO();
	//Paint JS Canvas
	jg.paint();
	//Place LRO at Initial Position
	setWhereIsLROloc(startx,starty);
	objlro.style.display = '';
	//Set Call to Move LRO
	lronowpt = (lronowpt+1);
	moveTimeNode = setTimeout("moveWhereIsLRO()",Number(newtime));
	//Check if LRO is over Poles
	poleNoticeWhereIsLRO(starty);
	//Set Terminators/Shadow Layer
	setupTerminators();
	//Set Call to Re-Load Page
	forcepagereloadTime = (Number(wil_forcepagereload)*60*60*1000);
	thisForcedReloadNode = setTimeout("wil_Submit();",Number(forcepagereloadTime));
	//Set Initial Timers
	simpleCountdown('nextmove',(Number(newtime)));
	simpleCountdown('forcereload',(Number(forcepagereloadTime)));
	return false;
}
//------------------------------------------------------------------------------------------------
function moveWhereIsLRO()
{
	var dropshadow = true;
/*
	TO DO
	need to develop mechanism into this function, as done in 'setup', that restricts where shadows
	are placed. Currently, LRO moves thru EVERY plot, even if it doesn't have an 'active' dot.
	active dots in weird places are 'hidden', to make it all look better. but when this func is called
	it results in LRO 'opening' and showing the dots (in gray) where it has been, even if those
	dots were not shown in red prior. this should be fixed!
*/
	if (lronowpt<numentries)
	{
		//Get New Location
		var thisplot = lroplots[lronowpt];
		var newx = Number(thisplot[1]);
		var newy = Number(thisplot[2]);
		var newnexttime = Number(thisplot[6]);
		var usetime = Number(Math.round((newnexttime*60*1000)));
		//Swap Dot Color
		var prevdot = 'shadow' + (lronowpt-1);
		var objprevdot = document.getElementById(prevdot);
		if (objprevdot!=null) { objprevdot.src = '/EPO/IMG/dot'+wil_shadowdot+'.gif'; }
		//Hide/Show Shadow (eliminates 'jumping' shadow issue)
		var newshadow = 'shadow'+lronowpt;
		var oldshadow = 'shadow'+(lronowpt-1);
		var objnewshadow = document.getElementById(newshadow);
		var objoldshadow = document.getElementById(oldshadow);
		objnewshadow.style.display = 'none';
		objoldshadow.style.display = '';
		//Flip Background Colors for Text Box
		var shadowbgprevid = 'shadowbg' + (lronowpt-1);
		var shadowbgnowid = 'shadowbg' + (lronowpt);
		var objshadowbgprev = document.getElementById(shadowbgprevid);
		var objshadowbgnow = document.getElementById(shadowbgnowid);
		if (objshadowbgprev!=null) { objshadowbgprev.style.background = wil_dottextbgo; }
		if (objshadowbgnow!=null) { objshadowbgnow.style.background = wil_dottextbg; }
		//Flip Color for Text Box BG
		if (objshadowbgnow!=null) { objshadowbgnow.style.color = wil_dottextbg; }
		if (objshadowbgprev!=null) { objshadowbgprev.style.color = wil_dottextbgo; }
		//Fix Opacity for Current Text Box
		if (objshadowbgprev!=null)
		{
			objshadowbgprev.style.opacity = wil_dottextopacity;
			objshadowbgprev.style.filter = 'alpha(opacity='+Math.round(Number(wil_dottextopacity)*100)+')';
		}
		if (objshadowbgnow!=null)
		{
			objshadowbgnow.style.opacity = 1;
			objshadowbgnow.style.filter = 'alpha(opacity='+100+')';
		}
		//Reset Pole Notice Box
		poleNoticeWhereIsLRO(newy);
		//Set New Location
		setWhereIsLROloc(newx,newy);
		//Re-Draw Shadow Lines
			//Get Current Plot Point
			var coni;
			var conplot;
			var conx;
			var cony;
			if (lronowpt==(numentries-1)) { coni = ((Number(lronowpt))-1); }
			else { coni = ((Number(lronowpt))-1); }
			conplot = lroplots[coni];
			conx = Number(conplot[1]);
			cony = Number(conplot[2]);
			//Start Drawing
			condiff = ((Math.abs(newx-conx))/30); //<<eliminates weird horizontal lines
			if (condiff<=1)
			{
				jgx.setColor(wil_shadowlinecolor);
				if (wil_showlines) { jgx.drawLine(newx,newy,conx,cony); }
			}
			//Paint
			jgx.paint();
		//Increment & Call
		if (lronowpt==(numentries+1)) { /*nothing*/ }
		else { lronowpt = (lronowpt+1); }
//alert('MOVE WHERE IS LRO FUNC\n\nusetime: '+usetime+'\nnewnexttime: '+newnexttime);
		moveTimeNode = setTimeout("moveWhereIsLRO()",usetime);
	}
	else if (lronowpt==numentries) { /*nothing*/ }
	//Reset Timer (for next move)
	clearInterval(TIMER_simpleCountdown_nextmove);
	simpleCountdown('nextmove',(Number(usetime)));
	return false;
}
//------------------------------------------------------------------------------------------------
function setWhereIsLROloc(x,y)
{
	var objlro = document.getElementById('whereislroloc');
	objlro.style.left = ((Number(x))-9) + 'px';
	objlro.style.top = ((Number(y))-9) + 'px';
}
//------------------------------------------------------------------------------------------------
function drawLinesWhereIsLRO()
{
	//...NOTES...
		/*used/called by main 'setup' function. This draws out all the initial connecting lines.
		the 'move' function re-draws the lines on each move, to effectively change the color.*/
	//Variables
	var dropshadow = true;
	var i = 0;
	var thisplot;
	var thisx;
	var thisy;
	if (lronowpt<numentries) { var nowplot = lroplots[lronowpt]; }
	else { var nowplot = lroplots[(numentries-1)]; }
	var nowx = nowplot[1];
	var nowy = nowplot[2];
	var conrow;
	var conx;
	var cony;
	var condiff;
	//Processing
	while (i<numentries)
	{
		dropshadow = true;
		//Get Current Plot Point
		thisplot = lroplots[i];
		thisx = Number(thisplot[1]);
		thisy = Number(thisplot[2]);
		//Get Connecting Plot Point
		if (i==(numentries-1)) { conrow = lroplots[(i-1)]; } else { conrow = lroplots[(i+1)]; }
		conx = Number(conrow[1]);
		cony = Number(conrow[2]);
		//Determine Whether to Drop
		condiff = (Math.abs(thisy-cony)); //<<eliminates weird vertical errors
		if (condiff<3)
		{
			dropshadow = false;
		}
// ~~~ TO DO test this code, it was breaking!!!~~~~~~~~~~~~~~
//		if (thisy<3) { dropshadow = false; }
//		if (thisy>347) { dropshadow = false; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//Start Drawing
		condiff = ((Math.abs(thisx-conx))/30); //<<eliminates weird horizontal lines
		if (condiff<=1)
		{
			if (i<lronowpt)
			{
				var uselinecolor = wil_shadowlinecolor;
			}
			else
			{
				var uselinecolor = wil_linecolor;
			}
			jgx.setColor(uselinecolor);
			if (wil_showlines && dropshadow)
			{
				jgx.drawLine(thisx,thisy,conx,cony);
			}
		}
		i = (i+1);
	}
	//Paint Canvas
	jgx.paint();
}
//------------------------------------------------------------------------------------------------
function setupTerminators()
{
	//Build out Shadows Layer
	if (wil_showterm)
	{
		//Parameters
		var left = 'leftTerm';
		var right = 'rightTerm';
		var objL = document.getElementById(left);
		var objR = document.getElementById(right);
		//Set Location of Boxes
		objL.style.left = ''+wil_term_start1+'px';
		objR.style.left = ''+wil_term_start2+'px';
		objR.style.width = ''+wil_term_width2+'px';
		//Show Boxes
		objL.style.display = '';
		objR.style.display = '';
	}
}
//------------------------------------------------------------------------------------------------
function poleNoticeWhereIsLRO(thisy)
{
	//NOTES: places notice on 'box' indicating LRO is over north/south pole.
	//Variables
	var doaction = false;
	var objnotebg = document.getElementById('whereislronotes');
	var objnote = document.getElementById('whereislronotestext');
	if (thisy<10) { var thispole = 'North'; doaction = true; }
	if (thisy>340) { var thispole = 'South'; doaction = true; }
	if (doaction)
	{
		//Place Notice Box
		objnote.innerHTML = 'LRO is currently over the Moon\'s '+thispole+' pole.';
		objnote.style.display = '';
		objnotebg.style.display = '';
	}
	else
	{
		//Hide Notice Box
		objnote.style.display = 'none';
		objnotebg.style.display = 'none';
	}
}
//------------------------------------------------------------------------------------------------
function wil_OptChg(thisobj)
{
	//Process
	var objname = thisobj.name;
}
//------------------------------------------------------------------------------------------------
function wil_toggleOpt(divID)
{
	//Variables
	var divobj = document.getElementById(divID);
	var curStyle = divobj.style.display;
	//Process
	if (curStyle=='none')
	{
		//Force Hide Others
		document.getElementById('optColors').style.display = 'none';
		document.getElementById('optDisplay').style.display = 'none';
		document.getElementById('optMisc').style.display = 'none';
		document.getElementById('optAbout').style.display = 'none';
		document.getElementById('optInfo').style.display = 'none';
		//Show Current
		showDIV('optGo');
		showDIV(divID);
		//Hide 'go' if About or Info
		if ((divID=='optAbout') || (divID=='optInfo')) { hideDIV('optGo'); }
	}
	else
	{
		//Hide Current
		hideDIV('optGo');
		hideDIV(divID);
	}
	return false;
}
//------------------------------------------------------------------------------------------------
function wil_Submit()
{
	//Clear Existing Timeouts/Intervals
	clearInterval(TIMER_startTime);
	clearInterval(TIMER_simpleCountdown_nextmove);
	clearInterval(TIMER_simpleCountdown_forcereload);
	clearTimeout(moveTimeNode);
	clearTimeout(thisForcedReloadNode);
	//Submit Form
	var entireContainer = 'whereislrocontainer';
	var formid = 'modform';
	var resultid = entireContainer;
	clearTimeout(thisForcedReloadNode);
	clearTimeout(moveTimeNode);
	doForm(formid,resultid);
	return false;
}
//------------------------------------------------------------------------------------------------



// ### IMAGE PRE-LOADING ###
wil_graydot = new Image(7,7);wil_graydot.src = "/EPO/IMG/dotgray.gif";
wil_darkgraydot = new Image(7,7);wil_darkgraydot.src = "/EPO/IMG/dotdarkgray.gif";
wil_blackdot = new Image(7,7);wil_blackdot.src = "/EPO/IMG/dotblack.gif";
wil_bluedot = new Image(7,7);wil_bluedot.src = "/EPO/IMG/dotblue.gif";
wil_brightbluedot = new Image(7,7);wil_brightbluedot.src = "/EPO/IMG/dotbrightblue.gif";
wil_brightgreendot = new Image(7,7);wil_brightgreendot.src = "/EPO/IMG/dotbrightgreen.gif";
wil_brightreddot = new Image(7,7);wil_brightreddot.src = "/EPO/IMG/dotbrightred.gif";
wil_darkorangedot = new Image(7,7);wil_darkorangedot.src = "/EPO/IMG/dotdarkorange.gif";
wil_golddot = new Image(7,7);wil_golddot.src = "/EPO/IMG/dotgold.gif";
wil_greendot = new Image(7,7);wil_greendot.src = "/EPO/IMG/dotgreen.gif";
wil_orangedot = new Image(7,7);wil_orangedot.src = "/EPO/IMG/dotorange.gif";
wil_reddot = new Image(7,7);wil_reddot.src = "/EPO/IMG/dotred.gif";
wil_whitedot = new Image(7,7);wil_whitedot.src = "/EPO/IMG/dotwhite.gif";
wil_yellowdot = new Image(7,7);wil_yellowdot.src = "/EPO/IMG/dotyellow.gif";




//------------------------------------------------------------------------------------------------
// !!! !!! !!! !!! OLD FUNCTIONS BELOW (FOR REFERENCE) !!! !!! !!! !!!
//------------------------------------------------------------------------------------------------
