$(document).ready(SetupPage);
				  
function SetupPage() 
{
	InitialisePanels();
	OverviewTabs();
	PanelClicks();
	FacilityTabs();
	ProductImages();
	AccessibilityTab();
	GradingLinks();
	AwardLinks();
	ArrangeTabs();
	
	// 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);
		});
	}
	
	$('#panel_overview_tab').attr("class","panel_overview_tab_focus");
	$('#panel_location_tab').attr("class","panel_location_tab_blur");
		
}

/* initialises the product images */
function ProductImages()
{
	var text = $("#productImage").attr("alt");
	$("#panel_overview .productimages .additionalImages p").html(text);
	$("#panel_overview .productimages .additionalImages a").click(function()
	{
		var text = $(this).find("img").attr("alt");
		$("#panel_overview .productimages .additionalImages p").html(text);
		return true;
	});
}

function InitialisePanels()
{
	// Initialise Panel Visible Status 
	$("#panel_overview").show();
	$("#panel_facilities").hide();
	$("#panel_location").hide();
	$("#panel_events").hide();
	$("#panel_specoffers").hide();

	// Check if facilities, events and special offers exist
	var chkFacilities = $("#panel_facilities").html();
	var chkFacilities = $.trim(chkFacilities);
	var chkEvents = $("#panel_events").html();
	var chkEvents = $.trim(chkEvents);
	var chkOffers = $("#panel_specoffers").html();
	var chkOffers = $.trim(chkOffers);
	var chkImages = $("#panel_overview div.productimages").html();
	var chkImages = $.trim(chkImages);
	
	// Hide Images Container if empty
	if (!chkImages.length) {$("#panel_overview div.productimages").hide();}
	
	// Hide Events If Non Exist
	if (!chkEvents.length) {$("#panel_events_tab").hide();}

	// Hide Special Offers If Non Exist
	if (!chkOffers.length) {$("#panel_specoffers_tab").hide();}

	// Hide Facilities If Non Exist
	if (!chkFacilities.length) {$("#panel_facilities_tab").hide();}
	
	if (chkEvents.length) {$("#panel_events_tab").show();}
}

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_facilities").fadeOut("slow");
		$("#panel_location").fadeOut("slow");
		$("#panel_events").fadeOut("slow");
		$("#panel_specoffers").fadeOut("slow");
		ToggleAccessTab(0);
		return false;
	});

	// Facilities Tab Click 
	$("#panel_facilities_tab").click(function() { 
		BlurAllTabs();					
		FocusTab($(this).attr("id"));
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_facilities").fadeIn("slow");		
		$("#panel_location").fadeOut("slow");
		$("#panel_events").fadeOut("slow");
		$("#panel_specoffers").fadeOut("slow");
		ToggleAccessTab(0);
		return false;
	});

	// Location Tab Click 
	$("#panel_location_tab").click(function() { 
		BlurAllTabs();					
		FocusTab($(this).attr("id"));
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_facilities").fadeOut("slow");		
		$("#panel_location").fadeIn("slow");
		$("#panel_events").fadeOut("slow");
		$("#panel_specoffers").fadeOut("slow");
		ToggleAccessTab(0);
		return false;
	});

	// Events Tab Click 
	$("#panel_events_tab").click(function() { 
		BlurAllTabs();					
		FocusTab($(this).attr("id"));
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_facilities").fadeOut("slow");		
		$("#panel_location").fadeOut("slow");
		$("#panel_events").fadeIn("slow");
		$("#panel_specoffers").fadeOut("slow");
		ToggleAccessTab(0);
		return false;
	});

	// Special Offer Tab Click 
	$("#panel_specoffers_tab").click(function() { 
		BlurAllTabs();					
		FocusTab($(this).attr("id"));
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_facilities").fadeOut("slow");
		$("#panel_location").fadeOut("slow");
		$("#panel_events").fadeOut("slow");
		$("#panel_specoffers").fadeIn("slow");
		ToggleAccessTab(0);
		return false;
	});
}

/* blurs all the tabs (ie puts then in none selected mode) */
function BlurAllTabs()
{
	BlurTab("panel_overview_tab");
	BlurTab("panel_facilities_tab");
	BlurTab("panel_location_tab");
	BlurTab("panel_events_tab");
	BlurTab("panel_specoffers_tab");	
	// BlurTab("panel_access_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);
//	$(id).css("background-color", "#ccc");
	$(id).attr("class", id.substr(1) + "_blur");
}

