
// styles

var styleA;
var styleB;
var styleC;
var styleD;
var stylemap = [{ground : "styleA" , alt : "styleC"},
                {ground : "styleB" , alt : "styleD"}]

function satStyle(name,scale,col)
{
  var styleMap = ge.createStyleMap(name);
  styleMap.setNormalStyle(singleSatStyle(scale,0,col));
  styleMap.setHighlightStyle(singleSatStyle(scale,1,col));
  return styleMap;
}

function singleSatStyle(scale,label,color)
{
  var style = ge.createStyle('');
  var iconStyle = style.getIconStyle();
  var icon = ge.createIcon('');
  var lineStyle = style.getLineStyle();
  var labelStyle = style.getLabelStyle();

  var smn =Math.ceil(Math.random()*3);

  icon.setHref('http://evernowa.com/earth/smile/'+smn+'-64x64.png');

  //alert('http://evernowa.com/earth/smile/'+smn+'-64x64.png');
  iconStyle.setScale(scale);
  iconStyle.getColor().set(color);
  labelStyle.setScale(label);
  iconStyle.setIcon(icon);
  lineStyle.getColor().set('5f40f0c0');
  return style;
}


function createStyles()
{
  styleA = satStyle('A',1.3,'ffffffff');
  styleB = satStyle('B',1.3,'ffd0d0ff');
  styleC = satStyle('C',1.3,'ffffffff');
  styleD = satStyle('D',1.3,'ffd0d0ff');
}



//


function Satellite(num,Line){  // new satellite object
  this.Icon = new PM(num);
  this.Vis = false;
  this.exVis = false;

  this.mylat=0;
  this.mylon=0;
  this.myalt=0;
  this.myamp=2000000;
  this.myValue=0;
  this.mycount=0;

  this.velx=(1-Math.random()*2)*0.001;
  this.vely=(1-Math.random()*2)*0.001;

  this.velx=0;
  this.vely=0;


  this.speed=Math.random()*10000;

  this.initData(Line);

}

Satellite.prototype.initData = function(Line) {    // initialize all the parameters associated with this satellite

  this.name = 'no name';
  this.parseTLE(Line);

  this.ltlnal1 = [0,0,0,0,0,0,0,0,0];
  this.pos = [0,0,0,0];
  this.lla = [0,0,0,0];
  this.vel = [0,0,0,0];

  this.eqsq   = 0;
  this.siniq  = 0;
  this.cosiq  = 0;
  this.rteqsq = 0;
  this.ao     = 0;
  this.cosq2  = 0;
  this.sinomo = 0;
  this.cosomo = 0;
  this.bsq    = 0;
  this.xlldot = 0;
  this.omgdt  = 0;
  this.xnodot = 0;
  this.xnodp  = 0;
  this.xll    = 0;
  this.omgasm = 0;
  this.xnodes = 0;
  this.xn     = 0;
  this.t      = 0;
  this.em    = 0;
  this.xinc   = 0;


  this.style = 0;
  if ((Math.random()*2)>1) {this.style = 1}

  this.Icon.setName(this.name);
  this.altMode(true);
  this.Icon.placemark.setVisibility(true);         // Performance hack
}


Satellite.prototype.altMode = function(mode)
{
  if(mode)
  {
    this.Icon.point.setAltitudeMode(ge.ALTITUDE_ABSOLUTE);
    this.Icon.setStyle(eval(stylemap[this.style].alt));
  }
  else
  {
    this.Icon.point.setAltitudeMode(ge.ALTITUDE_CLAMP_TO_GROUND);
    this.Icon.setStyle(eval(stylemap[this.style].ground));
  }
}


Satellite.prototype.getName = function() {return this.name;}


Satellite.prototype.FetchAttr = function(time) {
  var step = (time-this.ltlnal1[4])/(this.ltlnal1[7]);
  var latlon = destination(this.ltlnal1[0],this.ltlnal1[1],this.ltlnal1[5]*step,this.ltlnal1[6]);
  var alt = this.ltlnal1[2]+step*(this.ltlnal1[8]);
  return [latlon[0],latlon[1],alt,this.vel[3]];
}

