var Location = {
	init: function() {
		var contacts = $$('.contact-list');
		if (contacts.length > 5) {
			this.addFilter(contacts);
		}
	},
	
	addFilter: function(contacts) {
		var div = new Element('div', {
			'id': 'contact-list-filter'
		});
		var h2 = new Element('h2', {
			'text': 'Auswahl',
			'class': 'hidden'
		});
		var form = new Element('form', {
			'action': '#',
			'method': 'post'
		});
		div.adopt(h2, form);
		
		var fieldset = new Element('fieldset');
		form.adopt(fieldset);
		
		var label = new Element('label', {
			'text': 'Standorte: ',
			'for': 'contact-list-select'
		});
		var select = new Element('select', {
			'name': 'contact-list-select',
			'id': 'contact-list-select'
		});
		select.addEvent('change', function() {
			Location.display(this.value);
		});
		fieldset.adopt(label, select);
		
		contacts.each(function(item, index) {
			var name = item.getElement('h3').get('text');
			var value = 'contact-list-' + index;
			item.set('id', value);
			
			var option = new Element('option', {
				'value': value,
				'text': name
			});
			select.adopt(option);
		});
		
		div.inject($('contact-list-0'), 'before');
	},
	
	display: function(id) {
		$$('.contact-list').setStyle('display', 'none');
		$(id).setStyle('display', '');
	}
}

window.addEvent('domready', function() {
	Location.init();
});
