Ext.namespace("destination");

destination.record = Ext.data.Record.create(['value', 'text', 'misc']);

destination.locale = null;
destination.init = function(locale) {
	destination.locale = locale;
	Ext.getCmp("popularPlace").on("select", destination.onPopularPlaceChange);
	Ext.getCmp("request.countryId").on("select", destination.onCountryChange);
	Ext.each(Ext.select('.switch-mode-link'), function(link) {
		link.on("click", destination.switchMode);
	});
}

destination.init2 = function(locale, cityId) {
	destination.locale = locale;
	var coCmp = Ext.getCmp("person.countryId");
	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], 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 (callback != undefined) {
				callback();
			}
			if (success) {
				cmp.store.add(r);
				cmp.setValue(Ext.isDefined(defaultSelection) ? defaultSelection : -1);
			}
		}
	});
	
}

destination.onPopularPlaceChange = function(cmp) {
	var cityCountryPair = cmp.getValue();
	if (cityCountryPair != -1) {
		var cityId = cityCountryPair.split("#")[0]; 
		var countryId = cityCountryPair.split("#")[1]; 
		if (destination.isSuggestActive()) {
			var sCmp = Ext.getCmp("citySuggestion");
			sCmp.store.removeAll();
			destination.load(sCmp, accommodationBookingService.getCityJSON, [cityId, destination.locale], cityId);
		} else {
			var coCmp = Ext.getCmp("request.countryId");
			coCmp.setValue(countryId);
			coCmp.fireEvent("select", coCmp, null, null, cityId);
		}
	}
	cmp.setValue(-1);
}

destination.isSuggestActive = function(cmp) {
	return Ext.get('manualMode').hasClass("x-hidden");
}

destination.switchMode = function() {
	var manualMode = Ext.get('manualMode');
	var suggestMode = Ext.get('suggestMode');

	var ciCmp = Ext.getCmp("citySelect");
	var sCmp = Ext.getCmp("citySuggestion");
	var coCmp = Ext.getCmp("request.countryId");
	
	if (!destination.isSuggestActive()) {
		var cityId = ciCmp.getValue();
		if (cityId != "" && cityId != -1) {
			sCmp.store.removeAll();
			destination.load(sCmp, accommodationBookingService.getCityJSON, [cityId, destination.locale], cityId, function() {
				ciCmp.disable();
				coCmp.disable();
				sCmp.enable();
				manualMode.addClass("x-hidden");
				suggestMode.removeClass("x-hidden");
			});
		} else {
			ciCmp.disable();
			coCmp.disable();
			sCmp.enable();
			manualMode.addClass("x-hidden");
			suggestMode.removeClass("x-hidden");
		}
	} else {
		var sCmp = Ext.getCmp("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 {
			ciCmp.enable();
			coCmp.enable();
			sCmp.disable();
			suggestMode.addClass("x-hidden");
			manualMode.removeClass("x-hidden");
		}
	}	
}