
// Author: Edward R. Myers ed@myersfamily.com

	var oVEMap = null;
	var CurrentCenter = null;
	var Day = 0; // Used in animating planets
 
	var SunSize = 1391900; // Diameter in KM actual size

	var oEarth = null;
	var oMercury = null;
	var oVenus = null;
	var oMars = null;
	var oVesta = null;
	var oCeres = null;
	var oJupiter = null;
	var oSaturn = null;
	var oUranus = null;
	var oNeptune = null;
	var oPluto = null;
	var oVenus = null; 

	    
function Initialize(){
	Resize();

	// Create Planet objects: Title, Size, Distance from Sun, Eccentricity factor, Days per year
	oMercury = 	new Planet ("Mercury", 	4866, 	57950000,	0.2056, 	87.97);
	oVenus = 	new Planet ("Venus", 	12106, 	108110000,	0.0068, 	224.70);
	oEarth = 	new Planet ("Earth", 	12742, 	149570000,	0.016710219, 	365.256);
	oMars = 	new Planet ("Mars", 	6760, 	227840000,	0.093315, 	686.98);
	oVesta =	new Planet ("Vesta", 	532, 	353270000,	0.08917, 	1325.15);
	oCeres = 	new Planet ("Ceres", 	975, 	414628873,	0.07976017, 	1679.82);
	oJupiter = 	new Planet ("Jupiter", 	139516,	778140000,	0.048775, 	4331.57);
	oSaturn = 	new Planet ("Saturn", 	116438,	1427000000,	0.055723219, 	10832.33);
	oUranus = 	new Planet ("Uranus", 	46940,	2870300000,	0.04440558, 	30799.10);
	oNeptune = 	new Planet ("Neptune", 	45432,	4499900000,	0.011214269, 	60190);
	oPluto = 	new Planet ("Pluto", 	2274,	5913000000,	0.24880766, 	90613.31);
	oVenus = 	new Planet ("Venus", 	12106,	108110000,	0.0068, 	224.70);

	oVEMap = new VEMap('MapPanel');
	oVEMap.SetDashboardSize(VEDashboardSize.Tiny);
	oVEMap.LoadMap(null,13,"s",false,1,true);
	oVEMap.HideMiniMap()

	DrawOnMap();
}
	
function DrawOnMap(){

	$("tdMessage").innerHTML = "Computing..."

	var Addr = $("SunCenterAddr").value;
	var Type = null;
	var index = 0;
	var NumPts = 20;
	oVEMap.Clear(); 
	oVEMap.Find("", Addr, Type, null, index, NumPts, true, true, true, null, FindCenterCallBack);
	// Goto FindCenterCallBack
}


