var images_per_page = 15;
var num_of_artists  = 18;
var page_images = new Object(); 

if (document.images != null) {

    artists = [ false,
                { name : "Holly Antrum", id : 1417 },
                { name : "Salvatore	Arancio", id : 1418 },
                { name : "Dafni	Barbageorgopoulou", id : 1419 },
                { name : "Becky	Beasley", id : 1420 },
                { name : "Kiran	Kaur Brar", id : 1421 },
                { name : "Sarah	Bridgland", id : 1422 },
                { name : "Andrea Buttner", id : 1423 },
                { name : "Jennifer Evans", id : 1424 },
                { name : "Stephen Connolly", id : 1425 },
                { name : "Jessie Flood Paddock", id : 1426 },
                { name : "Katherine Kicinski", id : 1427 },
                { name : "Neil McNally", id : 1428 },
                { name : "Katy Moran", id : 1429 },
                { name : "Tom Price", id : 1430 },
                { name : "Florian Roithmayr", id : 1431 },
                { name : "Akiko and Masako Takada", id : 1432 },
                { name : "Douglas White", id : 1433 },
                { name : "Jeremy Willett", id : 1434 },            
              			];
  
    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/2006/"+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;
}
