jQuery(document).ready(function() {
	jQuery('.north_east_stations .sunderland a, .north_east .sunderland a').hover(function() {
		jQuery('.north_east .sunderland a').css('backgroundPosition', '-8px -51px');
	}, function() {
		jQuery('.north_east .sunderland a').css('backgroundPosition', '-8px -12px');
	});

	jQuery('.north_east_stations .hartlepool a, .north_east .hartlepool a').hover(function() {
		jQuery('.north_east .hartlepool a').css('backgroundPosition', '-131px -51px');
	}, function() {
		jQuery('.north_east .hartlepool a').css('backgroundPosition', '-131px -12px');
	});

	jQuery('.north_east_stations .eaglescliffe a, .north_east .eaglescliffe a').hover(function() {
		jQuery('.north_east .eaglescliffe a').css('backgroundPosition', '-258px -51px');
	}, function() {
		jQuery('.north_east .eaglescliffe a').css('backgroundPosition', '-258px -12px');
	});

	jQuery('.north_east_stations .northallerton a, .north_east .northallerton a').hover(function() {
		jQuery('.north_east .northallerton a').css('backgroundPosition', '-392px -51px');
	}, function() {
		jQuery('.north_east .northallerton a').css('backgroundPosition', '-392px -12px');
	});

	jQuery('.north_east_stations .thirsk a, .north_east .thirsk a').hover(function() {
		jQuery('.north_east .thirsk a').css('backgroundPosition', '-542px -51px');
	}, function() {
		jQuery('.north_east .thirsk a').css('backgroundPosition', '-542px -12px');
	});

	jQuery('.north_east_stations .york a, .north_east .york a').hover(function() {
		jQuery('.north_east .york a').css('backgroundPosition', '-624px -51px');
	}, function() {
		jQuery('.north_east .york a').css('backgroundPosition', '-624px -12px');
	});

	jQuery('.north_east_stations .north_east_london a, .north_east .london a').hover(function() {
		jQuery('.north_east .london a').css('backgroundPosition', '-692px -51px');
	}, function() {
		jQuery('.north_east .london a').css('backgroundPosition', '-692px -12px');
	});

	jQuery('.west_riding_stations .bradford a, .west_riding .bradford a').hover(function() {
		jQuery('.west_riding .bradford a').css('backgroundPosition', '-873px -51px');
	}, function() {
		jQuery('.west_riding .bradford a').css('backgroundPosition', '-873px -12px');
	});

	jQuery('.west_riding_stations .halifax a, .west_riding .halifax a').hover(function() {
		jQuery('.west_riding .halifax a').css('backgroundPosition', '-1004px -51px');
	}, function() {
		jQuery('.west_riding .halifax a').css('backgroundPosition', '-1004px -12px');
	});

	jQuery('.west_riding_stations .brighouse a, .west_riding .brighouse a').hover(function() {
		jQuery('.west_riding .brighouse a').css('backgroundPosition', '-1093px -51px');
	}, function() {
		jQuery('.west_riding .brighouse a').css('backgroundPosition', '-1093px -12px');
	});

	jQuery('.west_riding_stations .mirfield a, .west_riding .mirfield a').hover(function() {
		jQuery('.west_riding .mirfield a').css('backgroundPosition', '-1772px -51px');
	}, function() {
		jQuery('.west_riding .mirfield a').css('backgroundPosition', '-1772px -12px');
	});

	jQuery('.west_riding_stations .wakefield a, .west_riding .wakefield a').hover(function() {
		jQuery('.west_riding .wakefield a').css('backgroundPosition', '-1208px -51px');
	}, function() {
		jQuery('.west_riding .wakefield a').css('backgroundPosition', '-1208px -12px');
	});

	jQuery('.west_riding_stations .pontefract a, .west_riding .pontefract a').hover(function() {
		jQuery('.west_riding .pontefract a').css('backgroundPosition', '-1318px -51px');
	}, function() {
		jQuery('.west_riding .pontefract a').css('backgroundPosition', '-1318px -12px');
	});

	jQuery('.west_riding_stations .doncaster a, .west_riding .doncaster a').hover(function() {
		jQuery('.west_riding .doncaster a').css('backgroundPosition', '-1440px -51px');
	}, function() {
		jQuery('.west_riding .doncaster a').css('backgroundPosition', '-1440px -12px');
	});

	jQuery('.west_riding_stations .west_riding_london a, .west_riding .london a').hover(function() {
		jQuery('.west_riding .london a').css('backgroundPosition', '-692px -51px');
	}, function() {
		jQuery('.west_riding .london a').css('backgroundPosition', '-692px -12px');
	});

	jQuery('.popupclose').live('click', function() {
		if (jQuery('.timetablepopover').length > 0) {
			jQuery('.timetablepopover').remove();
		}
	});

	jQuery('.timetable_northeast').click(function(e) {
		var pos = jQuery(this).position();

		if (jQuery(this).attr('rel').match(/_/)) {
			var list = jQuery(this).attr('rel').split('_');
			var pick = list.shift();
			var origin = list.pop();

		} else {
			var origin = jQuery(this).attr('rel');
			var pick = jQuery(this).attr('rel');
		}

		var showOne = jQuery(this).hasClass('single');

		var date = new Date();

		var times = jQuery(this).html();
		times = times.replace(/<strong>/, '');
		times = times.replace(/<\/strong>/, '');

		var selected;

		var begin = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();

		var north_east = {0 : 'Sunderland',
							1 : 'Hartlepool',
							2 : 'Eaglescliffe',
							3 : 'Northallerton',
							4 : 'Thirsk',
							5 : 'York',
							6 : 'London Kings Cross'};

		var station;

		jQuery.each(north_east, function(key, value) {
			if (origin.toLowerCase() == value.toLowerCase()) {
				selected = key;
			}
		});

		jQuery.each(north_east, function(key, value) {
			if (origin.toLowerCase() != value.toLowerCase() && showOne == false) {
				station += '<option value="'+value+'">'+value+'</option>';
			} else if (pick.toLowerCase() == value.toLowerCase() && showOne == true) {
				station += '<option value="'+value+'">'+value+'</option>';
			}
		});

		var timetablechoice = '<div class="timetablepopover" style="background-image: url(/templates/siteground-j15-36sub/images/timetable_stageone.png);background-position: top left;background-repeat: no-repeat;height: 169px;left: '+(pos.left - 125)+'px;position: absolute; top: '+(pos.top - 167)+'px;width: 280px; z-index: 15;">';
		timetablechoice += '<div class="popupclose" style="cursor: pointer;position:absolute;right: 10px;top: 10px;">X</div>';
		timetablechoice += '<form action="https://www.buytickets.grandcentralrail.co.uk/DataPassedIn.aspx" method="post">';
		timetablechoice += '<input type="hidden" name="ORIGIN_STATION" value="'+origin+'" />';
		timetablechoice += '<label style="float: left;font-size: 14px; font-weight: bold;margin: 10px 10px 5px; width: 100%;">Where do you want to travel to?</label>';
		timetablechoice += '<input type="hidden" name="number_of_adults" value="1" />';
		timetablechoice += '<input type="hidden" name="number_of_children" value="0" />';
		timetablechoice += '<input type="hidden" name="outbound_hour" value="'+times.substring(0, 2)+'" />';
		timetablechoice += '<input type="hidden" name="outbound_min" value="'+times.substring(3, 5)+'" />';
		timetablechoice += '<select name="DESTINATION_STATION" style="float: left;margin: 10px 10px 5px; width: 250px;">'+station+'</select>';
		timetablechoice += '<label style="display: block; font-size: 14px; font-weight: bold;margin: 10px 10px 5px; width: 100%;">Date</label>';
		timetablechoice += '<input type="text" value="'+begin+'" name="outbound_date" id="date" style="float: left; margin: 10px 10px 5px;width: 94px;" />';
		timetablechoice += '<input type="submit" value="Find Tickets" name="submit" style="background: #f68e1e; background: -moz-linear-gradient(top, #f68e1e 0%, #ff5f00 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f68e1e), color-stop(100%,#ff5f00)); background: -o-linear-gradient(top, #f68e1e 0%,#ff5f00 100%); background: -ms-linear-gradient(top, #f68e1e 0%,#ff5f00 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=\'#f68e1e\', endColorstr=\'#ff5f00\',GradientType=0 ); border: none; border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; color: #ffffff; cursor: pointer;font-family: Arial, Helvetica, sans-serif; font-weight: bold; margin: 10px; padding: 2px;" /></form></div>';

		if (jQuery('.timetablepopover').length > 0) {
			jQuery('.timetablepopover').remove();
		}
		jQuery('#right').prepend(timetablechoice);
		jQuery( "#date" ).datepicker({buttonImage : '/images/calendar.gif',
									 buttonImageOnly : true,
									 dateFormat : 'dd/mm/y',
									 minDate : new Date(),
									 showOn: 'button'});
		e.preventDefault();
		return false;
	});

	jQuery('.timetable_westriding').click(function(e) {
		var pos = jQuery(this).position();

		if (jQuery(this).attr('rel').match(/_/)) {
			var list = jQuery(this).attr('rel').split('_');
			var pick = list.shift();
			var origin = list.pop();

		} else {
			var origin = jQuery(this).attr('rel');
			var pick = jQuery(this).attr('rel');
		}

		var showOne = jQuery(this).hasClass('single');

		var date = new Date();

		var times = jQuery(this).html();
		times = times.replace(/<strong>/, '');
		times = times.replace(/<\/strong>/, '');

		var selected;

		var begin = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();

		var westriding = {0 : 'Bradford Interchange',
							1 : 'Halifax',
							2 : 'Brighouse',
							3 : 'Wakefield Kirkgate',
							4 : 'Pontefract Monkhill',
							5 : 'Doncaster',
							6 : 'London Kings Cross'};

		var station;

		jQuery.each(westriding, function(key, value) {

			if (origin.toLowerCase() == value.toLowerCase() ) {
				selected = key;
			}
		});

		jQuery.each(westriding, function(key, value) {
			if (origin.toLowerCase() != value.toLowerCase() && showOne == false) {
				station += '<option value="'+value+'">'+value+'</option>';
			} else if (pick.toLowerCase() == value.toLowerCase() && showOne == true) {
				station += '<option value="'+value+'">'+value+'</option>';
			}
		});

		var timetablechoice = '<div class="timetablepopover" style="background-image: url(/templates/siteground-j15-36sub/images/timetable_stageone.png);background-position: top left;background-repeat: no-repeat;height: 169px;left: '+(pos.left - 125)+'px;position: absolute; top: '+(pos.top - 169)+'px;width: 280px; z-index: 15;">';
		timetablechoice += '<div class="popupclose" style="cursor: pointer;position:absolute;right: 10px;top: 10px;">X</div>';
		timetablechoice += '<form action="https://www.buytickets.grandcentralrail.co.uk/DataPassedIn.aspx" method="post">';
		timetablechoice += '<input type="hidden" name="ORIGIN_STATION" value="'+origin+'" />';
		timetablechoice += '<label style="float: left;font-size: 14px; font-weight: bold;margin: 10px 10px 5px; width: 100%;">Where do you want to travel to?</label>';
		timetablechoice += '<input type="hidden" name="number_of_adults" value="1" />';
		timetablechoice += '<input type="hidden" name="number_of_children" value="0" />';
		timetablechoice += '<input type="hidden" name="outbound_hour" value="'+times.substring(0, 2)+'" />';
		timetablechoice += '<input type="hidden" name="outbound_min" value="'+times.substring(3, 5)+'" />';
		timetablechoice += '<select name="DESTINATION_STATION" style="float: left;margin: 10px 10px 5px; width: 250px;">'+station+'</select>';
		timetablechoice += '<label style="display: block; font-size: 14px; font-weight: bold;margin: 10px 10px 5px; width: 100%;">Date</label>';
		timetablechoice += '<input type="text" value="'+begin+'" name="outbound_date" id="date" style="float: left; margin: 10px 10px 5px;width: 94px;" />';
		timetablechoice += '<input type="submit" value="Find Tickets" name="submit" style="background-color: #f68e1e; background: -moz-linear-gradient(top, #f68e1e 0%, #ff5f00 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f68e1e), color-stop(100%,#ff5f00)); background: -o-linear-gradient(top, #f68e1e 0%,#ff5f00 100%); background: -ms-linear-gradient(top, #f68e1e 0%,#ff5f00 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=\'#f68e1e\', endColorstr=\'#ff5f00\',GradientType=0 ); border: none; border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; color: #ffffff; cursor: pointer;font-family: Arial, Helvetica, sans-serif; font-weight: bold; margin: 10px; padding: 2px;" /></form></div>';

		if (jQuery('.timetablepopover').length > 0) {
			jQuery('.timetablepopover').remove();
		}
		jQuery('#right').prepend(timetablechoice);
		jQuery( "#date" ).datepicker({buttonImage : '/images/calendar.gif',
									 buttonImageOnly : true,
									 dateFormat : 'dd/mm/y',
									 minDate : new Date(),
									 showOn: 'button'});
		e.preventDefault();
		return false;
	});
});
