$(function() {
	// APPLY STYLES FOR JAVASCRIPT-ENABLED VERSION
	$('#main-content').addClass("js-version");

	// DATE DROP-DOWN
	$('#date-button').hide();
	$('#offset').change(function() {
		$('#date-select').submit();
	});

	// TUBE SERVICE BOARD

	// Change page links to tabs
	$('.pagelinks').removeClass('pagelinks').addClass('tabs');

	// Hide messages and stations list	
	$('.message, #message-detail, #stations').hide();

	// Lines and stations tabs
	$('#lines-tab a').click(function() {
		$('#lines-tab').addClass('selected');
		$('#stations-tab').removeClass('selected');
		$('#message-detail, #stations').hide();
		$('#lines').show();
		return false;
	});
	$('#stations-tab a').click(function() {
		$('#stations-tab').addClass('selected');
		$('#lines-tab').removeClass('selected');
		$('#message-detail, #lines').hide();
		$('#stations').show();
		return false;
	});



	// User clicks on a line name that has incidents associated with it
	$('#lines dd:has(.message)').prev()
	// Make the line name and status underline on hover
	.hover(
		function() { $(this).css({ textDecoration: "underline", cursor: "pointer" }).next().children("h3").css({ textDecoration: "underline" }) },
		function() { $(this).css({ textDecoration: "none", cursor: "default" }).next().children("h3").css({ textDecoration: "none" }) }
	)
	// When user clicks line name, trigger line status click
	.click(function() {
		$(this).next().click();
	});

	// When user hovers over status, also underline line name
	//$('#lines dd:has(.message)').hover(
	//	function(){$(this).parent().prev().css({textDecoration:"underline"})},
	//	function(){$(this).parent().prev().css({textDecoration:"none"})}	
	//	);

	// User clicks on a line status or station name
	$('#service-board dd:has(.message)')
	// Make the line status / station name (and the line name if viewing lines) underline on hover
	.hover(
		function() { $(this).children("h3").css({ textDecoration: "underline", cursor: "pointer" }) },
		function() { $(this).children("h3").css({ textDecoration: "none", cursor: "default" }) }
	).click(function() {
		// Create message title		
		var messageTitle;
		var lineClass = "station";
		if ($(this).parent('#lines').text() != '') {
			// Line messages
			messageTitle = $(this).prev().html() + " line";
			lineClass = $(this).prev().attr("class");
		} else {
			// Station messages
			messageTitle = $(this).children("h3").text();
		}
		// Prepend message title to message body
		messageHTML = "<h3 class='" + lineClass + "'>" + messageTitle + "</h3>" + $(this).find(".message").html();
		// Add full message into 'message-detail' span
		$('#message-detail span').empty().append(messageHTML);
		// Hide the service board
		$('#service-board dl').hide();
		// Show the message
		$('#message-detail').fadeIn("fast");
	});


	// Close incident message
	$('.close-message').click(function() {
		//Empty and hide the message detail			
		$('#message-detail span').empty();
		$('#message-detail').hide();
		// Return to lines or stations view, depending on which tab is selected
		$('#service-board:has(.tabs .selected[id="lines-tab"]) #lines').show();
		$('#service-board:has(.tabs .selected[id="stations-tab"]) #stations').show();
		return false;
	});

	// Show incidents by default - just #linename (i.e. #metropolitan)
	// for use in weekly closure emails
	var hash = location.hash;
	if (hash && /^#[a-z]+$/.test(hash)) {
		var dt = $('#lines dt.' + hash.substring(1));
		if (dt.length > 0 && dt.next("has:(.message)").length > 0) dt.click();
	}
});

function cacheBuster(url) {
	//clear xml cache
	try {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	} catch (e) {
		try {
			xmlDoc = document.implementation.createDocument("", "", null);
		} catch (e) {
		}
	}
	xmlDoc.async = false;
	xmlDoc.load(url);
	generated = xmlDoc.getElementsByTagName("feed")[0].getAttribute("generated");
}