var PAUSE_INTERVAL = 6000;
var CAROUSEL_COUNT = 0;
var carouselTimer = null;

$(init);

function init()
{
	$("html").addClass("js");

	var splashContainer = $("#splash-container");

	splashContainer
		.css("height", $(".splash").height())
		

	var navigationHTML = '<div class="splash-navigation"><ul>';

	for (var i = 0; i < data.length; i++)
	{
		navigationHTML += '<li><a href="#">' + (i + 1) + '</a></li>';
	}

	navigationHTML += '</ul></div>';

	var navigation = $(navigationHTML);

	$("li", navigation).eq(0).addClass("on");

	$(".splash-positionmenu")
		.before(navigation)

	setupNav(navigation);

	var preloadedImages = {};

	for (var i = 0; i < data.length; i++)
	{
		var newImage = $('<img class="preloadedImage" src="' + data[i].imageUrl + '" width="1" height="1" />');

		$("body").append(newImage);
	}

	carouselTimer = setTimeout(playCarousel, PAUSE_INTERVAL);
}




function preSetupNav(navigation)
{

	$("a", navigation)
		.click(function()
			{					
				return false;
			}
		)
}




function setupNav(navigation)
{
	$("a", navigation)
		.click(function()
			{
				if (carouselTimer != null)
				{
					clearTimeout(carouselTimer);
				}

				var navigation = $(".splash-navigation");
				var parent = $(this.parentNode);

				if (!parent.hasClass("on"))
				{
					$("li", navigation)
						.removeClass("on")

					parent
						.addClass("on")

					var oldSplash = $(".splash").eq(0);

					var splash = $('<div class="splash"></div>').html(oldSplash.html());
					preSetupNav($(".splash-navigation", splash));

					var curr = parseInt($(this).text()) - 1;
					

					$("div#splash-text-container", splash)
						.css("display", "none"); 
					$("div#splash-pic", splash)
						.css("backgroundImage", "url(" + data[curr].imageUrl + ")")
						
						/*switch (CAROUSEL_COUNT) {
						case 1:
						var contentmargin = 219;
						break;
						case 2:
						var contentmargin = 50;
						break;
						case 3:
						var contentmargin = 219;
						break;
						case 0:
						var contentmargin = 50;
						break;
					}
					
					$("div#splash-text-container", splash)
						.css("margin-top", "" + contentmargin +"px"); */					

					$(".splash-content p", splash)
						.html(data[curr].description)
						
					$(".splash-content h2", splash)
						.html('<a href="' + data[curr].titleUrl + '">' + data[curr].title + '</a>')

					
					//fix for Register button
					// - if first entry does not have register link it adds one in so subsequent results will display it
					if (typeof data[curr].actUrl != "undefined" && data[curr].actUrl != "")
					{
						if ( $(".splash-buttons img[alt=Register]", splash).parent().html()==null )
						{
							$(".splash-buttons", splash).prepend('<a href="'+data[curr].actUrl+'"><img class="button-actnow" src="http://mailmanportal.com/uploads/WMRF/splash/splash-btn-register.png" alt="Register" /></a>');
						}

						$(".splash-buttons img[alt=Register]", splash).parent()
							.css("display", "inline")
							.attr("href", data[curr].actUrl)
							
					}
					else
					{							
						$(".splash-buttons img[alt=Register]", splash).parent()
							.css("display", "none")
					}

					//learn more button - only if no act now link
					//if ((typeof data[curr].actUrl == "undefined" || data[curr].actUrl == "") && (typeof data[curr].learnUrl != "undefined" && data[curr].learnUrl != ""))
					if (typeof data[curr].learnUrl != "undefined" || data[curr].learnUrl != "")
					{	
						//fix for learn more button
						// - if first entry does not have learn more link it adds one in so subsequent results will display it
						if ( $(".splash-buttons img[alt=Learn more]", splash).parent().html()==null )
						{
							$(".splash-buttons", splash).prepend('<a href="'+data[curr].learnUrl+'"><img class="button-actnow" src="http://mailmanportal.com/uploads/WMRF/splash/splash-btn-learn.png" alt="Learn more" /></a>');
						}

						$(".splash-buttons img[alt=Learn more]", splash).parent()
							.css("display", "inline")
							.attr("href", data[curr].learnUrl)
					}
					else
					{
						$(".splash-buttons img[alt=Learn more]", splash).parent()
							.css("display", "none")
					}
					
					
					if (typeof data[curr].donateUrl != "undefined" && data[curr].donateUrl != "")
					{
						//fix for donate button
						// - if first entry does not have donate link it adds one in so subsequent results will display it
						if ( $(".splash-buttons img[alt=Donate]", splash).parent().html()==null )
						{
							$(".splash-buttons", splash).append('<a href="'+ data[curr].donateUrl +'"><img src="http://mailmanportal.com/uploads/WMRF/splash/splash-btn-donate.png" alt="Donate" /></a>');
						}

						$(".splash-buttons img[alt=Donate]", splash).parent()
							.css("display", "inline")
							.attr("href", data[curr].donateUrl)
					}
					else
					{
						$(".splash-buttons img[alt=Donate]", splash).parent()
							.css("display", "none")
					}
					

					splash
						.css("opacity", 0)
						.appendTo("#splash-container")

					$("#splash-container")
						.animate(
							{
								height: splash.height()
							},
							1000
						)

					splash
						.animate(
							{
								opacity: 1
							},
							600
						)

					oldSplash
						.animate(
							{
								opacity: 0
							},
							400,
							function()
							{
								oldSplash.remove();
								setupNav($(".splash-navigation", splash));
							}
						)
						$("div#splash-text-container", splash)
						.css("display", "block"); 
				}

				return false;
			}
		)
}




function playCarousel()
{
	var items = $(".splash-navigation li");

	CAROUSEL_COUNT++;

	if (CAROUSEL_COUNT >= items.length)
	{
		CAROUSEL_COUNT = 0;
	}

	$("a", items.eq(CAROUSEL_COUNT))
		.click()

	carouselTimer = setTimeout(playCarousel, PAUSE_INTERVAL);
}