
var AddressView = PanelView.extend({
	
	updating: false,
	templates: undefined,
	listCache: undefined,
	
	initialize: function(options) {
		// call super.initialize(options)		
		PanelView.prototype.initialize.call(this, this.attributes, options);
		
		this.templates = options.templates;

		this.model.bind('change:addressSelectionState', this.onAddressesChanged, this);
		this.model.bind('change:dataTable', this._renderAddressList, this);
		this.model.bind('change:selectedObject', this._highlightResultItem, this);
		
		this._initInterface();
		
		var self = this;
		$('#addressView .navigate').livequery('click', function () {
			self.model.set({event: CONSTANTS.events.LINK});
			var id = $(this).attr('data-id');
			self.model.goToObjectWithID(id);
			return false;
		});
	},
	
	show: function() {
		// call super.show()
		PanelView.prototype.show.call(this);
		
		//if (!this.updating && $('.list', this.el).html() === '') {
			this._reloadAddressList();
		//}
		
		// update view
		this.update();
	},
	
	hide: function() {
		// call super.hide()
		PanelView.prototype.hide.call(this);
		
		$('.intro', this.el).html('');
		$('.list', this.el).html('');
		// update view
		this.update();
	},
	
	onAddressesChanged: function(model) {
		// don't hide list if clicked a link
		var event = model.get('event');
		if (event && (event === CONSTANTS.events.LINK || event === CONSTANTS.events.MARKER)) {
			this.el.removeClass('loading');
			return;
		}
		this._reloadAddressList();

		// update view
		this.update();
	},

	_initInterface: function() {
		var TEMPLATE = _.template(this.templates.address.view);
		this.el.html(TEMPLATE());
	},
	
	_reloadAddressList: function() {
		this.updating = true;
		this.el.addClass('loading');
		this.model.findLocationsWithinBounds();
	},
	
	_renderAddressList: function(model) {
		if (this.hidden) {
			return;
		}
		this.updating = false;
		this.el.removeClass('loading');
		
		var table = model.get('dataTable');
		if (!table) {
			return;
		}
		
		var rowCount = table.getNumberOfRows(),
			i,
			seen = {},
			addressList = [],
			ADDRESS_LIST_ITEM_TEMPLATE = _.template(this.templates.address.listItem), 
			NAME_ADDRESS_LIST_ITEM_TEMPLATE = _.template(this.templates.address.listItemNameAddress);
		
		for (i = 0; i < rowCount; i = i + 1) {
			var id = model.getTableRowValue(table, i, 'ID_FIELD');					
			if (id !== 0 && seen[id]) {
				// object with ID 0 are valid, multiple may occur
				continue;
			}
			seen[id] = true;
			
			var geo = model.getTableRowValue(table, i, 'GEO_FIELD');
					
			var address = model.getTableRowValue(table, i, 'ADDRESS_FIELD_SHORT');
			if (address) {
				// Adds 1 space before numbers.
				address = address.replace(/(\d+)/g, ' $1');
				address = address.replace(/ {2}/g, ' ');
			}
			// some monuments have a specific name
			var name = model.getTableRowValue(table, i, 'NAME_FIELD');
			if (name) {
				// remove any quotes that are in the database
				name = name.replace(/\"/g, '');
				address = NAME_ADDRESS_LIST_ITEM_TEMPLATE({
					NAME: name,
					ADDRESS: address
				});
			}
			var listItem = ADDRESS_LIST_ITEM_TEMPLATE({
				ID: id,
				LATLNG: geo,
				ADDRESS: address
			});
			addressList.push(listItem);
		}

		// header
		var actualRowCount = addressList.length,
			actualRowsText = '';
			
		// use numRows for the textual feedback
		// because that makes that some dots are not clickable
		if (rowCount >= CONSTANTS.maxResponseRows) {
			actualRowsText = CONSTANTS.maxResponseRows + '+';
		} else {
			actualRowsText = actualRowCount;
		}
		
		var introTemplate = '';
		if (addressList.length > 0) {
			introTemplate = _.template(this.templates.address.introObjectsFound);
		} else {
			introTemplate = _.template(this.templates.address.introObjectsNotFound);		
		}
		var intro = introTemplate({
			COUNT: actualRowsText,
			MONUMENTS: (actualRowCount === 1) ? _.template(this.templates.address.object)() : _.template(this.templates.address.objects)()
		});
		$('.intro', this.el).html(intro);
		$('.list', this.el).html(addressList.join(''));
		$('.list li', this.el).sortElements(function(a, b) {
			return $(a).text() > $(b).text() ? 1 : -1;
		});
		
		// get highlighted item
		this._highlightResultItem(model);
		
		// update view
		this.update();
	},
	
	_highlightResultItem: function(model) {
		var $highlightedItem;
		
		// has previous item?
		var previous = model.previous('selectedObject');
		if (previous) {
			$highlightedItem = $('#addressView a[data-id=' + previous.id + ']');
			$highlightedItem.removeClass('highlightAddress');
		}
		
		var selectedObject = model.get('selectedObject');
		if (!selectedObject) {
			return;
		}
		if (selectedObject.id === undefined) {
			return;
		}
		
		$highlightedItem = $('#addressView a[data-id=' + selectedObject.id + ']');
		if (!$highlightedItem.length) {
			return;
		}
		$highlightedItem.addClass('highlightAddress');
		
		// update view
		this.update();
	}
	
});

