var search_contol_id='#example';

$(document).ready(function(){
// --- Автозаполнение ---
//alert('!');
var url_g='/index.php?f=getCountries';
var a_options={delay:10,minChars:1,matchContains:true,maxItemsToShow:10,
extraParams:{
	country:function(){return $('#geoparameters:first-child #geo-country-val').val()},
	region:function(){return $('#geoparameters:first-child #geo-region-val').val()}
},
formatItem:function(row){//alert(row[0]);
	return row[0]; 
	}
	};
	//alert('!');
$('#geo-country-val,#geo-region-val,#geo-city-val').val(0);

//var a_options={formatItem:function(item){ return item.title; },delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10};


function geoResult(event, data, formatted) {
	
	if(data[2]=='1'){
		if($('#geoparameters div:first-child #geo-country-val').val()!=data[1]){
			//Оотключаем кнопки управления у предшествующего блока
			if($('#geoparameters div:first-child').attr('id')!='geoparameter_h'){
					$('#geoparameters div:first-child .geoUnsetBut').css('display','none');
				};
			$('#geoparameter_h').clone().prependTo('#geoparameters').attr('id','geoparameter');
			$('#geoparameters div:first-child').css('display','inherit');
		}
		
		$('#geoparameters div:first-child #geo-country-val').val(data[1]);
		$('#geoparameters div:first-child #geo-country #val').text(data[0]);
		$('#geoparameters div:first-child #geo-country').css('display','inline');
		$(search_contol_id).css('background-image','url(/data/geo/geo-region.png)');
	}else
	if(data[2]=='2'){
		if($('#geoparameters div:first-child #geo-country-val').val()!=data[4]){
			//Оотключаем кнопки управления у предшествующего блока
			if($('#geoparameters div:first-child').attr('id')!='geoparameter_h'){
					$('#geoparameters div:first-child .geoUnsetBut').css('display','none');
				};
			$('#geoparameter_h').clone().prependTo('#geoparameters').attr('id','geoparameter');
			$('#geoparameters div:first-child').css('display','inherit');
		}
		$('#geoparameters div:first-child #geo-country-val').val(data[4]);
		$('#geoparameters div:first-child #geo-country #val').text(data[3]);
		$('#geoparameters div:first-child #geo-country').css('display','inline');
		
		$('#geoparameters div:first-child #geo-region-val').val(data[1]);
		$('#geoparameters div:first-child #geo-region #val').text(data[0]);
		$('#geoparameters div:first-child #geo-region').css('display','inline');
		$(search_contol_id).css('background-image','url(/data/geo/geo-city.png)');
	}else if(data[2]=='3'){
		if($('#geoparameters div:first-child #geo-country-val').val()!=data[6] && $('#geoparameters div:first-child #geo-region-val').val()!=data[4] ){
			//Оотключаем кнопки управления у предшествующего блока
			if($('#geoparameters div:first-child').attr('id')!='geoparameter_h'){
					$('#geoparameters div:first-child .geoUnsetBut').css('display','none');
				};
			$('#geoparameter_h').clone().prependTo('#geoparameters').attr('id','geoparameter');
			$('#geoparameters div:first-child').css('display','inherit');
		}
		$('#geoparameters div:first-child #geo-country #val').text(data[5]);
		$('#geoparameters div:first-child #geo-country-val').val(data[6]);
		$('#geoparameters div:first-child #geo-country').css('display','inline');
		
		$('#geoparameters div:first-child #geo-region-val').val(data[4]);
		$('#geoparameters div:first-child #geo-region #val').text(data[3]);
		$('#geoparameters div:first-child #geo-region').css('display','inline');
		
		$('#geoparameters div:first-child #geo-city-val').val(data[1]);
		$('#geoparameters div:first-child #geo-city #val').text(data[0]);
		$('#geoparameters div:first-child #geo-city').css('display','inline');
		
		$(search_contol_id).css('background-image','url(/data/geo/geo-country.png)');
            

	}
        $('#loading').css('display','block');
        $('.members-block').load('/index.php?f=getMembersList',$('#form_search_member').serializeArray(),function(){
        $('#loading').css('display','none');   
        });
        
		$(search_contol_id).val('');
		$(search_contol_id).unautocomplete().autocomplete(url_g,a_options).result(geoResult);


	
	}

$(search_contol_id).autocomplete(url_g,a_options).result(geoResult);


$('.parUnsetBut').live( 'click', function(event){
		$(this).parent('div').parent('div').remove();
        
        $('#loading').css('display','block');
        $('.members-block').load('/index.php?f=getMembersList',$('#form_search_member').serializeArray(),function(){
        $('#loading').css('display','none');   
        });
        
		});

	$('.geoUnsetBut').live( 'click', function(event){
		   $(search_contol_id).unautocomplete();
		   $(search_contol_id).val('');
			if($(this).attr('geoname')=='geo-country'){
			$('#geo-country-val,#geo-region-val,#geo-city-val').val(0);
			$(search_contol_id).css('background-image','url(/data/geo/geo-country.png)');
			
			$(search_contol_id).autocomplete('/index.php?f=getCountries',a_options);

		}else if($(this).attr('geoname')=='geo-region'){
			$('#geo-region-val,#geo-city-val').val(0);	

			$(search_contol_id).css('background-image','url(/data/geo/geo-region.png)');
			$(search_contol_id).autocomplete('/index.php?f=getRegions',a_options);

		}else{
			$('#geo-city-val').val(0);	
			$(search_contol_id).css('background-image','url(/data/geo/geo-city.png)');
			$(search_contol_id).autocomplete('/index.php?f=getCities',a_options);

		}
			
			
			$(search_contol_id).result(geoResult);
			$('#geoparameter #'+$(this).attr('geoname')).css('display','none');
            
        $('#loading').css('display','block');
        $('.members-block').load('/index.php?f=getMembersList',$('#form_search_member').serializeArray(),function(){
        $('#loading').css('display','none');   
        });
	});

});