Satellite.prototype.Move = function(time) {
  var step = (time-this.ltlnal1[4])/(this.ltlnal1[7]);
  var latlon = destinationr(this.ltlnal1[0],this.ltlnal1[1],this.ltlnal1[5]*step,this.ltlnal1[6]);
  var alt = this.ltlnal1[2]+step*(this.ltlnal1[8]);
//  this.Icon.setLoc((latlon[0]*57.2957795),fixLon(latlon[1]*57.2957795),alt.toPrecision(4));
this.mylat+=this.vely;
this.mylon+=this.velx;
var r=Math.sqrt(this.mylat*this.mylat + this.mylon*this.mylon);
//this.myalt+=this.speed;
this.myalt=(this.myValue/100)*this.myamp*(1+Math.sin(r/10+time/300));
//this.myalt+=this.speed*(Math.random()*2-1);
//debug=this.myalt;

	if(Math.random()*100>99){
//this.velx+=(1-Math.random()*2)*0.001;
//this.vely+=(1-Math.random()*2)*0.001;
//this.velx*=-1;
//this.vely*=-1;
	}

  this.Icon.setLoc(this.mylat,this.mylon,this.myalt);
}

function PM(num) {      // Create Placemark
  this.placemark = ge.createPlacemark(String(num));
//  this.placemark.setVisibility(false);         // Performance hack
  this.placemark.setVisibility(true);         // Performance hack
  this.point = ge.createPoint('');
  this.placemark.setGeometry(this.point);
  this.point.setExtrude(true);
  g_features.appendChild(this.placemark);

}

PM.prototype.setStyle = function(style) {this.placemark.setStyleSelector(style)}

PM.prototype.setName = function(name) {this.placemark.setName(name); this.name = name;}

PM.prototype.setLoc = function(lat,lon,alt) {this.point.setLatLngAlt(lat,lon,alt)}


Satellite.prototype.UpdatePositionData = function(nextTime, jdJulian, refresh)
{
  this.calcSatPosition(jdJulian);
  this.calcLatLonAlt(jdJulian);

  if (!refresh){this.ltlnal1 = this.ltlnal2}
  this.ltlnal2 = [this.lla[0], this.lla[1], this.lla[2], this.vel[3], nextTime];
  this.ltlnal1[5] = distance(this.ltlnal1[0],this.ltlnal1[1],this.ltlnal2[0],this.ltlnal2[1]);
  this.ltlnal1[6] = bearing(this.ltlnal1[0],this.ltlnal1[1],this.ltlnal2[0],this.ltlnal2[1]);
  this.ltlnal1[7] = this.ltlnal2[4]-this.ltlnal1[4];
  this.ltlnal1[8] = this.ltlnal2[2]-this.ltlnal1[2];
}


Satellite.prototype.getPosition = function(jdJulian)
{
  this.calcSatPosition(jdJulian);
  this.calcLatLonAlt(jdJulian);
  return [this.lla[0], this.lla[1], this.lla[2]*1000];
}


////////////////////////////////////////////////////////////////////////////////



Satellite.prototype.calcSatPosition = function(jdTime)
{
  var tSince = (jdTime - this.julianEpoch) * 1440;

  if (this.iDeep == 0){this.SGP4(tSince)}	// near earth
  else{this.SDP4(tSince)}	                // deep space

  this.pos[0] *= u_eqradius;      // km
  this.pos[1] *= u_eqradius;      // km
  this.pos[2] *= u_eqradius;      // km
  this.pos[3] = Math.sqrt(this.pos[0]*this.pos[0]+this.pos[1]*this.pos[1]+this.pos[2]*this.pos[2]);
  this.vel[0] *= u_eqradius/60;	// km/s
  this.vel[1] *= u_eqradius/60;	// km/s
  this.vel[2] *= u_eqradius/60;	// km/s
  this.vel[3] = Math.sqrt(this.vel[0]*this.vel[0]+this.vel[1]*this.vel[1]+this.vel[2]*this.vel[2]);
}

