var g_platform   = new Platform ();
var g_options    = new Options  ();
var g_flightInfo = new FlightInfo (g_platform.getType (), g_platform.getVersion (), GADGET_LANGUAGE, UpdateContent);

///////////////////////////////////////////////////////////////////////////////
// DYNAMIC CONTENT
///////////////////////////////////////////////////////////////////////////////
var g_rotRows      = new Array ();
var g_alertActive  = 0;
var g_updateActive = 0;

///////////////////////////////////////////////////////////////////////////////
function SendUpdateRequest (force)
{
  g_updateActive = 2;
  g_platform.setTitle   ('updateLight'  , GADGET_UPDATING);
  g_platform.setTitle   ('updateLightOn', GADGET_UPDATING);
	g_platform.setVisible ('img_updateLightOn', true);

  var airlineCode  = g_options.getOptionAirlineCode  ();
  var flightNumber = g_options.getOptionFlightNumber ();
  var airportCode  = g_options.getOptionAirportCode  ();
  var flightDate   = g_options.getOptionFlightDate   ();
    
  g_flightInfo.sendUpdateRequest (airlineCode, flightNumber, airportCode, flightDate, force);
}

///////////////////////////////////////////////////////////////////////////////
function UpdateContent ()
{
	var showDeparture = g_options.getOptionShowDeparture ();
	
	if (showDeparture)
	{
		g_platform.setTitle ('planeButton', GADGET_SHOW_ARRIVAL);
	}
	else
	{
		g_platform.setTitle ('planeButton', GADGET_SHOW_DEPARTURE);
	}

	var caption =     g_flightInfo.getAirlineCode  ();
	caption += "-"  + g_flightInfo.getFlightNumber ();
	caption += " "  + GetFlightStatus (showDeparture);
	caption += " "  + GetDetailTime   (showDeparture);
	caption += " (" + GADGET_NAME + ")";
	
	g_platform.setGadgetCaption (caption);

	g_platform.setText ('headerLink' , GetHeaderStr    (showDeparture));
	g_platform.setText ('airportLink', GetDirectionStr (showDeparture));

	g_rotRows["airlineCode"] .setStr (g_flightInfo.getAirlineCode  ());
	g_rotRows["flightNumber"].setStr (g_flightInfo.getFlightNumber ());
	
    g_rotRows["airportName"] .setStr (GetAirportName  (showDeparture));
 	g_rotRows["terminal"]    .setStr (GetTerminal     (showDeparture));
    g_rotRows["gate"]        .setStr (GetGate         (showDeparture));
	g_rotRows["flightStatus"].setStr (GetFlightStatus (showDeparture));

	g_rotRows["expectedHour"].setStr (GetDetailHour   (showDeparture));
	g_rotRows["expectedMin"] .setStr (GetDetailMin    (showDeparture));

	g_platform.setHRef  ('headerLink', GetAirportLink     ( showDeparture));
	g_platform.setTitle ('headerLink', GetAirportTooltip  ( showDeparture));

	g_platform.setHRef  ('airportLink', GetAirportLink    (!showDeparture));
	g_platform.setTitle ('airportLink', GetAirportTooltip (!showDeparture));
	
	g_platform.setHRef  ('flightLink', GetFlightLink      (g_flightInfo.getAirlineCode    (),
																												 g_flightInfo.getFlightNumber   (),
																												 g_options.getOptionAirportCode (),
																												 g_options.getOptionFlightDate  ()));
	g_platform.setTitle ('flightLink', GetFlightTooltip   ());

	SetTrackButtonState (g_flightInfo.isTrackingEnabled ());
	
  if (g_flightInfo.isUpdated ())
  {
		g_platform.setTitle   ('updateLight'  , GADGET_UPDATE_LIGHT);
		g_platform.setTitle   ('updateLightOn', GADGET_UPDATE_LIGHT);
		g_platform.setVisible ('img_updateLightOn', false);
  }
  else
  {
    g_platform.setTitle   ('updateLight'  , GADGET_UPDATE_FAILED);
    g_platform.setTitle   ('updateLightOn', GADGET_UPDATE_FAILED);
		g_platform.setVisible ('img_updateLightOn', true);
  }

	g_updateActive = 0;

	UpdateMessage   ();
	UpdateLocalTime ();
	UpdateAlert     ();
}

///////////////////////////////////////////////////////////////////////////////
function GetHeaderStr (showDeparture)
{
  var value;
  if (showDeparture)
  {
    value = g_flightInfo.getDepartureAirportCity ();
    if (value != "")
    {
      return value;
    }
    
    return GADGET_DEPARTURES;
  }
  else
  {
    value = g_flightInfo.getArrivalAirportCity ();
    if (value != "")
    {
      return value;
    }
    
    return GADGET_ARRIVALS;
  }
}

