window.addEvent('domready', function() {
	var CountrySelectors = new Class({
		
		COUNTRY_CONSTRAINT_NONE : 0,
		COUNTRY_CONSTRAINT_UNKNOWN : 1,
		COUNTRY_CONSTRAINT_DEPARTURE : 2,
		COUNTRY_CONSTRAINT_DESTINATION : 3,
		countryConstraint : 0,
		leavingSelector : null,
		arrivingSelector : null,
		submitterDiv : null,
		
		initialize: function(selector1, selector2, submitter) {
			try {
				this.countryConstraint = this.COUNTRY_CONSTRAINT_UNKNOWN;
				this.leavingSelector = $(selector1);
				this.arrivingSelector = $(selector2);
				this.submitterDiv = $(submitter);
				this.renderSubmitter();
				if (this.leavingSelector != null && this.arrivingSelector != null) {
					this.leavingSelector.addEvent('change', this.onchange.bindWithEvent(this));
					this.arrivingSelector.addEvent('change', this.onchange.bindWithEvent(this));
				}
			} catch (err) {}
		},		
		onchange: function(evt) {
			var target = evt.target;
			if (this.countryConstraint == this.COUNTRY_CONSTRAINT_UNKNOWN) {
				this.createConstraint(target);
			}

			var args = {};
			if (this.countryConstraint == this.COUNTRY_CONSTRAINT_DEPARTURE && target == this.leavingSelector) {
				args['departure'] = this.leavingSelector.value;
				this.updateSelector(args);
			} else if (this.countryConstraint == this.COUNTRY_CONSTRAINT_DESTINATION && target == this.arrivingSelector) {
				args['destination'] = this.arrivingSelector.value;
				this.updateSelector(args);
			} else {
				this.renderSubmitter();
			}
			this.removeSelectOption(target);
		},		
		createConstraint: function (target) {
			if (target == this.leavingSelector) {
				this.countryConstraint = this.COUNTRY_CONSTRAINT_DEPARTURE;
			} else if (target == this.arrivingSelector) {
				this.countryConstraint = this.COUNTRY_CONSTRAINT_DESTINATION;
			} else {
				this.countryConstraint = this.COUNTRY_CONSTRAINT_UNKNOWN;
			}
		},		
		updateSelector: function(params) {
			new Request.JSON ({
				url: '/json/getConnectedCountries.php',
				onComplete: this.updateSelecterOnComplete.bind(this)				
			}).get(params);
		},		
		updateSelecterOnComplete: function (output) {
			var target = this.countryConstraint == this.COUNTRY_CONSTRAINT_DEPARTURE ? this.arrivingSelector : this.leavingSelector;
			var selectedCountry = target.value;
			target.options.length = 0;
			if (output.length > 0) {
				var selectedIndex = 0;
				if (output.length > 1) {
					for(var i = 0; i < output.length; i++) {
						if (selectedCountry == output[i].id) {
   							selectedIndex = i;
							break;
						}
					}
					if (selectedIndex == 0) target.options.add(new Option('Select', 0));
				}
				for(var i = 0; i < output.length; i++) {
					target.options.add(new Option(output[i].name, output[i].id));
   				}
   				if (selectedIndex>0) {
   					target.options[selectedIndex].selected = true;
   				}
			}
			this.renderSubmitter();
		},		
		removeSelectOption: function(elm) {
			if (elm.options.length> 0 && elm.options[0].value == 0) {
				elm.remove(0);
			}
			if (elm.value != -1) {
				for(var i = 0; i < elm.options.length; i++) {
					if (elm.options[i].value == -1) {
						elm.remove(i);
						break;
					}
				}
			}
		},
		canSubmit: function() {
			var select1 = this.arrivingSelector.value > 0;
			var select2 = this.leavingSelector.value > 0;
			return select1 && select2;
		},
		renderSubmitter: function() {
			if (this.canSubmit()) {
				easeInElement(this.submitterDiv);
			} else {
				hideElement(this.submitterDiv);
			}
		}
	
	});
	
	new CountrySelectors('leavingCountryFind', 'arrivalCountryFind', 'submitForm');
	
	$$('img.calendarTrigger').addEvents({
		'click' : function(evt) {
			evt.stopPropagation();
			var id = evt.target.id;
			var idLength = id.length;
			var target = id.substring(0, idLength-3);
			$(target).focus();
		}
	});
	
	$$('input.text-date').addEvents({
		'focus': function(evt) {
			this.select();
			lcs(this);
		},
		'click': function(evt) {
			evt.stopPropagation();
			this.select();
			lcs(this);
		}
	});
	
//	var returnField = $('return');
//	if (returnField) {
//		returnField.addEvent('click', function(evt) {
//			evt.stopPropagation();
//			if (evt.target.checked) {
//				easeInElement('returnDateSection');
//			} else {
//				hideElement('returnDateSection');
//			}
//		});
//	}
//	hideElement('returnDateSection');
});