Region = {
	
	oReq : new HttpReq('regions'),
	
	sysVals : {country:'страну',region:'регион',city:'город',metro:'станцию'},
	selectedRegions : new Array(),
	
	getList : function( type, isStart ) {
		
		if ( _('btn_change_'+type) ) _('btn_change_'+type).style.display='none';
		
		_( type+'loader' ).style.display = '';
		_( type+'loader' ).style.visibility = 'visible';
		
		if ( _( 'country_id' ) != null ) {
			this.selectedRegions['country_id'] = _( 'country_id' ).options[_( 'country_id' ).selectedIndex].value;
		} else {
			this.selectedRegions['country_id'] = _( '_country_id' ).value;
		}
		
		if ( _( 'region_id' ) != null ) {
			this.selectedRegions['region_id'] = _( 'region_id' ).options[_( 'region_id' ).selectedIndex].value;
		} else {
			this.selectedRegions['region_id'] = _( '_region_id' ).value;
		}
		if ( _( 'city_id' ) != null) {
			this.selectedRegions['city_id'] = _( 'city_id' ).options[_( 'city_id' ).selectedIndex].value;
		} else {
			this.selectedRegions['city_id'] = _( '_city_id' ).value;
		}
		
		switch ( type ) {
			case 'region':
				id = this.selectedRegions['country_id'];
				break;
			case 'city':
				id = this.selectedRegions['region_id'];
				break;
			case 'metro' :
				id = this.selectedRegions['city_id'];
				break;
			default :
				id = 0;
				break;
		}
		
		if ( !id && type!='country' ) return;
		
		if ( !isStart ) {
			if ( type == 'region' ) {
		
				this._createList( 'region' );
			}
			if ( type == 'region' || type == 'city' ) this._createList( 'city' );
			this._createList( 'metro' );
		
		} else {
			this._createList( type );
		}
		this.oReq.setBack( {'true':'Region.getListAfter("'+type+'", "'+id+'")','false':''} );
		this.oReq.send( {a:'getRegList',type:type,id:id} );	
	},
	
	getListAfter : function( type, id ) {
		_( type+'loader' ).style.visibility = 'hidden';
		
		var aList = this.oReq.retParams['list'];
		if ( aList.length <= 0 ) {
			_( type+'_row' ).style.display = 'none';
			return;
		} else {
			_( type+'_row' ).style.display = '';
		}
		var iCount = aList.length;
		for ( var i = 0; i < iCount; i++ ) {
			var _o = new Option( aList[i].name_ru , aList[i][type+'_id']);
			if ( this.selectedRegions[type+'_id'] == aList[i][type+'_id'] )_o.selected = true;
			_( type+'_id' ).options[_( type+'_id' ).options.length] = _o;
		}
		_( type+'_id' ).disabled = false;
	},
	
	_createList : function( type ) {
		if (!_(type+'_list'))return;
		if ( !_( type + '_id' ) ) {
			_s = document.createElement('select');
			_s.setAttribute('name','_'+type+'_id');
			_s.style.width='400px';
			_s.id = type+'_id'; 
			_(type+'_list').innerHTML = '';
			_(type+'_list').appendChild( _s );
		}
		_( type + '_id' ).disabled = true;
		_( type + '_id' ).options.length = 0;
		_( type + '_id' ).options[0] = new Option( ' - Выберите ' + this.sysVals[type] + ' - ',0);
		_( type + '_id' ).options[0].selected = true;
		if ( type != 'metro' ) _( type+'_id' ).onchange = function(){Region.getList((type=='country'?'region':(type=='region'?'city':'metro')));}
		
	},
	
	getVRegion : function( type, id ) {
		Region.regionSelectHTML = _('regionselect').innerHTML?_('regionselect').innerHTML:Region.regionSelectHTML;
		SoAlert(Region.regionSelectHTML,'Выберите регион для просмотра объявлений',0,null, {width:450});
		_('regionselect').innerHTML = '';
		if (type){
			this.getList(type,id)
		}
	}
}


