var City_Select = {

	init : function(){

		var me = this;

		this.is_open = false;
		this.bKeep = false;

		this.city_select = $('#city_select');
		$('#city_select * a.pseudo_link')
			.click(function(evt){ return me.open(evt); })
			.mousedown(function(){ return false; });

		this.CitySelectKeep = function(evt){ me.keep(evt); };
		this.documentClickHandler = function(evt){ me.hide(evt); };
		this.documentKeyDownHandler = function(evt){ me.cancel(evt); };

		this.city_select
			.click(this.CitySelectKeep)
			.keydown(this.CitySelectKeep);



	},


	open : function(evt) {

		if (!this.is_open)
		{
			this.city_select.removeClass('disabled');
			this.is_open = true;
			$(document)
					.click(this.documentClickHandler)
					.keydown(this.documentKeyDownHandler);
		}

		else if (this.is_open)
		{
			this.bKeep = false;
			this.hide(evt);
		}


		return false;
	},


	keep : function(evt) {
		this.bKeep = true;
	},

	hide : function(evt) {

		if (this.is_open)
		{
			if (this.bKeep)
			{
				this.bKeep = false;
				return;
			}
			this.city_select.addClass('disabled');
			this.is_open = false;
			$(document)
				.unbind('mousedown', this.documentClickHandler)
				.unbind('keydown', this.documentKeyDownHandler);
		}

	},

	cancel : function(evt) {
		if (this.is_open)
		{
			if (window.event) {
				evt = window.event;
			}

			var code = evt.keyCode ? evt.keyCode : evt.which ? evt.which : null;
			if (code == 27) {
				this.hide(evt);
			}
		}

	}


}

$(function(){
	City_Select.init();
});