/*------------------------------------------------------------------------------------------- 
//
// Copyright © 1990-2009 by Simulation Curriculum Corp., All rights reserved.
//
// Contains:	Starry Night Web Service classes and utility functions
//
// Authors:		David Whipps, Ivan Pinzon
//
//
//--------------------------------------------------------------------------------------------
*/


var kServerURL="http://wanderer.starrynight.com/";var kServerIP="http://voyager.starrynight.com/";var kServerCGI=kServerURL+"cgi-bin/snclient.cgi?";var kRedirectPHP="http://www.starrynight.com/widget/snredirect.php?";var kServerImages=kServerIP+"snserverimages/";var kSearchPostalCodeRequest=1;var kGenerateImageRequest=2;var kGenerateImageHTMLRequest=3;var kGenerateImageAndMapRequest=4;var kGetNearestCityRequest=5;var kUnknownPlatform=0;var kWebApplication=1;var kDashboardWidget=2;var kGoogleGadget=3;var kYahooWidget=4;var kBlackBerryApp=5;var kVistaGadget=6;var kMEW_Dashboard=7;var kMEW_Yahoo=8;var kGetFullRSSFeedSummaries=false;var kDefaultWidgetRSSFeedURL="http://feeds.feedburner.com/StarryNightOnlineTicker";var kDynamicRSSFeedLocationFileURL="http://www.starrynight.com/widget/rss/feedlocations.xml";var kRSSLocationXMLPlatformStr="platform";var kRSSLocationXMLPlatConstStr="platformConst";var kRSSLocationXMLPlatVersionStr="vers";var kDefaultVersionStr="default";var kRSSLocationXMLURLStr="url";var kNumberOfRSSFeedEntriesToUse=10;var kTorontoLat=43.6555;var kTorontoLong=-79.4119;var kTorontoTimezone=-0.166667;var kApollo11Lat=0.67;var kApollo11Long=23.47;var kSunName="Sun";var kMercuryName="Mercury";var kVenusName="Venus";var kEarthName="Earth";var kMoonName="Moon";var kMarsName="Mars";var kPhobosName="Phobos";var kDeimosName="Deimos";var kJupiterName="Jupiter";var kIoName="Io";var kEuropaName="Europa";var kGanymedeName="Ganymede";var kCallistoName="Callisto";var kAmaltheaName="Almathea";var kHimaliaName="Himalia";var kElaraName="Elara";var kSaturnName="Saturn";var kMimasName="Mimas";var kEnceladusName="Enceladus";var kTethysName="Tethys";var kDioneName="Dione";var kRheaName="Rhea";var kTitanName="Titan";var kHyperionName="Hyperion";var kIapetusName="Iapetus";var kPhoebeName="Phoebe";var kJanusName="Janus";var kUranusName="Uranus";var kArielName="Ariel";var kUmbrielName="Umbriel";var kTitaniaName="Titania";var kOberonName="Oberon";var kMirandaName="Miranna";var kNeptuneName="Neptune";var kTritonName="Triton";var kNereidName="Nereid";var kPlutoName="Pluto";var kCharonName="Charon";var kCeresName="Ceres";var kErisName="Eris";var kSpaceStationName="ISS";var kHubbleScopeName="HST";var kChandraScopeName="CXO";var kDefaultLatitude=kTorontoLat;var kDefaultLongitude=kTorontoLong;var kDefaultTimezone=kTorontoTimezone;var kDefaultMoonLatitude=kApollo11Lat;var kDefaultMoonLongitude=kApollo11Long;var kDefaultLocationName=kEarthName;var kDefaultElevation=3.0;var kDefaultAltitude=15.0;var kDefaultAzimuth=180.0;var kDefaultFOV=90.0;var kDefaultAboveFOV=45.0;var kDefaultImageWidth=256;var kDefaultImageHeight=256;var kDefautlRequestType=kGenerateImageRequest;var kYES="Yes";var kNO="No";var kLocationNotFound="Not Found";var kUnknownVersionString="0.0.0";var kLatitude="&Lat=";var kLatitudeD="&LatD=";var kLatitudeM="&LatM=";var kLatitudeDir="&LatRG=";var kLongitude="&Lon=";var kLongitudeD="&LonD=";var kLongitudeM="&LonM=";var kLongitudeDir="&LonRG=";var kLocationName="&LocN=";var kElevation="&Elev=";var kLocMarkersMoonApollo="&LocMMA=";var kSurfaceGuides="&SG=";var kMonth="&MM=";var kDay="&DD=";var kYear="&YY=";var kHours="&HH=";var kMinutes="&Min=";var kAmPm="&AmPm=";var kFOV="&FOV=";var kPlanetLabels="&Lab=";var kStarLabels="&LabStar=";var kConstellations="&Cons=";var kHorizon="&Hor=";var kEcliptic="&Ecl=";var kShowDaylight="&SD=";var kPostalCode="&PC=";var kScenePixelWidth="&PW=";var kScenePixelHeight="&PH=";var kAltitude="&Alt=";var kAzimuth="&Az=";var kDST="&DST=";var kTimeZone="&TZ=";var kRequest="&Req=";var kPlatform="&Plat=";var kVersionString="&Vers=";var kTrack_Initialized=0;var kTrack_UnspecifiedError=1;var kTrack_ImageRequest=2;var kTrack_NearestCityRequest=3;var kTrack_ZipPostalRequest=4;var kTrack_tickerRSSRequest=5;var kTrack_tickerLinkClicked=6;var kTrack_openWebAppClicked=7;var kTrack_marketingLinkClicked=8;var kTrack_adDisplayed=9;var _hbEC=0;var _hbE=new Array;function _hbEvent(a,b)
{b=_hbE[_hbEC++]=new Object();b._N=a;b._C=0;return b;}
var hbx=_hbEvent("pv");hbx.vpc="HBX0200u";hbx.gn="ehg-space.hitbox.com";hbx.acct="DM5310155MZA92EN3";hbx.pn="PUT+PAGE+NAME+HERE";hbx.mlc="CONTENT+CATEGORY";hbx.pndef="title";hbx.ctdef="full";hbx.fv="";hbx.lt="none";hbx.dlf="n";hbx.dft="n";hbx.elf="n";hbx.seg="";hbx.fnl="";hbx.cmp="";hbx.cmpn="";hbx.dcmp="";hbx.dcmpn="";hbx.dcmpe="";hbx.dcmpre="";hbx.hra="";hbx.hqsr="";hbx.hqsp="";hbx.hlt="";hbx.hla="";hbx.gp="";hbx.gpn="";hbx.hcn="";hbx.hcv="";hbx.cp="null";hbx.cpd="";hbx.ci="";hbx.hc1="";hbx.hc2="";hbx.hc3="";hbx.hc4="";hbx.hrf="";hbx.pec="";function SNOptions()
{this.planetLabels=true;this.starLabels=true;this.constellations=true;this.horizon=true;this.ecliptic=false;this.daylight=true;this.markersMoonApollo=false;this.surfaceGuides=false;this.getParamsString=function()
{var optsParams="";optsParams+=kPlanetLabels+(this.planetLabels?kYES:kNO);optsParams+=kStarLabels+(this.starLabels?kYES:kNO);optsParams+=kConstellations+(this.constellations?kYES:kNO);optsParams+=kHorizon+(this.horizon?kYES:kNO);optsParams+=kEcliptic+(this.ecliptic?kYES:kNO);optsParams+=kShowDaylight+(this.daylight?kYES:kNO);optsParams+=kLocMarkersMoonApollo+(this.markersMoonApollo?kYES:kNO);optsParams+=kSurfaceGuides+(this.surfaceGuides?kYES:kNO);return optsParams;};}
function SNLocation(inLatitude,inLongitude,inTimezone,inIncludeTimezone,inLocName,inElevation,inUseDST)
{this.lat=inLatitude||kDefaultLatitude;this.lng=inLongitude||kDefaultLongitude;this.timezone=inTimezone||kDefaultTimezone;this.locName=inLocName||kDefaultLocationName;this.elevation=inElevation||kDefaultElevation;this.useDST=inUseDST||false;this.getParamsString=function(inIncludeTimezone)
{var locParams=kLatitude+this.lat;locParams+=kLongitude+this.lng;if(inIncludeTimezone===true)
{locParams+=kTimeZone+this.timezone;}
locParams+=kDST+(this.useDST?kYES:kNO);locParams+=kLocationName+this.locName;locParams+=kElevation+this.elevation;return locParams;};}
function SNGaze(inAltitude,inAzimuth,inFOV)
{this.alt=inAltitude||kDefaultAltitude;this.az=inAzimuth||kDefaultAzimuth;this.FOV=inFOV||kDefaultFOV;this.getParamsString=function()
{var gazeParams=kAltitude+this.alt;gazeParams+=kAzimuth+this.az;gazeParams+=kFOV+this.FOV;return gazeParams;};}
function SNSituation(inDateTime,inGaze,inSNLocation,inOptions,inUseTimezoneAndUTC)
{this.dateTime=inDateTime||new Date();this.location=inSNLocation||new SNLocation();this.options=inOptions||new SNOptions();this.gaze=inGaze||new SNGaze();this.useTimezoneAndUTC=inUseTimezoneAndUTC||false;this.getParamsString=function()
{var situParams="";situParams+=this.location.getParamsString(this.useTimezoneAndUTC);situParams+=this.gaze.getParamsString();situParams+=this.options.getParamsString();var useUTC=this.useTimezoneAndUTC;var date=this.dateTime;situParams+=kDay+(useUTC?date.getUTCDate():date.getDate());situParams+=kMonth+(useUTC?(date.getUTCMonth()+1):(date.getMonth()+1));situParams+=kYear+(useUTC?date.getUTCFullYear():date.getFullYear());var hours=(useUTC?date.getUTCHours():date.getHours());var ampm="1";if(hours>12)
{ampm="2";hours-=12;}
situParams+=kHours+hours;situParams+=kMinutes+(useUTC?date.getUTCMinutes():date.getMinutes());situParams+=kAmPm+ampm;return situParams;};}
function getRequestURL(inPlatform,inVersionString,inRequestType,inSituation,inImageWidth,inImageHeight,inZipPostal)
{var plat=inPlatform||kUnknownPlatform;var version=inVersionString||kUnknownVersionString;var type=inRequestType||kDefautlRequestType;var width=inImageWidth||kDefaultImageWidth;var height=inImageHeight||kDefaultImageHeight;var postal=inZipPostal||null;var situ=null;if(type!==kSearchPostalCodeRequest)
{situ=inSituation||new SNSituation();}
var url=kServerCGI;url+=kRequest+type;switch(type)
{case kGetNearestCityRequest:url+=situ.location.getParamsString(false);break;case kSearchPostalCodeRequest:url+=kPostalCode+postal;break;default:url+=kScenePixelWidth+width;url+=kScenePixelHeight+height;url+=situ.getParamsString();break;}
url+=kPlatform+plat;url+=kVersionString+version;return url;}
function getRedirectURL(inPlatform,inVersionString,inSituation)
{var plat=inPlatform||kUnknownPlatform;var version=inVersionString||kUnknownVersionString;var situ=inSituation||new SNSituation();var url=kRedirectPHP;var savedUseUTC=situ.useTimezoneAndUTC;situ.useTimezoneAndUTC=false;url+=situ.getParamsString();situ.useTimezoneAndUTC=savedUseUTC;url+=kPlatform+plat;url+=kVersionString+version;return url;}
function getWidgetRSSFeedURL(inCallback)
{if(XMLHttpRequest==undefined)
{return undefined;}
var widgetRSSFeedRequest=new XMLHttpRequest();widgetRSSFeedRequest.overrideMimeType('text/xml');widgetRSSFeedRequest.onreadystatechange=inCallback;widgetRSSFeedRequest.open('GET',kDynamicRSSFeedLocationFileURL,true);widgetRSSFeedRequest.send(null);return widgetRSSFeedRequest;}
function parseRSSXML(inResponseXML,inPlatform,inVersionString)
{var url=kDefaultWidgetRSSFeedURL;var xmldoc=inResponseXML;var platformArray=xmldoc.getElementsByTagName(kRSSLocationXMLPlatformStr);var bFoundNewVersion=false;var versionsArray;var typeConsts;var typeConst;var urls;var i,j;for(i=0;i<platformArray.length;i+=1)
{typeConsts=platformArray[i].getElementsByTagName(kRSSLocationXMLPlatConstStr);typeConst=typeConsts[0];if(typeConst.firstChild.data==inPlatform)
{versionsArray=platformArray[i].getElementsByTagName(kRSSLocationXMLPlatVersionStr);for(j=0;j<versionsArray.length;j+=1)
{if(versionsArray[j].getAttribute("value")==kDefaultVersionStr)
{if(!bFoundNewVersion)
{urls=versionsArray[j].getElementsByTagName(kRSSLocationXMLURLStr);url=urls[0].firstChild.data;}}
else if(versionsArray[j].getAttribute("value")>inVersionString)
{urls=versionsArray[j].getElementsByTagName(kRSSLocationXMLURLStr);url=urls[0].firstChild.data;bFoundNewVersion=true;}}}}
return url;}
function trackAction(inPlatform,inVersionString,inActionConst,inIsAnError,inOptionalString)
{var kUnknownPlatformFolder="unknown";var kWebApplicationFolder="snweb";var kDashboardWidgetFolder="dashboard";var kGoogleGadgetFolder="google";var kYahooWidgetFolder="yahoo";var kBlackBerryAppFolder="blackberry";var kVistaGadgetFolder="vista";var kMEWDashboardFolder="moonDashboard";var kMEWYahooFolder="moonYahoo";var kTrack_InitializedS="initialized";var kTrack_NearestCityRequestS="nearestCityRequest";var kTrack_ImageRequestS="imageRequest";var kTrack_ZipPostalRequestS="zipPostalRequest";var kTrack_tickerRSSRequestS="tickerRSSRequest";var kTrack_tickerLinkClickedS="tickerLink";var kTrack_openWebAppClickedS="openWebApp";var kTrack_marketingLinkClickedS="marketingLink";var kTrack_adDisplayedS="adDisplayed";var kTrack_ERRORS="ERROR";var contentCategoryName="/applets/";var kSkyChartAppletName="skyChart/";var kMoonExplorerAppletName="moonExplorer/";switch(inPlatform)
{case kDashboardWidget:contentCategoryName+=kSkyChartAppletName;contentCategoryName+=kDashboardWidgetFolder;break;case kGoogleGadget:contentCategoryName+=kSkyChartAppletName;contentCategoryName+=kGoogleGadgetFolder;break;case kYahooWidget:contentCategoryName+=kSkyChartAppletName;contentCategoryName+=kYahooWidgetFolder;break;case kWebApplication:contentCategoryName+=kSkyChartAppletName;contentCategoryName+=kWebApplicationFolder;break;case kBlackBerryApp:contentCategoryName+=kSkyChartAppletName;contentCategoryName+=kBlackBerryAppFolder;break;case kVistaGadget:contentCategoryName+=kSkyChartAppletName;contentCategoryName+=kVistaGadgetFolder;break;case kMEW_Dashboard:contentCategoryName+=kMoonExplorerAppletName;contentCategoryName+=kMEWDashboardFolder;break;case kMEW_Yahoo:contentCategoryName+=kMoonExplorerAppletName;contentCategoryName+=kMEWYahooFolder;break;case kUnknownPlatform:contentCategoryName+=kUnknownPlatformFolder;break;default:contentCategoryName+=kUnknownPlatformFolder;break;}
contentCategoryName+="/";if(inVersionString&&inVersionString.length>0)
{contentCategoryName+=inVersionString;}
else
{contentCategoryName+="unknownVersion";}
var pageName="";switch(inActionConst)
{case kTrack_Initialized:pageName=kTrack_InitializedS;break;case kTrack_ImageRequest:pageName=kTrack_ImageRequestS;break;case kTrack_NearestCityRequest:pageName=kTrack_NearestCityRequestS;break;case kTrack_ZipPostalRequest:pageName=kTrack_ZipPostalRequestS;break;case kTrack_tickerRSSRequest:pageName=kTrack_tickerRSSRequestS;break;case kTrack_openWebAppClicked:pageName=kTrack_openWebAppClickedS;break;case kTrack_UnspecifiedError:pageName=kTrack_ERRORS;break;case kTrack_adDisplayed:pageName=kTrack_adDisplayedS;break;case kTrack_tickerLinkClicked:if(inOptionalString&&inOptionalString.length>0)
{if(_hbSet&&_hbSend&&_hbLink)
{_hbSet('vcon',contentCategoryName);_hbSet('n',kTrack_tickerLinkClickedS);_hbSend();_hbLink(inOptionalString);}
return;}
break;case kTrack_marketingLinkClicked:if(inOptionalString&&inOptionalString.length>0)
{if(_hbSet&&_hbSend&&_hbLink)
{_hbSet('vcon',contentCategoryName);_hbSet('n',kTrack_marketingLinkClickedS);_hbSend();_hbLink(inOptionalString);}
return;}
break;default:pageName=kTrack_ERRORS;break;}
if(inIsAnError&&(inIsAnError===true))
{contentCategoryName+="/";contentCategoryName+=kTrack_ERRORS;contentCategoryName+="/";contentCategoryName+=pageName;if(inOptionalString&&inOptionalString.length>0)
{pageName=inOptionalString;}}
if(_hbPageView)
{_hbPageView(pageName,contentCategoryName);}}
function currentlyExperiencingDST(inSituation)
{var theLat=inSituation.location.lat;var theLong=inSituation.location.lng;var theDate=inSituation.dateTime;var dstStart;var dstEnd;var dayOfWeek;var daysToAdd;if((theLat>19.7)&&(theLong<-25))
{var marchNA=new Date(theDate.getFullYear(),2,1);dayOfWeek=marchNA.getDay();daysToAdd=(dayOfWeek==0)?7:14-dayOfWeek;dstStart=marchNA.setDate(daysToAdd+1);var novemberNA=new Date(theDate.getFullYear(),10,1);dayOfWeek=novemberNA.getDay();daysToAdd=(dayOfWeek==0)?0:7-dayOfWeek;dstEnd=novemberNA.setDate(daysToAdd+1);if((theDate>dstStart)&&(theDate<dstEnd))
return true;else
return false;}
if(((theLat>36)&&(theLong>-25)&&(theLong<46))||((theLat>54)&&(theLong>46)))
{var marchER=new Date(theDate.getFullYear(),2,31);while(marchER.getDay()!==0)
{marchER.setDate(marchER.getDate()-1);}
dstStart=marchER;var octoberER=new Date(theDate.getFullYear(),9,30);while(octoberER.getDay()!==0)
{octoberER.setDate(octoberER.getDate()-1);}
dstEnd=octoberER;if((theDate>dstStart)&&(theDate<dstEnd))
return true;else
return false;}
if((theLat>-44)&&(theLat<-29)&&(theLong>129)&&(theLong<155))
{var octoberAU=new Date(theDate.getFullYear(),9,30);while(octoberAU.getDay()!==0)
{octoberAU.setDate(octoberAU.getDate()-1);}
dstStart=octoberAU;var marchAU=new Date(theDate.getFullYear(),2,31);while(marchAU.getDay()!==0)
{marchAU.setDate(marchAU.getDate()-1);}
dstEnd=marchAU;if((theDate>dstStart)||(theDate<dstEnd))
return true;else
return false;}
if((theLat<-15)&&(theLat>-26)&&(theLong>-53)&&(theLong<-38))
{var octoberBR=new Date(theDate.getFullYear(),9,1);dayOfWeek=octoberBR.getDay();daysToAdd=(dayOfWeek==0)?7:7-dayOfWeek;dstStart=octoberBR.setDate(daysToAdd+1);var februaryBR=new Date(theDate.getFullYear(),1,11);dayOfWeek=februaryBR.getDay();daysToAdd=(dayOfWeek==0)?7:7-dayOfWeek;dstEnd=februaryBR.setDate(daysToAdd+11);if((theDate>dstStart)||(theDate<dstEnd))
return true;else
return false;}
return false;}