/*
	==========================================================================
	Module: rando.js
==========================================================================
	Pre-canned functions used to work with continually
	randomizing images.	
	
	Copyright (c) 2001, Slava Kozlov, All Rights Reserved.

	Loosely inspired by this script
	http://www.dynamicdrive.com/dynamicindex14/image4.htm

==========================================================================
*/


// a token to signify potential images
var imToken = "CvBn";
// regex to find potential images
var imTokenReg = "^" + imToken;

var position = new Array();


// where to store the index of the curent image for each image location
var currentimage = new Array();


// returns an unused random image index from the imgnames array

function getRandomIndex(){
	if (!imgnames)
		return
	var notUsedFlag = true; // go thru while loop at least once
	while (notUsedFlag){
		r= Math.floor((Math.random()*imgnames.length));
		notUsedFlag = false;
		// see if this index is currently in use
		for (var i = 0; i < currentimage.length ; i++){
			if (currentimage[i] == r) {notUsedFlag = true;}
		}
	}
	return r;
}


// returns a random number in the speed interval

function getRandomSpeed(){
	return Math.floor((Math.random()*(maxSpeed -minSpeed)*1000)) + minSpeed*1000;
}

// finds the images on the page which are to be rando-ized
// fills up the positon array so we can map back
// from document.images to our rando images
// and sets up the timers with random speed amounts

function seed(){
	var j =0; var s;
	for (var i = 0; i < document.images.length ; i++){
		if (document.images[i].name.match(imTokenReg)) {
			s = getRandomSpeed();
			position[i] = j++;
			setTimeout("changeImage("+i+", "
				+ s +")", s);
		}
	}
}

// changes a given image (given by its index) to a rando unused image;
// then creates a timer to repeat operation (in speed milliseconds)
// used a filter if IE 4+ and fadeOut flag set

function changeImage(iname, speed){
	if (!document.images)
		return
	if (document.all && fadeOut)
		document.images[iname].filters.blendTrans.apply()

	newIndex = getRandomIndex();
	document.images[iname].src= imgnames[newIndex]

	// remember which image is in this rando position
	currentimage[position[iname]] = newIndex

	if (document.all && fadeOut)
		document.images[iname].filters.blendTrans.play()

	setTimeout("changeImage("+iname+","+speed+")",speed)
}


// returns the url for a given rando image

// function trueLink(index){
//	window.location=urls[currentimage[index]];
//}


// counter used to insure each image is appropriately indexed
var imCounter = 0;

// pulls a random image from the imagenames array
// packages it into an appropriate random-rotating link
// and prints it to the page

// note: if pullImage is called more times than the number of elements in
// in imagenames you will receive, a possibly fatal, error

/// sample url
// <a href="javascript:trueLink(0)"><img // onMouseover="window.status=urls[currentimage[0]];return true" // onMouseout="window.status=''"src="http://www.nbc.com/imgs/header.jpg"
// width=200 height=99 name="foo" border=0 // style="filter:blendTrans(duration=3)"></a>


// some static strings used in function to compose image link
// the width and height of the rando images
var imWidth = 200;
var imHeight = 100;

var link0 = "<a href=\"javascript:trueLink(";
var link1 = ")\"><img onMouseover=\"window.status=urls[currentimage[";
var link2 = "]];return true\" onMouseout=\"window.status=''\" src=\"";
var link3 = "\" width=" + imWidth + " height=" + imHeight + " name=\"" + imToken;
var link4 = "\" border=0 style=\"filter:blendTrans(duration="+fadingTime+")\"></a>";

function pullImage()
{	
	// get a random but still useful index
	var r= getRandomIndex();

	/// compose the image link for the rth element
	var str=link0 + imCounter + link1 + imCounter + link2
	str +=  imgnames[r] + link3 + imCounter + link4;
	currentimage[imCounter++] = r;

	// write it out
	document.write(str);
}


// set errors to this so that settings get redone

function reapply(){
	setTimeout("seed()",2000)
	return true
}

