/****************************************************************************
*             OrbTrak: A satellite orbit visualization library              *
*                      Copyright Andrew T. West, 2008                       *
*                         Last update: 07-Jun-2008                          *
*****************************************************************************
*                                                                           *
* This program is free software; you can redistribute it and/or modify it   *
* under the terms of the GNU General Public License as published by the     *
* Free Software Foundation; either version 2 of the License or any later    *
* version.                                                                  *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
* General Public License for more details.                                  *
*                                                                           *
*****************************************************************************/

var Orb = 
	{
		satelliteMarkers: new Array(),

		startTracking: function(map, homeLat, homeLng)
		{
			Orb.map = map;
			Orb.crossBrowserSetStyle(map, "background-image: url(orbimages/world.jpg); overflow: hidden;", true);

			var frag = document.createDocumentFragment();
			var div = document.createElement("div");
			div.id = "home";
			Orb.crossBrowserSetStyle(div, "position:relative; width: 24px; height: 24px; background-image: url(orbimages/home.gif);", false);
			frag.appendChild(div);
			Orb.map.appendChild(frag);
			Orb.home = document.getElementById("home");

			PLib.InitializeData();
			Orb.setHomeCoordinates(homeLat, homeLng);
			Orb.createSatelliteMarkers();
			Orb.updateSatellites();
		},

		setHomeCoordinates: function(homeLat, homeLng)
		{
			PLib.configureGroundStation(homeLat, homeLng);

			Orb.home.style.top = ((-homeLat + 90) * 1.5 - 12.0) + "px";
			Orb.home.style.left =  ((parseInt(homeLng) + 180) * 1.5 - 12.0) + "px";
		},

		crossBrowserSetStyle: function(element, css, append)
		{
			var obj, attributeName;
			var useStyleObject = element.style.setAttribute;

			obj = useStyleObject ? element.style : element;
			attributeName = useStyleObject ? "cssText" : "style";

			if (append)
				css += obj.getAttribute(attributeName);

			obj.setAttribute(attributeName, css);
		},

		createOneMarker: function(txt)
		{
			var frag = document.createDocumentFragment();
			var markerCount = Orb.satelliteMarkers.length;

			var div = document.createElement("div");
			div.id = "satelliteMarker" + markerCount;
			if (txt != "ARISS")
			    if (txt=="NA-15"||txt=="NA-17"||txt=="NA-18"||txt=="NA-19"){Orb.crossBrowserSetStyle(div, "position:absolute; width: 32px; height: 24px; background-image: url(orbimages/satnoa.gif);", false);}
			    else 
			        if (txt=="LO-19"||txt=="RS-22"||txt=="YUBIL"||txt=="TI-69")
                         {Orb.crossBrowserSetStyle(div, "position:absolute; width: 32px; height: 24px; background-image: url(orbimages/saticon3.gif);", false);}      
			        else
			    {Orb.crossBrowserSetStyle(div, "position:absolute; width: 32px; height: 24px; background-image: url(orbimages/saticon2.gif);", false);}
			    
			    
			else
			    {txt="ISS";Orb.crossBrowserSetStyle(div, "position:absolute; width: 32px; height: 24px; background-image: url(orbimages/iss.gif);", false);}
			var innerDiv = document.createElement("div");
			if (txt != "ISS")
			    {Orb.crossBrowserSetStyle(innerDiv, "position:absolute; left: 3px; top: 6px;font-size:10px;font-family:Tahoma,Arial;font-weight:normal;");}
			else
			    {Orb.crossBrowserSetStyle(innerDiv, "position:absolute; left: 7px; top: 6px;font-size:10px;font-family:Tahoma,Arial;font-weight:normal;");}
			var txt = document.createTextNode(txt);

			innerDiv.appendChild(txt);
			div.appendChild(innerDiv);
			frag.appendChild(div);
			Orb.map.appendChild(frag);

			Orb.satelliteMarkers[markerCount] = document.getElementById(div.id)
		},

		createSatelliteMarkers: function()
		{
			for (var i = 1; i <= PLib.sat.length; i++)
				Orb.createOneMarker(PLib.tleData[i-1][0]);
		},

		updateSatellites: function()
		{
			var satInfo;
			document.getElementById('hora').innerHTML=" " + PLib.formatDateOnly(new Date())+" "+PLib.formatTimeWithSeconds(new Date())+ " at Grid-Locator: "+document.wwl.loc.value;

			for (var i = 0; i < PLib.sat.length; i++)
			{
				satInfo = PLib.QuickFind(PLib.sat[i].name);

				Orb.satelliteMarkers[i].style.top = ((-satInfo.latitude + 90) * 1.5 - 12.0) + "px";
				Orb.satelliteMarkers[i].style.left =  ((satInfo.longitude + 180) * 1.5 - 12.0) + "px";
			}

			parar = setTimeout("Orb.updateSatellites()", 5000);
		},

		createCell: function(tr, className, txt)
		{
			var td = document.createElement("td");
			td.className = className;
			txt = document.createTextNode(txt);
			td.appendChild(txt);
			tr.appendChild(td);
		},

		createHeaderColumn: function(tr, txt)
		{
			var th = document.createElement("th");
			th.className = "orb-header";
			txt = document.createTextNode(txt);
			th.appendChild(txt);
			tr.appendChild(th);
		},

		generateTable: function(divTable)
		{
			var tr, visibilityText, detailClassName;
			var frag = document.createDocumentFragment();
			var satInfoColl = PLib.getTodaysPasses();
		//	alert(satInfoColl[0].dateTimeStart);
			// Sort satInfoColl table by time satInfoColl[i].dateTimeStart
                function SortPassesByDate() {
                    for (m=0;m<satInfoColl.length-1;m++)
                        {
                            for (n=m;n<satInfoColl.length;n++)
                            {
                                fecha1=satInfoColl[m].dateTimeStart;
                                fecha2=satInfoColl[n].dateTimeStart;
                                    if (fecha1>fecha2) {
                                        savefecha=satInfoColl[m];
                                        satInfoColl[m]=satInfoColl[n];
                                        satInfoColl[n]=savefecha;
                                                        }
                            }
                         }   
                    }
                SortPassesByDate();
			// End Sort
			while (divTable.childNodes.length > 0)
			{
			    divTable.removeChild(divTable.firstChild);
			}
			
			var tbl = document.createElement("table");
			Orb.crossBrowserSetStyle(tbl, "border-collapse: collapse; margin-left: auto; margin-right: auto;text-align:center;", false);
			
			var thead = document.createElement("thead");
			tr = document.createElement("tr");
			
			//Orb.createHeaderColumn(tr, '#');
			Orb.createHeaderColumn(tr, 'Satellite');
			Orb.createHeaderColumn(tr, 'Orbit #');
			Orb.createHeaderColumn(tr, '* Date *');
			Orb.createHeaderColumn(tr, '* Local Time *');
			Orb.createHeaderColumn(tr, 'Elev.');
			Orb.createHeaderColumn(tr, 'Azimuths');
			Orb.createHeaderColumn(tr, 'Distances (km)');
			Orb.createHeaderColumn(tr, '* Visibility *');
			
			thead.appendChild(tr);
			tbl.appendChild(thead);
			
			var tbody = document.createElement("tbody");
			
			for (var i = 0; i < satInfoColl.length; i++)
			{
				tr = document.createElement("tr");
				
				detailClassName = satInfoColl[i].visibility == "+" ? "orb-detailVisible" : "orb-detail";
				detailClassNameright = satInfoColl[i].visibility == "+" ? "orb-detailVisibleright" : "orb-detailright";
				detailClassNamecenter = satInfoColl[i].visibility == "+" ? "orb-detailVisiblecenter" : "orb-detailcenter";
				
				//Orb.createCell(tr, detailClassName, satInfoColl[i].number);
				Orb.createCell(tr, detailClassNamecenter, satInfoColl[i].name);
				if (satInfoColl[i].name=="LO-19"||satInfoColl[i].name=="AO-16")
				    {Orb.createCell(tr, detailClassName, satInfoColl[i].orbitNumber+100000);}
				else {Orb.createCell(tr, detailClassName, satInfoColl[i].orbitNumber);}
				Orb.createCell(tr, detailClassName, PLib.formatDateOnly(satInfoColl[i].dateTimeStart));
				Orb.createCell(tr, detailClassNamecenter, PLib.formatTimeOnly(satInfoColl[i].dateTimeStart) + " - " + PLib.formatTimeOnly(satInfoColl[i].dateTimeEnd));
				Orb.createCell(tr, detailClassNameright, satInfoColl[i].peakElevation + "\u00B0");
				Orb.createCell(tr, detailClassNamecenter, satInfoColl[i].riseAzimuth + ", " + satInfoColl[i].peakAzimuth + ", " + satInfoColl[i].decayAzimuth);
				Orb.createCell(tr, detailClassNamecenter, satInfoColl[i].riseRange + ", " + satInfoColl[i].peakRange + ", " + satInfoColl[i].decayRange);
				
				switch(satInfoColl[i].visibility)
				{
					case "+":
						visibilityText = 'Visible';
						break;    
					case "*":
						visibilityText = 'Not Visible';
						break;
					default:
						visibilityText = 'Eclipsed';
				}
				
				Orb.createCell(tr, detailClassNamecenter, visibilityText);
				
				tbody.appendChild(tr);
			}
			
			tbl.appendChild(tbody);
			frag.appendChild(tbl);
			divTable.appendChild(frag);
		}
	}

