var DestinationCollection = Class.create
(
	{
		initialize: function()
		{
			this.travelinsurance		= false;
			this.cancellationinsurance	= false;
			
			this.destination_choices	= new Array();
			this.destination_choices[0]	= null;
			this.destination_choices[1]	= null;
			
			this.destinations 			= new Array();
		},
		
		init: function(travel_1, travel_2, travelinsurance, cancellationinsurance)
		{
			this.travelinsurance		= travelinsurance;
			this.cancellationinsurance	= cancellationinsurance;
			
			if (travel_1 || travel_2)
			{
				if(travel_1) { this.buildSelect(1, travel_1); }
				if(travel_2) { this.buildSelect(2, travel_2); } else { this.buildSelect(2); }
			}
			else
			{
				this.buildSelect(1);
				this.buildSelect(2);
			}
			
			this.writeData();
		},
		
		buildSelect: function(choice, init)
		{
			// element unlocken en het eerste element toevoegen
			
			if ($('destination' + choice))
			{
				$('destination' + choice).disabled = false;
				$('destination' + choice).options[0] = new Option('Maak een keuze');
				$('destination' + choice).stopObserving('change', this.changeDestination.bindAsEventListener(this, choice));
				$('destination' + choice).observe('change', this.changeDestination.bindAsEventListener(this, choice));
			}
			
			var i = 1; this.destinations.each(function(destination)
			{
				var select = false;
				
				// als we een init hebben is er een bestemming gekozen/geselecteerd
				if (init)
				{
					destination.travels.each(function(travel){ select = (travel.id == init) ? true : select; });
				}
				// geen init, dus geen bestemming geselecteerd, we resetten de boel
				else
				{
					this.destination_choices[choice - 1] = null;
				}
				
				if ($('destination' + choice))
				{
					$('destination' + choice).options[i] = new Option(destination.country +' - '+ destination.city, destination.id);
				}
				
				if (select)
				{
					// bestemming opslaan in de cache
					this.destination_choices[choice - 1] = Object.clone(destination);
					
					if ($('destination' + choice))
					{
						// gekozen bestemming selecteren
						$('destination' + choice).options[i].selected = true;
						
						// observe's inregelen en element enable'n
						$('travel' + choice).disabled = false;
						$('travel' + choice).options[0] = new Option('Maak een keuze');
						$('travel' + choice).stopObserving('change', this.changeTravel.bindAsEventListener(this, choice));
						$('travel' + choice).observe('change', this.changeTravel.bindAsEventListener(this, choice));
					}
					var j = 1; destination.travels.each(function(travel)
					{
						if ($('destination' + choice))
						{
							$('travel' + choice).options[j] = new Option('van ' + travel.date_from +' tot '+ travel.date_till, travel.id);
						}
						
						if (travel.id == init)
						{
							if ($('destination' + choice))
							{
								$('travel' + choice).options[j].selected = true;
							}
							this.destination_choices[choice - 1].resetTravels();
							this.destination_choices[choice - 1].addTravel(travel);
						}
						
						j++;
					}.bind(this));
				}
				
				i++;
			}.bind(this));
			
			if ($('destination' + choice))
			{
				this.disableTravels();
			}
		},
		
		addDestination: function(destination)
		{
			this.destinations[this.destinations.length] = destination;
		},
		
		changeDestination: function(event, choice)
		{
			if (event.element().value > 0)
			{
				this.destinations.each(function(destination)
				{
					if (event.element().value == destination.id)
					{
						this.clearTravels(choice);
						
						$('travel' + choice).disabled = false;
						$('travel' + choice).options[0] = new Option('Maak een keuze');
						$('travel' + choice).stopObserving('change', this.changeTravel.bindAsEventListener(this, choice));
						$('travel' + choice).observe('change', this.changeTravel.bindAsEventListener(this, choice));
						
						this.destination_choices[choice - 1] = Object.clone(destination);
						this.destination_choices[choice - 1].resetTravels();
						
						var i = 1; destination.travels.each(function(travel) {
							$('travel' + choice).options[i++] = new Option('van ' + travel.date_from +' tot '+ travel.date_till, travel.id);
						}.bind(this));
					}
				}.bind(this));
			}
			else
			{
				this.clearTravels(choice);
				this.destination_choices[choice - 1]= null;
				$('travel' + choice).disabled 		= true;
			}
			
			this.initData(choice);
			this.disableTravels();
		},
		
		changeTravel: function(event, choice)
		{
			// keuze loggen en writeData uitvoeren
			if (event.element().value > 0)
			{
				var selected_destination	= null;
				var selected_travel 		= null;
				
				this.destinations.each(function(destination)
				{						
					destination.travels.each(function(travel)
					{
						if (travel.id == event.element().value)
						{
							selected_destination	= destination;
							selected_travel 		= travel;
						}
					}.bind(this));
				}.bind(this));
				
				// er wordt alleen een andere keuze gemaakt, we moeten de choice storen
				
				if (selected_travel != null)
				{
					this.destination_choices[choice - 1] = Object.clone(selected_destination);
					
					this.destination_choices[choice - 1].resetTravels();
					this.destination_choices[choice - 1].addTravel(selected_travel);
				
					this.writeData(choice);
				}
				else
				{
					this.destination_choices[choicev].resetTravels();
					this.initData(choice);
				}
			}
			else
			{
				this.destination_choices[choice - 1].resetTravels();
				this.initData(choice);
			}
			
			this.disableTravels();
		},
		
		writeData: function()
		{
			var labels = new Array();
			
			this.destination_choices.each(function(destination, index)
			{
				var choice = index + 1;
				var travel = null;
				
				if (destination)
				{
					destination.travels.each(function(choosen_travel) { travel = choosen_travel; });
					
					if (travel != null)
					{
						var totalprice = 0;
						
						$('destination_travel_' + choice).innerHTML	= '<b>' + destination.city +'</b><br /><span style="padding-bottom:10px">'+ destination.country + '</span><br />Van ' + travel.date_from + ' tot ' + travel.date_till;
						
						$('price_travel_' + choice).innerHTML 		= this.formatPrice(travel.price);
						totalprice = totalprice + travel.price;
						
						if (this.travelinsurance || this.cancellationinsurance)
						{
							totalprice = totalprice + travel.policycosts;
							$('price_policycosts_' + choice).innerHTML	= this.formatPrice(travel.policycosts);
						}
						else
						{
							$('price_policycosts_' + choice).innerHTML	= '0,00';
						}
						
						if (this.travelinsurance)
						{
							totalprice = totalprice + travel.travelinsurance;
							$('price_travelinsurance_' + choice).innerHTML	= this.formatPrice(travel.travelinsurance);
						}
						else
						{
							$('price_travelinsurance_' + choice).innerHTML	= '0,00';
						}
						
						if (this.cancellationinsurance)
						{
							totalprice = totalprice + travel.cancellationinsurance;
							$('price_cancellationinsurance_' + choice).innerHTML	= this.formatPrice(travel.cancellationinsurance);
							
							// 
							
							var price_insuranceload = (travel.cancellationinsurance + travel.policycosts) * 0.075
								totalprice 			= totalprice + price_insuranceload;
							
							$('price_insuranceload_' + choice).innerHTML	= this.formatPrice(price_insuranceload);
						}
						else
						{
							$('price_cancellationinsurance_' + choice).innerHTML	= '0,00';
							$('price_insuranceload_' + choice).innerHTML			= '0,00';
						}
						
						$('price_total_' + choice).innerHTML 		= this.formatPrice(totalprice);
						
						labels[labels.length] = destination.type;
					}
					else
					{
						this.initData(choice);
					}
				}
				else
				{
					if (choice == 1 || choice == 2)
						this.initData(choice);
				}
			}.bind(this));
			
			if ($('types')) { $('types').value = labels.join(','); }
		},
		
		formatPrice: function (price)
		{
			price 		= price + '';
			
			if (price.include('.'))
			{
				price		= Math.round(price*100)/100;
				price 		= price + '';
				
				var array	= price.split('.');
				
				array[1]	= (array[1].length == 1) ? array[1] + '0' : array[1];
				price 		= array[0] + ',' + array[1];
			}
			else
			{
				price = price + ',00';
			}
			
			return price;
		},
		
		initData: function(choice)
		{
			if ($('destination_travel_' + choice))
			{
				$('destination_travel_' + choice).innerHTML 			= '<em>nog geen keuze gemaakt...</em>';
				
				$('price_travel_' + choice).innerHTML 					= '0,00';
				$('price_policycosts_' + choice).innerHTML 				= '0,00';
				$('price_travelinsurance_' + choice).innerHTML 			= '0,00';
				$('price_cancellationinsurance_' + choice).innerHTML 	= '0,00';
				$('price_insuranceload_' + choice).innerHTML 			= '0,00';
				$('price_total_' + choice).innerHTML 					= '0,00';
			}
		},
		
		clearTravels: function(choice)
		{
			var options_length = $('travel' + choice).options.length;
			
			for	(var i = 0; i < options_length; i++)
			{
				$('travel' + choice).remove(0);
			}
		},
		
		disableTravels: function()
		{
			var options_length_1 = $('travel1').options.length;
			var options_length_2 = ($('travel2')) ? $('travel2').options.length : 0;
			
			var destination_1 = this.destination_choices[0];
			var destination_2 = this.destination_choices[1];
			
			if (destination_1 != null && destination_2 != null)
			{
				if (destination_1.id == destination_2.id)
				{
					for	(var i = 0; i < $('travel1').options.length; i++)
					{
						//$('travel1').options[i].disabled = (destination_2.travels[0] && $('travel1').options[i].value == destination_2.travels[0].id) ? true : false;
					}
					
					for	(var i = 0; i < $('travel2').options.length; i++)
					{
						//$('travel2').options[i].disabled = (destination_1.travels[0] && $('travel2').options[i].value == destination_1.travels[0].id) ? true : false;
					}
				}
			}
		},
		
		setTravelInsurance: function(obj)
		{
			this.travelinsurance		= (obj.value == 'ja') ? true : false;
			
			this.writeData();
		},
		
		setCancellationInsurance: function(obj)
		{
			this.cancellationinsurance	= (obj.value == 'ja') ? true : false;
			
			this.writeData();
		}
	}
);

var Destination = Class.create
(
	{
		initialize: function(id, type, country, city, title)
		{
			this.selected	= null;			// when this travel will be booked this var must be set with a value of 1 or 2 
			
			this.id			= id;
			
			this.type		= type;
			
			this.country	= country;
			this.city		= city;
			this.title		= title;
			
			this.travels	= new Array();
		},
		
		addTravel: function(travel)
		{
			this.travels[this.travels.length] = travel;
		},
		
		resetTravels: function()
		{
			this.travels = new Array();
		}
	}
);

var Travel = Class.create
(
	{
		initialize: function(id, age_from, age_till, date_from, date_till, boys, girls, price, policycosts, travelinsurance, cancellationinsurance)
		{
			this.selected				= null;			// when this travel will be booked this var must be set with a value of 1 or 2 
			
			this.id						= id;
			
			this.age_from				= age_from;
			this.age_till				= age_till;
			
			this.date_from				= date_from;
			this.date_till				= date_till;
			
			this.boys					= boys;
			this.girls					= girls;
			
			this.price					= price;
			
			this.policycosts			= policycosts;
			this.travelinsurance		= travelinsurance;
			this.cancellationinsurance	= cancellationinsurance;
		}
	}
);