///////////////////////////////////////////////////////////////////////////////
function GetDirectionStr (showDeparture)
{
  if (showDeparture)
  {
    return GADGET_TO;
  }
  else
  {
    return GADGET_FROM;
  }
}

///////////////////////////////////////////////////////////////////////////////
function GetAirportName (showDeparture)
{
  if (showDeparture)
  {
    return g_flightInfo.getArrivalAirportCity   ();
  }
  else
  {
    return g_flightInfo.getDepartureAirportCity ();
  }
}

///////////////////////////////////////////////////////////////////////////////
function GetFlightStatus (showDeparture, old)
{
  var value = g_flightInfo.getFlightStatus (old);

  if (value == "SCHEDULED")
  {
    return GADGET_SCHEDULED;
  }
  else if (value == "CANCELLED")
  {
    return GADGET_CANCELLED;
  }
  else if (value == "ACTIVE")
  {
    if (showDeparture)
    {
      return GADGET_DEPARTED;
    }
    else
    {
      return GADGET_EXPECTED;
    }
  }
  else if (value == "LANDED")
  {
    if (showDeparture)
    {
      return GADGET_DEPARTED;
    }
    else
    {
      return GADGET_LANDED;
    }
  }
  else
  {
    value = GADGET_UNKNOWN;
  }
  
  return value;
}

///////////////////////////////////////////////////////////////////////////////
function GetDetailHour (showDeparture, old)
{
  var dt = GetDetailDT (showDeparture, old);
  if (dt == null)
  {
    return "";
  }

  return dt.getFormattedHour ();
}

///////////////////////////////////////////////////////////////////////////////
function GetDetailMin (showDeparture, old)
{
  var dt = GetDetailDT (showDeparture, old);
  if (dt == null)
  {
    return "";
  }

  return dt.getFormattedMin ();
}

///////////////////////////////////////////////////////////////////////////////
function GetDetailTime (showDeparture, old)
{
  return GetDetailHour (showDeparture, old) + ":" + GetDetailMin (showDeparture, old);
}

///////////////////////////////////////////////////////////////////////////////
function GetDetailDT (showDeparture, old)
{
  var dt;
  if (showDeparture)
  {
    dt = g_flightInfo.getFlightDepartureDT (old);
  }
  else
  {
    dt = g_flightInfo.getFlightArrivalDT (old);
  }

  return dt;
}

///////////////////////////////////////////////////////////////////////////////
function GetTerminal (showDeparture, old)
{
  if (showDeparture)
  {
    return g_flightInfo.getDepartureTerminal (old);
  }
  else
  {
    return g_flightInfo.getArrivalTerminal (old);
  }
}

///////////////////////////////////////////////////////////////////////////////
function GetGate (showDeparture, old)
{
  if (showDeparture)
  {
    return g_flightInfo.getDepartureGate (old);
  }
  else
  {
    return g_flightInfo.getArrivalGate (old);
  }
}

///////////////////////////////////////////////////////////////////////////////
function GetAirportLink (showDeparture)
{
  var baseUrl = "http://flightmonitor.kroonwijk.nl/redir.php?up_action=airport_details";

  var value;
  if (showDeparture)
  {
    value = g_flightInfo.getDepartureAirportCode ();
    if (value != "")
    {
      return  baseUrl + "&up_airportCode=" + value;
    }
    
    return baseUrl;
  }
  else
  {
    value = g_flightInfo.getArrivalAirportCode ();
    if (value != "")
    {
       return  baseUrl + "&up_airportCode=" + value;
    }
    
    return baseUrl;
  }
}

///////////////////////////////////////////////////////////////////////////////
function GetAirportTooltip (showDeparture)
{
  var tooltip = "";
  
  var name;
  var code;
  
  if (showDeparture)
  {
    name = g_flightInfo.getDepartureAirportName ();
    code = g_flightInfo.getDepartureAirportCode ();
  }
  else
  {
    name = g_flightInfo.getArrivalAirportName ();
    code = g_flightInfo.getArrivalAirportCode ();
  }
  
  if (code != "")
  {
    tooltip += code + " - ";
  }
  
  if (name != "")
  {
    tooltip += name + " ";
  }
  
  tooltip += "(" + GADGET_FIND_AIRPORT_DETAILS + ")";
  return tooltip;
}

///////////////////////////////////////////////////////////////////////////////
function GetFlightLink (ac, fn, da, dd)
{
  var baseUrl = "http://flightmonitor.kroonwijk.nl/redir.php";

  var value = "?up_airlineCode="   + ac;
  value    += "&up_flightNumber="  + fn;
  value    += "&up_airportCode="   + da;
  value    += "&up_departureDate=" + dd;
  value    += "&up_action=flight_status";
    
  return baseUrl + value;
}