flyReg = {
	
	oReq : new HttpReq('flyreg'),
	countryCache : new Array(),
	regionCache : new Array(),
	
	init : function() {
		$('#flyRegList .countryList a').click( flyReg.getRegions )
	},
	
	getRegions : function() {
		isOpener = 0;
		if ( this.id && this.id !='changereg' ){
			$('#flyRegList .countryList a.current').removeClass('current').addClass('orange');
			$(this).removeClass('orange').addClass('current');
		} else {
			if ($('#flyRegList').is(':hidden')) $('#flyRegList').fadeIn('fast'); else $('#flyRegList').fadeOut('fast');
			isOpener = 1;
		}
		$('#flyRegList .regionList')[0].innerHTML = '<h2>РЕГИОНЫ</h2><div class="loader"></div>';
		var countryID = parseInt($('#flyRegList .countryList a.current').attr('id'));
		if ( flyReg.countryCache[countryID] ) {
			flyReg.getRegionsAfter(1,countryID);
		} else {
			flyReg.oReq.setBack( {'true':'flyReg.getRegionsAfter(1,'+countryID+', '+isOpener+')','false':'flyReg.getRegionsAfter(0)'} ).send( {a:'fly_getRegList',loc:location.href,country:countryID} );	
		}
	},
	
	getRegionsAfter : function( result, countryID, isOpener ) {
		$('#flyRegList .regionList')[0].innerHTML = '<h2>РЕГИОНЫ</h2>';
		if ( result ) {
			arr = new Array();
			if (this.countryCache[countryID]) {
				arr = this.countryCache[countryID];
			} else {
				arr = this.oReq.retParams;
				if( arr ) {
					this.countryCache[countryID] = arr;
				} else return'';
			}
			open = '<h2>РЕГИОНЫ</h2>';
			for ( c in arr['regions'] ) {
				var regID = arr['regions'][c]['region_id'];				
				if ( arr.mode == 'default' || ( typeof arr['cities'] != 'undefined'  && typeof arr['cities'][regID] != 'undefined' ) ) {
					open += '<div class="region" id="'+regID+'reg"><h2 class="orange">'+arr['regions'][c]['name_ru']+'</h2><ul class="cities">';
					if (arr.mode != 'default') {
						for ( i in arr['cities'][regID] ) {
							open+='<li class="'+(arr['cities'][regID][i]['count']>650||parseInt(arr['cities'][regID][i]['is_capital'])?'big':'')+'"><a href="/'+arr['module']+'/'+arr['cities'][regID][i]['translit']+'.htm">'+arr['cities'][regID][i]['name_ru']+'</a> <span class="gray counter">('+arr['cities'][regID][i]['count']+')</span></li>';
						}
					}
					open+='</ul></div>';
					
				}
			}
			$('#flyRegList .regionList')[0].innerHTML=open+'<br clear="all">';
			$('#flyRegList .regionList .region h2').click(flyReg.openRegion);
			if ( isOpener ) {
				setTimeout( function() { $('#'+$('#curr___Region').val()+'reg h2').click(); }, 100 ); 
			}
		}
	},
	
	openRegion : function() {		
		if ( $(this).parent()[0].className.indexOf('selected')!=-1 ) $(this).parent().removeClass('selected');else $(this).parent().addClass('selected');
		if ( !$('.cities li',$(this).parent()).size() ) {
			var regionID = parseInt( $(this).parent()[0].id );
			flyReg.oReq.setBack( {'true':'flyReg.openRegionAfter(1,'+regionID+')','false':'flyReg.openRegionAfter(0)'} ).send( {a:'fly_getCitList',region:regionID} )
		}
	},
	
	openRegionAfter : function( result, regionID ) {
//		alert(result+' '+regionID)
		if ( result ) {
			var list = '';
			for ( i in this.oReq.retParams['cities']) {
				var city = this.oReq.retParams['cities'][i];
				list+= '<li><a href="/regions_set.htm?id='+city['city_id']+'&back='+encodeURIComponent(location.href)+'">'+city['name_ru']+'</a></li>';
			}
			$('#'+regionID+'reg ul.cities')[0].innerHTML=list;
		}
	}
}
