//------------------------------------------------------------------------------------------- 
//
// Copyright © 1990-2009 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 kWidthValue="420";var kHeightValue="262";var kDefaultAlt=25;var kDefaultAboveAlt=-90;var kAboveElevation=3000000;var kSurfaceState=0;var kAboveState=1;var kMaxFOV=90.0;var kMinFOV=0.05;var kAm=0;var kPm=1;var kBackHeight=310;var kCubeTransition=0;var kCoreImageTransition=1;var kFadeInOut=6;var kNorthIndex=0;var kWestIndex=1;var kSouthIndex=2;var kEastIndex=3;var kFullSkyIndex=4;var kWidgetTitleFrontElemID="widgetTitleFront";var kLocationFrontElemID="locationFront";var kDateStatusElemID="dateStatusFront";var kLocationStatusElemID="locationStatusFront";var kGazeStatusElemID="gazeStatusFront";var kCubeCompositionDivID="cubeCompositionDiv";var kCubeCompositionElemID="cubeComposition";var kCoreImageCompositionDivID="coreImageCompositionDiv";var kCoreImageCompositionElemID="coreImageComposition";var kJSBackgroundElemID="loadingFront";var kJSSkyChart1ElemID="skyChartFront";var kJSSkyChart2ElemID="skyChartFront2";var kLogoCompositionElemID="logoComposition";var kAboutTabView=0;var kLocationTabView=1;var kOptionsTabView=2;var kHelpTabView=3;var kTabsCount=kHelpTabView;var kEpsilon=0.0001;var gFadeTitleIn=false;var kMonthNames=new Array(12);kMonthNames[0]="January";kMonthNames[1]="February";kMonthNames[2]="March";kMonthNames[3]="April";kMonthNames[4]="May";kMonthNames[5]="June";kMonthNames[6]="July";kMonthNames[7]="August";kMonthNames[8]="September";kMonthNames[9]="October";kMonthNames[10]="November";kMonthNames[11]="December";var kMonthLength=new Array(31,28,31,30,31,30,31,31,30,31,30,31);var _acct="DM5310155MZA92EN3";var gInfoButton;var gMiniChartButton;var gSNWebButton;var glassDoneButton;var glassFindPostalCodeButton;var gRSSFeedURLHttpRequest=false;var gRSSFeedHttpRequest=false;var gMiniChartHttpRequest=null;var gGetNearestCityHttpRequest=false;var gFindPostalCodeHttpRequest=false;var gMiniChartRequestError=false;var gElevationState=kSurfaceState;var gPostalFound=true;var gPostalValidated=false;var gLatLongValidated=false;var gSituation=new SNSituation();var gCurrentLocation="";var gUserLocationPref="";var gAzimuthOptionsIndexPref=1;var gTransitionOptionsIndexPref=kFadeInOut;var gOldTransitionOptionsIndexPref=-1;var gTimeZonePref;var gCurrentTabPref=0;var gSetupWasCalled=false;var gIsCollapsed=true;var gUseChart2=true;var gCompositionElemID=kCubeCompositionElemID;var gOldImage1Count=0;var gOldImage2Count=0;var gRefreshTimer=null;var gRefreshPeriod=900;var gFadeOutEffect=null;var gTicker=null;var gTickerLastDate=new Date();var gTabImagesA=new Array();var gTabONImagesA=new Array();if(window.widget)
{widget.onshow=onShow;widget.onhide=onHide;}
function onShow()
{var front=document.getElementById("front");if(front!=null&&front.style.display!="none")
{restoreGaze();if(!gSetupWasCalled)
{sendMiniChartRequest();}
gSetupWasCalled=false;var showDate=new Date();var oneHour=1000*3600;var elapsedTime=(showDate-gTickerLastDate)/oneHour;if((document.getElementById("TICKER").innerHTML.length==0)||(elapsedTime>1))
{gRSSFeedURLHttpRequest=getWidgetRSSFeedURL(getRSSFeedXMLCallback);gTickerLastDate=showDate;}
else
{startTickerTimer();}
if(gRefreshTimer==null)
{gRefreshTimer=setInterval("sendMiniChartRequest();",1000*gRefreshPeriod);}
if(gTicker==null)
{gTicker=setTimeout("Ticker_tick();",30);}
if(gTransitionOptionsIndexPref!=kFadeInOut)
{var compositionElem=document.getElementById(gCompositionElemID);if(compositionElem&&compositionElem.paused())
{compositionElem.play();}}
setInfoButtonStyle();}}
function onHide()
{var front=document.getElementById("front");if(front!=null&&front.style.display!="none")
{if(gRefreshTimer!=null)
{clearInterval(gRefreshTimer);gRefreshTimer=null;}
if(gTicker!=null)
{clearTimeout(gTicker);gTicker=null;}
if(gComposition.timer!=null)
{clearInterval(gComposition.timer);gComposition.timer=null;}
if(gComposition.timeout!=null)
{clearTimeout(gComposition.timeout);gComposition.timeout=null;}
gComposition.canProcessRequests=true;gComposition.showLoading=true;var front=document.getElementById("front");if(gTransitionOptionsIndexPref!=kFadeInOut)
{var compositionElem=document.getElementById(gCompositionElemID);if(compositionElem)
{compositionElem.pause();}}}}
function resetRefreshTimer()
{if(gRefreshTimer!=null)
{clearInterval(gRefreshTimer);}
gRefreshTimer=setInterval("sendMiniChartRequest();",1000*gRefreshPeriod);}
function getZoomStep()
{var currentFOV=gSituation.gaze.FOV;var zoomStep=15;if(currentFOV<45)
{zoomStep=10;if(currentFOV<30)
{zoomStep=5;if(currentFOV<15)
{zoomStep=3;if(currentFOV<5)
{zoomStep=1;if(currentFOV<2)
{zoomStep=0.5;if(currentFOV<1)
{zoomStep=0.1;}}}}}}
return zoomStep;}
function processEvent(inKey)
{var saveAzimuth=gSituation.gaze.az;var sendRequest=true;var zoomStep=15.0;var kMaxAltitude=89.9;var kMinAltitude=4.001;var kFractionToMove=1/4;var amountToMove=gSituation.gaze.FOV*kFractionToMove;switch(inKey)
{case"N":case"n":if(gElevationState==kSurfaceState)
{gSituation.gaze.az=0;setDefaultFOV();setDefaultAltitude();}
else
{sendRequest=false;}
break;case"S":case"s":if(gElevationState==kSurfaceState)
{gSituation.gaze.az=180;setDefaultFOV();setDefaultAltitude();}
else
{sendRequest=false;}
break;case"E":case"e":if(gElevationState==kSurfaceState)
{gSituation.gaze.az=90;setDefaultFOV();setDefaultAltitude();}
else
{sendRequest=false;}
break;case"W":case"w":if(gElevationState==kSurfaceState)
{gSituation.gaze.az=270;setDefaultFOV();setDefaultAltitude();}
else
{sendRequest=false;}
break;case"elev":gElevationState=(gElevationState==kSurfaceState)?kAboveState:kSurfaceState;setDefaultElevation();setDefaultFOV();setDefaultAltitude();gSituation.gaze.az=0;var whichImage=(gElevationState==kSurfaceState)?"url(Images/liftOffButton.png)":"url(Images/liftOffButton2.png)";document.getElementById("elevationButton").setAttribute("style","background-image: "+whichImage);break;case"left":if(gElevationState==kSurfaceState)
{gSituation.gaze.az=(gSituation.gaze.az-amountToMove)%360;if(gSituation.gaze.az<0)
{gSituation.gaze.az=360+gSituation.gaze.az;}}
else
{gSituation.location.lng-=amountToMove;if(gSituation.location.lng<-180)
{gSituation.location.lng=360+gSituation.location.lng;}
findNearestCity();}
break;case"right":if(gElevationState==kSurfaceState)
{gSituation.gaze.az=(gSituation.gaze.az+amountToMove)%360;}
else
{gSituation.location.lng+=amountToMove;if(gSituation.location.lng>180)
{gSituation.location.lng-=360;}
findNearestCity();}
break;case"up":if(gElevationState==kSurfaceState)
{if(gSituation.gaze.alt<kMaxAltitude)
{gSituation.gaze.alt+=amountToMove;if(gSituation.gaze.alt>kMaxAltitude)
{gSituation.gaze.alt=kMaxAltitude;}}
else
{sendRequest=false;}}
else
{gSituation.location.lat=(gSituation.location.lat+amountToMove)%360;findNearestCity();}
break;case"down":if(gElevationState==kSurfaceState)
{if(gSituation.gaze.alt>kMinAltitude)
{gSituation.gaze.alt-=amountToMove;if(gSituation.gaze.alt<kMinAltitude)
{gSituation.gaze.alt=kMinAltitude;}}
else
{sendRequest=false;}}
else
{gSituation.location.lat=(gSituation.location.lat-amountToMove)%360;findNearestCity();}
break;case"+":zoomStep=getZoomStep();gSituation.gaze.FOV-=zoomStep;if(gSituation.gaze.FOV<kMinFOV)
{gSituation.gaze.FOV=kMinFOV;}
break;case"-":zoomStep=getZoomStep();gSituation.gaze.FOV+=zoomStep;if(gSituation.gaze.FOV>kMaxFOV)
{gSituation.gaze.FOV=kMaxFOV;}
break;default:sendRequest=widgetProcessEvent(inKey);break;}
if(sendRequest)
{if(gTransitionOptionsIndexPref!=kFadeInOut)
{var compositionElem=document.getElementById(gCompositionElemID);gComposition.duration=(event.shiftKey?3:1);compositionElem.setInputValue("duration",gComposition.duration);}
sendMiniChartRequest();}
return sendRequest;}
function keyPressed(e)
{var back=document.getElementById("back");if(back.style.display=="block")
{return;}
var key;if(e.which<63232)
{key=String.fromCharCode(e.which);}
else
{switch(e.keyCode)
{case 63232:key="up";break;case 63233:key="down";break;case 63234:key="left";break;case 63235:key="right";break;default:break;}}
if(processEvent(key))
{e.stopPropagation();e.preventDefault();}}
function keyPressedBack(e)
{var back=document.getElementById("back");if(back.style.display=="block"&&event.shiftKey)
{var key=String.fromCharCode(e.which);switch(key)
{case"t":case"T":document.getElementById("transitionsDiv").style.display=="block"?document.getElementById("transitionsDiv").style.display="none":document.getElementById("transitionsDiv").style.display="block";break;}}}
function goToStarryNight(event)
{if(window.widget)
{widget.openURL("http://www.starrynight.com");}}
function goToDigitalDownload(event)
{if(window.widget)
{widget.openURL("http://store.yahoo.com/cgi-bin/clink?yhst-49664100110157+58wNhQ+digitaldownload.html");}}
function goToCompany(event)
{if(window.widget)
{widget.openURL("http://www.simulationcurriculum.com");}}
function goToStarryNightServer(event)
{if(window.widget)
{var url=getRedirectURL(kWidgetID,kDashboardWidgetVersion,gSituation);widget.openURL(url);trackAction(kWidgetID,kDashboardWidgetVersion,kTrack_openWebAppClicked,false);}}
function getRSSFeedXMLCallback()
{if(gRSSFeedURLHttpRequest!=null&&gRSSFeedURLHttpRequest.readyState==4)
{if(gRSSFeedURLHttpRequest.status==200)
{var rssURL=parseRSSXML(gRSSFeedURLHttpRequest.responseXML,kWidgetID,kDashboardWidgetVersion);gRSSFeedHttpRequest=new XMLHttpRequest();gRSSFeedHttpRequest.onreadystatechange=RSSFeedXMLCallback;gRSSFeedHttpRequest.overrideMimeType("text/xml");gRSSFeedHttpRequest.open("GET",rssURL,true);gRSSFeedHttpRequest.setRequestHeader("Cache-Control","no-cache");gRSSFeedHttpRequest.send(null);}}}
function RSSFeedXMLCallback()
{if(gRSSFeedHttpRequest!=null&&gRSSFeedHttpRequest.responseXML)
{if(gRSSFeedHttpRequest.readyState==4)
{if(gRSSFeedHttpRequest.status==200)
{try
{var rss=findChild(gRSSFeedHttpRequest.responseXML,'rss');var channel=findChild(rss,'channel');if(channel!=null)
{var i=0;var html="";for(var child=channel.firstChild;child!=null;child=child.nextSibling)
{if(child.nodeName=='item')
{var title=findChild(child,'title');var link=findChild(child,'link');i++;html+="...  "
+"<a href='#' onclick=\"widget.openURL('"+link.firstChild.data+"'); trackAction("+kWidgetID+",'"+kDashboardWidgetVersion+"',"+kTrack_tickerLinkClicked+", false,'"+link.firstChild.data+"');\">"
+title.firstChild.data
+"</a> ";if(i>kNumberOfRSSFeedEntriesToUse)
{break;}}}}}
catch(ex)
{alert("XML parsing exception");}
document.getElementById("TICKER").innerHTML=html;ticker_start();}
else if(gRSSFeedHttpRequest.status==404)
{alert("URL doesn't exist");}
else
{alert("There was a problem with the RSS Feed request: "+gRSSFeedHttpRequest.status);alert(gRSSFeedHttpRequest.statusText);var html=" ... Can't connect to server ...";document.getElementById("TICKER").innerHTML=html;ticker_start();}}}}
function findChild(inElement,inNodeName)
{var aChild;for(aChild=inElement.firstChild;aChild!=null;aChild=aChild.nextSibling)
{if(aChild.nodeName==inNodeName)
return aChild;}
return null;}
var TICKER_CONTENT="";var TICKER_RIGHTTOLEFT=false;var TICKER_SPEED=1;var TICKER_STYLE="font-family:Lucida Grande; font-size:13px; color:white";var TICKER_PAUSED=false;var gTickerScrollPos=0;function ticker_start()
{TICKER_CONTENT=document.getElementById("TICKER").innerHTML;TICKER_WIDTH=document.getElementById("TICKER").clientWidth;var img="<img src=ticker_space.gif width="+TICKER_WIDTH+" height=0>";document.getElementById("TICKER").innerHTML="<TABLE  cellspacing='0' cellpadding='0' width='100%'><TR><TD nowrap='nowrap'>"+img+"<SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY' width='100%'>&nbsp;</SPAN>"+img+"</TD></TR></TABLE>";document.getElementById("TICKER").scrollLeft=TICKER_RIGHTTOLEFT?document.getElementById("TICKER").scrollWidth-document.getElementById("TICKER").offsetWidth:0;document.getElementById("TICKER_BODY").innerHTML=TICKER_CONTENT;document.getElementById("TICKER").style.display="block";startTickerTimer();}
function startTickerTimer()
{if(gTicker!=null)
{clearTimeout(gTicker);gTicker=null;}
Ticker_tick();}
function Ticker_tick()
{if(!TICKER_PAUSED)document.getElementById("TICKER").scrollLeft+=TICKER_SPEED*(TICKER_RIGHTTOLEFT?-1:1);if(TICKER_RIGHTTOLEFT&&document.getElementById("TICKER").scrollLeft<=0)document.getElementById("TICKER").scrollLeft=document.getElementById("TICKER").scrollWidth-document.getElementById("TICKER").offsetWidth;if(!TICKER_RIGHTTOLEFT&&document.getElementById("TICKER").scrollLeft>=document.getElementById("TICKER").scrollWidth-document.getElementById("TICKER").offsetWidth)document.getElementById("TICKER").scrollLeft=0;gTicker=setTimeout("Ticker_tick()",30);}
function saveTickerScroll()
{gTickerScrollPos=document.getElementById("TICKER").scrollLeft;}
function restoreTickerScroll()
{document.getElementById("TICKER").scrollLeft=gTickerScrollPos;}
function latLongTyping(event)
{var latitude=document.getElementById("latitudeField").value;var longitude=document.getElementById("longitudeField").value;if(latitude=='-')
{latitude='0';}
if(longitude=='-')
{longitude='0';}
if(!isNaN(latitude)&&!isNaN(longitude))
{gLatLongValidated=false;var s=latitude+" / "+longitude;setElementByIdInnerText("locationBack",s);setElementByIdValue("postalCodeField","");postLatLongTyping();}
else
{document.getElementById("latitudeField").value=gSituation.location.lat;document.getElementById("longitudeField").value=gSituation.location.lng;}}
function latLongKeyPress(event)
{switch(event.keyCode)
{case 13:case 3:case 9:if(!gLatLongValidated)
{gSituation.location.lat=document.getElementById("latitudeField").value;gSituation.location.lng=document.getElementById("longitudeField").value;findNearestCity();}
break;}}
function findNearestCity()
{if(window.widget)
{var saveElevation=gSituation.location.elevation;gSituation.location.elevation=kDefaultElevation;var url=getRequestURL(kWidgetID,kDashboardWidgetVersion,kGetNearestCityRequest,gSituation);gSituation.location.elevation=saveElevation;gGetNearestCityHttpRequest=new XMLHttpRequest();if(gGetNearestCityHttpRequest.overrideMimeType)
{gGetNearestCityHttpRequest.overrideMimeType('text/xml');}
document.getElementById("locationBack").innerText="Searching...";gGetNearestCityHttpRequest.onreadystatechange=nearestCityCallback;gGetNearestCityHttpRequest.open('GET',url,true);gGetNearestCityHttpRequest.setRequestHeader("Cache-Control","no-cache");gGetNearestCityHttpRequest.send(null);gLatLongValidated=false;preFindNearestCity();}}
function nearestCityCallback()
{if(gGetNearestCityHttpRequest.readyState==4)
{if(gGetNearestCityHttpRequest.status==200)
{var replyFields=new Array();replyFields=gGetNearestCityHttpRequest.responseText.split('|');document.getElementById("locationBack").innerText=replyFields[0];document.getElementById(kLocationFrontElemID).innerText=replyFields[0];gUserLocationPref=replyFields[0];gSituation.location.timezone=replyFields[1];gLatLongValidated=true;postFindNearestCity();trackAction(kWidgetID,kDashboardWidgetVersion,kTrack_NearestCityRequest,false);}
else if(gGetNearestCityHttpRequest.status==404)
{alert("URL doesn't exist");trackAction(kWidgetID,kDashboardWidgetVersion,kTrack_NearestCityRequest,true,"URL doesn't exist");}
else
{alert("There was a problem with the Nearest City request: "+gGetNearestCityHttpRequest.status);alert(gGetNearestCityHttpRequest.statusText);trackAction(kWidgetID,kDashboardWidgetVersion,kTrack_NearestCityRequest,true,"There was a problem with the Nearest City request: ");}}}
var gComposition={showLoading:true,canProcessRequests:true,startTime:0,time:0,startValue:0,endValue:0,duration:1,timer:null,timeout:null,onFinished:null};function handleChartDrawer(event)
{if(window.widget)
{if(!gIsCollapsed)
{document.getElementById(kLocationFrontElemID).innerText=kWidgetTitle;document.getElementById("SNFrontDiv").style.display="none";if(gTransitionOptionsIndexPref!=kFadeInOut)
{document.getElementById(gCompositionElemID).pause();document.getElementById("middle-contents").style.display=="none";}}
else
{document.getElementById(kLocationFrontElemID).innerText=gCurrentLocation;}
toggleView(event);setInfoButtonStyle();setTimeout(sendMiniChartRequest,1000);}}
function abortPendingChartRequest()
{var bResult=false;if(gMiniChartHttpRequest!=null&&gMiniChartHttpRequest.readyState!=4)
{gMiniChartHttpRequest.abort();gMiniChartHttpRequest=null;bResult=true;}}
function sendMiniChartRequest()
{if(!gIsCollapsed&&canProcessRequests())
{if(gTransitionOptionsIndexPref==kFadeInOut)
{}
document.getElementById("statusFront").innerText="";var url=generateRequest();var bReqAborted=abortPendingChartRequest();gMiniChartHttpRequest=new XMLHttpRequest();if(gMiniChartHttpRequest.overrideMimeType)
{gMiniChartHttpRequest.overrideMimeType('text/xml');}
if(!bReqAborted)
{var transitionClass=getTransitionIndexClass(gTransitionOptionsIndexPref);switch(transitionClass)
{case kCubeTransition:var compositionElem=document.getElementById(gCompositionElemID);if(gComposition.timer==null&&compositionElem!=null)
{gComposition.time=0;if(gComposition.showLoading)
{gComposition.startValue=0;gComposition.showLoading=false;}
else
{gComposition.startValue=gUseChart2?-90:90;}
gComposition.endValue=0;compositionElem.setInputValue("time",gComposition.time);compositionElem.setInputValue("startValue",gComposition.startValue);compositionElem.setInputValue("endValue",gComposition.endValue);compositionElem.setInputValue("duration",gComposition.duration);gOldImage1Count=compositionElem.getOutputValue("image1Ready");gOldImage2Count=compositionElem.getOutputValue("image2Ready");gComposition.startTime=new Date();gComposition.onFinished=checkCubeTransitionImgLoaded;gComposition.timer=setInterval(animateComposition,13);}
break;case kCoreImageTransition:var compositionElem=document.getElementById(gCompositionElemID);if(gComposition.timer==null&&compositionElem!=null)
{gComposition.time=0;if(gComposition.showLoading)
{gComposition.startValue=0;gComposition.showLoading=false;}
else
{gComposition.startValue=1;}
gComposition.endValue=0;compositionElem.setInputValue("loadingImageIndex",1);compositionElem.setInputValue("restartLoading",false);compositionElem.setInputValue("restartLoading",true);compositionElem.setInputValue("transitionIndex",gTransitionOptionsIndexPref-1);compositionElem.setInputValue("time",gComposition.time);compositionElem.setInputValue("startValue",gComposition.startValue);compositionElem.setInputValue("endValue",gComposition.endValue);compositionElem.setInputValue("duration",gComposition.duration/2.0);gOldImage1Count=compositionElem.getOutputValue("imageReady");gComposition.startTime=new Date();gComposition.onFinished=checkCITransitionImgLoaded;gComposition.timer=setInterval(animateComposition,13);}
break;case kFadeInOut:break;default:break;}}
gMiniChartHttpRequest.onreadystatechange=miniChartCallback;gMiniChartHttpRequest.open('GET',url,true);gMiniChartHttpRequest.setRequestHeader("Cache-Control","no-cache");gMiniChartHttpRequest.send(null);refreshSituationStatus();refreshWidgetStuff();}
else
{document.getElementById("statusFront").innerText="";}}
function miniChartCallback()
{if(gMiniChartHttpRequest.readyState==4)
{var imageURL=kServerImages;var imageName="";if(gMiniChartHttpRequest.status!=200)
{imageURL=document.URL;var i=imageURL.lastIndexOf("/");imageURL=imageURL.substr(0,i);imageName="/Images/SNAlert.png";gMiniChartHttpRequest.status=200;gMiniChartRequestError=true;}
else
{gMiniChartRequestError=false;imageName=gMiniChartHttpRequest.responseText;}
gMiniChartHttpRequest=null;gUseChart2=!gUseChart2;var transitionClass=getTransitionIndexClass(gTransitionOptionsIndexPref);switch(transitionClass)
{case kCubeTransition:{var compositionElem=document.getElementById(gCompositionElemID);var whichFace=gUseChart2?"Image1URL":"Image2URL";compositionElem.setInputValue(whichFace,imageURL+imageName);}
break;case kCoreImageTransition:{var compositionElem=document.getElementById(gCompositionElemID);compositionElem.setInputValue("imageURL",imageURL+imageName);}
break;case kFadeInOut:{var fadeOutDiv=null;var fadeInDiv=null;if(gUseChart2)
{fadeOutDiv="skyChartFront";fadeInDiv="skyChartFront2";}
else
{fadeOutDiv="skyChartFront2";fadeInDiv="skyChartFront";}
if(gFadeOutEffect!=null)
{gFadeOutEffect.cancel();}
gFadeOutEffect=Effect.Fade(fadeOutDiv,{duration:0.8,to:0.0});document.getElementById(fadeInDiv).setAttribute("style","opacity:0.0;");document.getElementById(fadeInDiv).src=imageURL+imageName;Effect.Appear(fadeInDiv,{duration:0.8,to:1.0});}
break;default:break;}
resetRefreshTimer();trackAction(kWidgetID,kDashboardWidgetVersion,kTrack_ImageRequest,false);}}
function canProcessRequests()
{return gComposition.canProcessRequests;}
function animateComposition()
{var front=document.getElementById("front");if(front!=null&&front.style.display!="none")
{var compositionElem=document.getElementById(gCompositionElemID);if(compositionElem!=null)
{var currentTime=new Date();gComposition.time=(currentTime-gComposition.startTime)/1000;compositionElem.setInputValue("time",gComposition.time);var angle=compositionElem.getOutputValue("currentValue");if(Math.abs(angle-gComposition.endValue)<0.0001)
{clearInterval(gComposition.timer);gComposition.timer=null;if(gComposition.onFinished)
{gComposition.timeout=setTimeout(gComposition.onFinished,0);}}}}}
function checkCubeTransitionImgLoaded()
{var front=document.getElementById("front");if(front!=null&&front.style.display!="none")
{var compositionElem=document.getElementById(gCompositionElemID);if(compositionElem!=null)
{var whichFace=gUseChart2?"Image1URL":"Image2URL";if((whichFace=="Image1URL"&&compositionElem.getOutputValue("image1Ready")==gOldImage1Count||whichFace=="Image2URL"&&compositionElem.getOutputValue("image2Ready")==gOldImage2Count)&&!gMiniChartRequestError)
{gComposition.timeout=setTimeout(checkCubeTransitionImgLoaded,200);}
else
{gComposition.timeout=null;gComposition.canProcessRequests=false;gComposition.time=0;gComposition.startValue=0;gComposition.endValue=gUseChart2?90:-90;compositionElem.setInputValue("time",gComposition.time);compositionElem.setInputValue("startValue",gComposition.startValue);compositionElem.setInputValue("endValue",gComposition.endValue);gComposition.startTime=new Date();gComposition.onFinished=compositionHasFinished;if(gComposition.timer!=null)
{clearInterval(gComposition.timer);}
gComposition.timer=setInterval(animateComposition,13);}}}}
function checkCITransitionImgLoaded()
{var front=document.getElementById("front");if(front!=null&&front.style.display!="none")
{var compositionElem=document.getElementById(gCompositionElemID);if(compositionElem!=null)
{if((compositionElem.getOutputValue("imageReady")==gOldImage1Count)&&!gMiniChartRequestError)
{gComposition.timeout=setTimeout(checkCITransitionImgLoaded,100);}
else
{gComposition.timeout=null;gComposition.canProcessRequests=false;gComposition.time=0;gComposition.startValue=0;gComposition.endValue=1;compositionElem.setInputValue("time",gComposition.time);compositionElem.setInputValue("startValue",gComposition.startValue);compositionElem.setInputValue("endValue",gComposition.endValue);compositionElem.setInputValue("duration",gComposition.duration);gComposition.startTime=new Date();gComposition.onFinished=compositionHasFinished;if(gComposition.timer!=null)
{clearInterval(gComposition.timer);}
gComposition.timer=setInterval(animateComposition,13);}}}}
function compositionHasFinished()
{var front=document.getElementById("front");if(front!=null&&front.style.display!="none")
{var compositionElem=document.getElementById(gCompositionElemID);if(compositionElem!=null)
{var transitionClass=getTransitionIndexClass(gTransitionOptionsIndexPref);if(transitionClass==kCoreImageTransition)
{compositionElem.setInputValue("loadingImageIndex",0);}}}
gComposition.canProcessRequests=true;}
function populateAzimuthOptions()
{var select=document.getElementById('azimuthOptions');var element;if(select!=null)
{element=document.createElement("option");element.innerText="North";select.appendChild(element);element=document.createElement("option");element.innerText="West";select.appendChild(element);element=document.createElement("option");element.innerText="South";select.appendChild(element);element=document.createElement("option");element.innerText="East";select.appendChild(element);}}
function azimuthChanged(select)
{gAzimuthOptionsIndexPref=select.selectedIndex;setAzimuth(gAzimuthOptionsIndexPref);}
function populateTransitionOptions()
{var select=document.getElementById('transitionOptions');var element;element=document.createElement("option");element.innerText="Cube";select.appendChild(element);element=document.createElement("option");element.innerText="Ripple";select.appendChild(element);element=document.createElement("option");element.innerText="Flash";select.appendChild(element);element=document.createElement("option");element.innerText="Page Curl";select.appendChild(element);element=document.createElement("option");element.innerText="Swipe";select.appendChild(element);element=document.createElement("option");element.innerText="Mod";select.appendChild(element);element=document.createElement("option");element.innerText="Fade In/Out";select.appendChild(element);}
function transitionChanged(select)
{gTransitionOptionsIndexPref=select.selectedIndex;}
function getTransitionIndexClass(inIndex)
{var transitionClass=inIndex;if(inIndex>=kCoreImageTransition&&inIndex<kFadeInOut)
{transitionClass=kCoreImageTransition;}
return transitionClass;}
function prepareForNewTransition()
{var oldTransitionClass=getTransitionIndexClass(gOldTransitionOptionsIndexPref);var newTransitionClass=getTransitionIndexClass(gTransitionOptionsIndexPref);if(oldTransitionClass==newTransitionClass)
{return;}
switch(oldTransitionClass)
{case kCubeTransition:{var compositionElem=document.getElementById(gCompositionElemID);compositionElem.pause();var compositionDiv=document.getElementById(kCubeCompositionDivID);compositionDiv.style.display="none";}
break;case kCoreImageTransition:{var compositionElem=document.getElementById(gCompositionElemID);compositionElem.pause();var compositionDiv=document.getElementById(kCoreImageCompositionDivID);compositionDiv.style.display="none";}
break;case kFadeInOut:{var backgroundDiv=document.getElementById(kJSBackgroundElemID);backgroundDiv.style.display="none";var whichDiv=gUseChart2?"skyChartFront":"skyChartFront2";Effect.Fade(whichDiv,{duration:0.8,to:0.0});}
break;default:break;}
switch(newTransitionClass)
{case kCubeTransition:{var compositionDiv=document.getElementById(kCubeCompositionDivID);compositionDiv.style.display="block";gCompositionElemID=kCubeCompositionElemID;}
break;case kCoreImageTransition:{var compositionDiv=document.getElementById(kCoreImageCompositionDivID);compositionDiv.style.display="block";gCompositionElemID=kCoreImageCompositionElemID;}
break;case kFadeInOut:{var backgroundDiv=document.getElementById(kJSBackgroundElemID);backgroundDiv.style.display="block";}
break;default:break;}
gOldTransitionOptionsIndexPref=gTransitionOptionsIndexPref;}
function fadeUpUI(e)
{var x=document.getElementById("middle-contents").clientWidth;var y=document.getElementById("middle-contents").clientHeight;if(document.getElementById("gazeControls").style.opacity<1.0)
{setTimeout('Effect.Appear(gazeControls, { duration:0.4, to:1.0 })',50);}
if(document.getElementById("zoomControls").style.opacity<1.0)
{setTimeout('Effect.Appear(zoomControls, { duration:0.4, to:1.0 })',50);}
if(document.getElementById("cardinalControls")!=null&&document.getElementById("cardinalControls").style.opacity<1.0)
{setTimeout('Effect.Appear(cardinalControls, { duration:0.4, to:1.0 })',50);}
if(document.getElementById("elevationControls").style.opacity<1.0)
{setTimeout('Effect.Appear(elevationControls, { duration:0.4, to:1.0 })',50);}}
function findPos(obj){var curleft=curtop=0;if(obj.offsetParent){curleft=obj.offsetLeft
curtop=obj.offsetTop
while(obj=obj.offsetParent){curleft+=obj.offsetLeft
curtop+=obj.offsetTop}}
return[curleft,curtop];}
function fadeDownUI(e)
{var x=document.getElementById("front").clientWidth;var y=document.getElementById("front").clientHeight;if(e.x>x-1||e.y>y-1||e.x<0||e.y<0)
{if(document.getElementById("gazeControls").style.opacity>0.0)
{setTimeout('Effect.Appear(gazeControls, { duration:0.4, to:0.0 })',50);}
if(document.getElementById("zoomControls").style.opacity>0.0)
{setTimeout('Effect.Appear(zoomControls, { duration:0.4, to:0.0 })',50);}
if(document.getElementById("cardinalControls")!=null&&document.getElementById("cardinalControls").style.opacity>0.0)
{setTimeout('Effect.Appear(cardinalControls, { duration:0.4, to:0.0 })',50);}
if(document.getElementById("elevationControls").style.opacity>0.0)
{setTimeout('Effect.Appear(elevationControls, { duration:0.4, to:0.0 })',50);}}}
function updateWidgetTitle()
{var divToFadeIn=null;var divToFadeOut=null;if(gFadeTitleIn)
{divToFadeIn=kLocationFrontElemID;divToFadeOut=kWidgetTitleFrontElemID;}
else
{divToFadeIn=kWidgetTitleFrontElemID;divToFadeOut=kLocationFrontElemID;}
gFadeTitleIn=!gFadeTitleIn;Effect.Fade(divToFadeOut,{duration:0.8,to:0.0});Effect.Appear(divToFadeIn,{duration:0.8,to:1.0});}
function refreshSituationStatus()
{var lat=parseFloat(gSituation.location.lat);var lng=parseFloat(gSituation.location.lng);var locationStr="Lat: "+lat.toFixed(2)+"  Long: "+lng.toFixed(2);var gazeStr="Alt: "+gSituation.gaze.alt.toFixed(1)+"  Az: "+gSituation.gaze.az.toFixed(1)+"  FOV: "+gSituation.gaze.FOV.toFixed(1);document.getElementById(kDateStatusElemID).innerText=gSituation.dateTime.toUTCString();document.getElementById(kLocationStatusElemID).innerText=locationStr;document.getElementById(kGazeStatusElemID).innerText=gazeStr;}
function showPrefs()
{if(gComposition.timer!=null||!canProcessRequests())
{return;}
onHide();saveTickerScroll();var front=document.getElementById("front");var back=document.getElementById("back");if(window.widget)
{if(gIsCollapsed)
{window.resizeTo(kMaxWidgetWidth,kBackHeight);}
widget.prepareForTransition("ToBack");}
front.style.display="none";back.style.display="block";if(window.widget)
{setTimeout('widget.performTransition();',0);window.resizeTo(kMaxWidgetWidth,kBackHeight);}
loadPrefsUI();gOldTransitionOptionsIndexPref=gTransitionOptionsIndexPref;}
function resizeWindowForPrefs()
{if(window.widget)
{var elemStyle=document.defaultView.getComputedStyle(divider,"");var topHeight=parseInt(elemStyle.getPropertyValue("top"));var currentTabViewHeight=document.getElementById("tabDiv"+gCurrentTabPref).clientHeight;var elemStyle=document.defaultView.getComputedStyle(divider2,"");var bottomHeight=parseInt(elemStyle.getPropertyValue("bottom"));var wH=topHeight+currentTabViewHeight+bottomHeight;window.resizeTo(kMaxWidgetWidth,wH);}}
function selectTab(inWhichTab)
{if(gCurrentTabPref!=inWhichTab)
{savePrefsFromUI();var currentTabView=document.getElementById("tabDiv"+gCurrentTabPref);var newTabView=document.getElementById("tabDiv"+inWhichTab);Effect.Fade(currentTabView,{duration:0.8,to:0.0});Effect.Appear(newTabView,{duration:0.8,to:1.0});var oldTab=gCurrentTabPref;gCurrentTabPref=inWhichTab;setTabImage("tabImg"+gCurrentTabPref,gCurrentTabPref,true);setTabImage("tabImg"+oldTab,oldTab,false);currentTabView.style.zIndex=999;newTabView.style.zIndex=1000;}}
function loadTabImages(inImage,inImageON,inIndex)
{var img=new Image;img.src=inImage;gTabImagesA[inIndex]=img;img=new Image;img.src=inImageON;gTabONImagesA[inIndex]=img;}
function setTabImage(inElem,inImageIndex,inTurnON)
{if(inElem)
{var elem=document.getElementById(inElem);if(elem)
{if(inTurnON)
{elem.src=gTabONImagesA[inImageIndex].src;}
else
{if(gCurrentTabPref!=inImageIndex)
{elem.src=gTabImagesA[inImageIndex].src;}}}}}
function restoreGaze()
{if(window.widget)
{setAzimuth(gAzimuthOptionsIndexPref);if(gElevationState==kSurfaceState)
{gSituation.gaze.alt=kDefaultAlt;gSituation.gaze.FOV=kDefaultFOV;}
else
{gSituation.gaze.alt=kDefaultAboveAlt;gSituation.gaze.FOV=45.0;}}}
function setPreference(inPref,inDefaultValue)
{var result=inDefaultValue;if(inPref&&inPref.length>0)
{result=inPref;}
return result;}
function createKey(key)
{return widget.identifier+"-"+key;}
function changePref(inField,inPref)
{var state=document.getElementById(inField).checked?"Yes":"No";}
function prefLabelClick(inField,inPref)
{var element=document.getElementById(inField);element.checked=!element.checked;changePref(inField,inPref);}
function setAzimuth(inAzimuthIndex)
{switch(inAzimuthIndex)
{case kNorthIndex:gSituation.gaze.az=0;break;case kWestIndex:gSituation.gaze.az=270;break;case kSouthIndex:gSituation.gaze.az=180;break;case kEastIndex:gSituation.gaze.az=90;break;}}
function setDefaultFOV()
{gSituation.gaze.FOV=(gElevationState==kSurfaceState)?kDefaultFOV:kDefaultAboveFOV;}
function setDefaultAltitude()
{gSituation.gaze.alt=(gElevationState==kSurfaceState)?kDefaultAlt:kDefaultAboveAlt;}
function setDefaultElevation()
{gSituation.location.elevation=(gElevationState==kSurfaceState)?kDefaultElevation:kAboveElevation;}
function isLeapYear(inYear)
{var year=parseInt(inYear);if(year%4==0)
{if(year%100!=0)
{return true;}
else
{if(year%400==0)
return true;else
return false;}}
return false;}
function setElementByIdValue(inElement,inValue)
{var element=document.getElementById(inElement);if(element!=null)
{element.value=inValue;}}
function setElementByIdInnerText(inElement,inText)
{var element=document.getElementById(inElement);if(element!=null)
{element.innerText=inText;}}
var kTopHeight=38;var kSituationStatusHeight=32;var kBottomHeight=31;var kSidesWidth=12;var kMaxImageHeight=262;var kMinImageHeight=0;var kMaxImageWidth=420;var kMaxWidgetHeight=kTopHeight+kSituationStatusHeight+kMaxImageHeight+kBottomHeight;var kMaxWidgetWidth=kMaxImageWidth+2*kSidesWidth;var kMinWidgetHeight=kTopHeight+kBottomHeight;var kMinWidgetWidth=kMaxImageWidth+2*kSidesWidth;var gResizeAnimation={startTime:0,duration:kMaxImageHeight,positionFrom:0,positionTo:0,positionNow:0,frameFrom:0,frameTo:0,frameNow:0,timer:null,element:null,image:null,background:null,onfinished:null};function limit_3(a,b,c)
{return a<b?b:(a>c?c:a);}
function computeNextFloat(from,to,ease)
{return from+(to-from)*ease;}
function calculateBackgroundHeight()
{var height;if(gIsCollapsed)
height=kMinImageHeight;else
height=kMaxImageHeight+kSituationStatusHeight;return height;}
function animate()
{var T;var ease;var time=(new Date).getTime();var yLoc;var frame;T=limit_3(time-gResizeAnimation.startTime,0,gResizeAnimation.duration);ease=0.5-(0.5*Math.cos(Math.PI*T/gResizeAnimation.duration));if(T>=gResizeAnimation.duration)
{yLoc=gResizeAnimation.positionTo;clearInterval(gResizeAnimation.timer);gResizeAnimation.timer=null;if(gResizeAnimation.onfinished)
setTimeout(gResizeAnimation.onfinished,0);}
else
yLoc=computeNextFloat(gResizeAnimation.positionFrom,gResizeAnimation.positionTo,ease);gResizeAnimation.positionNow=Math.round(yLoc);gResizeAnimation.element.style.height=gResizeAnimation.positionNow+"px";}
function animFinished()
{if(window.widget)
{if(gIsCollapsed)
{window.resizeTo(kMaxWidgetWidth,kMinWidgetHeight);}
else
{document.getElementById("SNFrontDiv").style.display="block";if(gTransitionOptionsIndexPref!=kFadeInOut)
{document.getElementById("middle-contents").style.display=="block";var compositionElem=document.getElementById(gCompositionElemID);if(compositionElem&&compositionElem.paused())
{compositionElem.play();}}}
widget.setPreferenceForKey(gIsCollapsed?kYES:kNO,createKey(kCollapsedKey));}}
function toggleView(event)
{var midDiv=document.getElementById("middle");var timeNow=(new Date).getTime();var multiplier=(event.shiftKey?10:1);var startingSize=Math.round(midDiv.clientHeight,10);gResizeAnimation.element=midDiv;if(gResizeAnimation.timer!=null)
{clearInterval(gResizeAnimation.timer);gResizeAnimation.timer=null;gResizeAnimation.duration-=(timeNow-gResizeAnimation.startTime);gResizeAnimation.positionFrom=gResizeAnimation.positionNow;}
else
{gResizeAnimation.duration=kMaxImageHeight*multiplier;gResizeAnimation.positionFrom=startingSize;}
gIsCollapsed=!gIsCollapsed;var resizeTo=calculateBackgroundHeight();if(!gIsCollapsed&&window.widget)
window.resizeTo(kMaxWidgetWidth,kMaxWidgetHeight);gResizeAnimation.positionTo=Math.round(resizeTo);gResizeAnimation.startTime=timeNow-13;gResizeAnimation.onfinished=animFinished;gResizeAnimation.element.style.height=startingSize+"px";gResizeAnimation.timer=setInterval("animate();",13);animate();}