var _prefix = ".ltn-", rxLtnElems = new RegExp("ltn-[a-z]+\s?");
$(function () {
	if (!$.flash.available) {
		$(".toggle-version").click(); //display text version instead
		return; // legacy flash or mobile devices without; requires jquery swfobject to work!
	}

	// TUBE SERVICE BOARD

	//LOOP THROUGH LINES LIST AND ADD LINE CLASS
	$('#lines .ltn-line').each(function (index) {
		$(this).addClass($(this).find(_prefix + "name").attr("class").replace(rxLtnElems, ''));
	});

	// TUBE SERVICE BOARD

	// Change page links to tabs
	$('.pagelinks').removeClass('pagelinks').addClass('tabs');

	$.each(["station", "line"], function (index, val) {
		$("#" + val + "s .message")
			.parents(_prefix + val)
			.find(_prefix + "name, " + _prefix + "title")
			.addClass("message-link")
			.mouseenter(function () { $(this).css("text-decoration", "underline").css("cursor", "pointer"); })
			.mouseleave(function () { $(this).css("text-decoration", "none").css("cursor", "auto"); })
			.click(function () {
				if ($(".js-version").length == 0) return false;
				var _parent = $(this).closest(_prefix + val), _class = 'station', _type = '', _htmlMessage = '', _name = '', message = _parent.find(".message").html();
				if (val == 'line') {
					_class = _parent.attr("class").replace(rxLtnElems, "");
					_type = ' ' + val;
				}
				_name = _parent.find(_prefix + "name").html();
				if (_name.toLowerCase() != "dlr" && _name.toLowerCase() != "overground") _name = _name + ' ' + _type;
				_htmlMessage = $("<h3 />").addClass(_class).append(_name);
				$('#message-detail #message>span').empty().append(_htmlMessage).append(message);
				$('#lines, #stations').hide();
				$('#message-detail').fadeIn("fast");
			});
	});

	$('.close-message').click(function () {
		$('#message-detail').hide();
		// Return to lines or stations view, depending on which tab is selected
		$($(".ui-tabs-selected a").attr("href")).show();
		return false;
	});

	// Lines and stations tabs
	$('#lines-tab a,#stations-tab a').click(function () {
		$('#lines-tab, #stations-tab').removeClass('ui-tabs-selected');
		$(this).parent().addClass("ui-tabs-selected");
		$('#lines,#stations').hide();
		$('.close-message').click();
		return false;
	}).slice(0, 1).click();

	// Fancy box - weekend closures email pop-up
	$("a.closures-eml-lnk").fancybox({
		padding: '0',
		'overlayColor': '#000000',
		'overlayOpacity': 0.4,
		'frameWidth': 620,
		'frameHeight': 480
	});

	// Show incidents by default - just #linename (i.e. #metropolitan) or #stop-{id}
	// for use in weekly closure emails
	var hash = location.hash || "", trigger = "";
	if (/^#stop\-\d{6,8}$/.test(hash)) trigger = "#stations-tab a," + hash + " " + _prefix + "name";
	else if (/^#stations$/.test(hash)) trigger = "#stations-tab a";
	else if (/^#[a-z]+$/.test(hash)) trigger = '#lines li.' + hash.substring(1) + " " + _prefix + "title";
	if (trigger.length > 0) $(trigger).click();
});

function flashResponse() {
	$("#tube-map-flash object").each(function(){ //it should find just one flash movie!!
		var _flashObj = this;
		$("#lines .message-link").click(function(){
			var _line = $.trim($(this).closest('.ltn-line').attr("class").replace(rxLtnElems, ""));
			_flashObj.js_to_asLine(_line);
		});
		$("#stations .message-link").click(function(){
			_flashObj.js_to_asStation($(this).html());
		});
		$("#lines-tab a").click(function(){
			_flashObj.js_to_asChangeTabToLines('Line');
		});
		$("#stations-tab a").click(function(){
			_flashObj.js_to_asChangeTabToSta('Tube');
		});
	});
}
function cacheBuster(url) {
	//clear xml cache - this is called from the tube map swf
	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");
}
