// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function go_to(url) {
  document.location.href = url;
}

function init_form(){
	$('search_form').onsubmit = function() { return submit_form(); }
	$('flight-no').observe('click', function(event){show_flight_no();})
	$('route').observe('click', function(event){show_route();})
	if ($('route').getValue() == 'route'){ show_route(); }
}

function submit_form() {
	$('search_form').request({
		requestHeaders: {Accept: 'application/json'},
		onCreate: function(transport) {
			$('not_found').hide();
			$('search_form').hide();
			$('loader_gif').show();
		},
		onSuccess: function(transport) {
			var flights = transport.responseText.evalJSON(true);
			if(flights.length == 1){
				go_to('/show/' + flights[0].flight.id);
			}
			else if (flights.length == 0){
				$('loader_gif').hide();
				$('search_form').show();
				$('not_found').show();
			}
			else {
				var tbody = document.createElement('tbody');
				for (var x = 0; x < flights.length; x++) {
					var flight = flights[x].flight;
					var name = document.createElement('td');
					var route = document.createElement('td');
					var time = document.createElement('td');
					
					var link = document.createElement('a');
					link.setAttribute('href','/show/' + flight.id);
					link.setAttribute('style','text-decoration:none;');
					link.appendChild(document.createTextNode(flight.airline_name + ' ' + flight.number));
					name.appendChild(link);
					
					var link = document.createElement('a');
					link.setAttribute('href','/show/' + flight.id);
					link.setAttribute('style','text-decoration:none;');
					link.appendChild(document.createTextNode(flight.origin + ' -> ' + flight.destination));
					route.appendChild(link);
					
					var link = document.createElement('a');
					link.setAttribute('href','/show/' + flight.id);
					link.setAttribute('style','text-decoration:none;');
					link.appendChild(document.createTextNode(flight.departure_time + ' - '+ flight.arrival_time));
					time.appendChild(link);
					
					var tr = document.createElement('tr');
					tr.setAttribute('class','link');
					tr.setAttribute('onclick',"go_to('/show/" + flight.id + "');");
					tr.appendChild(name);
					tr.appendChild(route);
					tr.appendChild(time);

					tbody.appendChild(tr);
				}
				var table = document.createElement('table');
				table.setAttribute('cellpadding','0');
				table.setAttribute('cellspacing','0');
				table.appendChild(tbody);
				
				$('loader_gif').hide();
				$('form_holder').appendChild(table);
			}
		},
		onFailure: function(transport){
			alert('Error communicating with server');
		}
	});
	return false;	
}

function show_route() {
	$('flight_airline_icao_code').hide();
	$('flight_number').hide();
	$('origin').show();
	$('destination').show();
}

function show_flight_no() {
	$('origin').hide();
	$('destination').hide();
	$('flight_airline_icao_code').show();
	$('flight_number').show();
}

Event.observe(window, 'load', function() { init_form() });

function check_alt_flight(sid, searchid) {
  new Ajax.Request('/check_alt_flights/'+sid+'/'+searchid, {
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(transport){
      var json = transport.responseText.evalJSON();
      if (json[0].count) {
        get_alt_flights(json[0].sid, json[0].searchid, json[0].count);
      } else {
        check_alt_flight.delay(1.0, json[0].sid, json[0].searchid);
      }
    },
    onFailure: function(transport){
      alert('Error communicating with the server, please try reloading the page.');
    }
  });
}

function get_alt_flights(sid, searchid, count) {
  new Ajax.Request('/load_alt_flights/'+sid+'/'+searchid+'/'+count, {
    onSuccess: function(transport){
      $('alt-flights').insert({
        bottom: transport.responseText
      });
      $('loader_gif').hide();
      $('alt-flights').show();
    },
    onFailure: function(transport){
      alert('Error communicating with the server, please try reloading the page.');
    }
  });
}