Satellite.prototype.calcLatLonAlt = function(jdTime)
{
  // Reference:  The 1992 Astronomical Almanac, page K12.
  var lat,lon,alt;
  var theta,r,e2,phi,c;

  theta = Math.atan2(this.pos[1],this.pos[0]);
  lon = mod2pi(theta-ThetaG(jdTime));

  r = Math.sqrt(this.pos[0]*this.pos[0] + this.pos[1]*this.pos[1]);
  e2 = u_f*(2 - u_f);
  lat = Math.atan2(this.pos[2],r);
  do {
    phi = lat;
    c = 1.0/Math.sqrt(1 - e2*Math.sin(phi)*Math.sin(phi));
    lat = Math.atan2( this.pos[2] + u_eqradius*c*e2*Math.sin(phi),r);
  }	while (Math.abs(lat - phi) > 1E-10);   // accuracy of iteration
  alt = r/Math.cos(lat) - u_eqradius*c;

  this.lla[0] = lat;   // radians
  this.lla[1] = lon;   // radians
  this.lla[2] = alt; 	// kilometers
  //this.lla[3] = theta; // radians

  if(this.lla[1] >u_pi){this.lla[1]-=u_2pi}
}


Satellite.prototype.parseTLE = function(line)
{
//IRIDIUM 33
//1 24946U 97051C   10167.26487467  .00000070  00000-0  18698-4 0  1788
//2 24946  86.3866 276.2805 0006139 300.0798  59.9801 14.32525470667712
  line = line.split(';');
  this.satelliteNumber = line[0];
  this.name = line[0];

  //this.mylat=parseFloat(line2.substring(8,16));
  //this.mylon=parseFloat(line2.substring(17,25));

  this.mylat=parseFloat(line[1]);
  this.mylon=parseFloat(line[2]);
  this.myValue=parseFloat(line[3]);
  this.myalt=parseFloat(line[3])*1000;
  this.mycount++;
//  this.myalt=line1.substring(2,7);

/*  var epYear = line1.substring(18,20)*1;
  var epDay = parseFloat(line1.substring(20,32));
  var temp = parseInt(line1.substring(59,61));
  this.radiationCoefficient = parseFloat(line1.substring(53,59))*Math.pow(10,-5+temp);*/
  /*
  this.inclination = deg2Rad(parseFloat(line2.substring(8,16)));
  this.rightAscending = deg2Rad(parseFloat(line2.substring(17,25)));
  this.eccentricity = parseFloat(line2.substring(26,33))*1e-7;
  this.peregee = deg2Rad(parseFloat(line2.substring(34,42)));
  this.meanAnomaly = deg2Rad(parseFloat(line2.substring(43,51)));
  this.meanMotion = parseFloat(line2.substring(52,63))*u_2pi/1440;

  if (epYear < 57){epYear+=2000}
  else {epYear+=1900}
  this.julianEpoch = julianDateOfYear(epYear)+epDay;
  */
  // Deep Space ?

/*  var a1,a0,del1,del0;
  a1 = Math.pow (u_xke/this.meanMotion, (2/3));
  temp = (1.5*u_ck2*(3*Math.cos(this.inclination)*Math.cos(this.inclination)-1)/Math.pow(1-this.eccentricity*this.eccentricity,1.5));
  del1 = temp /(a1*a1);
  a0 = a1*(1-del1*(0.5*(2/3)+del1*(1+134/81*del1)));
  del0 = temp/(a0*a0);
  this.xnodp = this.meanMotion/(1+del0);
  if (u_2pi/this.xnodp >= 225) {this.SDP4Init()}    // Yes
  else {this.SGP4Init()}*/
  this.iFlag = 1;
}
