
Geo = function(aId) {
	this.id = aId;

	this.fillSelects = function ( options, comment ) {
		$('#'+this.id+' option').remove();
		$('#'+this.id+'').append('<option value="0">-- '+comment+' --</option>');
		if ( options != null && options != undefined ) {
			for (var i = 0; i < options.length; i++ ) {
				$('#'+this.id+'').append('<option value="'+options[i].ID+'">'+options[i].NAME+'</option>');
			}
		}
		$('#'+this.id+' option:first').attr('selected', 'yes');
		$('#'+this.id+'').attr('disabled',false);
	};
	
	this.changeCountry = function ( country ) {
		$('#'+this.id+'city').attr('disabled',true);
		$('#'+this.id+'city').show();
		$('#'+this.id+'line').attr('disabled',true);
		$('#'+this.id+'line').hide();
		$('#'+this.id+'metro').attr('disabled',true);
		$('#'+this.id+'metro').hide();

		$.getJSON ( '/bitrix/php_interface/ExBitrix/public/get_geo.php', {country:country, id:this.id}, function ( data ) {
			var geo = new Geo(data.id+'city');
			geo.fillSelects(data.cities, 'Выберите город');
		});
	};
	
	this.changeCity = function ( city ) {
		$('#'+this.id+'line').attr('disabled',true);
		$('#'+this.id+'line').show();
		$('#'+this.id+'metro').attr('disabled',true);
		$('#'+this.id+'metro').show();

		$.getJSON ( '/bitrix/php_interface/ExBitrix/public/get_geo.php', {city:city, id:this.id}, function ( data ) {
			var geo = new Geo(data.id+'line');
			geo.fillSelects(data.lines, 'Выберите ветку метро');
			geo = new Geo(data.id+'metro');
			geo.fillSelects(data.metros, 'Выберите станцию метро');
		});
	};
	
	this.changeLine = function ( line ) {
		$('#'+this.id+'metro').attr('disabled',true);
		$('#'+this.id+'metro').show();

		$.getJSON ( '/bitrix/php_interface/ExBitrix/public/get_geo.php', {line:line, id:this.id}, function ( data ) {
			geo = new Geo(data.id+'metro');
			geo.fillSelects(data.metros, 'Выберите станцию метро');
		});
	};

}
