function Selector(){
	$.extend(this, {
		ishash: '96734f06e89d009d3ecffdd3e81af35e', //b29fd9987ec0accec95d651a370479a8
		host: '', //'554838a8.services.gismeteo.ru', //0b3f45b2
		icon_path: 'http://ewg.new.gismeteo.ru/static/images/icons/chrome/small/',
		link: 'http://www.gismeteo.ru/',
		selectFirstCity: true, // при обновлении списка городов выделять первый или ставить опцию "Выбрать город" (all_cities_title)
		all_regions_title: 'Все области',
		submit_btn: null,
		all_cities_title: 'Выбрать город',
		selectors: { country: null, district: null, city: null, year: null, month: null },
		data: { country:156, district: 251, city: 4368, year: 2011, month: 9, year: (new Date).getFullYear(), month: (new Date).getMonth()},
		updating: false,
		getDataCallbacks: []
	});
	// this.url = (this.host && this.host!==''?'http://':'')+this.host+'/inform-service/'+this.ishash+'/';
	// this.url = '/proxy/';
	this.url = '/inform-service/'+this.ishash+'/';
}
Selector.prototype.init = function(args){
	var data = $.extend(this.data, args.data);
	$.extend(this, args);
	this.data = data;
	this.initUI();
	this.onChange('all', true);
	site.log('Selector class initializing...');
}
Selector.prototype.initUI = function(){
	$.extend(this.selectors, {
		country: $('#'+this.selectors.country),
		district: $('#'+this.selectors.district),
		city: $('#'+this.selectors.city),
		year: $('#'+this.selectors.year),
		month: $('#'+this.selectors.month)
	});
	this.submit_btn = $('#'+this.submit_btn);
	this.selectors.country.bind('change', $.proxy(function(index, obj){this.onChange('country', true)}, this));
	this.selectors.district.bind('change', $.proxy(function(index, obj){this.onChange('district', true)}, this));
	this.selectors.city.bind('change', $.proxy(function(index, obj){this.onChange('city', true)}, this));
	this.submit_btn.click($.proxy(function(){
		if(this.data.city && this.data.year && this.data.month)
			document.location='/diary/'+this.data.city+'/'+this.data.year+'/'+this.data.month+'/';
		return false;
	}, this));
}
Selector.prototype.onChange = function(what, synch){
	// site.log(['changed', what]);
	if(this.updating===true && synch!==true) return;
	var obj = this.selectors[what];
	var obj_option = obj ? obj.get(0).options[obj.get(0).selectedIndex] : null;
	var obj_value = obj_option ? this.nullORint(obj_option.value) : null;
	this.updating = true;
	switch(what){
		case 'all':
			this.getData('country', {}, $.proxy(function(data){this.fillSelect('country', data);}, this));
			this.getData('district', {country:this.data.country}, $.proxy(function(data){this.fillSelect('district', data);}, this));
			this.getData('city', (this.data.district?{district:this.data.district}:{country:this.data.country}), $.proxy(function(data){this.fillSelect('city', data);}, this));
		break;
		case 'country':
			$.extend(this.data, {country: obj_value, district: null, city: null});
			this.getData('district', {country:this.data.country}, $.proxy(function(data){this.fillSelect('district', data);}, this));
			this.getData('city', {country:this.data.country}, $.proxy(function(data){this.fillSelect('city', data);}, this));
		break;
		case 'district':
			$.extend(this.data, {district: obj_value, city: null});
			if(obj_value===null)
				this.getData('city', {country:this.data.country}, $.proxy(function(data){this.fillSelect('city', data);}, this));
			else
				this.getData('city', {district:this.data.district}, $.proxy(function(data){this.fillSelect('city', data);}, this));
		break;
		case 'city':
			var distr = $(obj_option).attr('district');
			$.extend(this.data, {district: this.nullORint(distr), city: obj_value});
			if(this.highlightDataInSelect('district')){
				this.getData('city', {district:this.data.district}, $.proxy(function(data){this.fillSelect('city', data);}, this));
			}
		break;
	}
	if(synch!==true)
		this.updating = false;
	// site.log([this.data.country, this.data.district, this.data.city, this.data.year, this.data.month]);
}
Selector.prototype.getData = function(what, data, callback){
	this.getDataCallbacks[what] = callback;
	this.clearSelect(this.selectors[what].get(0));
	data.fr='sel';
	if(what=='city') data.has_facts=1;
	$.ajax({
		type: 'GET',
		url: this.url+(what=='country'?'countries':(what=='district'?'districts':(what=='city'?'cities':'')))+'/',
		data: data,
		context: {what:what, obj:this},
		dataType: 'xml',
		success: function(data){
			if(this.obj.getDataCallbacks[this.what]!=undefined && this.obj.getDataCallbacks[this.what]!=null)
				this.obj.getDataCallbacks[this.what](data);
  	},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			// alert(XMLHttpRequest.responseText);
			// alert(XMLHttpRequest.responseText);
		}
	});
}
Selector.prototype.fillSelect = function(what, data){
	var items = $(data).find('item');
	var sel = this.selectors[what].get(0);
	if(what=='district'){
		sel.options[sel.options.length]=new Option(this.all_regions_title, '', true, false);
		sel.options[sel.options.length]=new Option('------------', '', false, false);
		sel.options[sel.options.length-1].disabled=true;
	}
	$(items).each($.proxy(function(index, obj){
		var id = parseInt($(obj).attr('id'));
		var opt = new Option($(obj).attr('n'), id, this.data[what]==id, this.data[what]==id);
		sel.options[sel.options.length]=opt;
		if(what=='city'){
			var city_name = $(obj).attr('district_name');
			if(this.data.district===null && city_name)
				opt.text += ', '+city_name;
			$(opt).attr('district', this.nullORint($(obj).attr('district_id')));
		}
	}, this));
	if(this.selectFirstCity===true && what=='city'){
		var city_id = sel.options[sel.selectedIndex].value;
		this.data.city = this.nullORint(city_id);
	}
	if(what=='district' && sel.options.length==2){
		this.clearSelect(sel);
	}
	// site.log([this.data.country, this.data.district, this.data.city, this.data.year, this.data.month]);
}
Selector.prototype.refreshDate = function(){
	var y = this.selectors.year.get(0);
	var m = this.selectors.month.get(0);
	$.extend(this.data, {
		year:parseInt(y.options[y.selectedIndex].value), 
		month:parseInt(m.options[m.selectedIndex].value, 10)
	});
	// site.log([this.data.country, this.data.district, this.data.city, this.data.year, this.data.month]);
}
Selector.prototype.nullORint = function(arg){
	return (arg==='' || arg===undefined || isNaN(arg))?null:parseInt(arg);
}
Selector.prototype.highlightDataInSelect = function(what){
	var curr, sel = this.selectors[what].get(0), prevIdx=sel.selectedIndex, i=0, founded=null, l=sel.options.length;
	while(i<l && founded===null){
		curr = sel.options[i];
		if(curr.value==this.data[what]) founded = curr;
		i++;
	}
	if(founded!==null)
		founded.selected=true;
	return (founded!==null && founded!==sel.options[prevIdx]);
}
Selector.prototype.clearSelect = function(sel){
	// while(sel.options.length>0) $.removeFromArray(sel.options, sel.options.length-1);
	sel.options.length = 0;
	// sel.options[sel.options.length]=new Option(this.loading, '', false, false);
}