///////////////////////////////////////////////////////////////////////////////
function GetFlightTooltip ()
{
  var tooltip = "";
  
  var name   = g_flightInfo.getAirlineName  ();
  var code   = g_flightInfo.getAirlineCode  ();
  var number = g_flightInfo.getFlightNumber ();
  
  if (code != "")
  {
    tooltip += code + " - ";
  }
  
  if (name != "")
  {
    tooltip += name + " ";
  }
  
  if (number != "")
  {
    tooltip += number + " ";
  }
  
  tooltip += "(" + GADGET_FIND_FLIGHT_DETAILS + ")";
  return tooltip;
}

///////////////////////////////////////////////////////////////////////////////
function GetTrackerLink (ac, fn, da, dd)
{
  var baseUrl = "http://flightmonitor.kroonwijk.nl/redir.php";

  var value = "?up_airlineCode="   + ac;
  value    += "&up_flightNumber="  + fn;
  value    += "&up_airportCode="   + da;
  value    += "&up_departureDate=" + dd;
  value    += "&up_action=flight_tracker";
    
  return baseUrl + value;
}

///////////////////////////////////////////////////////////////////////////////
function GetScheduledDT (showDeparture)
{
  var dt;
  if (showDeparture)
  {
    dt = g_flightInfo.getScheduledDepartureDT ();
  }
  else
  {
    dt = g_flightInfo.getScheduledArrivalDT ();
  }

  return dt;

}

///////////////////////////////////////////////////////////////////////////////
function GetDT (showDeparture, showClock)
{
  var dt;
  if (showClock)
  {
    dt = GetLocalDT (showDeparture);
  }
  else
  {
    dt = GetScheduledDT (showDeparture);
  }

  return dt;
}

///////////////////////////////////////////////////////////////////////////////
function GetLocalDT (showDeparture)
{
  var now = new Date ();
  
  var e = now.getTime ();
  var f = now.getTimezoneOffset ();

  var tzOffset;
  if (showDeparture)
  {
    tzOffset = g_flightInfo.getDepartureTZ ();
  }
  else
  {
    tzOffset = g_flightInfo.getArrivalTZ ();
  }

  e += ((parseInt (tzOffset) * 60) + f) * 60 * 1000;
  now.setTime (e);

  var dt = new DTUtil (null, "now", now);
  return dt;
}

///////////////////////////////////////////////////////////////////////////////
function GetMonth (showDeparture, showClock)
{
  var dt = GetDT (showDeparture, showClock);
  if (dt == null)
  {
    return "";
  }

  return dt.getFormattedMonth ();
}

///////////////////////////////////////////////////////////////////////////////
function GetDay (showDeparture, showClock)
{
  var dt = GetDT (showDeparture, showClock);
  if (dt == null)
  {
    return "";
  }

  return dt.getFormattedDay ();
}

///////////////////////////////////////////////////////////////////////////////
function GetHour (showDeparture, showClock)
{
  var dt = GetDT (showDeparture, showClock);
  if (dt == null)
  {
    return "";
  }

  return dt.getFormattedHour ();
}

///////////////////////////////////////////////////////////////////////////////
function GetMin (showDeparture, showClock)
{
  var dt = GetDT (showDeparture, showClock);
  if (dt == null)
  {
    return "";
  }

  return dt.getFormattedMin ();
}

///////////////////////////////////////////////////////////////////////////////
function UpdateMessage ()
{
  var value = g_flightInfo.getMessage ();
  if ((value == null) || (value == ""))
  {
    if (g_platform.getVisible ("gadgetMessage"))
    {
      hideMessage ();
    }
    
    g_platform.setText ("descriptionLink", "");
  }
  else
  {
    var str = "";
    if (-1 != value.indexOf ("Specify an airline code"))
    {
      str += GADGET_SPECIFY_AIRLINE_CODE + "\n\n";
    }
    if (-1 != value.indexOf ("Specify a flight number"))
    {
      str += GADGET_SPECIFY_FLIGHT_NUMBER + "\n\n";
    }
    if (-1 != value.indexOf ("An airport must be provided"))
    {
      str += GADGET_SPECIFY_AIRPORT_CODE + "\n\n";
    }
    if (-1 != value.indexOf ("The airline code is invalid"))
    {
      str += GADGET_INVALID_AIRLINE_CODE + "\n\n";
    }
    if (-1 != value.indexOf ("The flight number is invalid"))
    {
      str += GADGET_INVALID_FLIGHT_NUMBER + "\n\n";
    }
    if (-1 != value.indexOf ("The airport is invalid"))
    {
      str += GADGET_INVALID_AIRPORT_CODE + "\n\n";
    }
    if (-1 != value.indexOf ("Flight not found on selected date"))
    {
      str += GADGET_FLIGHT_NOT_FOUND;
    }

    if (g_platform.getText ("descriptionLink") != str)
    {
      showMessage ();
    }

    g_platform.setText ("descriptionLink", str);
  }
}

