function Destinations()
{
	//-Properties---------------------------------------------------------------------------------------------------------

	/**
	 * @property
	 * @type (Array) 
	 */
	this.destinations = null;

	/**
	 * @property
	 * @type (DestinationDropdown) 
	 */
	this.dropdown = null;

	/**
	* @property
	* @type (Array) 
	*/
	this.arrivalsData = null;

	/**
	* @property
	* @type (Array) 
	*/
	this.departuresData = null;

	/**
	 * @property
	 * @type (DOM-Element) 
	 */
	this.tableArrivalsView = null;

	/**
	* @property
	* @type (DOM-Element) 
	*/
	this.tableDeparturesView = null;
	
	/**
	 * @property
	 * @type (DOM-Element) 
	 */
	this.bookArrivalsButton = null;


	/**
	* @property
	* @type (DOM-Element) 
	*/
	this.bookDeparturesButton = null;
	
	/**
	 * @property
	 * @type (Weekdays) 
	 */
	this.weekDays = null;
	
	//-Private Methods---------------------------------------------------------------------------------------------------------
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.initialize = function(){
		this.destinations = [];
		this.loadDestinations();
		this.loadArrivalsData();
		this.loadDeparturesData();
		this.tableArrivalsView = $('#arrivalsTable');
		this.tableDeparturesView = $('#departuresTable');
		this.bookArrivalsButton = $('#btnArrivalsBook');
		this.bookDeparturesButton = $('#btnDeparturesBook');
		this.weekdays = new Weekdays();
	};
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.loadDestinations = function() {
	    var service = new Service();
	    service.url = DestinationConfig.DESTINATION_DATA_URL;
	    service.format = DestinationConfig.DESTINATION_DATA_FORMAT;
	    service.resultCallback = $.makeCallback(this, this.processDestinations);
	    //service.faultCallback = function(error) { console.log(error) };
	    service.faultCallback = function(error) { };
	    service.send();
	};
	
	/**
	 * @method
	 * @private 
	 */	
	Destinations.prototype.processDestinations = function(data){		
		var destinationsData = $(data).find("Destination");
		
		this.dropDown = new DestinationDropdown(destinationsData);
		this.dropDown.create($('div.europaKarte')); 
		this.dropDown.bind(DestinationEvents.DESTINATION_CHANGED, $.makeCallback(this,this.handleDropdownChanged));
		
		var self = this;
		$.each(destinationsData,function(index, destinationData){
			var destination =  new Destination($(destinationData));
			destination.create($('div.europaKarte'));			
			destination.bind(DestinationEvents.DESTINATION_CHANGED, $.makeCallback(self,self.handleDestinationClicked));
			self.destinations.push(destination);
		});
	};	
	
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.selectDestination = function(id) {
	    this.clearFlightsTable();
	    this.updateArrivalsTable(this.getArrivalsForDestination(id));
	    this.updateDeparturesTable(this.getDeparturesForDestination(id));
	    $.each(this.destinations, function(index, destination) {
	        destination.unselect();
	        if (destination.id == id) {
	            destination.select();
	        }
	    });
	};
	

	/**
	* @method
	* @private 
	*/
	Destinations.prototype.getSelectedDestination = function() {
	    var selected = null;
	    $.each(this.destinations, function(index, destination) {
	        if (destination.selected == true) {
	            selected = destination;
	            return false;
	        }
	    });
	    return selected;
	};
	
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.loadArrivalsData = function() {
	    var service = new Service();
	    service.url = DestinationConfig.ARRIVAL_DATA_URL;
	    service.format = DestinationConfig.ARRIVAL_DATA_FORMAT;
	    service.resultCallback = $.makeCallback(this, this.processArrivalsData);
	    //service.faultCallback = function(error) { console.log(error) };
	    service.faultCallback = function(error) { };
	    service.send();
	};

	/**
	* @method
	* @private 
	*/
	Destinations.prototype.loadDeparturesData = function() {
	    var service = new Service();
	    service.url = DestinationConfig.DEPARTURES_DATA_URL;
	    service.format = DestinationConfig.DEPARTURES_DATA_FORMAT;
	    service.resultCallback = $.makeCallback(this, this.processDeparturesData);
	    //service.faultCallback = function(error) { console.log(error) };
	    service.faultCallback = function(error) { };
	    service.send();
	};
	
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.processArrivalsData = function(data) {
	    this.arrivalsData = $(data).find('flight_timetable');
	};

	/**
	* @method
	* @private 
	*/
	Destinations.prototype.processDeparturesData = function(data) {
	    this.departuresData = $(data).find('flight_timetable');
	};
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.getDeparturesForDestination = function(id) {
	    var flights = [];
	    $.each(this.departuresData, function(index, flight) {
	        if ($(flight).find('To').text() == id) {
	            flights.push(flight);
	        }
	    });
	    return flights;
	};


	/**
	* @method
	* @private 
	*/
	Destinations.prototype.getArrivalsForDestination = function(id) {
	    var flights = [];
	    $.each(this.arrivalsData, function(index, flight) {
	        if ($(flight).find('From').text() == id) {
	            flights.push(flight);
	        }
	    });
	    return flights;
	};
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.updateArrivalsTable = function(flights) {
	    var url = '';
	    var self = this;
	    $.each(flights, function(index, flight) {
	        self.createTableRow(flight, self.tableArrivalsView, 'From');
	        if (index == 0) {
	            if (app.lang == 'de') {
	                url = $(flight).find('link').text();
	            } else if (app.lang == 'fr') {
	                url = $(flight).find('link_FR').text();
	            } else {
	                url = $(flight).find('link_EN').text();
	            }                
                //url = app.lang == 'de' ? $(flight).find('link').text() : $(flight).find('link_EN').text();
	        }
	    });

	    this.updateBookArrivalsButton(url);
	};

	/**
	* @method
	* @private 
	*/
	Destinations.prototype.updateDeparturesTable = function(flights) {
	    var url = '';
	    var self = this;
	    $.each(flights, function(index, flight) {
	        self.createTableRow(flight, self.tableDeparturesView, 'To');
	        if (index == 0) {
	            if (app.lang == 'de') {
	                url = $(flight).find('link').text();
	            } else if (app.lang == 'fr') {
	                url = $(flight).find('link_FR').text();
	            } else {
	                url = $(flight).find('link_EN').text();
	            }  
                
                //url = app.lang == 'de' ? $(flight).find('link').text() : $(flight).find('link_EN').text();
	        }
	    });

	    this.updateBookDeparturesButton(url);
	};
	
	/**
	 * @method
	 * @private 
	 */	
	Destinations.prototype.clearFlightsTable = function(){
	    $('tr:gt(0)', this.tableArrivalsView).remove();
	    $('tr:gt(0)', this.tableDeparturesView).remove();
	};

	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.createTableRow = function (flight, view, direction) {

	    var row = $('<tr></tr>');
	    var days = '';
	    var rawDays = $(flight).find('Day').text().split(',');
	    var self = this;

	    var target = '';

	    if (direction == "From") {
	        if (app.lang == 'de') {
	            target = $(flight).find('FromAirport_de').text();
	        } else if (app.lang == 'fr') {
	            target = $(flight).find('FromAirport_fr').text();
	        } else {
	            target = $(flight).find('FromAirport_en').text();
	        }
	        //target = app.lang == 'de' ? $(flight).find('FromAirport_de').text() : $(flight).find('FromAirport_en').text();
	    } else {
	        if (app.lang == 'de') {
	            target = $(flight).find('ToAirport_de').text();
	        } else if (app.lang == 'fr') {
	            target = $(flight).find('ToAirport_fr').text();
	        } else {
	            target = $(flight).find('ToAirport_en').text();
	        }
	        //target = app.lang == 'de' ? $(flight).find('ToAirport_de').text() : $(flight).find('ToAirport_en').text();
	    }

	    var info;

	    if (app.lang == 'de') {
	        info = $(flight).find('Info').text();
	    } else if (app.lang == 'fr') {
	        info = $(flight).find('Info_fr').text();
	    } else {
	        info = $(flight).find('Info_gb').text();
	    }

	    //var info = app.lang == 'de' ? $(flight).find('Info').text() : $(flight).find('Info_gb').text();

	    $.each(rawDays, function (index, rawDay) {
	        days += (app.lang == 'en') ? rawDay + ' ' : self.weekdays.getDayForIndex(self.weekdays.getIndexForDay(rawDay, 'en'), app.lang) + ' ';
	    });

	    //Flight_number
	    row.append($('<td></td>').text($(flight).find('Flight_number').text()));
	    //To
	    //row.append($('<td></td>').text($(flight).find(direction).text()));
	    row.append($('<td></td>').text(target));
	    //Day
	    row.append($('<td class="upper"></td>').text(days));
	    //Departure
	    row.append($('<td></td>').text($(flight).find('Departure').text()));
	    //Arrival
	    row.append($('<td></td>').text($(flight).find('Arrival').text()));
	    //Stop
	    row.append($('<td></td>').text($(flight).find('Stop').text()));
	    //Info
	    row.append($('<td></td>').text(info));


	    view.append(row);
	}; 
    
	
	
	/**
	 * @method
	 * @private 
	 */	
	Destinations.prototype.updateBookArrivalsButton = function(url){
		this.bookArrivalsButton.attr('href', url);
    };
    
    /**
    * @method
    * @private 
    */
    Destinations.prototype.updateBookDeparturesButton = function(url) {
        this.bookDeparturesButton.attr('href', url);
    };
	
	
	//-Event Handler-----------------------------------------------------------------------------------------------------------
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.handleDropdownChanged = function(event, id) {
	    this.selectDestination(id);
	};
	
	/**
	 * @method
	 * @private 
	 */
	Destinations.prototype.handleDestinationClicked  = function(event,id){
		this.dropDown.setValue(id);	
		this.selectDestination(id);			
	};
	
	//-Public------------------------------------------------------------------------------------------------------------------
	
}

jQuery(document).ready(function()
{
	new Destinations().initialize();
});
