$(document).ready(function () {

	metroModules.serviceUpdates();
	metroModules.plannedDisruptions();

	if ($('body').hasClass('home2')) {
		metroModules.homepageV2_tabs();
		metroModules.homepageV2_accordion();
		metroModules.homepageV2_news();
	}

	metroModules.homepage_newsPreviewer();

	if ($('body[class=home], body[class=home2]')) {
		metroModules.homepage_carousel();
	}

	metroModules.homeOld();
	metroModules.listingHover();

	$('body.careers map area, body.careersIntl map area').hover(function () {
		$('.tabArea ul li:eq(' + $('.tabArea ul li').index($('.tabArea ul li:contains(' + $(this).attr("title") + ')')) + ')').addClass('hover');
	}, function () {
		$('.tabArea ul li:eq(' + $('.tabArea ul li').index($('.tabArea ul li:contains(' + $(this).attr("title") + ')')) + ')').removeClass('hover');
	});

	metroModules.careersHoverAreas();
	metroModules.careersIntlFx();
});

var metroModules = {

	listingHover: function () {
		$('.stationStatus li').hover(function () {
			if (!$(this).hasClass('listHeader')) {
				$(this).addClass('listingHover');
			}
		}, function () {
			if (!$(this).hasClass('listHeader')) {
				$(this).removeClass('listingHover');
			}
		});

		$('.block.disruptions li').hover(function () {
			if (!$(this).hasClass('listHeader')) {
				$(this).addClass('listingHover');
			}
		}, function () {
			if (!$(this).hasClass('listHeader')) {
				$(this).removeClass('listingHover');
			}
		});
		$('.serviceUpdates tr:last').addClass('last');


		$('.viewport li').hover(function () {
			$(this).addClass('listingHover');
		}, function () {
			$(this).removeClass('listingHover');
		});

		$('.listing .moduleContent li').hover(function () {
			$(this).addClass('listingHover');
		}, function () {
			$(this).removeClass('listingHover');
		});
		/*
		$('.listing .moduelContent li').click(function(){
		window.location.href = $(this).find('.moreInfo a').attr('href');
		});	
		//*/
	},

	homepage_newsPreviewer: function () {
		$('.newsPreview li').hover(function () {
			$(this).addClass('listingHover');
		}, function () {
			$(this).removeClass('listingHover');
		});
		$('.newsPreview .previewContent li:last').addClass('last');


		$('.previewContent li').click(function () {
			var isActive = ($(this).find('.lnkExpColl').hasClass('show'));
			$('.previewContent li .lnkExpColl').each(function () {
				$(this).siblings('.boxExp').slideUp('fast');
				$(this).siblings('.boxColl').show();
				$(this).removeClass('show');
			});
			if (!isActive) {
				$(this).find('.boxExp').show('fast');
				$(this).find('.boxColl').hide();
				$(this).find('.lnkExpColl').toggleClass('show');
			}
		});
	},

	homepage_carousel: function () {
		// Homepage Carousel
		if ($('.carousel').length > 0) {
			var autoscrolling = true;
			$('.carousel .wrapper li').hover(function () {
				$(this).addClass('listingHover');
			}, function () {
				$(this).removeClass('listingHover');
			}).mousedown(function () {
				window.location = $(this).find('a').attr('href');
			});
			$('.carousel').infiniteCarousel().mouseover(function () {
				autoscrolling = false;
			}).mouseout(function () {
				autoscrolling = true;
			});
			setInterval(function () {
				if (autoscrolling) {
					$('.carousel').trigger('next');
				}
			}, 10000);
		}
	},

	homepageV2_tabs: function () {
		// Homepage Tabs (Journey Planner/Timetable search)
		$('body.home2 .tabArea ul li a').click(function () {
			if ($(this).hasClass('tabJourneyPlanner')) {
				$('.tab#journeyPlanner').show();
				$('.tab#timetableSearch').hide();
				$('.tabTimetableSearch').parent('li').removeClass('active');
				$(this).parent('li').addClass('active');
			}
			else if ($(this).hasClass('tabTimetableSearch')) {
				$('.tab#journeyPlanner').hide();
				$('.tab#timetableSearch').show();
				$('.tabJourneyPlanner').parent('li').removeClass('active');
				$(this).parent('li').addClass('active');
			}
			return false;
		});

		// Homepage Tabs Error output
		var errorVisible = false;
		$('body.home2 .msgError').each(function () {
			if ($(this).css('display') == 'block') {
				errorVisible = true;
				return;
			}
		});
		if ($('body.home2 .msgError').length > 0 && errorVisible) {
			var tabIndex = $('.tabArea .tab').index($('.msgError').parents('.tab'));
			$('.tabArea li').removeClass('active');
			$('.tabArea li:eq(' + tabIndex + ')').addClass('active');
			$('.msgError').parents('.tab').siblings('.tab').hide();
			$('.msgError').parents('.tab').show();
			$('.msgError').click(function () {
				$(this).hide();
			});
		}
	},

	homepageV2_accordion: function () {
		// Homepage Accordion area (Live service updates/Planned disruptions)
		if ($('.regionUpperLeft').length) {
			$('.regionUpperLeft').accordion({
				autoHeight: false,
				changestart: function (event, ui) {
					ui.newHeader.addClass("active");
					ui.oldHeader.removeClass("active");
					ui.oldContent.css("overflow", "auto");
					ui.newContent.css("overflow", "auto");
				},
				change: function (event, ui) {
					ui.newContent.css("overflow", "visible");
				}
			});
			$('.regionUpperLeft td a, .regionUpperLeft h2 a').click(function () {
				var activeAccContent = $('.regionUpperLeft').accordion('option', 'active');
				if ($(this).hasClass('liveServiceUpdates')) {
					activeAccContent = (activeAccContent == 0) ? 1 : 0;
				}
				else if ($(this).hasClass('plannedDisruptions')) {
					activeAccContent = (activeAccContent == 1) ? 0 : 1;
				}
				$('.regionUpperLeft').accordion({ active: activeAccContent });
				return false;
			});
		}
	},

	homepageV2_news: function () {
		// Homepage News Important line news dropdown
		var inList = false;
		$('a.importantLineNews').click(function () {
			if (inList) {
				$('.listLines').slideUp('fast');
				inList = false;
			}
			else {
				$('.listLines').slideDown('fast');
				inList = true;
			}
			return false;
		});
		$('a.importantLineNews').hover(function () {
			if ($('.listLines').css('display') == 'block') inList = true;
		}).mouseleave(function () {
			setTimeout(function () {
				if (!inList) {
					$('.listLines').slideUp('fast');
					$('a.importantLineNews').removeClass('hover');
				}
			}, 500);
			inList = false;
		});
		$('.listLines').hover(function () {
			inList = true;
			$('a.importantLineNews').addClass('hover');
		}).mouseleave(function () {
			setTimeout(function () {
				if (!inList) {
					$('.listLines').slideUp('fast');
					$('a.importantLineNews').removeClass('hover');
				}
			}, 500);
			inList = false;
		});
	},

	homeOld: function () {
		var mousePosY;

		$().mousemove(function (e) {
			mousePosY = e.pageY;
		});

		$('.plannedDisruptions li').each(function () {
			$('.infoBox', this).hide();
			$('.infoBox', this).addClass(($('.plannedDisruptions li').index(this) < $('.plannedDisruptions li').length - 1) ? 'below' : 'above');
			$(this).hover(infoBox.open(), infoBox.close());
		});

		$(".serviceUpdates li").each(function () {
			if ($('.infoBox', this).length > 0) {
				$('.infoBox', this).hide();
				$(this).hover(function (e) {
					$('.infoBox', this).css({ top: ($('.serviceUpdates li').index(this) * 25) });
					$('.infoBox', this).show();
				}, infoBox.close());
			}
		});

		$("body.home .serviceUpdates li").each(function () {
			if ($('.infoBox', this).length > 0) {
				$('.infoBox', this).hide();
				$('.infoBox', this).addClass(($('.serviceUpdates li').index(this) < $('.serviceUpdates li').length - 2) ? 'below' : 'above');
				$(this).hover(function (e) {
					//$('.infoBox', this).css({top: ($('.serviceUpdates li').index(this)*25) });
					$('.infoBox', this).css({ top: ($('.serviceUpdates li').index(this) * 25) - ($('.infoBox', this).hasClass('above') ? 70 : 0) });
					$('.infoBox', this).show();
				}, infoBox.close());
			}
		});

	},

	serviceUpdates: function () {
		$(".serviceUpdates td.col2").each(function () {

			if ($('.infoBox', this).length) {
				$(this).hover(function (e) {
					//$('.infoBox', this).css({paddingBottom: $('.infoBox', this).hasClass('above')? 80 : 0 });
					$('.infoBox', this).show();
				}, function () {
					$('.infoBox', this).hide()
				});
				$("p:last", this).addClass("last");
			}
		});
		$(".serviceUpdates tr").hover(function () {
			if ($(".serviceUpdates tr").index(this) != 0) {
				$(this).addClass('listingHover');
			}
		}, function () {
			if ($(".serviceUpdates tr").index(this) != 0) {
				$(this).removeClass('listingHover');
			}
		}).mousedown(function () {
			if ($(".serviceUpdates tr").index(this) > 0) {
				window.location = $(this).find('.col1 a').attr('href');
			}
		});
	},

	plannedDisruptions: function () {
		$(".plannedDisruptions td.col2").each(function () {
			if ($('.infoBox', this).length > 0) {
				$('.infoBox', this).hide();
			}
		});
		$(".plannedDisruptions tr").hover(function () {
			if ($(".plannedDisruptions tr").index(this) != 0) {
				$(this).addClass('listingHover');
				$('.infoBox', this).show();
			}
		}, function () {
			if ($(".plannedDisruptions tr").index(this) != 0) {
				$(this).removeClass('listingHover');
				$('.infoBox', this).hide();
			}
		}).mousedown(function () {
			if ($(".plannedDisruptions tr").index(this) > 0) {
				window.location = $(this).find('.col1 a').attr('href');
			}
		});
	},

	careersHoverAreas: function () {
		if ($(".tabContent .hoverArea").length) {
			$(".tabContent .hoverArea a").hover(function () {
				$(this).siblings(".hoverBox").show();
			}, function () {
				$(this).siblings(".hoverBox").hide();
			}).click(function () {
				return false;
			});
		}
	},

	careersIntlFx: function () {
	/*
		var linksList = $("body.careersIntl .regionLowerRight .wysiwyg ul"),
			listLength = $("li", linksList).length,
			listMid = ((listLength / 2) + listLength % 2),
			group = $("li:lt(" + listMid + ")", linksList).remove();


		while (group.length) {
			$("<ul />").append(group).appendTo(".regionLowerRight .wysiwyg");
			group = linksList.find("li:lt(" + listMid + ")").remove();
		}
		$("body.careersIntl .regionLowerRight .wysiwyg ul").addClass("cols");
	*/

		if ($("body.careersIntl .summary a.overlay").length) {
			$("body.careersIntl .summary a.overlay[href*=youtube]").click(function () {
				if ($("[href*=showinfo]", this).length == 0) {
					$(this).attr("href", $(this).attr("href") + "&showinfo=0");
				}				
				$.fancybox({
					'hideOnContentClick': false,
					'overlayOpacity': 0.3,
					'overlayColor': '#000',
					'centerOnScroll': true,
					'showCloseButton': true,
					'href': $(this).attr("href").replace(new RegExp("watch\\?v=", "i"), 'v/'),
					'type': 'swf',
					'swf': { 'allowfullscreen': 'true' }
				});
				return false;
			});
			return false;
		}
	}
};