///////////////////////////////////////////////////////////////////////////////
function UpdateLocalTime ()
{
  var showClock     = g_options.getOptionShowClock     ();
  var showDeparture = g_options.getOptionShowDeparture ();

  if (showClock)
  {
    g_platform.setText  ('dateLink'   , GADGET_LOCAL_TIME);
    g_platform.setTitle ('clockButton', GADGET_SHOW_SCHEDULED_TIME);
  }
  else
  {
    g_platform.setText  ('dateLink'   , GADGET_SCHEDULED_TIME);
    g_platform.setTitle ('clockButton', GADGET_SHOW_LOCAL_TIME);
  }

  g_rotRows["flightDay"]    .setStr (GetDay   (showDeparture, showClock));
  g_rotRows["flightMonth"]  .setStr (GetMonth (showDeparture, showClock));
  g_rotRows["scheduledHour"].setStr (GetHour  (showDeparture, showClock));
  g_rotRows["scheduledMin"] .setStr (GetMin   (showDeparture, showClock));
  
  if (g_alertActive == 1)
  {
		g_platform.setVisible ('img_alertLightOn', false);

    g_alertActive = 2;
  }
  else if (g_alertActive == 2)
  {
		g_platform.setVisible ('img_alertLightOn', true);

    g_alertActive = 1;
  }
  else
  {
		g_platform.setVisible ('img_alertLightOn', false);
  }

  if (g_updateActive == 1)
  {
		g_platform.setVisible ('img_updateLightOn', true);
    g_updateActive = 2;
  }
  else if (g_updateActive == 2)
  {
		g_platform.setVisible ('img_updateLightOn', false);
    g_updateActive = 1;
  }
}

///////////////////////////////////////////////////////////////////////////////
function UpdateAlert ()
{
  var showDeparture = g_options.getOptionShowDeparture ();

  var oldStatus = GetFlightStatus (showDeparture, true);
  var newStatus = GetFlightStatus (showDeparture, false);


	var url = GetFlightLink (g_flightInfo.getAirlineCode    (),
													 g_flightInfo.getFlightNumber   (),
													 g_options.getOptionAirportCode (),
													 g_options.getOptionFlightDate  ());

  var source =       g_flightInfo.getAirlineCode  ();
	source    += "-" + g_flightInfo.getFlightNumber ();
  source    += " " + GADGET_FROM.toLowerCase ();
  source    += " " + g_flightInfo.getArrivalAirportCity ();
  source    += " " + GADGET_TO.toLowerCase ();
  source    += " " + g_flightInfo.getDepartureAirportCity ();

  if (oldStatus != newStatus)
  {
    var snippet = GADGET_STATUS_CHANGED;
    snippet = snippet.replace (/%1/, oldStatus).replace (/%2/, newStatus);

    g_platform.showAlert (GADGET_NAME, source, snippet, url);

    g_platform.setTitle  ('alertLight'  , snippet);
    g_platform.setTitle  ('alertLightOn', snippet);

    g_alertActive = 1;
  }

  var oldTime = GetDetailTime (showDeparture, true);
  var newTime = GetDetailTime (showDeparture, false);

  var depArr = showDeparture ? GADGET_DEPARTURE : GADGET_ARRIVAL;
  if (oldTime != newTime)
  {
    var snippet = GADGET_TIME_CHANGED;
    snippet = snippet.replace (/%1/, depArr).replace (/%2/, oldTime).replace (/%3/, newTime);

    g_platform.showAlert (GADGET_NAME, source, snippet, url);

    g_platform.setTitle  ('alertLight'  , snippet);
    g_platform.setTitle  ('alertLightOn', snippet);

    g_alertActive = 1;
  }

  var oldTerminalGate = "T" + GetTerminal (showDeparture, true ) + "-G" + GetGate (showDeparture, true );
  var newTerminalGate = "T" + GetTerminal (showDeparture, false) + "-G" + GetGate (showDeparture, false);

  if (oldTerminalGate != newTerminalGate)
  {
    var snippet = GADGET_TERMINAL_GATE_CHANGED;
    snippet = snippet.replace (/%1/, depArr).replace (/%2/, oldTerminalGate).replace (/%3/, newTerminalGate);

    g_platform.setTitle  ('alertLight'  , snippet);
    g_platform.setTitle  ('alertLightOn', snippet);

    g_platform.showAlert (GADGET_NAME, source, snippet, url);
    g_alertActive = 1;
  }

  if (g_alertActive == 1)
	{
    setTimeout ("ShowAlert ()", 1000 * 30);
	}
}