/* 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);
//	$(id).css("background-color", "#0099FF");	
	$(id).attr("class", id.substr(1) + "_focus");
}
	

function OverviewTabs()
// Add tabs to overview items
{
	// Awards Tab 
	// !!!! Removed clickable Awards Tab to make awards always visible !!!!
	// AddOverviewTab("#panel_overview div.awards ")	

	// Opening Times Tab
	// !!! Removed clickable Opening Times
	// AddOverviewTab("#panel_overview div.openingTimes ")	

	// Road Directions Tab
	AddOverviewTab("#panel_overview div.roaddirections ")	

	// Public Transport Directions Tab
	AddOverviewTab("#panel_overview div.publicdirections ")	
}

function AddOverviewTab(CurrentTab)
// Add collapsible tab to an overview items
{
	var CurrentBgd = $(CurrentTab + "h2").css("background-image");

	// Hide Tab Details
	$(CurrentTab + "dl").hide();
	$(CurrentTab + "table").hide();
	$(CurrentTab + "p").hide();	
	$(CurrentTab + "ul").hide();
	$(CurrentTab + "h3").hide();

	$(CurrentTab).click( function() 
	{
		$(CurrentTab + "dl").toggle();
		$(CurrentTab + "table").toggle();
		$(CurrentTab + "p").toggle();
		$(CurrentTab + "ul").toggle();
		$(CurrentTab + "h3").toggle();		
		
		if ($(CurrentTab + "p").css("display") == "block" || $(CurrentTab + "ul").css("display") == "block") 
		{ 
			var newBgd = CurrentBgd.replace('closed','open');
			$(CurrentTab + "h2").css("background-image",newBgd); 
		}
		else 
		{ 
			var newBgd = CurrentBgd.replace('open','closed');					
			$(CurrentTab + "h2").css("background-image",newBgd); 
		}

	});
}

function FacilityTabs()
// Add tabs to facilities
{
	$("#panel_facilities h3").each( function(CurrentTabIndex) 
	{
		var CurrentClass = $(this).attr("class");
		var CurrentTabPanel = "#panel_facilities ul." + CurrentClass;
		
		// Initialise Tab Panels to hidden;
		$(CurrentTabPanel).hide();
		
		// Add Click Event to Tab
		$(this).click( function() 
		{
			$(CurrentTabPanel).toggle();
			
			// Switch Tab Background
			var CurrentTab = "#panel_facilities h3." + CurrentClass;
			CurrentBgd = $(CurrentTab).css("background-image");
			
			if ($(CurrentTabPanel).css("height") == "1px" || $(CurrentTabPanel).css("display") == "block")
			{ 
				var newBgd = CurrentBgd.replace('closed','open');
				$(CurrentTab).css("background-image",newBgd); 
			}
			else 
			{ 
				var newBgd = CurrentBgd.replace('open','closed');					
				$(CurrentTab).css("background-image",newBgd); 
			}
		});
		
	});
}

function AccessibilityTab()
// Accessibility Tab
{
	if ( $("#colMain .ctl_ProductDetail div.tfa").length )
	// If Welcome All Link Exists
	{
		// Change Welcome All Link to Tab
		$("#colMain .ctl_ProductDetail div.tfa").css({'visibility' : 'hidden'});
		
		// Load Access Statement
		$("#panel_overview").before("<div id='panel_access'></div>");
		$("#panel_access").hide();
		GetAccessURL = $("#colMain .ctl_ProductDetail span.tfa a").attr("href");
		$("#panel_access").load(GetAccessURL + " #colMain div.ctl_TFA");

		$("#colMain .ctl_ProductDetail div.tfa").html("<a href='#' id='panel_access_tab' class='panel_access_tab_blur'>Accessibility</a>");
		$("a#panel_access_tab").click(function()
		  {
			ToggleAccessTab(1);
			return false;
		  });
		
		$("#colMain .ctl_ProductDetail div.tfa").css({'visibility' : 'visible'});

	}
}

function ToggleAccessTab(boolState) 
{
	if (boolState) 
	{
		BlurAllTabs();
		// $("#colMain .ctl_ProductDetail span.tfa img").attr("src","http://www.visitnortheastengland.com/images/accessibility_tab_focus.gif"); 
//		$("a#panel_access_tab").css("background-color", "#0099FF");
		$("a#panel_access_tab").attr("class", "panel_access_tab_focus");
		$("#panel_access").show();		
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_facilities").fadeOut("slow");		
		$("#panel_location").fadeOut("slow");
		$("#panel_events").fadeOut("slow");
		$("#panel_specoffers").fadeOut("slow");		
	}
	else
	{
		// $("#colMain .ctl_ProductDetail span.tfa img").attr("src","http://www.visitnortheastengland.com/images/accessibility_tab_blur.gif"); 
//		$("a#panel_access_tab").css("background-color", "#ccc");
		$("a#panel_access_tab").attr("class", "panel_access_tab_blur");
		$("#panel_access").fadeOut();		
	}
}

function GradingLinks()
{
	$("#colMain div.ctl_ProductDetail div.gradings img").each(function(i) {
		altText = $(this).attr("alt");
		$(this).css("cursor","pointer");
		var GradePageURL = "";
		
		if (altText.indexOf('Accessibility') != -1)	{
			GradePageURL = "/site/accommodation/search-results/accessibility-gradings";
			$(this).attr("alt",GetGradingAltTag(altText));
		}
		else {GradePageURL = "/site/accommodation/search-results/accommodation-gradings"}

		$(this).click(function(){
			window.open('http://www.visitnortheastengland.com' + GradePageURL,"GradingWindow","width=550px,location=0,titlebar=0,scrollbars=1");					   
		});
				
	});
	
	// $("#colMain div.ctl_ProductDetail div.gradings").append("<p>Click a logo to find its meaning</p>");
}

function AwardLinks()
{
	$("#colMain div.ctl_ProductDetail div.awards img").each(function(i) {
		$(this).css("cursor","pointer");
		$(this).click(function(){
			window.open('http://www.visitnortheastengland.com/site/accommodation/search-results/awards',"GradingWindow","width=550px,location=0,titlebar=0,scrollbars=1");					   
		});				
	});
}

function GetGradingAltTag(strCurrentAlt)
{
	var strNewAlt;
	
	switch(strCurrentAlt)
	{
		case "1 Accessibility - Mobility":
			strNewAlt = "Elderly and less mobile";
			break;
		case "2 Accessibility - Mobility":
			strNewAlt = "Part-time wheelchair users";
			break;
		case "3i Accessibility - Mobility":
			strNewAlt = "Independent wheelchair users";
			break;
		case "3a Accessibility - Mobility":
			strNewAlt = "Exceptional : Assisted wheelchair users";
			break;
		case "3ae Accessibility - Mobility":
			strNewAlt = "Exceptional : Independent wheelchair users";
			break;
		case "1 Accessibility - Visual":
			strNewAlt = "Visually impaired in need of key services and facilities";
			break;
		case "2 Accessibility - Visual":
			strNewAlt = "Visually impaired in need of higher level of services and facilities";
			break;			
		case "1 Accessibility - Hearing":
			strNewAlt = "Hearing impaired in need of key services and facilities";
			break;
		case "2 Accessibility - Hearing":
			strNewAlt = "Hearing impaired in need of higher level of services and facilities";
			break;
		default:
			strNewAlt = strCurrentAlt;
	}
	
	return strNewAlt;
}

function ArrangeTabs()
// Tidy up Tabs 
{
	var chkFacilities = $("#panel_facilities").html();
	var chkFacilities = $.trim(chkFacilities);
	var chkFacilities = chkFacilities.length;
	var leftFac = 75;
	var widthFac = 75;
	var chkLocation = $("#panel_location").html();
	var chkLocation = $.trim(chkLocation);	
	var chkLocation = chkLocation.length;	
	var leftLoc = 150;
	var widthLoc = 75;
	var chkEvents = $("#panel_events").html();
	var chkEvents = $.trim(chkEvents);
	var chkEvents = chkEvents.length;	
	var leftEvent = 225;
	var widthEvent = 75;	
	var chkOffers = $("#panel_specoffers").html();
	var chkOffers = $.trim(chkOffers);
	var chkOffers = chkOffers.length;	
	var leftOffer = 300;
	var widthOffer = 75;	
	var chkAccess = $("#panel_access").length;
	var leftAccess = 375;
	var widthAccess = 75;	
	
	if (!chkFacilities) {leftLoc = leftLoc - widthFac; leftEvent = leftEvent - widthFac; leftOffer = leftOffer - widthFac; leftAccess = leftAccess - widthFac;}
	if (!chkLocation) {leftEvent = leftEvent - widthLoc; leftOffer = leftOffer - widthLoc; leftAccess = leftAccess - widthLoc;}
	if (!chkEvents) {leftOffer = leftOffer - widthEvent; leftAccess = leftAccess - widthEvent;}	
	if (!chkOffers) {leftAccess = leftAccess - widthOffer;}	
		
	// Position Tabs	
	$("#panel_facilities_tab").css({"position":"absolute", "top":"47px", "left": leftFac + "px"});
	$("#panel_location_tab").css({"position":"absolute", "top":"47px", "left": leftLoc + "px"});
	$("#panel_events_tab").css({"position":"absolute", "top":"47px", "left":leftEvent + "px"});
	$("#panel_specoffers_tab").css({"position":"absolute", "top":"47px", "left": leftOffer + "px"});
	$("#panel_access_tab").css({"position":"absolute", "top":"47px", "left" : leftAccess + "px"});	
	
}