﻿var departure = '';
	var list = $('#list_pdf');
    $(document).ready(function() {
		$('#cities_start').flexbox('request.php?loc='+_location+'&action=getcities', {
			watermark : "Ville de départ",
			autoCompleteFirstMatch: false,
			highlightMatches: false,
			onSelect: function () {
				list.html('');
				var departure = this.getAttribute('hiddenValue');
				$('#cities_end').html('');
				$.getJSON('request.php?loc='+_location+'&action=getdest&departure='+encodeURIComponent(departure), {id:departure}, function(data) {
             		$('#cities_end').flexbox( 'request.php?loc='+_location+'&action=getdest&departure='+encodeURIComponent(departure), {
						watermark : "Ville d'arrivée",
						autoCompleteFirstMatch: false,
						highlightMatches: false,
						onSelect: function () {
							$.getJSON('request.php?loc='+_location+'&action=getlines&&d='+encodeURIComponent(departure)+'&a='+encodeURIComponent(this.getAttribute('hiddenValue')), {id:this.getAttribute('hiddenValue')}, function(data) {
								data = data.results;
								list.html('');
								for (i=0;i<data.length;i++)
								{
									var cities = '';
									for (j=0;j<data[i].cities.length;j++)
										cities += data[i].cities[j].name + ' - ';
									cities = cities.substr(0, cities.length-3);

									var li = $('<li/>');
									var a;
									if (data[i].disponibility == false)
									{
										a = $('<div/>') //'<a/>')
										.addClass('link')
										.html( (data[i].name?data[i].name:'') + ' <span class="error">[Indisponible]</span>');
									}
									else
									{
										a = $('<a/>')
										.attr('href', data[i].pdf)
										.attr('target', '_blank')
										.text(data[i].name?data[i].name:'');
									}

									a.append($('<div/>').addClass('cities').text(cities));
									li.append(a);
									list.append(li);
								}
							});
						}
					});
				});

			}
		});
		$('#cities_end').flexbox('request.php', {
			watermark : "Ville d'arrivée"
		});
     });  
