var savedTrainsKey = 'saved-trains-' + $(document.body).attr('data-country');
var form = $('#search-form');
var input = $('#train-no-input');
var button = $('#submit-button');
var saved = $('#saved-trains');
var saveLink;

$.pjax.defaults.timeout = 3000;

Mousetrap.bind('/', function() { input.focus(); });
Mousetrap.bind('h', function() { $('#stations').collapse('toggle'); });
Mousetrap.bind('r', function() { form.submit(); });
Mousetrap.bind('c', function() { $('#country').modal('show'); });
Mousetrap.bind('?', function() { $('#shortcuts').modal('show'); });
Mousetrap.bindGlobal('esc', function() { input.blur(); });

$(document).ready(function() {
	generateMyTrainsList();

	if (input.val()) {
		input.keyup();
		decorateVoyageReport(input.val());
	}
});

input.keyup(function() {
	var value = $(this).val();
	var empty = value.length == 0;
	var disabled = button.is(':disabled');

	if (!empty) {
		form.attr('action', value);
		if (disabled) {
			button.removeAttr('disabled');
		}
	}
	else if (!disabled) {
		form.removeAttr('action');
		button.attr('disabled', 'disabled');
	}
});

form.submit(function(event) {
	$.pjax.submit(event, '#train-data');
});

$('a[data-train]').click(function(event) {
	var train = $(this).attr('data-train');
	input.val(train);
	input.keyup();
	$.pjax.click(event, '#train-data');
});

$('#train-data').on('pjax:success', function() {
	var trainNo = input.val();
	decorateVoyageReport(trainNo);
});

function saveMyTrains(myTrains) {
	localStorage.setItem(savedTrainsKey, JSON.stringify(myTrains));
}

function loadMyTrains() {
	return JSON.parse(localStorage.getItem(savedTrainsKey)) || [];
}

function addTrain(trainNo) {
	var myTrains = loadMyTrains();

	if (myTrains.indexOf(trainNo) != -1) {
		return;
	}

	myTrains.push(trainNo);
	saveMyTrains(myTrains);
}

function removeTrain(trainNo) {
	var myTrains = loadMyTrains();
	var index = myTrains.indexOf(trainNo);

	if (index == -1) {
		return;
	}

	myTrains.splice(index);
	saveMyTrains(myTrains);
}

function containsTrain(trainNo) {
	var myTrains = loadMyTrains();
	return myTrains.indexOf(trainNo) != -1;
}

function generateMyTrainsList() {
	saved.empty();
	var myTrains = loadMyTrains();

	if (myTrains.length > 0) {
		myTrains.sort(function(a, b) {
			return a - b;
		});

		myTrains.forEach(function(train) {
			if (!saved.is(':empty')) {
				saved.append('&nbsp;')
			}
			saved.append('<a class="btn btn-primary btn-sm" href="' + train
				+ '" role="button" data-train="' + train + '">' + train + '</a>');
		});

		$('.remove-my-train').click(function(event) {
			var trainNo = $(this).attr('data-train-no');
			event.preventDefault();
			removeTrain(trainNo);
			if (trainNo == saveLink.attr('data-train-no')) {
				prepareSaved(saveLink);
			}
			generateMyTrainsList();
		});
	}
	else {
		saved.append('<p class="text-left text-muted">' + messages['saved.empty'] + '</p>')
	}
}

function prepareNotSaved(link) {
	link.attr('title', messages['saved.remove'])
		.find('span.glyphicon')
		.removeClass('glyphicon-star-empty')
		.addClass('glyphicon-star');
}

function prepareSaved(link) {
	link.attr('title', messages['saved.add'])
		.find('span.glyphicon')
		.removeClass('glyphicon-star')
		.addClass('glyphicon-star-empty');
}

function decorateVoyageReport(trainNo) {
	$('#generated-time').text(messages['voyage.generated'].replace('{0}',
		new Date().toTimeString()));

	input.blur();
	saveLink = $('#save');

	if (containsTrain(trainNo)) {
		prepareNotSaved(saveLink);
	}
	else {
		prepareSaved(saveLink);
	}

	saveLink.click(function(event) {
		event.preventDefault();
		var trainNo = $(this).attr('data-train-no');

		if (containsTrain(trainNo)) {
			removeTrain(trainNo);
			prepareSaved(saveLink);
		}
		else {
			addTrain(trainNo);
			prepareNotSaved(saveLink);
		}

		generateMyTrainsList();
	});
}

@@ -1164,283 +1164,8 @@
 );%0A%0A
-$('a%5Bdata-train%5D').click(function(event) %7B%0A%09var train = $(this).attr('data-train');%0A%09input.val(train);%0A%09input.keyup();%0A%09$.pjax.click(event, '#train-data');%0A%7D);%0A%0A$('#train-data').on('pjax:success', function() %7B%0A%09var trainNo = input.val();%0A%09decorateVoyageReport(trainNo);%0A%7D);%0A%0A
 func
@@ -2603,16 +2603,301 @@
 /p%3E')%0A%09%7D
+%0A%0A%09$('a%5Bdata-train%5D').click(function(event) %7B%0A%09%09var train = $(this).attr('data-train');%0A%09%09input.val(train);%0A%09%09input.keyup();%0A%09%09$.pjax.click(event, '#train-data');%0A%09%7D);%0A%0A%09$('#train-data').on('pjax:success', function() %7B%0A%09%09var trainNo = input.val();%0A%09%09decorateVoyageReport(trainNo);%0A%09%7D);
 %0A%7D%0A%0Afunc

Fix pjax handling of saved trains links