function FindCenterCallBack(oVEShapeLayer, oVEFindResult, oVEPlace, bHasMore, e)
{
	// Move map so entered address is at the center
	if(null==oVEPlace) return;
	if (0==oVEPlace.length) return;
			
	var Lat =  oVEPlace[0].LatLong.Latitude;
	var Lon =  oVEPlace[0].LatLong.Longitude;
	CurrentCenter = new VELatLong(Lat,Lon);
	var ScaleFactor = (.001* parseFloat($("SunSize").value)) / SunSize;
	
 		
	var shape = new VEShape(VEShapeType.Pushpin, CurrentCenter );
	var Addr=oVEPlace[0].Name; 
	var URL = "http://en.wikipedia.org/wiki/sun"
	shape.SetTitle("<div><B><a href='" + URL + "' target='_new'>Sun</a></B> " + Math.round(1000000*ScaleFactor*SunSize) + " mm at " + Addr + "</div>");
	shape.SetDescription("<div><a href='" + URL + "' target='_new'><img src='Sun.jpg' width='100' /></a></div>")
	shape.SetCustomIcon("<img src='Ball.gif' width='32' height='32' style='position:relative;top:-3px;left:-3px;' />");
	oVEMap.AddShape(shape);
	
	oVEMap.SetCenter(CurrentCenter );

	var isShowEcc = $("ShowEcc").checked;
	var CircleIncr = parseInt($("CompSpeed").value)
	var LightSpeed = ScaleFactor * 299792458 * 3.600; // m/sec  -- > km/hr

	var Color1 = new VEColor(255,0,255,.5) ;
	var Color2 = new VEColor(255,0,153,.5) ;
	var Color3 = new VEColor(255,0,0,.5) ;
	var Color4 = new VEColor(255,153,0,.5) ;
	var Color5 = new VEColor(255,255,0,.5) ;
	var Color6 = new VEColor(0,255,0,.5) ;
	var Color7 = new VEColor(53,53,255,.5) ;
	var Color8 = new VEColor(0,0,153,.5) ;
	var Color9 = new VEColor(102,53,153,.5) ;
	var Color10 = new VEColor(153,102,255,.5) ;
	var Color11 = new VEColor(255,0,255,.5) ;


	oMercury.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color1);
	oVenus.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color2);
	oEarth.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color3);
	oMars.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color4);
	oVesta.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color5);
	oCeres.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color6);
	oJupiter.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color7);
	oSaturn.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color8);
	oUranus.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color9);
	oNeptune.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color10);
	oPluto.Show (	CurrentCenter, ScaleFactor, isShowEcc, CircleIncr, Color11);

	$("tdMessage").innerHTML = "&nbsp;light&nbsp;speed=&nbsp;" + (Math.round(LightSpeed*100.0)/100) + "&nbsp;kph"
	
	setTimeout("Animate()", 3000);	
	
}


function Animate()
{
	// move planets around sun
	var Incr = parseInt($("Speed").value);
	if (Incr!=0) 
	{
		Day = Day + Incr
		Day = Day % oPluto.DaysPerYear; // keep count from getting too large
		$("tdDay").innerText = Math.round(Day);

		oMercury.Move(Day);
		oVenus.Move(Day);
		oEarth.Move(Day);
		oMars.Move(Day);
		oVesta.Move(Day);
		oCeres.Move(Day);
		oJupiter.Move(Day);
		oSaturn.Move(Day);
		oUranus.Move(Day);
		oNeptune.Move(Day);
		oPluto.Move(Day);
	} else Day=0;
	setTimeout("Animate()",100);
}

function Resize(){
	// If the user resizes the window this will resize all the layers
	
	var Height = GetScreenHeight()-50;
	var Width =  GetScreenWidth();
	var oDiv = $("MapPanel");

	oDiv.style.width = Width + "px"; 
	oDiv.style.height = Height + "px";
	onresize = Resize;
}

