/*
-----------------------------------------------
lmsre.com
Script: vdwUtil.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 29 April 2008
----------------------------------------------- */

vdwSearchMap = {
	summaryTabId : 'county_summary', // Id of summary tab
	townsPaneId : 'towns_pane', // Id of towns pane
	tabNavigationId : 'nav_counties', // Id of tabs navigation
	submitBtnId : 'search_submit', // Id of submit button
	searchMapId : 'search_map_graphic', // Id of container of search map
	selectAllTownsClass : 'select_all_towns', // Class of container for select all towns checkboxes
	paneControlsClass : 'pane_controls', // Class of pane controls (remove, select all)
	townsClass : 'towns_list', // Class of container for towns checkboxes
	disabledTabClass: 'ui-tabs-disabled', // Class of disabled tabs
	tabContentIdPrefix : 'county_', // Prefix of id of tab content
	overlayIdPrefix : 'overlay_', // Prefix of id for overlays
	init : function() {
		// Load overlays, uncheck all checkboxes, initialized the tabs with all but the summary disabled, add the update summary function to the checkboxes, add remove buttons to the tab controls and hide the submit button
		vdwSearchMap.clearTownCheckboxes();
		$jq('#' + vdwSearchMap.tabNavigationId).tabs({ disabled : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] });
		$jq('#' + vdwSearchMap.townsPaneId + ' li.' + vdwSearchMap.selectAllTownsClass + ' input[type="checkbox"]').click(vdwSearchMap.toggleTownCheckboxes);
		$jq('#' + vdwSearchMap.townsPaneId + ' input[type="checkbox"]').click(vdwSearchMap.updateSummary);
		$jq('#' + vdwSearchMap.submitBtnId).hide();
		vdwSearchMap.noTownsSummaryHtml = $jq('#' + vdwSearchMap.summaryTabId).html();
		$jq.get('/assets/includes/search-map-overlays.php', function(data) {
			$jq('#' + vdwSearchMap.searchMapId).append(data);
		});
		$jq.each($jq('.towns_list'), function() {
			if ($jq(this).attr('id') != vdwSearchMap.summaryTabId) {
				var county = $jq(this).attr('id').replace('county_', '');
				$jq(this).find('ul.' + vdwSearchMap.paneControlsClass).prepend('<li class="remove_county"><a href="javascript:vdwSearchMap.toggleCounty(\'' + county + '\', \'\')">Remove</a></li>');
			}
		});
    },
    enableCounty : function(tabContentId, tabLabel, index) {
		// Enable and select tab, show overlay
		$jq('#' + vdwSearchMap.tabNavigationId).tabs('enable', index);
		$jq('#' + vdwSearchMap.tabNavigationId).tabs('select', index);
		$jq('#' + vdwSearchMap.tabContentIdPrefix + tabContentId).removeClass('ui-tabs-hide');
		$jq('#' + vdwSearchMap.overlayIdPrefix + tabContentId).show();    
    },
    disableCounty : function(tabContentId, index) {
		// Disable tab, hide overlay, select the summary tab and uncheck all associated checkboxes
		$jq('#' + vdwSearchMap.tabNavigationId).tabs('disable', index);
		$jq('#' + vdwSearchMap.tabNavigationId).tabs('select', 0);
		$jq('#' + vdwSearchMap.tabContentIdPrefix + tabContentId).addClass('ui-tabs-hide');
		$jq('#' + vdwSearchMap.overlayIdPrefix + tabContentId).hide();
		$jq.each($jq('#' + vdwSearchMap.tabContentIdPrefix + tabContentId + ' input:checked'), function() { this.checked = false; });
		vdwSearchMap.updateSummary();
    },
	toggleCounty : function(tabContentId, tabLabel) {
		// Toggle whether the tab is enabled or disabled
		var tabContent = $jq('#' + vdwSearchMap.tabContentIdPrefix + tabContentId);
		var index = $jq('#' + vdwSearchMap.tabNavigationId + ' a').index($jq('a[href="#' + vdwSearchMap.tabContentIdPrefix + tabContentId + '"]')[0]);
		var disabled = $jq('a[href="#' + vdwSearchMap.tabContentIdPrefix + tabContentId + '"]').parent('li').hasClass(vdwSearchMap.disabledTabClass);
		if (disabled) {
			vdwSearchMap.enableCounty(tabContentId, tabLabel, index);
		} else {
			vdwSearchMap.disableCounty(tabContentId, index);
		}
	},
	updateSummary : function() {
		// Update the summary tab with a list of selected towns with their states
		var summary_html = '';
		$jq.each($jq('.' + vdwSearchMap.townsClass + ' input:checked'), function() {
			if (!$jq(this).parent('li').hasClass(vdwSearchMap.selectAllTownsClass)) {
				var state = ($jq(this).parents('div').eq(0).hasClass('vt')) ? 'VT' : 'NH';
				var selectedTown = $jq(this).val();
				if (selectedTown.indexOf('|') != -1) { selectedTown = selectedTown.split('|')[0]; }
				summary_html += '<li>' + selectedTown + ', ' + state + '</li>';
 			}
		});
		// Update summary and show submit button or show no towns message and hide submit button
		if (summary_html == '') {
			summary_html = vdwSearchMap.noTownsSummaryHtml;			
			$jq('#' + vdwSearchMap.submitBtnId).hide();
		} else {
			summary_html = '<ul>' + summary_html + '</ul>';			
			$jq('#' + vdwSearchMap.submitBtnId).show();
		}
		$jq('#' + vdwSearchMap.summaryTabId).html(summary_html);		
	},
	toggleTownCheckboxes : function() {
		// Toggle all checkboxes in a tab
		if ($jq(this).parent('li').hasClass(vdwSearchMap.selectAllTownsClass)) {
			var checked = this.checked;
			$jq.each($jq(this).parents('div').eq(0).find('input[type="checkbox"]'), function() { this.checked = checked; });
		}
	},
	clearTownCheckboxes : function() {
		// Clear all checkboxes on first load
		$jq.each($jq('#' + vdwSearchMap.townsPaneId + ' input[type="checkbox"]'), function() { this.checked = false; });
	}
}

$jq(document).ready(function() {
	vdwSearchMap.init();
});