var W = 200;
var H = 250;

var pics = new Array(
           new RandomPic("1", 1, "1"),
           new RandomPic("2", 1, "2"),
           new RandomPic("3", 1, "3"),
           new RandomPic("4", 1, "4"),
           new RandomPic("5", 1, "5"),
           new RandomPic("6", 1, "6"),
           new RandomPic("7", 1, "7"),
           new RandomPic("8", 1, "8"),
           new RandomPic("9", 1, "9"),
           new RandomPic("10", 1, "10"),
           new RandomPic("11", 1, "11"),
           new RandomPic("12", 1, "12"),
           new RandomPic("13", 1, "13"),
           new RandomPic("14", 1, "14"),
           new RandomPic("15", 1, "15"),
           new RandomPic("16", 1, "16"),
           new RandomPic("17", 1, "17"),
           new RandomPic("18", 1, "18"),
           new RandomPic("19", 1, "19"),
           new RandomPic("20", 1, "20"),
           new RandomPic("21", 1, "21"),
           new RandomPic("22", 1, "22"),
           new RandomPic("23", 1, "23"),
           new RandomPic("24", 1, "24"),
           new RandomPic("25", 1, "25"),
           new RandomPic("26", 1, "26"),
           new RandomPic("27", 1, "27"),
           new RandomPic("28", 1, "28"),
           new RandomPic("29", 1, "29"),
           new RandomPic("30", 1, "30"),
           new RandomPic("31", 1, "31")
);

function makeAttr(name, value) {
        return ' ' + name + '="' + value + '"';
}



function RandomPic(pic, id, name) {
        this.pic = pic;
        this.id = id;
        this.name = name;
        this.JSname = name.replace(/'/,"\\'");
        this.writePic = RandonPicWrite;
        this.writePicLinked = RandonPicWriteLinked;
        this.writeIMG = RandonPicWriteIMG;
        this.select = RandomPicSelect;
}

function RandonPicWriteIMG(path, w, h) {
        document.write("<img",
                        makeAttr("src", path + "/" + this.pic + ".jpg"),
                        //makeAttr("src", this.pic + ".jpg"),
                        makeAttr("border", 0),
                        makeAttr("width", w == null ? W : w),
                        makeAttr("height", h == null ? H : h),
                        makeAttr("alt", this.name),
                        ">");
}

function RandonPicWrite(path, w, h) {
        this.writeIMG(path, w, h);
        document.write("&nbsp;");
}



function RandonPicWriteLinked(path, w, h) {
        document.write("<a",
                        makeAttr("href","javascript:selectRandomPic(" + this.id + ");"),
                        makeAttr("onmouseover", "window.status='" + this.JSname + "'; return true;"),
                        makeAttr("onmouseout", "window.status=''; return true;"),
                        ">");

        this.writeIMG(path, w, h);
        document.write("</a>&nbsp;");
}



function RandomPicSelect() {
        alert(this.name);
}



// returns an array of m numbers, without duplicates, in the range 0 to n - 1

function selectRandom(m, n) {
        // create an array containing the numbers 0 .. n
        var a = new Array(n);
        var i;

        for (i = 0; i < n; i++)
                a[i] = i;

        // select m from n
        for (i = 0; i < m; i++, n--) {

                // select a number in the range 0 .. n
                var r = Math.floor(Math.random() * (n));

                // swap it with the number at position n-1
                var temp = a[n-1];
                a[n-1] = a[r];
                a[r] = temp;
        }

        // return the selected numbers
        return a.slice(n);
}



function writePics(m, path, w, h) {
        var a = selectRandom(m, pics.length);
        var i;

        for (i = 0; i < a.length; i++)
                pics[a[i]].writePic(path, w, h);
}



function writeLinkedPics(m, path, w, h) {
        var a = selectRandom(m, pics.length);
        var i;

        for (i = 0; i < a.length; i++)
                pics[a[i]].writePicLinked(path, w, h);
}

function selectRandomPic(id) {
        var i;
        for (i = 0; i < pics.length; i++)
                if (pics[i].id == id)
                        pics[i].select();
}