var images_per_page = 15;
var num_of_artists  = 32;
var page_images = new Object(); 

if (document.images != null) {

    artists = [ false,
                { name : "Oliver Bancroft", id : 11 },
                { name : "Thomas Bangstead", id : 21 },
                { name : "David Blandy", id : 22 },
                { name : "Margarita Bofiliou", id : 23 },
                { name : "David Carbone", id : 24 },
                { name : "Lali Chetwynd", id : 25 },
                { name : "Petros Chrisostomou", id : 26 },
                { name : "Joe Clark", id : 27 },
                { name : "James Connelly", id : 28 },
                { name : "Tessa Farmer", id : 29 },
                { name : "Lynette Yiadom-Boakye", id : 52 },
                { name : "Sarah Gilder", id : 31 },
                { name : "Karoly Keseru", id : 38 },
                { name : "Mauricio Guillen", id : 33 },
                { name : "Samson Kambalu", id : 36 },
                { name : "Thomas Hylander", id : 34 },
                { name : "Yvonne Jones", id : 35 },
                { name : "Ahn Kang-Hyun", id : 37 },
                { name : "Anton Goldenstein", id : 32 },
                { name : "Heidi Kilpeleinen", id : 39 },
                { name : "Steven Lowery", id : 40 },
                { name : "Nicky Magliulo", id : 41 },
                { name : "Gary McDonald", id : 42 },
                { name : "Sarah Michael", id : 43 },
                { name : "Thomas Needham", id : 44 },
                { name : "Robert Nicol", id : 45 },
                { name : "David Rowland", id : 46 },
                { name : "Michael Sailstorfer", id : 47 },
                { name : "Margaret Salmon", id : 48 },
                { name : "Heiko Tiemann", id : 49 },
                { name : "Douglas White", id : 50 },
                { name : "Oriana Fox", id : 30 }
              ];
  
    seen = new Array();
    // Set up the artist list, with the 0 element = false so the indexes work
    artist_list = new Array(false);

    for (var i = 0; i < images_per_page; i++) {
        // Choose a random number from 1 to 32
        var artist_id = get_rand(num_of_artists - 1) + 1;
        if (!seen[artist_id]) {
            // Add this id to the list of seen numbers
            seen[artist_id] = true;
            // Push the id onto the list of artists to show
            artist_list[artist_list.length] = artist_id;
        } else {
            // We've already seen this number;
            // decrement the loop counter and look for one we haven't seen.
            i--;
        }
    }
    
    for (var i = 0; i <  artist_list.length; i++) {
        if (artist_list[i]) {
            // Get the artist id from the list
            var aid = artist_list[i];
            // Convert the artist id and the image id to strings
            var img_str = int_to_text(i);
            var aid_str = int_to_text(aid);

            // Create an object to hold the image information
            var image = new Object();
            image.artist_name = artists[aid].name; // The name of the artist for this image
            image.artist_real_id = artists[aid].id; // The real (database) artist id
            imgs = new Array(); // An array for all the fade in/out images
            for (var j = 0; j < 5; j++) {
                img = new Image();
                img.src = "images/fades/2004/"+aid_str+j+".jpg";
                imgs[j] = img;
            }
            image.fade_imgs = imgs;
            // Set up the fade counters and timers
            image.fade_ctr = 0;
            image.in_timeout_id = image.out_timeout_id = null;
            image.in_timer = image.out_timer = false;

            // Put the finished object into the page_images array
            page_images[img_str] = image;
        }
    }
}

function fadeIn(imgName) {
    if (document.images != null) {
        var i = ++page_images[imgName].fade_ctr;
        if (page_images[imgName].out_timer) {
            clearTimeout(page_images[imgName].out_timeout_id);
        }

        var img;
        if (document.getElementById != null) {
            img = document.getElementById("IMG"+imgName);
        } else {
            img = document.images[imgName];
        }
        img.src = page_images[imgName].fade_imgs[i].src;

        page_images[imgName].in_timer = true;
        if (i < 4) {
            page_images[imgName].in_timeout_id = setTimeout("fadeIn('"+imgName+"')", 50);
        } else {
            page_images[imgName].in_timer = false;
        }
    }
}

function fadeOut(imgName) {
    if (document.images != null) {
        var i = --page_images[imgName].fade_ctr;
        if (page_images[imgName].in_timer) {
            clearTimeout(page_images[imgName].in_timeout_id);
        }

        var img;
        if (document.getElementById != null) {
            img = document.getElementById("IMG"+imgName);
        } else {
            img = document.images[imgName];
        }
        img.src = page_images[imgName].fade_imgs[i].src;

        page_images[imgName].out_timer = true;

        if (i > 0) {
            page_images[imgName].out_timeout_id = setTimeout("fadeOut('"+imgName+"')", 100);
        } else {
            page_images[imgName].out_timer = false;
        }
    }
}

function initImages() {
    if (document.images != null) {
        for (var n = 1; n <= images_per_page; n++) {

            var name = int_to_text(n);
            var img; var a;
            if (document.getElementById != null) {
                img = document.getElementById("IMG"+name);
                a   = document.getElementById("A"+name);
            } else {
                img = document.images[name]; 
                a   = document.links[name];
            }

            img.src = page_images[name].fade_imgs[0].src;
            img.alt = page_images[name].artist_name;
            a.href = "http://www.newcontemporaries.org.uk/artist_single.php?aid="+page_images[name].artist_real_id;
        }
    }
}

function get_rand(max) {
    var rand_num = Math.round(Math.random() * max);
    return rand_num;
}

function int_to_text(i) {
    var units_and_teens = new Array("zero", "one", "two", "three", "four", 
                                "five", "six", "seven", "eight", "nine",
                                "ten", "eleven", "twelve", "thirteen", "fourteen",
                                "fifteen", "sixteen", "seventeen", "eighteen", "nineteen");
    var tens = new Array("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety");

    if ((i < 0) || (i > 99)) {
        return "int_to_text: Input out of bounds";
    }

    var str = "";
    if (i > 19) {
        str += tens[Math.floor(i / 10) - 2];
        i %= 10;
    }
    if ((i > 0) || (i == 0 && str == "")) {
        name = units_and_teens[i];
        str += name;
    }
    return str;
}
