(function($) {



	function geo(options) {

		

		this.autoinit = false,

		

		this.options = {

		

			country: {

				

				value: null,

				

				object_id: null

				

			},

			

			region: {

				

				value: null,

				

				object_id: null

				

			},

			

			city: {

				

				value: null,

				

				object_id: null

				

			}

		

		},

		

		this.initialize(options)

		

	}



	$.geo = function(options) {

		

    return new geo(options);

    

  };



	geo.prototype = {

		

		initialize: function(options) {

			

			var me = this;

			

			$.extend(this.options, options);

			

			// init country

			if(this.options.country.object_id) {

						

				$('#' + this.options.country.object_id).change(

						

					function() {

								

						me.set_country_value($('#' + me.options.country.object_id).attr('value'));

								

					}

							

				);

						

			}

			

			

			// init region

			if(this.options.region.object_id) {

				

				$('#' + this.options.region.object_id).change(

						

					function() {

								

						me.set_region_value($('#' + me.options.region.object_id).attr('value'));

								

					}

							

				);

						

			}

			

			

			// init city

			if(this.options.city.object_id) {

				

				$('#' + this.options.city.object_id).change(

						

					function() {

								

						me.set_city_value($('#' + me.options.city.object_id).attr('value'));

								

					}

							

				);

					

			}



		},

		

		get_countries: function() {

			

			var me = this;

			

			$.post(

			

				"libraries/geo/countries", 

				

				{}, 

				

				function(msg) { 

					

					$('#' + me.options.country.object_id).attr('length', 1);

					

					for(var i=0; i<msg.length; i++) {

						

						$('<option value="' + msg[i]['country_id'] + '">' + msg[i]['country_name'] + '</option>').appendTo($('#' + me.options.country.object_id));

						

					}

					

		    	me.set_country_value(me.options.country.value);

		    	

		  	},

		  	

		  	'json'

		  	

			);

			

		},

		

		get_regions: function() 
		{
			
			if (parent.location.protocol == "http:") {
	
				base_url = $('#BASE_URL').attr('value')
				
			}
			else {
				
				base_url = $('#BASE_URL_HTTPS').attr('value')
				
			}
			
			var me = this;
			
			$.post
			(
				base_url+"libraries/geo/regions"
				,
				{id : this.options.country.value}
				,
				function(msg) 
				{
					$('#' + me.options.region.object_id).attr('length', 1);

					for( var i = 0; i < msg.length; i++ ) 
					{
						$('<option value="' + msg[i]['region_id'] + '">' + msg[i]['region_name'] + '</option>').appendTo($('#' + me.options.region.object_id));
					}
					
					me.set_region_value(me.options.region.value);
					
					jQuery( '#' + me.options.region.object_id ).trigger('RegionsDropdownContentChanged');
				}
				,
				'json'
			)
			;
		}
		,

		

		get_cities: function() {

			

			var me = this;

			

			$.post(

			

				"libraries/geo/cities", 

				

				{id : this.options.region.value}, 

				

				function(msg) { 

					

					$('#' + me.options.city.object_id).attr('length', 1);

					

					for(var i=0; i<msg.length; i++) {

						

						$('<option value="' + msg[i]['city_id'] + '">' + msg[i]['city_name'] + '</option>').appendTo($('#' + me.options.city.object_id));

						

					}

		    	

		  	},

		  	

		  	'json'

		  	

			);

			

		},

		

		set_country_value: function(value) {

			

			this.options.country.value = value;

			

			if(this.options.region.object_id) {

				

				this.get_regions();

				

			}

			

		},

		

		set_region_value: function(value) {

			

			this.options.region.value = value;

			

			if(this.options.city.object_id) {

				

				this.get_cities();

				

			}

			

		},

		

		set_city_value: function(value) {

			

			this.options.city.value = value;

			

		}

		

	}



}(jQuery));
