ERMapPois = {
	tabCssOffset: 'bottom left',
	el_search:null,
	el_results:null,
	el_resultCount:null,
	viewToolsViewText: 'POIs',
	viewToolsHideText: 'LISTINGS',
	
	categories: null,
	items:[],
	itemIdMap:{},
	
	init: function() {
		ERMapPois.categories = ERMAPDATA.poiMapCategories;
		ERMapPois.items = ERMAPDATA.poiItems;
		
		// Setup search fields
		ERMapPois.setupSearch();
		
		// Assign pointers
		ERMapPois.el_search = jQuery('#poiMapCategories').css('display','none');
		ERMapPois.el_results = jQuery('#poiResults').css('display', 'none');
		ERMapPois.el_resultCount = jQuery('<div>0</div>');
		
		// Initialize Results
		ERMapPois.Results.init();
		
		// Add to the search tab
		ERMapSearch.addTab('pois', ERMapPois);
	},
	
	
	addItem: function(itemindex, itemdata) {
		jQuery.extend(itemdata, {
			mapIconUrl: '/map/images/mapIcons/'+ERMapPois.categories[itemdata.icon].img
		});
		
		var obj = new ERMapItem({
			data:			itemdata,
			searchFields:	['icon'],
			onByDefault:	false,
			resultBox:		ERMapPois.Results,
			onClickIcon:	ERMapPois.onClickMapIcon
		});
		
		ERMapPois.itemIdMap[itemdata.id] = obj;
		ERMapPois.items[itemindex] = obj;
},


	setupSearch: function() {
		// Setup an item in the display list for each
		// POI category
		var el = jQuery('#poiMapCategories');
		var mit = el.find('li').remove();
		jQuery.each(ERMapPois.categories, function(id, n) {
			ERMapPois.categories[id].active = false;
			ERMapPois.categories[id].el = mit.clone().appendTo(el);
			ERMapPois.categories[id].el.find('a')
				.attr({'lang':id})
				.html(n.title.toUpperCase())
				.click(ERMapPois.onClickCategory)
				[0].style.backgroundImage = 'url(/map/icons/'+n.img+')'
			;
		});
	},
	
	onClickCategory: function() {
		var mapCatId = this.lang;
		var mapCat = ERMapPois.categories[mapCatId];
		mapCat.active = !mapCat.active;
		
		jQuery(this).toggleClass('active');
		
		var on = [];
		jQuery.each(ERMapPois.categories, function(id, n) {
			if (n.active) on.push(id);
		});
		
		for (var i in ERMapPois.items) {
			ERMapPois.items[i].doSearch({'icon':"in\t"+on.join("\t")});
		}
		ERMapPois.Results.stripe();
		return false;
	},
	
	onClickResult: function(e) {
		ERMap.showInfo('poi', ERMapPois.itemIdMap[this.lang]);
		return false;
	},
	
	onClickMapIcon: function(id) {
		ERMap.showInfo('poi', ERMapPois.itemIdMap[id]);
	},
	
	Results: {
		el_cat: null,
		el_catTitle: null,
		el_item: null,
		toggleClass: '',
		count:0,
		
		init: function() {
			/* Extract and store the "template" HTML for the results, then remove them from the document */
			ERMapPois.Results.el_cat = ERMapPois.el_results.find('div:first').remove();
			ERMapPois.Results.el_row = ERMapPois.Results.el_cat.find('a').remove();
			ERMapPois.Results.toggleClass = ERMapPois.Results.el_row[0].className;
			ERMapPois.Results.el_row.removeClass(ERMapPois.Results.toggleClass);
			/* Create a category container in the results box for each ERMap.poiCategories */
			$.each(ERMap.poiCategories, function(id, name) {
				ERMapPois.Results.el_cat.clone()
					.find('div:first')
						.html(name + ':')
					.end()
					.attr('id', 'poiCat'+id)
					/* hide by default */
					.css('display', 'none')
					.appendTo(ERMapPois.el_results)
				;
			});
		},
		
		/**
		 * Adds an item to the results
		 *
		 * @param	n	The item to add
		 */
		add: function(n) {
			var P = ERMapPois.el_results.find('#poiCat'+n.cat);
			var el = ERMapPois.Results.el_row.clone()
				.attr({'id':'poiResults'+n.id, 'lang':n.id})
				.html(n.title)
				.click(ERMapPois.onClickResult)
				.appendTo(P)
			;
		},
		
		/**
		 * Makes an item visible in the results page
		 *
		 * @param	n	The item to show
		 */
		show: function(n) {
			ERMapPois.el_resultCount.html(++ERMapPois.Results.count);
			document.getElementById('poiCat'+n.cat).style.display = '';
			document.getElementById('poiResults'+n.id).style.display = '';
		},
		
		/**
		 * Hides an item in the results
		 *
		 * @param	n	The item to hide
		 */
		hide: function(n) {
			if (ERMapPois.Results.count > 0) {
				ERMapPois.el_resultCount.html(--ERMapPois.Results.count  ||  '0');
				document.getElementById('poiResults'+n.id).style.display = 'none';
				var catEl = document.getElementById('poiCat'+n.cat);
				if (jQuery(catEl).children(':visible').size() == 1)
					catEl.style.display = 'none';
			}
		},
		
		/**
		 * Alternates backgrounds
		 */
		stripe: function() {
			jQuery('#poiResults .category').each(function(i) {
				jQuery(this).children().each(function(i) {
					if (i%2) jQuery(this).addClass(ERMapPois.Results.toggleClass);
					else jQuery(this).removeClass(ERMapPois.Results.toggleClass);
				});
			});
		}
	}
};
