Ext.namespace("destination");

destination.init2 = function(locale, withRestrictions, cityId) {
	destination.locale = locale;
	destination.withRestrictions = withRestrictions;
	var coCmp = Ext.getCmp("person.countryId");
	if(coCmp){
		coCmp.on("select", destination.onCountryChange);
		if (cityId != undefined) {
			coCmp.fireEvent("select", coCmp, null, null, cityId);
		}
	}
}

destination.onCountryChange = function(cmp, record, index, cityId, callback) {
	var ciCmp = Ext.getCmp("citySelect");
	ciCmp.store.removeAll();
	
	var countryId = cmp.getValue();
	if (countryId != -1) {
		destination.load(ciCmp, accommodationBookingService.getCityOptionsJSON, [countryId, destination.locale, destination.withRestrictions], cityId, callback);
	} else {
		ciCmp.setValue("");
	}
}

destination.load = function(cmp, dwrFunction, dwrArgs, defaultSelection, callback) {
	var store = new Ext.data.Store({
		proxy: new Ext.ux.data.DwrProxy({
			apiActionToHandlerMap : {
				read : {
					dwrFunction : dwrFunction,
					getDwrArgsFunction : function(trans) {
						return dwrArgs;
					}
				}
			}
		}),
		reader: new Ext.data.JsonReader({
			root : 'rows',
			fields : [
				{name: 'value'},
				{name: 'text'},
				{name: 'misc'}
			]
		})
	});
	store.load({
		callback: function(r, success) {
			if (success) {
				cmp.store.add(r);
				if (Ext.isDefined(defaultSelection) && cmp.store.findExact("value", defaultSelection + "") != -1) {
					cmp.setValue(defaultSelection);
				} else {
					cmp.setValue(-1);
				}
			}
			if (callback != undefined) {
				callback();
			}
		}
	});
	
},

destination.hasLocation = null;

destination.setHasLocation = function(hasLocation) {
	destination.hasLocation = hasLocation;
}

IDestinations = Class.create();

