var getxml = "http://www.flighttracker.gleff.com/getdata/getxml.php?getxml=http://118.127.34.203/~gleffcom/flighttracker/getdata/aircraft.xml";      //For Webhost
//var getxml = "http://maps/getdata/getxml.php?getxml=http://gleff.no-ip.info/aircraft.xml"; //For Localhost
var results = null;
var sChar = String.fromCharCode(34);
var sChar1 = String.fromCharCode(39);
var allaircraftmystring = "<li><a id=" + sChar + "ALL" + sChar + " href=" + sChar +"#" + sChar + ">" + "All" + "</a><br /><span class=" + sChar + "airline" + sChar + ">" + "Track all Aircraft" + "</span></li>";
itemclicked = "ALL";
var acraftdata = new Object();
var existing = "0";
var allonce = "0"; //Home into Home location only once when itemclicked = ALL
var Track = "0";
var Groundspeed = "0";
var Vertical_Rate = "0";

function getAircraft() //The function to parse the xml
{
  //map.clearOverlays();  //Clear all aircraft markers
  $.get(getxml, function(xml) //get the results of the XML file
    { //map.clearOverlays();
      existing = "0";
      var aircraft = $('Aircraft', xml);
      if (aircraft.length) {
        map.clearOverlays();
        aircraft.each(function(index, element)
          {//start of collection of aircraft data
            var allaircraftmystring = "<li><a id=" + sChar + "ALL" + sChar + " href=" + sChar +"#" + sChar + ">" + "HOME" + "</a><br /><span class=" + sChar + "airline" + sChar + ">" + "Default location" + "</span></li>";
            ModeS = $(this).find("ModeS").text(); //get the ModeS data
            acraftdata['ModeS'+ModeS] = ModeS; //Assign ModeS data to acraftdata object as the word ModeS+(the contents of ModeS variable)
            if (existing == "0")
            {
              if (itemclicked == ModeS)
              {
                existing = "1";
              }
            }
            Airline = $(this).find("Airline").text();
            acraftdata['Airline'+ModeS] = Airline;
            Callsign = $(this).find("Callsign").text();
            acraftdata['Callsign'+ModeS] = Callsign;
            Aircraft_Registration = $(this).find("Aircraft_Registration").text();
            acraftdata['Aircraft_Registration'+ModeS] = Aircraft_Registration;
            Aircraft_Type = $(this).find("Aircraft_Type").text();
            acraftdata['Aircraft_Type'+ModeS] = Aircraft_Type;
            Latitude = $(this).find("Latitude").text();
            acraftdata['Latitude'+ModeS] = Latitude;
            Longitude = $(this).find("Longitude").text();
            acraftdata['Longitude'+ModeS] = Longitude;
            Groundspeed = $(this).find("Groundspeed").text();
            acraftdata['Groundspeed'+ModeS] = Groundspeed;
            Track = $(this).find("Track").text();
            acraftdata['Track'+ModeS] = Track;
            Vertical_Rate = $(this).find("Vertical_Rate").text();
            acraftdata['Vertical_Rate'+ModeS] = Vertical_Rate;
            Altitude = $(this).find("Altitude").text();
            acraftdata['Altitude'+ModeS] = Altitude;
            Squawk = $(this).find("Squawk").text();
            acraftdata['Squawk'+ModeS] = Squawk;
            Usertag = $(this).find("Usertag").text();
            acraftdata['Usertag'+ModeS] = Usertag;
            Route_Origin = $(this).find("Route_Origin").text();
            acraftdata['Route_Origin'+ModeS] = Route_Origin;
            Route_Destination = $(this).find("Route_Destination").text();
            acraftdata['Route_Destination'+ModeS] = Route_Destination;
            Route_Via = $(this).find("Route_Via").text();
            acraftdata['Route_Via'+ModeS] = Route_Via;
            if(Callsign=="Nil Data") {
              var mystring = "<li><a id=" + sChar + ModeS + sChar + " href=" + sChar +"#" + sChar + ">" + ModeS + "</a><br /><span class=" + sChar + "airline" + sChar + ">" + Airline + "</span></li>";
            } else {
              var mystring = "<li><a id=" + sChar + ModeS + sChar + " href=" + sChar +"#" + sChar + ">" + Callsign + "</a><br /><span class=" + sChar + "airline" + sChar + ">" + Airline + "</span></li>";
            }

            if (Vertical_Rate == "Nil Data") {mystring=""}
            results = results + mystring

            $("#content_flightlist").html("<ul>" + allaircraftmystring + results + "</ul>");
            $('#content_flightlist ul li a').each(function()
              {
                $(this).click(function() //click function start
                  {
                    $('#content_flightlist ul li a').removeClass('selected');
                    $('#' + this.id).addClass('selected');
                    itemclicked = this.id;
                    updatedata() //update all aircraft data when new item is clicked
                    if (itemclicked=="ALL") {
                       allonce = "1";
                    $("#distancedata").html("Nil Data");
                      map.setZoom(11);
                      map.panTo(new GLatLng(-33.945759, 151.180358)); //Center on the marker
                      //map.setCenter(new GLatLng(-33.85851, 151.11059), 10);
                      map.addOverlay(new GMarker(point)); //Plot the marker
                    } else {
                      menuoption(itemclicked);
                      whichphoto() //Function to choose the aircraft photo
                      calculatedistance() //Function to calculate the distance from Home
                      allonce = "0";
                      plotsingleaircraft() //Function to plot the single aircraft by panning
                    }
                  } //click function end
                );
              }
            );

            plotallaircraft() //Plot All Aircraft
          } //aircraft.each function end
        );
      } else {
        map.clearOverlays();
        allaircraftmystring = "<li><span class=" + sChar + "airline" + sChar + ">" + "No visible Aircraft" + "</span></li>";
        results = "";
        $("#content_flightlist").html("<ul>" + allaircraftmystring + results + "</ul>");
      };
      if (existing == "0")
      {
        itemclicked = "ALL";

        if (allonce == "0") {
          allonce = "1";

          $("#distancedata").html("Nil Data"); //Clear the Distance Data
          updatedata() // Update the data so all the Nil Datas occur
          //alert(itemclicked)
          map.setZoom(11);
          map.panTo(new GLatLng(-33.945759, 151.180358)); //Center on the marker at HOME
          map.addOverlay(new GMarker(point)); //Overlay all the new aircraft markers

          menuoption(itemclicked); //Plot the marker
        } else {
          menuoption(itemclicked);}
      }
      if (itemclicked != "ALL")
      {
        plotsingleaircraft()
      }

      updatedata() //update all aircraft data when getxml function is complete

      return[existing, itemclicked, allonce];

    } //getxml function end
  );
  results = "";

} //getAircraft function end
timerstart()

function timerstart()
{
  sixtysecond = self.setInterval("getAircraft()", 10000);

  //sixtysecond = self.setInterval("test()", 5000); // For Localhost
}


