﻿$(document).ready(function()
{
	App.preloadImages();
	App.bindHeroImage();
	App.bindHomeNav();
	App.bindNewsFeed();
});

App.preloadImages = function()
{
	for (var i = 0; i < heroImages.length; i++)
	{
		new Image().src = heroImages[i].url;
	}
};

App.bindHeroImage = function()
{
	window.setTimeout(function()
	{
		var container = $("#RotatingHero");
		var image = $("#RotatingHero img");
		var dataKey = "currentIndex";
		var newIndex = isNaN(container.data(dataKey)) ? 1 : container.data(dataKey) + 1;
		if (newIndex >= heroImages.length) newIndex = 0;
		container.data(dataKey, newIndex);
		container.css("background-image", "url(" + heroImages[newIndex].url + ")");
		image.fadeOut(3000, function()
		{
			image.attr("src", heroImages[newIndex].url);
			image.attr("alt", heroImages[newIndex].altText);
			image.fadeIn();
			App.bindHeroImage();
		});
	}, 10000);
};

App.bindHomeNav = function()
{
	// Standardise height
	var maxPanelHeight = 0;
	var list = $("#HomeNav ul:first");
	var items = $("#HomeNav ul:first > li");
	items.each(function()
	{
		var panel = $(this).find("div");
		if (panel.height() > maxPanelHeight) maxPanelHeight = panel.height();
	});
	list.height(maxPanelHeight);

	var currentClassName = "Current";
	var transitionTimer = null;
	var performTransitions = true;

	var activateItem = function(item, animate)
	{
		if (list.find("." + currentClassName)[0] != item[0])
		{
			if (animate)
			{
				// Get/build fader overlay
				var fader = list.find("#Fader");
				if (!fader.length)
				{
					var panel = item.find("div");
					var width = panel.outerWidth();
					var height = panel.outerHeight();
					list.append("<span id=\"Fader\" style=\"width:" + (width - 20) + "px;height:" + (height - 20) + "px;\"></span>");
					fader = list.find("#Fader");
					fader.show();
				}

				// Transition
				fader.fadeIn(1000, function()
				{
					items.removeClass(currentClassName);
					item.addClass(currentClassName);
					fader.fadeOut(1000, function()
					{
						action();
					});
				});
			}
			else
			{
				items.removeClass(currentClassName);
				item.addClass(currentClassName);
			}
		}
	};

	items.find("a:first").mouseover(function()
	{
		window.clearTimeout(transitionTimer);
		performTransitions = false;
		activateItem($(this).parents("li"), false);
	});

	var action = function()
	{
		if (performTransitions)
		{
			transitionTimer = window.setTimeout(function()
			{
				var newIndex = 0;
				for (var i = 0; i < items.length; i++)
				{
					if ($(items[i]).hasClass(currentClassName))
					{
						newIndex = i + 1;
						break;
					}
				}
				if (newIndex >= items.length) newIndex = 0;
				activateItem($(items[newIndex]), true);
			}, 8000);
		}
	};
	activateItem($(items[0]), true);
};

App.bindNewsFeed = function()
{
	var container = $("#NewsFeedContainer");
	var list = container.find("ul");
	var items = list.find("li");
	if (items.length > 1)
	{
		// Normalise height
		var maxHeight = 0;
		var paddingMarginHeight = 0;
		items.each(function(i, item)
		{
			var itemHeight = $(item).height();
			if (paddingMarginHeight == 0)
			{
				paddingMarginHeight = $(item).outerHeight({ margin: true }) - itemHeight;
			}
			if (itemHeight > maxHeight) maxHeight = itemHeight;
		});
		container.height(maxHeight);
		items.height(maxHeight);

		var moveOffset = maxHeight + paddingMarginHeight;
		var move = function()
		{
			var currentTop = parseInt(list.css("top"));
			list.animate(
			{
				top: "-=" + (moveOffset + currentTop) + "px"
			},
			{
				easing: "linear",
				duration: (4000 / (moveOffset / (moveOffset + currentTop))),
				complete: function()
				{
					var topItem = $(items[0]);
					$(items[0]).remove().appendTo(list);
					list.css("top", "0px");
					items = list.find("li");
					move();
				}
			});
		};
		list.css("position", "absolute");
		window.setTimeout(move, 1000);
		list.hover(function() { list.stop(); }, move);
	}
};