/**
 * @author gavinwilliams
 */
var TabDataManager = function(table){
	this.response = new Array();
	this.table = $(table);
}

TabDataManager.prototype.processResponse  = function(response){
	this.response = response;
	this.populate();
}

TabDataManager.prototype.populate = function(){
	
	$('tbody tr', this.table).each(function(){
			$(this).remove();
	});
	
	if (this.response.data.length > 0) {
	
		for (var i = 0; i < this.response.data.length; i++) {
			data = this.response.data[i];

			var directory = 'directory';
			
			if (data.business.folderId == 4) {
				directory = 'fuelpoints';
			}
			
			if(window.location.href.match('fuelpoints')){
				if(data.fuelPrice.price == '0.01'){
					telephone = 'Not Reported';
				} else {
					telephone = data.fuelPrice.price + ' ' + data.address.country.currency;
				}
				datetime = data.fuelPrice.created.split(' ');
				date = datetime[0].split('-');
				county = date[2] + '/' + date[1] + '/' + date[0];
			} else {
				county = data.address.county.county
				telephone = data.business.telephone;
			}

			var tr = '<tr>' +
			'<td class="business">' +
			data.business.name +
			'</td>' +
			'<td class="town">' +
			data.address.town.town +
			'</td>' +
			'<td class="county">' +
			county +
			'</td>' +
			'<td class="telephone">' +
			telephone +
			'</td>' +
			'<td class="actions"><a href="/' +
			directory +
			'/view/?id=' +
			data.business.id +
			'" target="_blank">view</a></td>' +
			'</tr>';
			
			$('tbody', this.table).append(tr);

		}
	}
	
	if($('tbody tr', this.table).length < 0){
			var tr = '<tr>' +
			'<td colspan="5" class="noResults">There are currently no listings from your search criteria.</td>' +
			'</tr>';
				
			$('tbody', this.table).append(tr);			
	}

	
}
