$(document).ready(SetupPage);
				  
function SetupPage() 
{
	InitialisePanels();
	AddRouteTab();
	PanelClicks();
	ImagePanel();

	// add a show map event to the map link tab
	var jqMapContainer = $('div.dynMapContainer');
	
	if (jqMapContainer.length > 0)
	{
		var strMapId = jqMapContainer[0].id;
		$('#panel_location_tab').one('click', function() {
			toggleSearchResultsMap(strMapId);
		});
	}
}

function InitialisePanels()
{
	// Position Tabs
	$("#panel_location_tab").css({"position":"absolute", "top":"-24px", "left":"102px"});
	$("#panel_images_tab").css({"position":"absolute", "top":"-24px", "left":"205px"});
	
	
	// Initialise Panel Visible Status 
	$("#panel_overview").show();
	$("#panel_location").hide();
	$("#panel_images").hide();
}

function PanelClicks() 
// Add click to top panels
{
	// Overview Tab Click 
	$("#panel_overview_tab").click(function() { 
		BlurAllTabs();					
		FocusTab($(this).attr("id"));
		$("#panel_overview").fadeIn("slow"); 
		$("#panel_location").fadeOut("slow");
		$("#panel_images").fadeOut("slow");		
		return false;
	});

	// Location Tab Click 
	$("#panel_location_tab").click(function() { 
		BlurAllTabs();					
		FocusTab($(this).attr("id"));
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_location").fadeIn("slow");
		$("#panel_images").fadeOut("slow");		
		return false;
	});

	// Images Tab Click 
	$("#panel_images_tab").click(function() { 
		BlurAllTabs();					
		FocusTab($(this).attr("id"));
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_location").fadeOut("slow");
		$("#panel_images").fadeIn("slow");		
		return false;
	});

}

/* blurs all the tabs (ie puts then in none selected mode) */
function BlurAllTabs()
{
	BlurTab("panel_overview_tab");
	BlurTab("panel_location_tab");
	BlurTab("panel_images_tab");
}

/* blurs the specified tab */
function BlurTab(id)
{
	var bgStyle;
	
	if( id.indexOf("#") != 0 ) id = "#" + id;
	bgStyle = $(id).css("background-image");
	bgStyle = bgStyle.replace("_focus", "_blur");
	$(id).css("background-image", bgStyle);
}

/* focus the specified tab */
function FocusTab(id)
{
	var bgStyle;
	
	if( id.indexOf("#") != 0 ) id = "#" + id;
	bgStyle = $(id).css("background-image");
	bgStyle = bgStyle.replace("_blur", "_focus");
	$(id).css("background-image", bgStyle);
}

function AddRouteTab()
{
	$("#colmain #routedetail div.description ol").hide();
	
	var RecRouteTab = $("#colmain #routedetail div.description h3:contains('RECOMMENDED ROUTE')");
	var CurrentBgd = RecRouteTab.css("background-image");
	RecRouteTab.css({"cursor":"pointer"});
	RecRouteTab.append("&nbsp;&nbsp;&nbsp;(click to see full route detail)</br>");
	var RecRouteTabPanel = $("#colmain #routedetail div.description ol");
	
	RecRouteTab.click(function()
	{ 
		RecRouteTabPanel.toggle();	
		RecRouteTab.html("RECOMMENDED ROUTE");												

		if (RecRouteTabPanel.css("display") == "block") 
		{ 
			var newBgd = CurrentBgd.replace('closed','open');
			RecRouteTab.css("background-image",newBgd); 
		}
		else 
		{ 
			var newBgd = CurrentBgd.replace('open','closed');					
			RecRouteTab.css("background-image",newBgd); 
		}		
	});
}

function ImagePanel() 
{
	// Get Image List
	imgList = $("#colmain #routedetail #panel_images div.downloads dl dt.grph a");
	
	// If no images to display
	if (imgList.length == 0) 
	{
		$("#panel_images_tab").hide();
		$("#panel_image").hide();
	}
	else
	{
		// Display Images
		
		imgHTML = "";
		
		imgList.each(function(x)
		{
			imgSrc = $(this).attr("href"); 
			imgAlt = $(this).html();
			
			imgHTML = imgHTML + "<span class='caption'>" + imgAlt + "</span><span class='route_image'><img class='route_image' src='" + imgSrc + "' alt='" + imgAlt + "' /></span>";			
		});
				
		$("#colmain #routedetail #panel_images div.downloads").html(imgHTML);
	}
}
