var noOfItems;
var currentItem;

function generateSlide(currentItem) {
	if (currentItem == noOfItems) {
		currentItem = 0;
		prev = noOfItems;
	} else {
		prev = currentItem;
	}

	$('#slides li').removeClass('current');
	$('#slides li:nth-child('+(currentItem+1)+')').addClass('current').show();
	$('#slides li:nth-child('+(currentItem+1)+') img').fadeIn('slow');
	
	textTimer = setInterval(function() {
      $('#slides li:nth-child('+(currentItem+1)+') span').fadeIn('slow');
		clearInterval(textTimer);
		
		$('#slides li:nth-child('+prev+')').hide();
		$('#slides li:nth-child('+prev+') img').hide();
		$('#slides li:nth-child('+prev+') span').hide();	
	}, 1000);
}

$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "slides.xml",
		dataType: "xml",
		success: function(xml) {
			idNum = 1;
			$(xml).find('slide').each(function(){
				var image = $(this).find('image').text();
				var text = $(this).find('text').text();
				
				var top = $(this).find('text').attr('top') + 'px';
				var left = $(this).find('text').attr('left') + 'px';
				var width = $(this).find('text').attr('width') + 'px';
				var height = $(this).find('text').attr('height') + 'px';
				$('#slides').append('<li id="item'+idNum+'"><img src="images/'+image+'" /><span style="top:'+top+'; left:'+left+'; width:'+width+'; height:'+height+';">'+text+'</span></li>');
				idNum++
			});
			
			$('#slides li').hide();
			$('#slides li img').hide();
			$('#slides li span').hide();
			
			noOfItems = $('#slides li').length;
			generateSlide(0);
			
			slideTimer = setInterval(function() {
				currentItem = $('.current').index() + 1;
				generateSlide(currentItem);
			}, 4000);
		}
	});
});
