// JavaScript Document
// Version: v1.1

	// Program Variables
	var image_fade_speed = 550; // milliseconds
	var timer_delay = 10; // seconds

	// Set Current Image (start_image can be defined on HTML page to override)
	start_image = 0;
	var current_image = start_image;
	
	// Set AutoStart to True
	var auto_cycle = true;
	
	// Add to jQuery ability to preload images
	jQuery.preloadImages = function()
	{
		for(var i = 0; i<arguments.length; i++)
		{
			jQuery("<img>").attr("src", arguments[i]);
		}
	}

	// Function to perform the rotation
	function do_image_flip(i, t, a, c) { 
		if (i >= a.length) { i = 0; }
		if (i < 0) { i = a.length-1; }
		$('#rotate_image')
			// Fade out the image
			.fadeOut(t, function () {
				// When done fading out...
				// Change the SRC of the IMG
				$('#rotate_image').attr('src', a[i]);
			});
		$('#rotate_content')
			.fadeOut(t, function () {
				$('#rotate_content').load(c[i], '', function () { 
					$('#rotate_content').fadeIn(image_fade_speed); 
				});
			});
		return i;
	}	


	// On Load do the following...
	$(function () {

		// $("ul.nav").superfish();
		
		// Preload Images
		$.each(image_names, function(i, f) {
			$.preloadImages(f);
		});
		
		var flip_cycle = function() {
			current_image++; 
			current_image = do_image_flip(current_image, image_fade_speed, image_names, content_names);
		}
		
		// When the image finishes loading, fade it in
		$('#rotate_image').load(function () {
			$('#rotate_image').fadeIn(image_fade_speed);
		});

		// When the content finishes loading, fade it in
		// $('#rotate_content').load(function () {
		// 	$('#rotate_content').fadeIn(image_fade_speed);
		// });

		// Add CLICK functionality to NEXT button
		$('#next').click(function () {
			clearInterval(image_timer); auto_cycle = false;
			current_image++;
			current_image = do_image_flip(current_image, image_fade_speed, image_names, content_names);
			return false;
		});
		
		// Add CLICK functionality to PREV button
		$('#prev').click(function () {
			clearInterval(image_timer); auto_cycle = false;
			current_image--;
			current_image = do_image_flip(current_image, image_fade_speed, image_names, content_names);
			return false;
		});
		
		// Automatically Start Rotation
		var image_timer;
		if (auto_cycle) { image_timer = setInterval(flip_cycle, timer_delay*1000); }
		
		// $('#next').click();
		
	});
	
	// Debug
	// function debug(m) { $('#debug_text').prepend(m + "<br>"); }
	
	