IDestinations.prototype = {

	initialize : function(anId, options) {
		this.id = anId;
		this.setOptions(options);
		var _this = this;
		Ext.getCmp(this.options.popularPlace).on("select", function(cmp) {_this.onPopularPlaceChange(cmp)});
		var ppRadioCnt = $('popularPlaceRadios');
		if (ppRadioCnt) {
			ppRadioCnt.select('input').each(function(radio) {
				Event.observe(radio, "click", function() {
						_this.onPopularPlaceClick(radio);
				});
			});
		}
		Ext.getCmp(this.options.countryId).on("select", function(cmp, record, index, cityId, callback) {_this.onCountryChange(cmp, record, index, cityId, callback)});
		
		Ext.getCmp(this.options.citySelect).on("select", function(ciCmp, record, index, cityId, callback, countryChanged) {_this.onCityChange(ciCmp, record, index, cityId, callback, countryChanged)});
		Ext.getCmp(this.options.citySuggestion).on("select", function(ciCmp, record, index, cityId, callback, countryChanged) {_this.onCityChange(ciCmp, record, index, cityId, callback, countryChanged)});

		Ext.each(Ext.select(this.options.switchLink), function(link) {
			link.on("click", function() {_this.switchMode()});
		});
	},

	setOptions : function(options) {
		var defaultOptions = {
				switchLink : ".switch-mode-link",
				popularPlace : "popularPlace",
				citySelect : "citySelect",
				citySuggestion : "citySuggestion",
				countryId : "request.countryId",
				manualMode : "manualMode",
				suggestMode : "suggestMode",
				withRestrictions : false
		};
		this.options = Object.extend(defaultOptions, options || {});
	},

	onCountryChange : function(cmp, record, index, cityId, callback) {
		var ciCmp = Ext.getCmp(this.options.citySelect);
		ciCmp.store.removeAll();
		
		var countryId = cmp.getValue();
		if (countryId != -1) {
			destination.load(ciCmp, accommodationBookingService.getCityOptionsJSON, [countryId, this.options.locale, this.options.withRestrictions], cityId, callback);
			if(destination.hasLocation){
				ciCmp.fireEvent("select", ciCmp, record, index, cityId, null, true);
			}
		} else {
			ciCmp.setValue("");
		}
	},
	
	onCityChange : function(ciCmp, record, index, cityId, callback, countryChanged) {
		if(cityId == null){
			cityId = ciCmp.getValue();
		}
		
		var ppRadioCnt = $('popularPlaceRadios');
		if (ppRadioCnt) {
			var ppRadios = ppRadioCnt.select('input');
			ppRadios.each(function(elem) {
				if (elem.value.split("#")[0] == cityId) {
					elem.checked = true;
				} else {
					elem.checked = false;
				}
			});
		}
		
		if(destination.hasLocation){
			var deCmp = Ext.getCmp(this.options.destinationSelect);
			var previousValue = deCmp.getValue();
			deCmp.store.removeAll();
			
			if (cityId != -1) {
				destination.load(deCmp, accommodationBookingService.getAirportHotelOptionsJSON, [cityId, this.options.locale], previousValue, callback);
			} else {
				deCmp.setValue("");
			}
			if (!countryChanged && this.options.alterManualMode != undefined) {
				if (this.isAlterSuggestActive()) {
					var aSCmp = Ext.getCmp(this.options.alterCitySuggestion);
					if (aSCmp.getValue() == "") {
						aSCmp.store.removeAll();
						destination.load(aSCmp, accommodationBookingService.getCityJSON, [cityId,this.options.locale], cityId);
						
						var aDeCmp = Ext.getCmp(this.options.alterDestinationSelect);
						aDeCmp.store.removeAll();
						destination.load(aDeCmp, accommodationBookingService.getAirportHotelOptionsJSON, [cityId, this.options.locale], null, null);
					}
				} else {
					var aCiCmp = Ext.getCmp(this.options.alterCitySelect);
					var aCityId = aCiCmp.getValue();
					if (aCityId == "" || aCityId == "-1") {
						if (this.isSuggestActive()) {
							Ext.getCmp(this.options.citySuggestion).store.data.items.each(function(item) {
								if (item.json.value == cityId) {
									countryId = item.json.misc; 
								}
							});
						} else {
							var coCmp = Ext.getCmp(this.options.countryId);
							countryId = coCmp.getValue();
						}
						var aCoCmp = Ext.getCmp(this.options.alterCountryId);
						aCoCmp.setValue(countryId);
						aCoCmp.fireEvent("select", aCoCmp, null, null, cityId);
					}
				}
			}
		}
	},

	onPopularPlaceClick : function(radio) {
		var cityCountryPair = radio.value;
		if (cityCountryPair != -1) {
			this.modifyPopular(cityCountryPair);
		}
		//radio.checked = false;
		//cmp.setValue(-1);
	},
	
	onPopularPlaceChange : function(cmp) {
		var cityCountryPair = cmp.getValue();
		if (cityCountryPair != -1) {
			this.modifyPopular(cityCountryPair);
		}
		cmp.setValue(-1);
	},
	
	modifyPopular : function(cityCountryPair){
			var cityId = cityCountryPair.split("#")[0]; 
			var countryId = cityCountryPair.split("#")[1]; 
			if (this.isSuggestActive()) {
				var sCmp = Ext.getCmp(this.options.citySuggestion);
				sCmp.store.removeAll();
				destination.load(sCmp, accommodationBookingService.getCityJSON, [cityId,this.options.locale], cityId);
				
				if(destination.hasLocation){
					var deCmp = Ext.getCmp(this.options.destinationSelect);
					deCmp.store.removeAll();
					destination.load(deCmp, accommodationBookingService.getAirportHotelOptionsJSON, [cityId, this.options.locale], null, null);
				}
				
			} else {
				var coCmp = Ext.getCmp(this.options.countryId);
				coCmp.setValue(countryId);
				coCmp.fireEvent("select", coCmp, null, null, cityId);
			}
	},

	isSuggestActive : function() {
		return Ext.get(this.options.manualMode).hasClass("x-hidden");
	},
	
	isAlterSuggestActive : function() {
		return Ext.get(this.options.alterManualMode).hasClass("x-hidden");
	},

	switchMode : function() {
		var manualMode = Ext.get(this.options.manualMode);
		var suggestMode = Ext.get(this.options.suggestMode);

		var ciCmp = Ext.getCmp(this.options.citySelect);
		var sCmp = Ext.getCmp(this.options.citySuggestion);
		var coCmp = Ext.getCmp(this.options.countryId);
		
		if (!this.isSuggestActive()) {
			var cityId = ciCmp.getValue();
			
			if (cityId != "" && cityId != -1) {
				sCmp.store.removeAll();
				destination.load(sCmp, accommodationBookingService.getCityJSON, [cityId, this.options.locale], cityId, function() {
					ciCmp.disable();
					coCmp.disable();
					sCmp.enable();
					manualMode.addClass("x-hidden");
					suggestMode.removeClass("x-hidden");
				});
			} else {
				coCmp.setValue("");
				sCmp.setValue("");
				sCmp.store.removeAll();
				ciCmp.disable();
				coCmp.disable();
				sCmp.enable();
				manualMode.addClass("x-hidden");
				suggestMode.removeClass("x-hidden");
			}
		} else {
			var sCmp = Ext.getCmp(this.options.citySuggestion);
			var cityId = sCmp.getValue();
			if (cityId != "" && cityId != -1) {
				var countryId = sCmp.store.getAt(sCmp.store.find("value", cityId)).get("misc");
				coCmp.setValue(countryId);
				coCmp.fireEvent("select", coCmp, null, null, cityId, function() {
					ciCmp.enable();
					coCmp.enable();
					sCmp.disable();
					suggestMode.addClass("x-hidden");
					manualMode.removeClass("x-hidden");
				});
			} else {
				coCmp.setValue("");
				ciCmp.setValue("");
				ciCmp.store.removeAll();
				ciCmp.enable();
				coCmp.enable();
				sCmp.disable();
				suggestMode.addClass("x-hidden");
				manualMode.removeClass("x-hidden");
			}
		}	
	}
};

