var W = 80;

var H = 80;

var LocationTemplate = "";

var Sparklers;



function makeAttr(name, value) {

	return ' ' + name + '="' + value + '"';

}



var PicId = 0;

function RandomPic(pic, id, name) {

	this.pic = pic + ((HasExtension(pic)) ? '' : '.jpg');

	this.id = id;

	this.elementId = PicId++;

	this.name = name;

	this.JSname = name.replace(/'/,"\\'");

	this.visible = false;

	this.writePic = RandomPicWrite;

	this.writePicLinked = RandomPicWriteLinked;

	this.writePicHidden = RandomPicWriteHidden;

	this.writeIMG = RandomPicWriteIMG;

	this.select = RandomPicSelect;

	this.replaceWith = RandomPicReplace;

	this.imageHTML = RandomPicImageHTML;

}

function HasExtension(pic) {

	var dPos = pic.lastIndexOf(".");

	if (dPos < 0) return false;

	return 'jpg.jpeg.gif'.indexOf(pic.substring(dPos+1)) >= 0;

}

function RandomPicWriteIMG(path, w, h, visible) {

	document.write("<img class='frontBabies'",

			makeAttr("src", path + "/" + this.pic),

			makeAttr("border", 0),

			makeAttr("width", (visible ? (w == null ? W : w) : 1)),

			makeAttr("height", (visible ? (h == null ? H : h) : 1)),

			(visible ? "" : makeAttr("style", "visibility:hidden")),

			">");

}



function RandomPicWrite(path, w, h) {

	document.write(

		"<span",

		makeAttr("id", "randompic_" + this.elementId),

		">");

	this.writeIMG(path, w, h, true);

	document.write("</span>&nbsp;");

	this.visible = true;

}



function RandomPicWriteHidden(path, w, h) {

	this.writeIMG(path, w, h, false);

	this.visible = false;

	trace("make pics[" + this.elementId + "] hidden");

}



function RandomPicWriteLinked(cellNo, path, w, h) {

	document.write(

		"<span",

		makeAttr("id", "randompic_" + cellNo),

		">");

	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, true);		

	document.write("</a>");

	document.write("</span>&nbsp;");

	this.visible = true;

}

function RandomPicSelect() {

	location = LocationTemplate.replace("{0}", this.id);

}

function RandomPicReplace(pic, path, w, h) {

	this.visible = false;

	pic.visible = true;

	trace("make pics[" + this.elementId + "] hidden");

	trace("make pics[" + pic.elementId + "] visible");

	picEl = getObject("randompic_" + this.elementId);

	if (picEl)

	{

		if (picEl.innerHTML.toUpperCase().indexOf("<A") >= 0) 

			{

			picEl.innerHTML = "<a" +

				makeAttr("href","javascript:selectRandomPic(" + pic.id + ");") +

				makeAttr("onmouseover", "window.status='" + pic.JSname + "'; return true;") +

				makeAttr("onmouseout", "window.status=''; return true;") +

				">" +

				pic.imageHTML(path, w, h) +

				"</a>";

			picEl.id = pic.elementId;

			}

		else

			picEl.innerHTML = pic.imageHTML(path, w, h);

	}

}

function RandomPicImageHTML(path, w, h) {

	return "<img class='frontBabies'" +

				makeAttr("src", path + "/" + this.pic) +

				makeAttr("border", 0) +

				makeAttr("width", w == null ? W : w) +

				makeAttr("height", h == null ? H : h) +

				">";

}

// 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);

}

var VisiblePics;

var HiddenPics;

var SparkleDuration = 0;

var SparkleMaxDuration = 0;

// entry point, e.g. writeSparklePics(15,20,0,6,3,"images",80,80,"rhwl_{0}.aspx");

function writeSparklePics(m, n, delay, interval, deviation, path, w, h, template, duration) {

	LocationTemplate = template;

	SparkleMaxDuration = duration

    VisiblePics = new Array(m);

    HiddenPics = new Array (n-m);

	var a = selectRandom(n, pics.length);

	var i;

	for (i = 0; i < m; i++)

	{

		VisiblePics[i] = pics[a[i]];

		pics[a[i]].writePicLinked(i, path, w, h);

	}

	for (i = m; i < n; i++)

	{

		HiddenPics[i-m] = pics[a[i]];

		pics[a[i]].writePicHidden(path, w, h);

	}

	SparkleDuration += delay;

	setTimeout("sparkle(" + interval + "," + deviation + ",'" + path + "'," + w + "," + h + ")", delay*1000)

}

function sparkle(interval, deviation, path, w, h) {

	//if (picNo == Sparklers.length) return;

	if (SparkleDuration >= SparkleMaxDuration) return;

	// select a random index in each of the arrays VisiblePics, HiddenPics

	var iVisible = Math.floor(Math.random() * (VisiblePics.length));

	var iHidden = Math.floor(Math.random() * (HiddenPics.length));

	var picToReveal = HiddenPics[iHidden];

	HiddenPics[iHidden] = VisiblePics[iVisible];

	VisiblePics[iVisible] = picToReveal;

	var picEl = getObject("randompic_" + iVisible);

	if (picEl)

	{

		picEl.innerHTML = "<a" +

			makeAttr("href","javascript:selectRandomPic(" + picToReveal.id + ");") +

			makeAttr("onmouseover", "window.status='" + picToReveal.JSname + "'; return true;") +

			makeAttr("onmouseout", "window.status=''; return true;") +

			">" +

			picToReveal.imageHTML(path, w, h) +

			"</a>";

	}

	var nextDelay = (interval-deviation) + (Math.random() * 2*deviation)

	SparkleDuration += nextDelay

	setTimeout("sparkle(" + interval + "," + deviation + ",'" + path + "'," + w + "," + h + ")", nextDelay *1000)

}

function selectRandomPic(id) {

	var i;

	for (i = 0; i < pics.length; i++)

		if (pics[i].id == id)

			pics[i].select();

}

// used for Jane's pics e.g. of people

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);

}

