
var mover;
var newsXMLPath = "storage/xml/Foundation_challenge_news.xml";
var currentNewsNum = 0;
var currentNewsItem;
var totalNumItems = 0;

var quoteArray = new Array('storage/images/homepage_quote_1.png', 'storage/images/homepage_quote_1.png');

var initHomepage = function() {
	
	var quoteNum = Math.floor(Math.random() * quoteArray.length);
	$('#quoteDiv').css('background-image', 'url('+quoteArray[quoteNum]+')');
	
	$('#newsContent').empty();
	
	$.ajax({
		type: "GET",
		url: encodeURI(newsXMLPath),
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
				
				var i = 0;
				
				$(this).find('item').each(function(){
					
					var title = $(this).attr("title");
					var thumb = $(this).attr("thumbnail");
					var excerpt = $(this).attr("excerpt");
					var linkURL = $(this).attr("link");
					
					var newsEntry = $('<div class="newsItem" id="newsItem'+i+'"><a href="'+linkURL+'"><h2>'+title+'</h2><img src="'+thumb+'" /><p>'+excerpt+'</p><div style="clear:both;"></div></a></div>').appendTo('#newsContent');
					
					if(i == 0)
					{
						currentNewsItem = newsEntry;
					}
					else
					{
						newsEntry.hide();
					}
					
					/*
					newsEntry.click( function() {
						showNewsItem(title, content);
					});
					*/
					
					i++
					
				});
				
				totalNumItems = i;
				
				$('#newsArrowLeft').click( function() {
					getNewsItem(-1);
				});
				
				$('#newsArrowRight').click( function() {
					getNewsItem(1);
				});

					
			});
		}
	});

	
}

var getNewsItem = function(direction)
{
	var lastNum = currentNewsNum;
	currentNewsNum += direction;
	
	if(currentNewsNum < 0)
	{
		currentNewsNum = totalNumItems - 1;
	}
	else if(currentNewsNum > totalNumItems - 1)
	{
		currentNewsNum = 0;
	}
	
	if(direction == 1)
	{
		currentNewsItem.stop().animate({
				marginLeft: -377
				}, 300, "swing",
			function() { showNextNewsItem(); }
			);		
	}
	else
	{
		currentNewsItem.stop().animate({
				marginLeft: 377
				}, 300, "swing",
			function() { showNextNewsItem(); }
			);		
	}
	
}

var showNextNewsItem = function()
{
	currentNewsItem.hide();
	
	currentNewsItem = $('#newsItem'+currentNewsNum);
	
	$('h2', currentNewsItem).css('margin-left', 0);
	$('h2', currentNewsItem).css('opacity', 0);
	$('h2', currentNewsItem).animate({
		marginLeft: 16,
		opacity: 1
		}, 300, "swing");
	
	$('img', currentNewsItem).css('margin-top', 0);
	$('img', currentNewsItem).css('opacity', 0);
	$('img', currentNewsItem).delay(300).animate({
		marginTop: 20,
		opacity: 1
		}, 300, "swing");
	
	$('p', currentNewsItem).css('margin-left', 0);
	$('p', currentNewsItem).css('opacity', 0);
	$('p', currentNewsItem).delay(600).animate({
		marginLeft: 16,
		opacity: 1
		}, 300, "swing");
	
	currentNewsItem.show();
	currentNewsItem.css('margin-left', 0);
	
}















