//------------------------------------------------------------------------------------------- 
//
// Copyright © 1990-2007 by Simulation Curriculum Corp., All rights reserved.
//
// Contains:	Moon Exploration Widget:
//				- sends a request to the SN Server to generate a sky chart for the selected Moon location and time
//
// Author:		Ivan Pinzon
//
// Date			Initials	Version		Comments
// ----------	---------	----------	---------------------------
// 2007/06/18	IPA			1.0			New
//
//------------------------------------------------------------------------------------------- 

var kDashboardWidgetVersion="1.01";var kWidgetID=kMEW_Dashboard;var kWidgetTitle="Moon Exploration Widget";var kLocationKey="locationKey";var kTimeZoneKey="timeZoneKey";var kLocationIndexKey="locationIndex";var kLatitudeKey="latitudeKey";var kLongitudeKey="longitudeKey";var kYearKey="yearKey";var kMonthKey="monthKey";var kDayKey="dayKey";var kHourKey="hourKey";var kAzimuthKey="azimuthKey";var kShowStarLabelsKey="showStarLabelsKey";var kShowPlanetLabelsKey="showPlanetLabelsKey";var kShowConstellationsKey="showConstellationsKey";var kShowEclipticKey="showEclipticKey";var kShowMarkersKey="showMarkersKey";var kShowSurfaceGuidesKey="showSurfaceGuidesKey";var kCollapsedKey="collapsedKey";var kTransitionKey="transitionKey";var gZoomSlider=null;var gLocationsA;var gLocationIndexPref=0;var gUserLocationIndex=0;var gDatesA;var gDatesIndexPref=0;function widgetProcessEvent(inKey)
{var sendRequest=true;switch(inKey)
{default:sendRequest=false;break;}
return sendRequest;}
function setup()
{trackAction(kWidgetID,kDashboardWidgetVersion,kTrack_Initialized,false);gInfoButton=new AppleInfoButton(document.getElementById("infoButton"),document.getElementById("front"),"white","white",showPrefs);gMiniChartButton=new AppleButton(document.getElementById("chartDrawerButton"),"",20,"Images/miniChartButton.png","Images/miniChartButtonOn.png",20,"","","","",0,handleChartDrawer);gSNWebButton=new AppleButton(document.getElementById("goSNWebButton"),"",22,"Images/goSNWebButton.png","Images/goSNWebButtonON.png",22,"","","","",0,goToStarryNightServer);glassDoneButton=new AppleGlassButton(document.getElementById("doneButton"),"Done",hidePrefs);TICKER_STYLE="font-family:Lucida Grande; font-size:13px; color:black";gZoomSlider=new Control.Slider('zoomSliderHandle','zoomSliderTrack',{axis:'vertical',range:$R(kMinFOV,kMaxFOV),sliderValue:gSituation.gaze.FOV});gZoomSlider.setEnabled();gZoomSlider.options.onChange=zoomSliderChanged;gZoomSlider.options.onSlide=zoomSliderSlid;kDefaultAlt=4.0;gSituation.location.lat=kApollo11Lat;gSituation.location.lng=kApollo11Long;gSituation.options.daylight=false;populateAzimuthOptions();populateTransitionOptions();populateLocationOptions();populateMonthsAndTime();loadPrefs();gIsCollapsed=true;var dummyEvent=document.createEvent("MouseEvents");toggleView(dummyEvent);sendMiniChartRequest();gSetupWasCalled=true;if(gRefreshTimer==null)
{gRefreshTimer=setInterval("sendMiniChartRequest();",1000*gRefreshPeriod);}
document.addEventListener("keypress",keyPressed,true);document.addEventListener("keypress",keyPressedBack,true);switch(gTransitionOptionsIndexPref)
{case kCubeTransition:gCompositionElemID=kCubeCompositionElemID;break;case kCoreImageTransition:gCompositionElemID=kCoreImageCompositionElemID;break;default:break;}
for(i=0;i<=kTabsCount;i++)
{loadTabImages('Images/tab'+i+'.png','Images/tab'+i+'ON.png',i);}
setTabImage("tabImg"+gCurrentTabPref,gCurrentTabPref,true);}
function generateRequest()
{gSituation.useTimezoneAndUTC=true;gSituation.location.locName=kMoonName;var url=getRequestURL(kMEW_Dashboard,kDashboardWidgetVersion,kGenerateImageRequest,gSituation,kWidthValue,kHeightValue);return url;}
function postLatLongTyping()
{document.getElementById('locationOptions').options[gUserLocationIndex].selected=true;gSituation.location.lat=parseFloat(document.getElementById("latitudeField").value);gSituation.location.lng=parseFloat(document.getElementById("longitudeField").value);}
function preFindNearestCity()
{}
function postFindNearestCity()
{gLocationIndexPref=gUserLocationIndex;gCurrentLocation=gUserLocationPref;setUserLocation(gSituation.location.lat,gSituation.location.lng);saveLocationPrefs();document.getElementById('locationOptions').options[gUserLocationIndex].selected=true;setLocationCaption();}
function hidePrefs()
{if(!gLatLongValidated)
{findNearestCity();}
savePrefs();var front=document.getElementById("front");var back=document.getElementById("back");if(window.widget)
{if(!gIsCollapsed)
{window.resizeTo(kMaxWidgetWidth,kMaxWidgetHeight);}
widget.prepareForTransition("ToFront");}
back.style.display="none";front.style.display="block";if(window.widget)
{setTimeout('widget.performTransition();',0);if(gIsCollapsed)
{window.resizeTo(kMinWidgetWidth,kMinWidgetHeight);}}
setTimeout(prepareForNewTransition,80);gComposition.showLoading=true;setTimeout(sendMiniChartRequest,100);resetRefreshTimer();startTickerTimer();restoreTickerScroll();document.getElementById("locationFront").innerText=gCurrentLocation;setInfoButtonStyle();refreshSituationStatus();}
function loadPrefs()
{if(window.widget)
{gIsCollapsed=(setPreference(widget.preferenceForKey(createKey(kCollapsedKey)),kYES)==kYES)?true:false;gTransitionOptionsIndexPref=setPreference(widget.preferenceForKey(createKey(kTransitionKey)),kFadeInOut);gAzimuthOptionsIndexPref=setPreference(widget.preferenceForKey(createKey(kAzimuthKey)),kNorthIndex);gLocationIndexPref=setPreference(widget.preferenceForKey(createKey(kLocationIndexKey)),0);gUserLocationPref=setPreference(widget.preferenceForKey(createKey(kLocationKey)),gLocationsA[gLocationIndexPref].locationName);gCurrentLocation=(gLocationIndexPref==gUserLocationIndex)?gUserLocationPref:gLocationsA[gLocationIndexPref].locationName;var userLat=setPreference(widget.preferenceForKey(createKey(kLatitudeKey)),kDefaultMoonLatitude);var userLng=setPreference(widget.preferenceForKey(createKey(kLongitudeKey)),kDefaultMoonLongitude);setUserLocation(userLat,userLng);if(gLocationsA[gLocationIndexPref].date==null)
{gLocationsA[gLocationIndexPref].date=new Date();}
var year=setPreference(widget.preferenceForKey(createKey(kYearKey)),1969);var month=setPreference(widget.preferenceForKey(createKey(kMonthKey)),6);var day=setPreference(widget.preferenceForKey(createKey(kDayKey)),20);var hour=setPreference(widget.preferenceForKey(createKey(kHourKey)),13);gLocationsA[gLocationIndexPref].date.setUTCFullYear(year,month,day);gLocationsA[gLocationIndexPref].date.setUTCHours(hour);gLocationsA[gLocationIndexPref].date.setUTCMinutes(0);gLocationsA[gLocationIndexPref].date.setUTCSeconds(0);gSituation.dateTime=gLocationsA[gLocationIndexPref].date;gSituation.options.starLabels=setPreference(widget.preferenceForKey(createKey(kShowStarLabelsKey)),kNO)==kYES?true:false;gSituation.options.planetLabels=setPreference(widget.preferenceForKey(createKey(kShowPlanetLabelsKey)),kYES)==kYES?true:false;gSituation.options.constellations=setPreference(widget.preferenceForKey(createKey(kShowConstellationsKey)),kNO)==kYES?true:false;gSituation.options.horizon=true;gSituation.options.ecliptic=setPreference(widget.preferenceForKey(createKey(kShowEclipticKey)),kNO)==kYES?true:false;gSituation.options.markersMoonApollo=setPreference(widget.preferenceForKey(createKey(kShowMarkersKey)),kYES)==kYES?true:false;gSituation.options.surfaceGuides=setPreference(widget.preferenceForKey(createKey(kShowSurfaceGuidesKey)),kNO)==kYES?true:false;loadPrefsUI();setAzimuth(gAzimuthOptionsIndexPref);gSituation.gaze.alt=kDefaultAlt;setSituationLocationForIndex(gLocationIndexPref);gLatLongValidated=true;}}
function loadPrefsUI()
{document.getElementById("locationFront").innerText=gCurrentLocation;document.getElementById("locationBack").innerText=gCurrentLocation;document.getElementById("latitudeField").value=gSituation.location.lat;document.getElementById("longitudeField").value=gSituation.location.lng;document.getElementById("showStarLabelsChk").checked=gSituation.options.starLabels;document.getElementById("showLabelsChk").checked=gSituation.options.planetLabels;document.getElementById("showConstellationsChk").checked=gSituation.options.constellations;document.getElementById("showEclipticChk").checked=gSituation.options.ecliptic;document.getElementById("showMarkersChk").checked=gSituation.options.markersMoonApollo;document.getElementById("showSurfaceGuidesChk").checked=gSituation.options.surfaceGuides;document.getElementById('transitionOptions').options[gTransitionOptionsIndexPref].selected=true;document.getElementById('monthOptions').options[gSituation.dateTime.getUTCMonth()].selected=true;populateDays(gSituation.dateTime.getUTCFullYear(),gSituation.dateTime.getUTCMonth());document.getElementById('dayOptions').options[gSituation.dateTime.getDate()-1].selected=true;document.getElementById("yearField").value=gSituation.dateTime.getFullYear();var hour=gSituation.dateTime.getUTCHours();var amPm=hour>11?kPm:kAm;if(hour>11)
{hour-=12;}
document.getElementById("timeOptions").options[hour].selected=true;document.getElementById('amPmOptions').options[amPm].selected=true;}
function savePrefs()
{if(window.widget)
{savePrefsFromUI();saveLocationPrefs();widget.setPreferenceForKey(gSituation.dateTime.getUTCFullYear(),createKey(kYearKey));widget.setPreferenceForKey(gSituation.dateTime.getUTCMonth(),createKey(kMonthKey));widget.setPreferenceForKey(gSituation.dateTime.getUTCDate(),createKey(kDayKey));widget.setPreferenceForKey(gSituation.dateTime.getUTCHours(),createKey(kHourKey));widget.setPreferenceForKey(gAzimuthOptionsIndexPref,createKey(kAzimuthKey));widget.setPreferenceForKey(gSituation.options.starLabels,createKey(kShowStarLabelsKey));widget.setPreferenceForKey(gSituation.options.planetLabels,createKey(kShowPlanetLabelsKey));widget.setPreferenceForKey(gSituation.options.constellations,createKey(kShowConstellationsKey));widget.setPreferenceForKey(gSituation.options.ecliptic,createKey(kShowEclipticKey));widget.setPreferenceForKey(gSituation.options.markersMoonApollo,createKey(kShowMarkersKey));widget.setPreferenceForKey(gSituation.options.surfaceGuides,createKey(kShowSurfaceGuidesKey));widget.setPreferenceForKey(gTransitionOptionsIndexPref,createKey(kTransitionKey));}}
function saveLocationPrefs()
{widget.setPreferenceForKey(gUserLocationPref,createKey(kLocationKey));widget.setPreferenceForKey(gLocationIndexPref,createKey(kLocationIndexKey));widget.setPreferenceForKey(gLocationsA[gUserLocationIndex].lat,createKey(kLatitudeKey));widget.setPreferenceForKey(gLocationsA[gUserLocationIndex].lng,createKey(kLongitudeKey));widget.setPreferenceForKey(gSituation.location.timezone,createKey(kTimeZoneKey));}
function savePrefsFromUI()
{var year=0;var month=0;var day=0;var hour=0;var front=document.getElementById("front");var back=document.getElementById("back");if(back&&back.style.display=="block")
{switch(gCurrentTabPref)
{case kLocationTabView:if(gLocationIndexPref==gUserLocationIndex)
{gUserLocationPref=document.getElementById("locationBack").innerText;}
year=document.getElementById("yearField").value;month=document.getElementById('monthOptions').selectedIndex;day=document.getElementById('dayOptions').selectedIndex+1;gSituation.dateTime.setUTCFullYear(year,month,day);hour=document.getElementById('timeOptions').selectedIndex;if(document.getElementById('amPmOptions').selectedIndex>0)
{hour+=12;}
gSituation.dateTime.setUTCHours(hour);gSituation.location.lat=parseFloat(document.getElementById("latitudeField").value);gSituation.location.lng=parseFloat(document.getElementById("longitudeField").value);break;case kOptionsTabView:gSituation.options.starLabels=document.getElementById("showStarLabelsChk").checked;gSituation.options.planetLabels=document.getElementById("showLabelsChk").checked;gSituation.options.constellations=document.getElementById("showConstellationsChk").checked;gSituation.options.ecliptic=document.getElementById("showEclipticChk").checked;gSituation.options.markersMoonApollo=document.getElementById("showMarkersChk").checked;gSituation.options.surfaceGuides=document.getElementById("showSurfaceGuidesChk").checked;break;default:break;}}}
function populateLocationOptions()
{gLocationsA=new Array();gLocationsA[0]=new moonLocation("Apollo 11",kApollo11Lat,kApollo11Long,1969,7,20);gLocationsA[1]=new moonLocation("Apollo 12",-3.01,-23.42,1969,11,12);gLocationsA[2]=new moonLocation("Apollo 14",-3.65,-17.47,1971,2,5);gLocationsA[3]=new moonLocation("Apollo 15 Station 8",26.13,3.63,1971,7,30);gLocationsA[4]=new moonLocation("Apollo 15 Station 2",26.13,3.631,1971,7,30);gLocationsA[5]=new moonLocation("Apollo 15 Station 10",26.13,3.632,1971,7,30);gLocationsA[6]=new moonLocation("Apollo 16",-8.97,15.5,1972,4,20);gLocationsA[7]=new moonLocation("Apollo 16 Station 2",-8.97,15.51,1972,4,20);gLocationsA[8]=new moonLocation("Apollo 16 Station 2",-8.97,15.52,1972,4,20);gLocationsA[9]=new moonLocation("Apollo 17",20.19,30.77,1972,12,11);gLocationsA[10]=new moonLocation("Apollo 17 Station 5",20.19,30.771,1972,12,11);gLocationsA[11]=new moonLocation("User's Lat / Long",gSituation.location.lat,gSituation.location.lng);gUserLocationIndex=gLocationsA.length-1;var select=document.getElementById('locationOptions');var element;var i=0;for(i=0;i<gLocationsA.length;i++)
{element=document.createElement("option");element.innerText=gLocationsA[i].locationName;select.appendChild(element);}}
function locationChanged(select)
{if(gLocationIndexPref==gUserLocationIndex)
{var lat=document.getElementById("latitudeField").value;var lng=document.getElementById("longitudeField").value;setUserLocation(lat,lng);}
gLocationIndexPref=select.selectedIndex;setLocationCaption();setSituationLocationForIndex(gLocationIndexPref);setDate(gLocationIndexPref);document.getElementById("latitudeField").value=gSituation.location.lat;document.getElementById("longitudeField").value=gSituation.location.lng;if(gLocationIndexPref==gUserLocationIndex)
{gCurrentLocation=gUserLocationPref;}
else
{gCurrentLocation=gLocationsA[gLocationIndexPref].locationName;}
document.getElementById("locationBack").innerText=gCurrentLocation;document.getElementById('monthOptions').options[gSituation.dateTime.getMonth()].selected=true;populateDays(gSituation.dateTime.getFullYear(),gSituation.dateTime.getMonth());document.getElementById('dayOptions').options[gSituation.dateTime.getDate()-1].selected=true;document.getElementById("yearField").value=gSituation.dateTime.getFullYear();}
function setLocationCaption()
{var locationCaption=gLocationIndexPref==gUserLocationIndex?"Location:":"Location (Landing Site):";document.getElementById("locationCaptionBack").innerText=locationCaption;}
function setSituationLocationForIndex(inIndex)
{gSituation.location.lat=gLocationsA[inIndex].lat;gSituation.location.lng=gLocationsA[inIndex].lng;}
function setUserLocation(inLat,inLng)
{gLocationsA[gUserLocationIndex].lat=inLat;gLocationsA[gUserLocationIndex].lng=inLng;}
function setDate(inIndex)
{gSituation.dateTime=gLocationsA[inIndex].date;}
function populateMonthsAndTime()
{var select=document.getElementById('monthOptions');var element;var i=0;for(i=0;i<kMonthNames.length;i++)
{element=document.createElement("option");element.innerText=kMonthNames[i];select.appendChild(element);}
select=document.getElementById('timeOptions');for(i=0;i<=11;i++)
{element=document.createElement("option");element.innerText=String(i);select.appendChild(element);}
select=document.getElementById('amPmOptions');element=document.createElement("option");element.innerText="am";select.appendChild(element);element=document.createElement("option");element.innerText="pm";select.appendChild(element);}
function populateDays(inYear,inMonth)
{var select=document.getElementById('dayOptions');var element;var i=0;var month=parseInt(inMonth);var monthLength=kMonthLength[parseInt(inMonth)];if(month==1&&isLeapYear(inYear))
{monthLength=29;}
if(select.length!=monthLength)
{for(i=select.length;i>0;i--)
{select.remove(i-1);}
for(i=0;i<monthLength;i++)
{element=document.createElement("option");element.innerText=String(i+1);select.appendChild(element);}}}
function dateChanged(select)
{switch(select.id)
{case"monthOptions":gLocationsA[gUserLocationIndex].date.setUTCFullYear(document.getElementById("yearField").value);gLocationsA[gUserLocationIndex].date.setUTCMonth(select.selectedIndex);populateDays(gLocationsA[gUserLocationIndex].date.getFullYear(),gLocationsA[gUserLocationIndex].date.getUTCMonth());break;case"dayOptions":gLocationsA[gUserLocationIndex].date.setUTCDate(select.selectedIndex);break;case"timeOptions":break;case"amPmOptions":break;}}
function yearTyping(event)
{var year=document.getElementById("yearField").value;if(!isNaN(year))
{gLocationsA[gUserLocationIndex].date.setUTCFullYear(year);populateDays(gLocationsA[gUserLocationIndex].date.getUTCFullYear(),gLocationsA[gUserLocationIndex].date.getUTCMonth());}
else
{document.getElementById("yearField").value=gSituation.dateTime.getUTCFullYear();}}
function yearKeyPress(event)
{switch(event.keyCode)
{case 13:case 3:case 9:break;}}
function moonLocation(inLocationName,inLat,inLng,inYear,inMonth,inDay)
{this.locationName=inLocationName||"Apollo 11";this.lat=inLat||kApollo11Lat;this.lng=inLng||kApollo11Long;var locationDate=new Date();if(inYear!=null&&inMonth!=null&&inDay!=null)
{locationDate.setUTCFullYear(inYear,inMonth-1,inDay);locationDate.setUTCMinutes(0);locationDate.setUTCSeconds(0);}
this.date=locationDate;}
function setInfoButtonStyle()
{gInfoButton.setStyle("white","white");}
function zoomSliderSlid(value)
{var whichDiv=gUseChart2?"skyChartFront2":"skyChartFront";var imageSrcToChange=document.getElementById(whichDiv);var prevValue=gSituation.gaze.FOV;var scaling=prevValue/value;var origHeight=document.getElementById("skyDiv").clientHeight;var origWidth=document.getElementById("skyDiv").clientWidth;imageSrcToChange.style.width=Math.round(origWidth*scaling)+'px';imageSrcToChange.style.height=Math.round(origHeight*scaling)+'px';imageSrcToChange.style.top=(origHeight-imageSrcToChange.clientHeight)/2.0+'px';imageSrcToChange.style.left=(origWidth-imageSrcToChange.clientWidth)/2.0+'px';var gazeStr="Alt: "+gSituation.gaze.alt.toFixed(1)+"  Az: "+gSituation.gaze.az.toFixed(1)+"  FOV: "+value.toFixed(1);document.getElementById(kGazeStatusElemID).innerText=gazeStr;}
function zoomSliderChanged(value)
{if(Math.abs(gSituation.gaze.FOV-value)>kEpsilon)
{gSituation.gaze.FOV=value;refreshSituationStatus();sendMiniChartRequest();}}
function zoomAction(inValue)
{processEvent(inValue);gZoomSlider.setValue(gSituation.gaze.FOV);}
function refreshWidgetStuff()
{if(Math.abs(gZoomSlider.value-gSituation.gaze.FOV)>kEpsilon)
{gZoomSlider.setValue(gSituation.gaze.FOV);}}