function openDirections(frm, listing_id)
{
	var location_id = frm.options[frm.selectedIndex].value;
	if (location_id == '') { return; }
	popUpWindow('/search/directions.html?listing_id='+listing_id+'&location_id='+location_id, 100, 100, 685, 600);
}

function openDirectionsNonMember(form, listing_id)
{
	var address;
  	var zip;

	for( var i=0; i<form.elements.length; i++)
        {
	  if(form.elements[i].name=='address')
            address=form.elements[i].value
          else if(form.elements[i].name=='zip')
            zip=form.elements[i].value
        }
	popUpWindow('/search/directions.html?nonMember=1&address='+address+'&zip='+zip+'&listing_id='+listing_id, 100, 100, 685, 600);
	return false;
}

function tabshow(section, listing_id) {
	//reset tabs
	tab_comm = 'commlink' + listing_id;
	tab_desc = 'desclink' + listing_id;
	tab_amen = 'amenlink' + listing_id;
	tab_incentives = 'incentiveslink' + listing_id;
	tab_directions = 'directionslink' + listing_id;
	tab_min = 'minlink' + listing_id;
    if(document.getElementById(tab_comm))
	  document.getElementById(tab_comm).className = "";
    if(document.getElementById(tab_amen))
	  document.getElementById(tab_amen).className = "";
    if(document.getElementById(tab_desc))
	  document.getElementById(tab_desc).className = "";
	if(document.getElementById(tab_incentives))
	  document.getElementById(tab_incentives).className = "";
	if(document.getElementById(tab_directions))
	  document.getElementById(tab_directions).className = "";
	if(document.getElementById(tab_min))
	  document.getElementById(tab_min).className = "";
	//reset copy blocks
	copy_comm = 'comm' + listing_id;
	copy_desc = 'desc' + listing_id;
	copy_amen = 'amen' + listing_id;
	copy_incentives = 'incentives' + listing_id;
	copy_directions = 'directions' + listing_id;
	copy_min = 'min' + listing_id;
	if(document.getElementById(copy_comm))
	  document.getElementById(copy_comm).style.display = "none";
	if(document.getElementById(copy_desc))
	  document.getElementById(copy_desc).style.display = "none";
	if(document.getElementById(copy_amen))
	  document.getElementById(copy_amen).style.display = "none";
	if(document.getElementById(copy_incentives))
	  document.getElementById(copy_incentives).style.display = "none";
	if(document.getElementById(copy_directions))
	  document.getElementById(copy_directions).style.display = "none";
	if(document.getElementById(copy_min))
	  document.getElementById(copy_min).style.display = "none";

	//set new tab and copy block
	current_tab = section + 'link' + listing_id;
	current_block = section + listing_id;
	document.getElementById(current_tab).className = "selected_tab";
	document.getElementById(current_block).style.display = "block";
}

var switchMap_in_progress = [];
var switchMap_already_loaded = [];
function switchMap(listing_num, listing_id, only_tab) {
	var activeBlock = 'inter_map'+listing_num;
	var inactiveBlock = 'inter_gal'+listing_num;
	var selectedLink = 'map'+listing_num;
	var deselectedLink = 'gal'+listing_num;	

	for (var i=0; i < switchMap_already_loaded.length; i++)
	{
		if (switchMap_already_loaded[i]==listing_id) {
			document.getElementById(activeBlock).style.display = 'block';
			document.getElementById(activeBlock).style.height = '160px';			
			document.getElementById(selectedLink).className = 'selected';
			if (document.getElementById(inactiveBlock)) { document.getElementById(inactiveBlock).style.display = 'none'; document.getElementById(activeBlock).style.height = '160px'; }
			if (document.getElementById(deselectedLink)) { document.getElementById(deselectedLink).className = ''; }
			return;
		}
	}

	// 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]==listing_id) { return; } }
	switchMap_in_progress.push(listing_id)

	document.getElementById(activeBlock).style.display = 'block';
	document.getElementById(activeBlock).style.height = '160px';
	document.getElementById(selectedLink).className = 'selected';
	if (document.getElementById(inactiveBlock)) { document.getElementById(inactiveBlock).style.display = 'none'; }
	if (document.getElementById(deselectedLink)) { document.getElementById(deselectedLink).className = ''; }
	document.getElementById(activeBlock).innerHTML = '<div style="padding: 10px; height:84px; font-size:12px; font-weight:bold; text-align: center; margin: 0 auto; background-color:#ffffee;"><br/><img style="border:0; background none;" src="/images/indicator.gif" /><br />Retrieving map...</div>';

	// Retrieve using ajax
	new Ajax.Request('/includes/listing_map.html?listing_id='+listing_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]==listing_id) {
								switchMap_in_progress.splice(i, 1);
							  }
							}
							switchMap_already_loaded.push(listing_id);
					   },
					   onFailure: function (t) {
							for (var i=0; i < switchMap_in_progress.length; i++)
							{ if (switchMap_in_progress[i]==listing_id) {
								switchMap_in_progress.splice(i, 1);
							  }
							}
							document.getElementById(activeBlock).innerHTML = '&nbsp;';
					   }
					   
					 });

}
function switchGal(listing_num) {
	var activeBlock = 'inter_gal'+listing_num;
	var inactiveBlock = 'inter_map'+listing_num;
	var selectedLink = 'gal'+listing_num;
	var deselectedLink = 'map'+listing_num;
	document.getElementById(activeBlock).style.display = 'block';
	document.getElementById(selectedLink).className = 'selected';
	if (document.getElementById(inactiveBlock)) { document.getElementById(inactiveBlock).style.display = 'none'; }
	if (document.getElementById(deselectedLink)) { document.getElementById(deselectedLink).className = ''; }
}
	
function switchImage(listing_num, linkblock, thumbnail, caption) {
	var changeImage = new Image();
	changeImage.src = thumbnail;	
	var currGallery = 'mini_gallery'+listing_num;
	var imageBlock = 'gal_nav'+linkblock+'_'+listing_num;
	document.getElementById(currGallery).src = changeImage.src;
	document.getElementById(currGallery).setAttribute('alt', caption);

	for (var i=1; i <= 4; i++) {
		var imgBlk = document.getElementById('gal_nav'+i+'_'+listing_num);
		if (imgBlk) { imgBlk.className = ''; }
	}	
	document.getElementById(imageBlock).className = 'selected';
}
