/**
 * Класс для фильтрации подключенных домов
 * 
 */
function  CoverageFilter(){
    var _this  =  this;	


	$('body').ready(function(){

		$('<table>').attr('id', 'rageTable').appendTo('#rageCheck');
		$('<tr>').attr('id', 'rageTableTr').appendTo('#rageTable');
		
		//$('<p>').attr('id', 'street').appendTo('#rageCheck');
		//$('<p>').attr('id', 'house').appendTo('#rageCheck');
		
		$('<td>').attr('id', 'street').appendTo('#rageTableTr');
		$('<td>').attr('id', 'house').appendTo('#rageTableTr');

		$('<div>').attr('id', 'houseConnect').html($('#houseConnectInfo').html()).appendTo('#rageCheck');
		$('<div>').attr('id', 'houseNotConnect').html($('#houseNotConnectInfo').html()).appendTo('#rageCheck').css('display', 'none');

		_this.locationComboBox  =  new Ext.form.ComboBox({
								store: new Ext.data.ArrayStore({
											fields: ['id', 'name'],
											data : streets,
											idIndex: 2
										}),
								displayField:'name',
								valueField:'id',
								minSize: 175,
								maxSize: 400,
								typeAhead: true,
								mode: 'local',
								triggerAction: 'all',
								emptyText:'Выберите улицу',
								selectOnFocus:true,
								listeners:{
									'select':function(combo, record){
										_this.houseComboBox.reset();
										_this.houseComboBox.setDisabled(true);
										_this.loadHouses(record.data.name);
									}
								},
								renderTo: $('#street').get(0)
						});

		_this.houseComboBox  =  
				new Ext.form.ComboBox({
								store: new Ext.data.ArrayStore({
											fields: ['id', 'name', 'status'],
											data : [],
											idIndex: 1
										}),
								displayField:'name',
								valueField:'id',
								minSize: 175,
								maxSize: 400,
								typeAhead: true,
								mode: 'local',
								triggerAction: 'all',
								emptyText:'Выберите дом',
								disabled: true,
								selectOnFocus:true,
								listeners:{
									'select':function(combo, record){
										_this.drowButton(record.data.id, record.data.status);
									},
									
									'disable':function(){
										$('#houseConnect').hide('fast');
										$('#houseNotConnect').hide('fast');
									}
								},
								renderTo: $('#house').get(0)
				});
	});
}


/**
 * Отрисовываем кнопку добавления
 */
CoverageFilter.prototype.drowButton  =  function(houseId, status){

	if(!houseId || houseId == 0){
		$('#houseConnect').hide('fast');
		$('#houseNotConnect').hide('fast');
	}
	else{
		if(status != 'cold'){
			$('#houseConnect').show('fast');
			$('#houseNotConnect').hide('fast');
			var  a  =  $('#houseConnect a').get(0);
			a.href  =  a.href + '?houseId='+houseId;
		}
		else{
			$('#houseConnect').hide('fast');
			$('#houseNotConnect').show('fast');
		}
	}
}


CoverageFilter.prototype.loadHouses  =  function(street_name){

	var  comboBox  =  this.houseComboBox;
		
	$.ajax({
		dataType: 'json',
		type: 'POST',
		url: '/coverage/getHouses/',
		data: {
				street:street_name, 
				output_method:'json'
			   },
		success:(function(data){
			var select  =  $('#house');
			var  store  =  comboBox.getStore();			
			
			if(data['success']){
				store.removeAll();
				store.loadData(data['houses']);
				comboBox.setDisabled(false);
			}
			else{
				store.removeAll();
			}
		})
	});
}
