//configuration
var timeout = 6000 //10 second
var image_rotate_ajax_script = "http://judybradford.com/wp/wp-content/themes/pretty-parchment/js/rotate_image.php";
var random = false;
//end configuration

var image_list = [];
var current = 0;

$(document).ready( function() {
    $.get(image_rotate_ajax_script, {}, function(resp) {
        image_list = resp;
        if (random == true) {
            random_image();
        } else {
            next_image();
        }
    }, "json");
});

function next_image() {
    if (image_list.length > 0) {
        if (current > (image_list.length - 1)) {
            current = 0;
        }
        set_image(image_list[current]);
        current++;
        setTimeout( function() {
           next_image();
        }, timeout);
    }
}

function random_image() {
    if (image_list.length > 0) {
        var random = Math.floor(Math.random() * image_list.length);
        set_image(image_list[random]);
        setTimeout(function() {
           random_image();
        }, timeout);
    }
}

function set_image(url) {
    $(".SeeingInteractive_temp_img").remove();
    $("body").append('<img src="'+ url +'" class="SeeingInteractive_temp_img" style="position: absolute; left: -9999" ></img>');
    $(".SeeingInteractive_temp_img").load(function() {
        $(this).remove();
        $("#SeeingInteractive_header_img").fadeOut(1000, function() {
            $("#SeeingInteractive_header_img").replaceWith('<img src="'+ url +'" id="SeeingInteractive_header_img" style="display: none;"/>');
            $("#SeeingInteractive_header_img").fadeIn(1000);
        });
    });
}