(function ($) {
	var CSS_HREF = '/assets/scripts/masthead.js.css';
	$(function () {
		var mastheads = $('.masthead-carousel');

		if (mastheads.length > 0) {
			$('<link rel="stylesheet" href="' + CSS_HREF + '" type="text/css" media="screen" />').appendTo('head');
		}

		$('.masthead-carousel').each(function () {
			var carousel = $(this).find('.carousel');

			carousel.find('li').hover(function () {
				$(this).siblings('.on').removeClass('on');
				$(this).addClass('on');
			}, function () {
			});

			var ROTATION_DELAY = 4000;

			var pauseRotation = false;
			carousel.hover(function () {
				pauseRotation = true;
			}, function () {
				pauseRotation = false;
			});

			function transition() {
				if (!pauseRotation) {
					var on = carousel.find('li.on');
					on.removeClass('on');
					var next = on.next();
					if (next.length <= 0) {
						next = on.prevAll(':first-child');
					}
					next.addClass('on');
				}
				setTimeout(transition, ROTATION_DELAY);
			}

			setTimeout(transition, ROTATION_DELAY);
		});
	})
})(jQuery);
