var g_FlightInfo;

///////////////////////////////////////////////////////////////////////////////
// FLIGHT INFORMATION
///////////////////////////////////////////////////////////////////////////////
function FlightInfo (type, ver, lang, callBack)
{
  this._type     = type;
  this._lang     = lang;
  this._ver      = ver;
  this._callBack = callBack;
  this._updated  = false;
  this._queryStr = "";
  
  g_FlightInfo = this;
}
   
FlightInfo.prototype._newInfo;
FlightInfo.prototype._oldInfo;

FlightInfo.prototype._newDT;
FlightInfo.prototype._oldDT;

FlightInfo.prototype._flightDepartureDT;
FlightInfo.prototype._flightArrivalDT;
FlightInfo.prototype._oldFlightDepartureDT;
FlightInfo.prototype._oldFlightArrivalDT;

FlightInfo.prototype._scheduledDepartureDT;
FlightInfo.prototype._scheduledArrivalDT;

FlightInfo.prototype._type;
FlightInfo.prototype._ver;
FlightInfo.prototype._lang;
FlightInfo.prototype._ajaxUpdate;
FlightInfo.prototype._updated;
FlightInfo.prototype._queryStr;

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.sendUpdateRequest = function (airlineCode, flightNumber, airportCode, flightDate, forceUpdate)
{
  var UPDATE_URL = "http://flightmonitor.kroonwijk.nl/getdata.php";

  var queryStr = "?type=" + this._type + "&lang=" + this._lang + "&ver=" + this._ver + "&up_arrDep=D";
  queryStr += "&up_airlineCode="  + airlineCode;
  queryStr += "&up_flightNumber=" + flightNumber;
  queryStr += "&up_airportCode="  + airportCode;
  queryStr += "&up_flightDate="   + flightDate;

  if (this._queryStr != queryStr)
  {
    this._queryStr = queryStr;
    this._newInfo = null;
    this._newDT   = null;

    this._flightDepartureDT = null;
    this._flightArrivalDT   = null;
  }

  if (forceUpdate)
  {
    queryStr += "&up_forceUpdate=magic";
  }
  
  this._ajaxUpdate = new AjaxUpdate (UPDATE_URL + queryStr, this.update);
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.update = function ()
{
  var responseXml = g_FlightInfo._ajaxUpdate.getResponseXml ();
  
  var messageNode = responseXml.getElementsByTagName ("message");
  var fhNode      = responseXml.getElementsByTagName ("fh");

  if (((messageNode == null) || (messageNode.item (0) == null)) &&
      ((fhNode      == null) || (fhNode     .item (0) == null)))
  {
    g_FlightInfo._updated = false;

    if (g_FlightInfo._newInfo == null)
    {
      g_FlightInfo._newInfo = new Array ();
      g_FlightInfo._newDT   = new Array ();
    }
  }
  else
  {
		g_FlightInfo._oldInfo = g_FlightInfo._newInfo;
		g_FlightInfo._newInfo = null;
		g_FlightInfo._newInfo = new Array ();

		g_FlightInfo._oldDT   = g_FlightInfo._newDT;
		g_FlightInfo._newDT   = null;
		g_FlightInfo._newDT   = new Array ();

		g_FlightInfo._oldFlightDepartureDT = g_FlightInfo._flightDepartureDT;
		g_FlightInfo._oldFlightArrivalDT   = g_FlightInfo._flightArrivalDT;

		if ((messageNode != null) && (messageNode.item (0) != null))
		{
			var messageItem = messageNode.item (0);
			g_FlightInfo._newInfo["message"] = messageItem.getAttribute ("description");
		}
		
		if ((fhNode != null) && (fhNode.item (0) != null))
		{
			var fhItem = fhNode.item (0);
			for (var index = 0; index < fhItem.attributes.length; index++)
			{
				var name  = fhItem.attributes[index].nodeName;
				var value = fhItem.attributes[index].nodeValue;
				
				if (value == null)
				{
					value = "";
				}
				
				g_FlightInfo._newInfo[name] = value;
			}

      g_FlightInfo._updated = true;
		}
		
		g_FlightInfo.updateDT ();
  }
  
  g_FlightInfo._callBack ();
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.updateDT = function ()
{
  this.convertDT ();
  this.parseDT   ();
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.convertDT = function ()
{
  this.getSpecificDT ("sgd");
  this.getSpecificDT ("egd");
  this.getSpecificDT ("agd");
  this.getSpecificDT ("srd");
  this.getSpecificDT ("erd");
  this.getSpecificDT ("ard");

  this.getSpecificDT ("sga");
  this.getSpecificDT ("ega");
  this.getSpecificDT ("aga");
  this.getSpecificDT ("sra");
  this.getSpecificDT ("era");
  this.getSpecificDT ("ara");
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getSpecificDT = function (code)
{
  var info = this._newInfo[code];
  if (info != "")
  {
    this._newDT[code] = new DTUtil (info, code);
  }
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.parseDT = function ()
{
  this._flightDepartureDT    = this.findMostSignificantDT ("d");
  this._flightArrivalDT      = this.findMostSignificantDT ("a");
  
  this._scheduledDepartureDT = this.findScheduledDT       ("d");
  this._scheduledArrivalDT   = this.findScheduledDT       ("a");
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.findMostSignificantDT = function (postFix)
{
  var dt;

  if (this._newDT != null)
  {
      var nag = this._newDT["ag" + postFix];
      var neg = this._newDT["eg" + postFix];
      var nar = this._newDT["ar" + postFix];
      var ner = this._newDT["er" + postFix];
  }

  if (this._oldDT != null)
  {
      var  ag = this._oldDT["ag" + postFix];
      var  eg = this._oldDT["eg" + postFix];
      var  ar = this._oldDT["ar" + postFix];
      var  er = this._oldDT["er" + postFix];
  }  
 
  // Check for new actual gate 
  if ((nag != null) && (ag != null))
  {
    if (nag.getTime () != ag.getTime ())
    {
      return nag;
    }
  }
  
  // Check for new actual runway
  if ((nar != null) && (ar != null))
  {
    if (nar.getTime () != ar.getTime ())
    {
      return nar;
    }
  }
  
  if (nag != null)
  {
    return nag;
  }
  
  if (nar != null)
  {
    return nar;
  }
  
  // Check for new expected gate
  if ((neg != null) && (eg != null))
  {
    if (neg.getTime () != eg.getTime ())
    {
      return neg;
    }
  }
  
  // Check for new expected runway
  if ((ner != null) && (er != null))
  {
    if (ner.getTime () != er.getTime ())
    {
      return ner;
    }
  }
  
  if (neg != null)
  {
    return neg;
  }
  
  if (ner != null)
  {
    return ner;
  }
  
  return this.findScheduledDT (postFix);
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.findScheduledDT = function (postFix)
{
  if (this._newDT != null)
  {
      var nsg = this._newDT["sg" + postFix];
      var nsr = this._newDT["sr" + postFix];
  }

  if (this._oldDT != null)
  {
      var  sg = this._oldDT["sg" + postFix];
      var  sr = this._oldDT["sr" + postFix];
  }
  
  // Check for new scheduled gate
  if ((nsg != null) && (sg != null))
  {
    if (nsg.getTime () != sg.getTime ())
    {
      return nsg;
    }
  }
  
  // Check for new scheduled runway
  if ((nsr != null) && (sr != null))
  {
    if (nsr.getTime () != sr.getTime ())
    {
      return nsr;
    }
  }
  
  if (nsg != null)
  {
    return nsg;
  }
  
  if (nsr != null)
  {
    return nsr;
  }
  
  return null;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getAirlineCode = function ()
{
  var str = this._newInfo["ac"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getAirlineName = function ()
{
  var str = this._newInfo["an"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getFlightNumber = function ()
{
  var str = this._newInfo["fn"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getMessage = function ()
{
  var str = this._newInfo["message"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.isTrackingEnabled = function ()
{
  var str = this._newInfo["te"];
  if ((str == null) || (str == "") || (str == "False"))
  {
    return false;
  }

  return true;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getDepartureAirportCode = function ()
{
  var str = this._newInfo["dac"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getArrivalAirportCode = function ()
{
  var str = this._newInfo["aac"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getDepartureAirportName = function ()
{
  var str = this._newInfo["dan"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getArrivalAirportName = function ()
{
  var str = this._newInfo["aan"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getDepartureAirportCity = function ()
{
  var str = this._newInfo["dat"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getArrivalAirportCity = function ()
{
  var str = this._newInfo["aat"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getDepartureTZ = function ()
{
  var str = this._newInfo["dtz"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getArrivalTZ = function ()
{
  var str = this._newInfo["atz"];
  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getDepartureTerminal = function (old)
{
  var str = this._newInfo["dt"];
  if ((old != null) && (old == true) && (this._oldInfo != null))
  {
    str = this._oldInfo["dt"];
  }

  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getArrivalTerminal = function (old)
{
  var str = this._newInfo["at"];
  if ((old != null) && (old == true) && (this._oldInfo != null))
  {
    str = this._oldInfo["at"];
  }

  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getDepartureGate = function (old)
{
  var str = this._newInfo["dg"];
  if ((old != null) && (old == true) && (this._oldInfo != null))
  {
    str = this._oldInfo["dg"];
  }

  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getArrivalGate = function (old)
{
  var str = this._newInfo["ag"];
  if ((old != null) && (old == true) && (this._oldInfo != null))
  {
    str = this._oldInfo["ag"];
  }

  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getFlightStatus = function (old)
{
  var str = this._newInfo["st"];
  if ((old != null) && (old == true) && (this._oldInfo != null))
  {
    str = this._oldInfo["st"];
  }

  if ((str == null) || (str == ""))
  {
    return "";
  }

  return str.toUpperCase ();
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getScheduledDepartureDT = function ()
{
  return this._scheduledDepartureDT;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getScheduledArrivalDT = function ()
{
  return this._scheduledArrivalDT;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getFlightDepartureDT = function (old)
{
  if ((old != null) && (old == true) && (this._oldFlightDepartureDT != null))
  {  
    return this._oldFlightDepartureDT;
  }

  return this._flightDepartureDT;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.getFlightArrivalDT = function (old)
{
  if ((old != null) && (old == true) && (this._oldFlightArrivalDT != null))
  {  
    return this._oldFlightArrivalDT;
  }

  return this._flightArrivalDT;
}

///////////////////////////////////////////////////////////////////////////////
FlightInfo.prototype.isUpdated = function ()
{
  return this._updated;
}

