$(function(){
	jQuery.preloadImages = function()
	{
	  for(var i = 0; i<arguments.length; i++)
	  {
	    jQuery("<img>").attr("src", arguments[i]);
	  }
	}
	$.preloadImages("images/photo1.jpg", "images/photo2.jpg",
	"images/photo3.jpg", "images/photo4.jpg");
	
	var captions = new Array();
	$.ajax({
	    type: "GET",
		url: "captions.xml",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('text').each(function(){
				var text = $(this);
				captions.push(text);
			});
			$('#caption').html(captions[0]);
		}
	});
	var images = ['images/homepage/photo4.jpg','images/homepage/photo1.jpg', 'images/homepage/photo2.jpg', 'images/homepage/photo3.jpg'];
	//var captions = ['caption 1', 'caption2', 'caption3', 'caption4'];	
	$('#image').attr('style', 'background: url('+images[0]+');');

	$('#rightarrow').click(function(){
		var lastItem = images[3];
		images.pop();
		images.unshift(lastItem);
		var lastcaption = captions[3];
		captions.pop();
		captions.unshift(lastcaption);
		$('#image').fadeOut('normal', function(){
			$('#image').attr('style', 'background: url('+images[0]+');display:none;');			
			$('#image').fadeIn('normal');
			$('#caption').html(captions[0]);				
		});
	});
	$('#leftarrow').click(function(){
		var firstItem = images[0];
		images.shift();
		images.push(firstItem);
		var firstcaption = captions[0];
		captions.shift();
		captions.push(firstcaption);
		$('#image').fadeOut('normal', function(){	
			$('#image').attr('style', 'background: url('+images[0]+');display:none;');
			$('#image').fadeIn('normal');
			$('#caption').html(captions[0]);
		});
	});
});
