// JavaScript Document

function formatDestination(val, inList) {
  		var dest = val.toString().split(";");
  		if(dest[6] == '2') {
  			var str = dest[0] + " " + dest[1] + " " + dest[5] + " (" + dest[2] + ")";
  			return (inList) ? "<img src='http://www.letenky.cz/images/flags/" + dest[4] + ".gif'/> " + str : str;
  		} else if(dest[6] == '1') {
  			var str = dest[0] + " " + dest[1] + " (" + dest[2] + ")";
  			return (inList) ? "<img src='http://www.letenky.cz/images/space.gif'/>" + str : str;
  		} else {
  			var str = dest[0] + " " + dest[5] + " (" + dest[3] + ")";
  			return (inList) ? "<img src='http://www.letenky.cz/images/flags/" + dest[4] + ".gif'/> " + str : str;
  		}
  }
  
  function formatAirport(val, inList) {
  		var dest = val.toString().split(";");
  		var str = dest[2].replace(',',' ');
  		return (inList) ? "<img src='http://www.letenky.cz/images/flags/" + dest[1] + ".gif'/> " + str : str;
  }
  
  function formatCity(val, inList) {
  		var dest = val.toString().split(";");
  		var str = dest[1] + " " + dest[3] + " (" + dest[0] + ")";
  		return (inList) ? "<img src='http://www.letenky.cz/images/flags/" + dest[2] + ".gif'/> " + str : str;
  }
  	
  $(document).ready(function(){
  	
    $("#from_destination_edit").autocomplete("http://www.letenky.cz/whisper/sa-proxy.php?l=cs", {
			  formatItem: function(value, i, n, search) {
			  	return formatAirport(value, true)
			  },
			  formatResult: function(value) {
					return formatAirport(value, false);
			  },
			  matchSubset: false,
			  minChars: 3,
				max: 30,
				//cacheLength: 1,
				width: 300
		});
		
		$("#to_destination_edit").autocomplete("http://www.letenky.cz/whisper/sa-proxy.php?l=cs", {
			  formatItem: function(value, i, n, search) {
			  	return formatAirport(value, true)
			  },
			  formatResult: function(value) {
					return formatAirport(value, false);
			  },
			  matchSubset: false,
			  minChars: 3,
				max: 30,
				//cacheLength: 1,
				width: 300
		});
  	
		$("#arrival_destination_edit").autocomplete("http://www.letenky.cz/whisper/whisper_city.php?lng=cs", {
			  formatItem: function(value, i, n, search) {
			  	return formatCity(value, true)
			  },
			  formatResult: function(value) {
					return formatCity(value, false);
			  },
			  matchSubset: false,
			  minChars: 3,
				max: 30,
				//cacheLength: 1,
				width: 300
		});
		
  });  

