ERMapSearch = {
	el_tabs:null,
	el_search:null,
	el_resultCount:null,
	el_results:null,
	
	tabs: {},
	currentTab: null,
	
	init: function(params) {
		// Pointer
		ERMapSearch.el_tabs = jQuery('#TabGroup');
		ERMapSearch.el_search = jQuery('#SearchContainer');
		ERMapSearch.el_viewToolView = jQuery('#viewToolView');
		ERMapSearch.el_viewToolHide = jQuery('#viewToolHide');
		ERMapSearch.el_resultCount = jQuery('#ResultCount');
		ERMapSearch.el_results = jQuery('#ResultContainer');
		
		// Set the height of the search sidebar
		document.getElementById('TabbedPanels').style.height = params.height + 'px';
		
		// Set the height of the result container
		document.getElementById('ResultContainer').style.height = (params.height-362) + 'px';
		
		// Attach listeners for buttons like "View All POIs", "Hide all POIs", etc.
		jQuery('#ViewTools a').each(function() {
			jQuery(this).bind('click', {'act':this.lang}, ERMapSearch.viewTools);
		});
		
		// Assign listeners to search panel tabs to switch in between them
		jQuery('#TabGroup a').click(ERMapSearch.clickTab);
	},
	
	/**
	 * Switches the search sidebar between different search results
	 * @param Object newTab Pointer to new tab to switch to
	 */
	switchToTab: function(newTab) {
		// If the new tab is already the current tab, don't do anything
		if (ERMapSearch.currentTab == newTab) return;
		
		// Set tab
		ERMapSearch.el_tabs.css('background-position', newTab.tabCssOffset);
		// Remove current search field and add new one
		if (ERMapSearch.currentTab) ERMapSearch.currentTab.el_search.css('display', 'none');
		newTab.el_search.css('display', '');
		// Set view tools
		ERMapSearch.el_viewToolView.html(newTab.viewToolsViewText);
		ERMapSearch.el_viewToolHide.html(newTab.viewToolsHideText);
		// Set result count
		ERMapSearch.el_resultCount.empty().append(newTab.el_resultCount);
		// Remove current result field and append new one
		if (ERMapSearch.currentTab) ERMapSearch.currentTab.el_results.css('display', 'none');
		newTab.el_results.css('display', '');
		
		ERMapSearch.currentTab = newTab;
	},
	
	/**
	 * Called when one of the tabs in the search sidebar are clicked
	 * Switches between Sales/POI search panels
	 *
	 * @param	e	Event object for the click
	 */
	clickTab: function(e) {
		ERMapSearch.switchToTab(ERMapSearch.tabs[e.target.name]);
		return false;
	},
	
	/**
	 * add a tab
	 */
	addTab: function(name, obj) {
		ERMapSearch.tabs[name] = obj;
	},
	
	/**
	 * Handle special commands that view/hide all real estate or POI locations
	 */
	viewTools: function(e) {
		switch(e.data.act) {
			// Show all icons on the map for a point type (POI or real estate)
			case 'all':
				// Show all Real Estate Sales icons
				if (ERMapSearch.currentTab == ERMapSales) {
					// Set the "Categories" drop-down field to be "All Categories"
					document.getElementById('reCategories').selectedIndex = 0;
					// Set all min/max fields to be their extreme value (min or max)
					var minMax = ['resPrice', 'resBeds', 'resBaths', 'resSqft'];
					jQuery.each(minMax, function(i, n) {
						var min = document.getElementById(n+'_min');
						var max = document.getElementById(n+'_max');
						min.selectedIndex = 0;
						max.selectedIndex = max.options.length-1;
					});
				}
				// Show all POI icons
				else if (ERMapSearch.currentTab == ERMapPois) {
					// Click any non-selected POI categories
					jQuery('#poiMapCategories li a:not([class*=active])').click();
				}
				break;
			
			// Hide all icons on the map for a point type
			case 'hide':
				// Hide all Real Estate Sales icons
				if (ERMapSearch.currentTab == ERMapSales) {
					// Hide all POIs
					jQuery('#poiMapCategories li a[class*=active]').click();
				} else {
					jQuery.each(document.getElementById('reCategories').options, function(i, n) { n.selected = false; });
				}
				break;
			case 'clear':
				jQuery('#poiMapCategories li a[class*=active]').click();
				jQuery.each(document.getElementById('reCategories').options, function(i, n) { n.selected = false; });
				break;
		}
		
		// Update the search results
		ERMap.search('*');
		
		return false;
	}
};

