﻿/**************************************************************************************
    
Ticker
    
Author: Steve Hobbs
Site: SMG
Date: 02/06/2009
    
Dependencies: jquery.1.3.2
    
*************************************************************************************/

var paused = false;         // Whether or not the news item swap is paused
var interval = 10000;        // The length of time items stay on the page for (ms)
var speed = 800;            // The animation time (ms)

$(document).ready(function()
{
	$(".additionalContent").hide();
	//$(".tickerContainer").hide();

	// Pause the current item when mousing over
	$(".tickerContainer").mouseenter(function()
	{
		paused = true;
	});

	// Unpause the item when mousing-out
	$(".tickerContainer").mouseleave(function()
	{
		paused = false;
	});

	// Grab all the stories that we need
	$.ajax({
	dataType: "xml",
	type: "POST",
			data: {},
			error: function(request, textStatus, errorThrown)
			{
				alert(errorThrown + ": " + textStatus);
			},
			url: "/services/ticker.ashx",
			success:  function(transport)
				{
					$(".additionalContent").empty();

					var itemCount = $(transport).find("item").length;

					$(transport).find("item").each(function()
					{
						var headline = $(this).find("headline").text();
						var story = $(this).find("storyContent").text();
						var location = $(this).find("location").text();
						var storyUrl = $(this).find("storyUrl").text();

						var htmlContent = "<p class='container'>" + story + "&nbsp;&nbsp;<strong>" + headline + ", " + location + "</strong></p>";

						$(".additionalContent").append(htmlContent);
					});

					var first = $(".additionalContent .container:first").html();
					$(".tickerContainer").empty();
					$(".tickerContainer").append(first);
					$(".additionalContent .container:first").remove();

					//$(".tickerContainer").show();

					if (itemCount > 1)
				        setInterval(swapContent, interval);
			}
		});

});

function swapContent()
{
	if (!paused)
	{
		var current = $(".tickerContainer").html();
		var next = $(".additionalContent .container:first").html();

		$(".tickerContainer").slideUp(speed, function()
		{
			$(".tickerContainer").empty();
			$(".additionalContent .container:last").after("<div class='container'>" + current + "</div>");

			$(".tickerContainer").append(next);
			$(".additionalContent .container:first").remove();

			$(".tickerContainer").slideDown(speed);
		});
	}
}
