// PRODUCT DETAIL TABS

$(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);
		});
	}

}

/* 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");	
}

/* 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 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 ")	

	// Information Tab
	AddOverviewTab("#panel_overview div.externallinks ")

	// Road Directions Tab
	AddOverviewTab("#panel_overview div.roaddirections ")

	// Public Transport Directions Tab
	AddOverviewTab("#panel_overview div.publicdirections ")
	
	// Downloads Tab
	AddOverviewTab("#panel_overview div.downloads ")
	
	// Downloads Tab
	AddOverviewTab("#panel_overview div.tfa ")
}

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");
		if (CurrentClass != "")
        {

		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 ( $("#colCenter .ctl_ProductDetail div.tfa").length )
	// If Welcome All Link Exists
  	{
		// Change Welcome All Link to Tab
		$("#colCenter .ctl_ProductDetail div.tfa").css({'visibility' : 'hidden'});
									
		// Load Access Statement
		//	$("#panel_overview div.roaddirections").before("div#panel_facilities div.facilities");
		
		$("#panel_facilities div.facilities").before("<h3 id='access_link'>Accessibility</h3><div id='panel_access'></div>");
	
		//$("#panel_overview div.roaddirections").before("<h3 id='access_link'>Access Info</h3><div id='panel_access'></div>");
	
		GetAccessURL = $("#colCenter .ctl_ProductDetail span.tfa a").attr("href");
		$("#panel_access").load(GetAccessURL + "#colCenter div.ctl_TFA");
	
		// Add Click Event to Access Link
		$("h3#access_link").click( function() 
		{
			$("#panel_access").toggle(); 
		
			// Switch Tab Background
			CurrentBgd = $("h3#access_link").css("background-image");
			if ($("#panel_access").css("height") == "1px" || $("#panel_access").css("display") == "block")
			{ 
				var newBgd = CurrentBgd.replace('closed','open');
				$("h3#access_link").css("background-image",newBgd); 
			}
			else 
			{ 
				var newBgd = CurrentBgd.replace('open','closed');	
				$("h3#access_link").css("background-image",newBgd); 
			}
		});
	}
} 


function ToggleAccessTab(boolState) 
{
	if (boolState) 
	{
		BlurAllTabs();
		$("#colCenter .ctl_ProductDetail span.tfa img").attr("src","/images/accessibility_tab_focus.gif"); 
	    $("#AccessPanel").show();		
		$("#panel_overview").fadeOut("slow"); 
		$("#panel_facilities").fadeOut("slow");		
		$("#panel_location").fadeOut("slow");
		$("#panel_events").fadeOut("slow");
		$("#panel_specoffers").fadeOut("slow");		
	}
	else
	{
		$("#colCenter .ctl_ProductDetail span.tfa img").attr("src","/images/accessibility_tab_blur.gif"); 
		$("#AccessPanel").fadeOut();		
	}
}

// function GradingLinks()
// {
//	$("#colCenter 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('/' + GradePageURL,"GradingWindow","width=550px,location=0,titlebar=0,scrollbars=1");					   
//		});
				
//	});
	
	// $("#colCenter div.ctl_ProductDetail div.gradings").append("<p>Click a logo to find its meaning</p>");
// }

// function AwardLinks()
// {
//	$("#colCenter div.ctl_ProductDetail div.awards img").each(function(i) {
//		$(this).css("cursor","pointer");
//		$(this).click(function(){
//			window.open('/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 = 102;
	var widthFac = 103;
	var chkLocation = $("#panel_location").html();
	var chkLocation = $.trim(chkLocation);	
	var chkLocation = chkLocation.length;	
	var leftLoc = 205;
	var widthLoc = 103;
	var chkEvents = $("#panel_events").html();
	var chkEvents = $.trim(chkEvents);
	var chkEvents = chkEvents.length;	
	var leftEvent = 308;
	var widthEvent = 92;	
	var chkOffers = $("#panel_specoffers").html();
	var chkOffers = $.trim(chkOffers);
	var chkOffers = chkOffers.length;	
	var leftOffer = 400;
	var widthOffer = 127;	
	var chkAccess = $("#AccessPanel").length;
	var leftAccess = 527;
	var widthAccess = 150;	
	
	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":"5px", "left": leftFac + "px"});
	$("#panel_location_tab").css({"position":"absolute", "top":"5px", "left": leftLoc + "px"});
//	$("#"panel_events_tab").css({"position":"absolute", "top":"5px", "left":leftEvent + "px"});
	$("#panel_specoffers_tab").css({"position":"absolute", "top":"5px", "left": leftOffer + "px"});
	$("#colCenter .ctl_ProductDetail div.tfa").css({"left" : leftAccess + "px"});	
	
}

// Replace Related Offers text with new text: Accommodation Durham Deals

$("body.Atractions ul.relatedOffers li p a").text("Durham Deals");
$("body.Eating ul.relatedOffers li p a").text("Durham Deals");
$("body.Shopping ul.relatedOffers li p a").text("Durham Deals");
//$("body.Accommodation ul.relatedOffers li p a").text("accommodation Deals");



