var images_per_page = 15;
var num_of_artists  = 37;
var page_images = new Object(); 

if (document.images != null) {

    artists = [ false,
                { name : "Steve Bishop", id : 1520 },
                { name : "Sarah Bowker-Jones", id : 1518 },
                { name : "Lucy Coggle", id : 1521 },
                { name : "Rhys Coren", id : 1522 },
                { name : "Charlie Crane", id : 1523 },
                { name : "Mary Ferguson", id : 1524 },
                { name : "David Fletcher", id : 1525 },
                { name : "Katarina Forss", id : 1526 },
                { name : "Alistair Frost", id : 1527 },
                { name : "Dido Hallett", id : 1528 },
                { name : "Gabriel Hartley", id : 1531 },
                { name : "Alexander Heim", id : 1532 },
                { name : "Laurie Hill", id : 1533 },
                { name : "Margot Hill", id : 1534 },
                { name : "Adam Holmes-Davies", id : 1535 },
                { name : "Luke Jackson", id : 1536 },
                { name : "Jackson Webb", id : 1537 },
                { name : "Hannah James", id : 1538 },
                { name : "Heike Kabisch", id : 1539 },
                { name : "Maria-Brigita Karantzi", id : 1540 },
                { name : "Camilla Kesterton", id : 1541 },
                { name : "Penny Klepuszewska", id : 1542 },
                { name : "Ian Larson", id : 1543 },
                { name : "Francisco Lobo", id : 1544 },
                { name : "Fiona Mackay", id : 1545 },
                { name : "Amy Grace McDonough", id : 1546 },
                { name : "Janine Mclellan", id : 1547 },
                { name : "Andrew Mealor", id : 1548 },
                { name : "Jason Nelson", id : 1549 },
                { name : "Jack Newling", id : 1550 },
                { name : "Gemma Pardo", id : 1551 },
                { name : "Daniel Pasteiner", id : 1552 },
                { name : "James Ryan", id : 1553 },
                { name : "Charlie Tweed", id : 1554 },
                { name : "Tom Walker", id : 1555 },
                { name : "Gesche WŸrfel", id : 1556 },
                { name : "Yohei Yashi", id : 1558 },     
              			];
  
    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/2007/"+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;
}
