var main_images = ["01.jpg", "02.jpg", "03.jpg"];
var main_links = ["custom_invitations", "custom_invitations", "custom_invitations"];

var preload = [];
function preload_main_images ()
{
	for (i in main_images)
	{
		preload[i] = new Image();
		preload[i].src = "http://thoughtfulday.com/images/front_page/main/"+main_images[i];
	}
}

function swap_main_image ()
{
	var img = find_element ("main");
	var link = find_element ("main_link");
	var next_image_is_it = false;

	for (i in main_images)
	{
		var image = "http://thoughtfulday.com/images/front_page/main/"+main_images[i];

		// If the last image in the array was the current src, then change the src to this image and update its link.
		if (next_image_is_it == true)
		{
			img.src = image;
			if (main_links[i]) link.href = main_links[i];
			next_image_is_it = false;

		// If the src is this image in the array, then the next image in the array is the one we're looking for.
		} else if (img.src == image) {
			next_image_is_it = true;
		}
	}

	// If the src is the last image in the array, then change the src to the first.
	if (next_image_is_it == true)
	{
		img.src = "http://thoughtfulday.com/images/front_page/main/"+main_images[0];
		link.href = main_links[0];
	}

	// If there's more than one image to alternate between, run again after a pause.
	if (main_images.length > 1) setTimeout('swap_main_image ();', 5000);
}