var image_folder = "/images/main/grapebay/";
var main_images = ["img1_1.jpg", "img2_1.jpg", "img3_1.jpg", "img4_1.jpg", 
				   "img5_1.jpg", "img6_1.jpg", "img7_1.jpg"];
var side_images = ["img1_2.jpg", "img2_2.jpg", "img3_2.jpg", "img4_2.jpg", 
				   "img5_2.jpg", "img6_2.jpg", "img7_2.jpg"];

var transition_delay = 10000;
var fade_delay = 500;
var second_timer_delay = 2000;

var main_index = 0;
var side_index = 0;
var main_timer = 0;
var side_timer = 0;

$(function() {
	main_index = Math.floor(Math.random() * main_images.length);
	side_index = main_index + 3;
	if (side_index >= side_images.length)
		side_index -= side_images.length;
		   
	$("#image1").html("<img src=\"" + image_folder + main_images[main_index] + "\" />");
	$("#image2").html("<img src=\"" + image_folder + side_images[side_index] + "\" />");
	
	$("#image1 img").load(function() { $(this).animate({opacity: 1}, fade_delay); });
	$("#image2 img").load(function() { $(this).animate({opacity: 1}, fade_delay); });
	
	main_timer = setInterval("update_image1()", transition_delay);
	setTimeout("start_second_timer()", second_timer_delay);
	
	preload_image(1);
	preload_image(2);
});

function start_second_timer() {
	side_timer = setInterval("update_image2()", transition_delay);
}

function update_image1() {
	main_index++;
	if (main_index >= main_images.length)
		main_index = 0;
	
	var src = image_folder + main_images[main_index];
	
	$("#image1 img").animate({opacity: 0}, fade_delay, function() {
		$(this).attr('src', src);
	});

	preload_image(1);
}
function update_image2() {
	side_index++;
	if (side_index >= side_images.length)
		side_index = 0;
	
	var src = image_folder + side_images[side_index];
	
	$("#image2 img").animate({opacity: 0}, fade_delay, function() {
		$(this).attr('src', src);
	});

	preload_image(2);
}

function preload_image(index) {
	var image_path = "";
	
	if (index == 1) {
		var next_index = main_index + 1;
		if (next_index >= main_images.length)
			next_index = 0;
		image_path = image_folder + main_images[next_index];
	} else {
		var next_index = side_index + 1;
		if (next_index >= side_images.length)
			next_index = 0;
		image_path = image_folder + side_images[next_index];
	}
	
	var img = new Image();
	img.src = image_path;
}

