
var tabNames = ['map','vid','links'];
function selectTab(tab_name, tab_id)
{
	for (var i=0; i < tabNames.length; i++)
	{
		var tabName = tabNames[i];
		var tab = document.getElementById(tabName+tab_id);
		var block = document.getElementById('inter_'+tabName+tab_id);
		
		if (tab_name == tabName)
		{
			if (tab) { tab.className = 'selected'; }
			if (block) { block.style.display = 'block'; }
		} else {
			if (tab) { tab.className = ''; }
			if (block) { block.style.display = 'none'; }
		}
		
	}
}


var switchMap_in_progress = [];
var switchMap_already_loaded = [];
function switchMap(neighborhood_id) {
	for (var i=0; i < switchMap_already_loaded.length; i++)
	{
		if (switchMap_already_loaded[i]==neighborhood_id) {
			selectTab('map',neighborhood_id);
			return;
		}
	}

	var activeBlock = 'inter_map'+neighborhood_id;

	// Don't do ajax if the tab is already displayed
	if (document.getElementById(activeBlock) && document.getElementById(activeBlock).style.display != 'none') { return; }
	
	// "Lock" this tab
	for (var i=0; i < switchMap_in_progress.length; i++)
	{ if (switchMap_in_progress[i]==neighborhood_id) { return; } }
	switchMap_in_progress.push(neighborhood_id)

	selectTab('map',neighborhood_id);
	document.getElementById(activeBlock).innerHTML = '<div style="padding: 10px; height:161px; font-size:12px; font-weight:bold; text-align: center; margin: 0 auto; color: #fff;"><br/><img style="border:0; background none;" src="/images/indicator_black.gif" /><br />Retrieving map...</div>';

	// Retrieve using ajax
	new Ajax.Request('/includes/neighborhood_map.html?neighborhood_id='+neighborhood_id,
					 { method: 'get',
					   onSuccess: function (t) {
							document.getElementById(activeBlock).innerHTML = t.responseText;

							for (var i=0; i < switchMap_in_progress.length; i++)
							{ if (switchMap_in_progress[i]==neighborhood_id) {
								switchMap_in_progress.splice(i, 1);
							  }
							}
							switchMap_already_loaded.push(neighborhood_id);
					   },
					   onFailure: function (t) {
							for (var i=0; i < switchMap_in_progress.length; i++)
							{ if (switchMap_in_progress[i]==neighborhood_id) {
								switchMap_in_progress.splice(i, 1);
							  }
							}
							document.getElementById(activeBlock).innerHTML = '&nbsp;';
					   }
					   
					 });

}
