var mainImages = new Array(
	"/images/front00.jpg",
	"/images/front01.jpg",
	"/images/front02.jpg",
	"/images/front03.jpg",
	"/images/front04.jpg",
	"/images/front05.jpg"
	);
var counter = 1;

$(document).ready(function()
{
    /* Set up the scheduling form */
    $("#scheduling select").change( function () {
        calculateVisibility();
    });
    $("#scheduling :submit").click( function() {
        return beginScheduling();
    });
    calculateVisibility();
    
	setTimeout(rotateMainImages, 7 * 1000);
	
	/* While we're waiting, lets preload the images */
	for(i = 0; i < mainImages.length; i++) {
		var tempImage = new Image();
		tempImage.src = mainImages[i];
	}
});


function calculateVisibility()
{
    if($("#project_type").val() == "existing_project") {
        $("#client_type_cont").hide();
        $("#project_type_submit_cont").show();
    }
    else {
        $("#client_type_cont").show();
        $("#project_type_submit_cont").hide();
    }
}

function beginScheduling()
{
    if($("#project_type").val() == "existing_project") {
        window.location = "/scheduling/existing_project/";
        return (false);
    }
    else if ($("#client_type").val() == "new_client") {
        window.location = "/scheduling/new_client/";
        return (false);
    }
    else {
        window.location = "/scheduling/new_project/";
        return (false);
    }
}

function displayPhoto()
{
	/* We can't (always) just fadeIn because the fadeIn will usually start before the next image
	 * has finished loading, causing the image swap to be during or after the fadeIn.  As a result,
	 * we have to check whether the image has been loaded and, if not, wait a while before trying again.
	 * This is less of a problem when the images have been preloaded.
	 */
	var tempImage = new Image();
	tempImage.src = $("#spotlight_photo img").attr("src");
	
	if (tempImage.complete != false) {
		$("#spotlight_photo img").fadeIn("slow");		
	}
	else {
		setTimeout(
			function() {
				displayPhoto();
			}, 100
			);
	}

}

function rotateMainImages()
{
	$("#spotlight_photo img").fadeOut(
		"slow",
		function () {
			/* Set the image's source to the next value in our mainImages array, wrapping to the first
			 * element when necessary
			 */
			$("#spotlight_photo img").attr({src:mainImages[(counter<mainImages.length?++counter:counter=1)-1]});
			displayPhoto();
		}
		);

	// Swap images without fading
	//$("#spotlight_photo img").attr({src:mainImages[(counter<mainImages.length?++counter:counter=1)-1]});


	setTimeout(rotateMainImages, 7 * 1000);
}