///////////////////////////////////////////////////////////////////////////////
// VIEW ONOPEN HANDLER
///////////////////////////////////////////////////////////////////////////////
function view_onOpen ()
{
  g_platform.setGadgetCaption (GADGET_NAME);

  var value = g_platform.getElementById ("airlineCodeEdit").value.toUpperCase ();
  if (value != "")
  {
    g_options.setOptionAirlineCode (value);
  }
  
  value = g_platform.getElementById ("flightNumberEdit").value.toUpperCase ();
  if (value != "")
  {
    g_options.setOptionFlightNumber (value);
  }
  
  value = g_platform.getElementById ("airportCodeEdit" ).value.toUpperCase ();
  if (value != "")
  {
    g_options.setOptionAirportCode (value);
  }

  var divAirlineCode   = g_platform.getElementById ("divAirlineCode");
  var divFlightNumber  = g_platform.getElementById ("divFlightNumber");
  var divFlightDay     = g_platform.getElementById ("divFlightDay");
  var divFlightMonth   = g_platform.getElementById ("divFlightMonth");
  var divScheduledHour = g_platform.getElementById ("divScheduledHour");
  var divScheduledMin  = g_platform.getElementById ("divScheduledMin");
  var divAirportName   = g_platform.getElementById ("divAirportName");
  var divFlightStatus  = g_platform.getElementById ("divFlightStatus");
  var divExpectedHour  = g_platform.getElementById ("divExpectedHour");
  var divExpectedMin   = g_platform.getElementById ("divExpectedMin");
  var divTerminal      = g_platform.getElementById ("divTerminal");
  var divGate          = g_platform.getElementById ("divGate");

  g_rotRows["airlineCode"]   = new RotRow ( 3, GADGET_ROTATE_ALL , divAirlineCode,   0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["flightNumber"]  = new RotRow ( 4, GADGET_ROTATE_NUM , divFlightNumber,  0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["flightDay"]     = new RotRow ( 2, GADGET_ROTATE_NUM , divFlightDay,     0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["flightMonth"]   = new RotRow ( 3, GADGET_ROTATE_TEXT, divFlightMonth,   0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["scheduledHour"] = new RotRow ( 2, GADGET_ROTATE_NUM , divScheduledHour, 0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["scheduledMin"]  = new RotRow ( 2, GADGET_ROTATE_NUM , divScheduledMin,  0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["airportName"]   = new RotRow (13, GADGET_ROTATE_TEXT, divAirportName,   0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["flightStatus"]  = new RotRow (10, GADGET_ROTATE_TEXT, divFlightStatus,  0, 0, 22, 18, 24, -6, -6, "#010101", "#EEEEEE");
  g_rotRows["expectedHour"]  = new RotRow ( 2, GADGET_ROTATE_NUM , divExpectedHour,  0, 0, 29, 18, 24, -5, -3, "#010101", "#EEEEEE");
  g_rotRows["expectedMin"]   = new RotRow ( 2, GADGET_ROTATE_NUM , divExpectedMin,   0, 0, 29, 18, 24, -5, -3, "#010101", "#EEEEEE");
  g_rotRows["terminal"]      = new RotRow ( 3, GADGET_ROTATE_ALL , divTerminal,      0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  g_rotRows["gate"]          = new RotRow ( 3, GADGET_ROTATE_ALL , divGate,          0, 0, 12, 10, 11, -1, -2, "#010101", "#EEEEEE");
  
  g_platform.addButton ('trackButton'   , 'TrackFlight'    , 'track_button.png'   , 206,  28);
  g_platform.addButton ('clockButton'   , 'ToggleClock'    , 'clock_button.png'   , 206,  55);
  g_platform.addButton ('planeButton'   , 'ToggleDeparture', 'plane_button.png'   , 206,  82);
  g_platform.addButton ('movieButton'   , 'RunMovie'       , 'movie_button.png'   , 206, 109);
  g_platform.addButton ('optionsButton' , 'ShowOptions'    , 'options_button.png' , 206, 136);

  g_platform.addLight  ('updateLight'   , 'ShowUpdate'     , 'yellowlight_off.png', 160, 53);
  g_platform.addLight  ('updateLightOn' , 'ShowUpdate'     , 'yellowlight_on.png' , 160, 53);
  g_platform.addLight  ('alertLight'    , 'ShowAlert'      , 'redlight_off.png'   , 176, 53);
  g_platform.addLight  ('alertLightOn'  , 'ShowAlert'      , 'redlight_on.png'    , 176, 53);

  g_platform.setTitle   ('updateLight'  , GADGET_UPDATE_LIGHT);
  g_platform.setTitle   ('updateLightOn', GADGET_UPDATE_LIGHT);
  g_platform.setTitle   ('alertLight'   , GADGET_ALERT_LIGHT);
  g_platform.setTitle   ('alertLightOn' , GADGET_ALERT_LIGHT);

	g_platform.setVisible ('img_updateLightOn', false);
	g_platform.setVisible ('img_alertLightOn' , false);

  g_platform.setTitle ('optionsButton' , GADGET_CHANGE_FLIGHT_DATA);
  g_platform.setTitle ('movieButton'   , GADGET_UPDATE);
  g_platform.setTitle ('doneButton'    , GADGET_ACCEPT_FLIGHT_DATA);
  g_platform.setTitle ('cancelButton'  , GADGET_DISCARD_FLIGHT_DATA);
  g_platform.setTitle ('findFlightLink', GADGET_FIND_FLIGHT_LINK);

  g_platform.setText  ('flightLink'      , GADGET_FLIGHT);
  g_platform.setText  ('terminalLink'    , GADGET_TERMINAL);
  g_platform.setText  ('gateLink'        , GADGET_GATE);
  g_platform.setText  ('optionsLink'     , GADGET_FLIGHT_OPTIONS);
  g_platform.setText  ('doneButton'      , GADGET_DONE);
  g_platform.setText  ('cancelButton'    , GADGET_CANCEL);
  g_platform.setText  ('findFlightLink'  , GADGET_FIND_FLIGHT_LINK);
  g_platform.setText  ('airlineCodeLink' , GADGET_AIRLINE_CODE);
  g_platform.setText  ('flightNumberLink', GADGET_FLIGHT_NUMBER);
  g_platform.setText  ('airportCodeLink' , GADGET_DEPARTURE_AIRPORT_CODE);
  g_platform.setText  ('flightDateLink'  , GADGET_DEPARTURE_DATE);
  g_platform.setText  ('messageLink'     , GADGET_MESSAGE);
  g_platform.setText  ('continueLink'    , GADGET_CONTINUE);

  g_platform.setHRef  ('findFlightLink'  , GetFlightLink ("", "", "", ""));
  g_platform.setHRef  ('cancelButton'    , "#");
  g_platform.setHRef  ('doneButton'      , "#");
  g_platform.setHRef  ('continueLink'    , "#");

  var sel = g_platform.getElementById ("flightDayEdit");
  g_platform.addListItem (sel, "-", "");
  
  for (var i=1; i<32; i++)
  {
    g_platform.addListItem (sel, String (i), String (i));
  }

  sel = g_platform.getElementById ("flightMonthEdit");
  g_platform.addListItem (sel, "-", "");
  
  for (i=1; i<13; i++)
  {  
    g_platform.addListItem (sel, g_monthArray[i-1], String (i));
  }
  
  g_platform.setLayout ();
  
  setInterval ("SendUpdateRequest ()", 1000 * 60 * 5);
  setInterval ("UpdateLocalTime   ()", 1000);

  setTimeout  ("SendUpdateRequest ()", 1000);
}

///////////////////////////////////////////////////////////////////////////////
function view_onMouseOver ()
{
  g_platform.onMouseOver ();
}

///////////////////////////////////////////////////////////////////////////////
function view_onMouseOut ()
{
  g_platform.onMouseOut ();
}

///////////////////////////////////////////////////////////////////////////////
function ShowAlert ()
{
  g_alertActive = 0;
  g_platform.setTitle   ('alertLight'  , GADGET_ALERT_LIGHT);
  g_platform.setTitle   ('alertLightOn', GADGET_ALERT_LIGHT);
  g_platform.setVisible ('img_alertLightOn', false);

  return false;
}

///////////////////////////////////////////////////////////////////////////////
function ShowUpdate ()
{
  g_updateActive = 0;
  g_platform.setTitle   ('updateLight'  , GADGET_UPDATE_LIGHT);
  g_platform.setTitle   ('updateLightOn', GADGET_UPDATE_LIGHT);
  g_platform.setVisible ('img_updateLightOn', false);
}

///////////////////////////////////////////////////////////////////////////////
function ShowOptions ()
{
  g_platform.getElementById ("airlineCodeEdit" ).value = g_options.getOptionAirlineCode  ();
  g_platform.getElementById ("flightNumberEdit").value = g_options.getOptionFlightNumber ();
  g_platform.getElementById ("airportCodeEdit" ).value = g_options.getOptionAirportCode  ();
  
  g_platform.setValue ("flightDayEdit"  , g_options.getOptionFlightDay   ());
  g_platform.setValue ("flightMonthEdit", g_options.getOptionFlightMonth ());

  SetTrackButtonState (false);
  
  g_platform.setEnabled ("clockButton"  , false);
  g_platform.setEnabled ("planeButton"  , false);
  g_platform.setEnabled ("movieButton"  , false);
  g_platform.setEnabled ("optionsButton", false);

  g_platform.setCursor ("clockButton"  , "arrow");
  g_platform.setCursor ("planeButton"  , "arrow");
  g_platform.setCursor ("movieButton"  , "arrow");
  g_platform.setCursor ("optionsButton", "arrow");

  g_platform.setVisible ("gadgetContent", false);
  g_platform.setVisible ("gadgetMessage", false);
  g_platform.setVisible ("gadgetOptions", true );
  
  g_platform.setFocus ("airlineCodeEdit");
}

///////////////////////////////////////////////////////////////////////////////
function AcceptOptions ()
{
  g_platform.setVisible ("gadgetContent", true );
  g_platform.setVisible ("gadgetMessage", false);
  g_platform.setVisible ("gadgetOptions", false);

  g_platform.setText ("descriptionLink", "");

  g_options.setOptionAirlineCode  (g_platform.getElementById ("airlineCodeEdit" ).value.toUpperCase ());
  g_options.setOptionFlightNumber (g_platform.getElementById ("flightNumberEdit").value.toUpperCase ());
  g_options.setOptionAirportCode  (g_platform.getElementById ("airportCodeEdit" ).value.toUpperCase ());
  
  g_options.setOptionFlightDay    (g_platform.getValue ("flightDayEdit"  ));
  g_options.setOptionFlightMonth  (g_platform.getValue ("flightMonthEdit"));

  g_options.saveSettings ();
  
  SetTrackButtonState (g_flightInfo.isTrackingEnabled ());

  g_platform.setCursor ("clockButton"  , "hand");
  g_platform.setCursor ("planeButton"  , "hand");
  g_platform.setCursor ("movieButton"  , "hand");
  g_platform.setCursor ("optionsButton", "hand");

  g_platform.setEnabled ("clockButton"  , true);
  g_platform.setEnabled ("planeButton"  , true);
  g_platform.setEnabled ("movieButton"  , true);
  g_platform.setEnabled ("optionsButton", true);

  SendUpdateRequest ();
}

///////////////////////////////////////////////////////////////////////////////
function AcceptMessage ()
{
  g_platform.setText ("descriptionLink", "");
  hideMessage ();
  
  return false;
}

///////////////////////////////////////////////////////////////////////////////
function CancelOptions ()
{
  g_platform.setVisible ("gadgetContent", true );
  g_platform.setVisible ("gadgetOptions", false);

  SetTrackButtonState (g_flightInfo.isTrackingEnabled ());
  
  g_platform.setCursor ("clockButton"  , "hand");
  g_platform.setCursor ("planeButton"  , "hand");
  g_platform.setCursor ("movieButton"  , "hand");
  g_platform.setCursor ("optionsButton", "hand");

  g_platform.setEnabled ("clockButton"  , true);
  g_platform.setEnabled ("planeButton"  , true);
  g_platform.setEnabled ("movieButton"  , true);
  g_platform.setEnabled ("optionsButton", true);
}

///////////////////////////////////////////////////////////////////////////////
function RunMovie ()
{
  g_rotRows["airlineCode"]  .setStr ("");
  g_rotRows["flightNumber"] .setStr ("");
  g_rotRows["flightDay"]    .setStr ("");
  g_rotRows["flightMonth"]  .setStr ("");
  g_rotRows["scheduledHour"].setStr ("");
  g_rotRows["scheduledMin"] .setStr ("");
  g_rotRows["airportName"]  .setStr ("");
  g_rotRows["flightStatus"] .setStr ("");
  g_rotRows["expectedHour"] .setStr ("");
  g_rotRows["expectedMin"]  .setStr ("");
  g_rotRows["terminal"]     .setStr ("");
  g_rotRows["gate"]         .setStr ("");

  SendUpdateRequest (true);
}

///////////////////////////////////////////////////////////////////////////////
function ToggleClock ()
{
  g_options.setOptionShowClock (!g_options.getOptionShowClock ());
  g_options.saveSettings ();
  
  UpdateLocalTime ();
}

///////////////////////////////////////////////////////////////////////////////
function ToggleDeparture ()
{
  g_options.setOptionShowDeparture (!g_options.getOptionShowDeparture ());
  g_options.saveSettings ();

  UpdateContent ();
}

///////////////////////////////////////////////////////////////////////////////
function FindFlight ()
{
  g_platform.setHRef ('findFlightLink', GetFlightLink (g_platform.getElementById  ("airlineCodeEdit" ).value,
                                                       g_platform.getElementById  ("flightNumberEdit").value,
                                                       g_platform.getElementById  ("airportCodeEdit" ).value,
                                                       g_options.convertToDate (g_platform.getValue ("flightDayEdit"   ),
                                                                                g_platform.getValue ("flightMonthEdit" ))));
}

///////////////////////////////////////////////////////////////////////////////
function TrackFlight ()
{
  g_platform.setHRef ('trackButton', GetTrackerLink (g_flightInfo.getAirlineCode          (),
                                                     g_flightInfo.getFlightNumber         (),
                                                     g_flightInfo.getDepartureAirportCode (),
                                                     g_options.getOptionFlightDate        ()));
}

///////////////////////////////////////////////////////////////////////////////
function SetTrackButtonState (enabled)
{
  g_platform.setEnabled ("trackButton", enabled);

  if (enabled)
  {
    g_platform.setTitle  ("trackButton", GADGET_TRACK_FLIGHT);
    g_platform.setCursor ("trackButton", "hand");
  }
  else
  {
    g_platform.setTitle  ("trackButton", GADGET_TRACK_FLIGHT_DISABLED);
    g_platform.setCursor ("trackButton", "arrow");
  }
}

//change the opacity for different browsers 
///////////////////////////////////////////////////////////////////////////////
function moveOptions ()
{
  optionsPane.x = event.value; 
}

///////////////////////////////////////////////////////////////////////////////
function hideMessage ()
{
  g_platform.setVisible ("gadgetContent", true );
  g_platform.setVisible ("gadgetMessage", false);
  g_platform.setVisible ("gadgetOptions", false);

  SetTrackButtonState (g_flightInfo.isTrackingEnabled ());

  g_platform.setCursor ("clockButton"  , "hand");
  g_platform.setCursor ("planeButton"  , "hand");
  g_platform.setCursor ("movieButton"  , "hand");
  g_platform.setCursor ("optionsButton", "hand");

  g_platform.setEnabled ("clockButton"  , true);
  g_platform.setEnabled ("planeButton"  , true);
  g_platform.setEnabled ("movieButton"  , true);
  g_platform.setEnabled ("optionsButton", true);
  
  g_platform.setLayout ();
}

///////////////////////////////////////////////////////////////////////////////
function showMessage ()
{
  SetTrackButtonState (false);
  
  g_platform.setEnabled ("clockButton"  , false);
  g_platform.setEnabled ("planeButton"  , false);
  g_platform.setEnabled ("movieButton"  , false);
  g_platform.setEnabled ("optionsButton", false);

  g_platform.setCursor ("clockButton"  , "arrow");
  g_platform.setCursor ("planeButton"  , "arrow");
  g_platform.setCursor ("movieButton"  , "arrow");
  g_platform.setCursor ("optionsButton", "arrow");

  g_platform.setVisible ("gadgetContent", false);
  g_platform.setVisible ("gadgetOptions", false);
  g_platform.setVisible ("gadgetMessage", true );
  
  g_platform.setLayout ();
}

///////////////////////////////////////////////////////////////////////////////
function keyPressAc ()
{
  if (event.keyCode == 9)
  {
    event.returnValue = false;
    g_platform.setFocus ("flightNumberEdit");
  }
  else if (event.keyCode == 13)
  {
    event.returnValue = false;
    AcceptOptions ();
  }
}

///////////////////////////////////////////////////////////////////////////////
function keyPressFn ()
{
  if (event.keyCode == 9)
  {
    event.returnValue = false;
    g_platform.setFocus ("airportCodeEdit");
  }
  else if (event.keyCode == 13)
  {
    event.returnValue = false;
    AcceptOptions ();
  }
}

///////////////////////////////////////////////////////////////////////////////
function keyPressDa ()
{
  if (event.keyCode == 9)
  {
    event.returnValue = false;
    g_platform.setFocus ("flightDayEdit");
  }
  else if (event.keyCode == 13)
  {
    event.returnValue = false;
    AcceptOptions ();
  }
}

///////////////////////////////////////////////////////////////////////////////
function keyPressDd ()
{
  if (event.keyCode == 9)
  {
    event.returnValue = false;
    g_platform.setFocus ("flightMonthEdit");
  }
  else if (event.keyCode == 13)
  {
    event.returnValue = false;
    AcceptOptions ();
  }
}

///////////////////////////////////////////////////////////////////////////////
function keyPressDm ()
{
  if (event.keyCode == 9)
  {
    event.returnValue = false;
    g_platform.setFocus ("airlineCodeEdit");
  }
  else if (event.keyCode == 13)
  {
    event.returnValue = false;
    AcceptOptions ();
  }
}

