﻿
// INIT =========================================================
jQuery(document).ready(function() {
	SnowReportInit();
	SnowSummaryInit();
	TabGroupingInit();
});

// Snow Report ==================================================
// Content Snow Report Links HTML Module
function SnowReportListOthers(listHtml) {
	
	var startHtml =	'<div class="other-mountains">'+
					'<h3>View other mountains</h3>'+
					'<ul>';
	
	var endHtml =	'</ul>'+
					'</div>';
	
	var fullHtml = startHtml + listHtml + endHtml;
	return (fullHtml);
}

// Content Snow Report Initialise
function SnowReportInit() {

	if (jQuery(".content .snow-report").length > 1) {
		
		jQuery(".content .snow-report").hide();
		jQuery(".content .snow-report:first").show();

		jQuery(".content .snow-report").each(function() {
			var thisMountainName = jQuery(this).children(".mountain").children(".holder").children("h2").text();
			
			jQuery(this).attr("title", thisMountainName);
		});

		jQuery(".content .snow-report").each(function() {
			var listHtml = "";
			jQuery(this).siblings(".snow-report").each(function() {
				var listItemName = jQuery(this).attr("title");
				var listItemHtml = '<li><a href="#" title="' + listItemName + '">' + listItemName + '</a></li>';

				listHtml = listHtml + listItemHtml;
			});
			
			jQuery(this).children(".mountain").children(".holder").append(SnowReportListOthers(listHtml));
		});
	}

	jQuery(".content .snow-report .mountain .other-mountains a").click(function() {
		var openReport = jQuery(this).text();
		jQuery(".content .snow-report").hide();
		jQuery(".content .snow-report[title=" + openReport + "]").show();
		return false;
	});
}

// Snow Summary ==================================================
// Content Snow Report Links HTML Module
function SnowSummaryListOthers(listHtml) {

	var startHtml = '<div class="other-mountains">' +
					'<div class="holder">' +
					'<h3>View other mountains</h3>' +
					'<ul>';

	var endHtml =	'</ul>' +
					'</div>' +
					'</div>';

	var fullHtml = startHtml + listHtml + endHtml;
	return (fullHtml);
}

function SnowReportUpdateOthers() {
	jQuery(".sidebar .snow-report:visible").each(function() {
		var listHtml = "";

		jQuery(this).siblings(".snow-report").each(function() {
			var listItemName = jQuery(this).attr("title");
			var listItemHtml = '<li><a href="#" title="' + listItemName + '">' + listItemName + '</a></li>';

			listHtml = listHtml + listItemHtml;
		});

		jQuery(this).parent().append(SnowSummaryListOthers(listHtml));

		jQuery(".sidebar .snow-summary .other-mountains a").bind("click", function() {
			SnowSummaryChange(jQuery(this));
			return false;
		});
	});
}

function SnowSummaryChange(placement) {
	var openReport = jQuery(placement).text();
	jQuery(".sidebar .snow-report").hide();
	jQuery(".sidebar .snow-report[title=" + openReport + "]").show();
	jQuery(".sidebar .snow-summary .other-mountains").remove();
	SnowReportUpdateOthers();
}

// Content Snow Report Initialise
function SnowSummaryInit() {

	if (jQuery(".sidebar .snow-report").length > 1) {
		jQuery(".sidebar .snow-report").hide();
		jQuery(".sidebar .snow-report:first").show();

		jQuery(".sidebar .snow-report").each(function() {
			var thisMountainName = jQuery(this).children(".holder").children("h3").text();
			jQuery(this).attr("title", thisMountainName);
		});
		
		SnowReportUpdateOthers();
	}	
}

// Tab Groupings  ==================================================
function TabGroupingInit() {
	if(jQuery(".tab-grouping").length > 0 ) {
		jQuery(".tab-grouping").each(function(){
			var File = document.location.toString();
			var AnchorName = "";
			var ShowAnchored = false;
			var GroupingLocation = jQuery(this);
			
			if (File.match('#')) {	// Is the page linking to an anchor
				var AnchorName = '#' + File.split('#')[1];
				if (AnchorName.length > 1) { // Is the anchor more than just "#"
					
					if (jQuery(GroupingLocation).children(AnchorName+".tab-group").length > 0)	// Is the anchor linking to a tab group in the grouping
						ShowAnchored = true;
				}
			}
			TabGroupHide(GroupingLocation);
			
			if (ShowAnchored == true) {	// Show the anchor tab group from url else show the first tab group
				jQuery(GroupingLocation).children(AnchorName).show();
			} else {
				jQuery(GroupingLocation).children(".tab-group:first").show();
			}
			
			GroupingLocation.find(".tab-group .tabs ul li a").click(function() {
				var ShowTabGroupID = jQuery(this).attr("href");
				TabGroupShow(GroupingLocation,ShowTabGroupID);
				return false;
			});
	
		});
	}
}

function TabGroupShow(Location,ShowGroup) {
	TabGroupHide(Location);
	jQuery(Location).children(ShowGroup).show();	// Show the pod that has been selected
}

function TabGroupHide(Location) {
	jQuery(Location).children(".tab-group").hide(); // Hide all .book-now pods
}