function Planet(title, size, distance, ecc, daysperyear)
{
	// Object representing a single planet's characteristics and methods
	this.Size = size;
	this.Distance = distance;
	this.Ecc = ecc;
	this.DaysPerYear = daysperyear;
	this.ID = 0;
	this.Title = title;
	this.CenterPt = null;
	this.CircleIncr = 6;
	this.ScaleFactor = 1;

	var EarthRadius = 6371; // earth's mean radius KM (3959 mi)


	this.Move = function (Day)
	{
		// move a planet to it's orbital position after "Day" days
		var Angle = 2*Math.PI*(Day/this.DaysPerYear)
		var lat = (this.CenterPt.Latitude * Math.PI) / 180; //rad
		var lon = (this.CenterPt.Longitude * Math.PI) / 180; //rad
		var d = (this.Distance*this.ScaleFactor)/EarthRadius;  // d = angular distance covered on earth's surface
           
		var pt = oVEMap.GetCenter();
		pt.Latitude = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(Angle));
  	pt.Longitude = ((lon + Math.atan2(Math.sin(Angle)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(pt.Latitude))) * 180) / Math.PI;
		pt.Latitude = (pt.Latitude * 180) / Math.PI;
		var Shape = oVEMap.GetShapeByID(this.ID)
		if(Shape)Shape.SetPoints(pt)
	}

	this.Show = function (CntrPt, Scale, isShowEcc, CircleIncr, Color)
	{
		this.CenterPt = CntrPt;
		this.CircleIncr = CircleIncr;
		this.ScaleFactor =Scale;
	
		var lat = (CntrPt.Latitude * Math.PI) / 180; //rad
		var lon = (CntrPt.Longitude * Math.PI) / 180; //rad
		var d = (this.Distance*this.ScaleFactor)/EarthRadius;  // d = angular distance covered on earth's surface
		var SizeTxt = Math.round(1000000.0 * this.ScaleFactor * this.Size) + " mm";
		var pt = new VELatLong(0,0)            
		brng = 0*2*Math.PI; //rad
            
		pt.Latitude = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
		pt.Longitude = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(pt.Latitude))) * 180) / Math.PI;
 		pt.Latitude = (pt.Latitude * 180) / Math.PI;
    
		var Newshape = new VEShape(VEShapeType.Pushpin, pt)
		var URL = "http://en.wikipedia.org/wiki/" + this.Title
		Newshape.SetTitle("<div><B><a href='" + URL + "' target='_new'>" + this.Title + "</a></B>&nbsp;" + SizeTxt + "</div>");
		Newshape.SetDescription("<div><a href='" + URL + "' target='_new'><img src='" + this.Title + ".jpg' width='100' /></a></div>")
		Newshape.SetCustomIcon("<img src='ball.gif' width='16' height='16' style='position:relative;top:6px;left:6px;' />");
		oVEMap.AddShape(Newshape);
		this.ID = Newshape.GetID();
	
		var OrbitRadius = this.ScaleFactor*this.Distance // Radius in KM
		this.DrawCircle(OrbitRadius, Color,3);	
		if (!isShowEcc) return;
		this.DrawCircle(OrbitRadius*(1.0+this.Ecc),Color,1); 
		this.DrawCircle(OrbitRadius*(1.0-this.Ecc),Color,1); 
	}

	this.DrawCircle = function(Rad, Color, width)
	{
		// Simulate drawing a circle on the map by drawing a polygon
		var lat = (this.CenterPt.Latitude * Math.PI) / 180; //rad
		var lon = (this.CenterPt.Longitude * Math.PI) / 180; //rad
		var d = parseFloat(Rad)/EarthRadius;  // d = angular distance covered on earth's surface
		var pts = new Array();

    		for (x = 0; x <= 360; x+=this.CircleIncr) 
    		{ 
        		var p2 = new VELatLong(0,0)            
        		brng = x * Math.PI / 180; //rad
        		p2.Latitude = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
        		p2.Longitude = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(p2.Latitude))) * 180) / Math.PI;
        		p2.Latitude = (p2.Latitude * 180) / Math.PI;
        		pts.push(p2);
   	 	}
		var Shape = new VEShape(VEShapeType.Polyline, pts);
		Shape.SetLineColor(Color);
		Shape.SetLineWidth(width);
		Shape.HideIcon();
		oVEMap.AddShape(Shape);	
	}

}

function GetScreenHeight()
{
  if (window.innerHeight) return parseInt(window.innerHeight,10); // Netscape
  if (document.documentElement && document.documentElement.clientHeight) return parseInt(document.documentElement.clientHeight,10);
  if (document.body) return parseInt(document.body.clientHeight,10);
  return 500;
}

function GetScreenWidth()
{
  if (window.innerWidth) return parseInt(window.innerWidth,10); // Netscape
  if (document.documentElement && document.documentElement.clientWidth) return parseInt(document.documentElement.clientWidth,10);
  if (document.body) return parseInt(document.body.clientWidth,10)-5;
  return 500;
}

function $(id)
{
	return document.getElementById(id);
}