// ****************************************************************************
// Thumbnail Rotator
// -----------------
// Description: Presents a set of thumbnails and then randomly changes each
//              to another from the available set.  Makes use of the jQuery
//              library to handle the image fading.
//
// Author(s)
// ---------
// SJB Steven Bradley steve.bradley@ctsltd.net
//
// Revision History
// ----------------
// 08/07/11 SJB Created
//
// Copyright 2011 Chambers Technology Support Ltd. http://www.ctsltd.net
// ****************************************************************************

var n = 4; // Number of thumbnails
var c = 0; // Thumbnail being changed
var p = 2500; // Time interval (ms) between changes

var t; // Timer

// Array for thumbail indexes

var m = new Array (-1,-1,-1,-1);

// Array for thumbnail titles

var b = new Array ("Sylvia Allen","John Benson","Simon Canter","Jeremy Chipperfield","Charles Crinion","Roger Daniells-Smith","Francesca Delany","Ranjeet Dulay","Iain Edwards","Ignatius Fessal","Christa Fielden","Anthony Gifford","Benjamin Hawkin","Jaqcues Howell","Shashi Jaisri","Jacinta Jones","Parveen Judge","Anthony Katz","Jemma Levinson","Jacqueline Lule","Tom Mackinnon","Parveen Mansoor","Jacqueline McIntosh","Peter Mullen","Campbell Munro","Shuyeb Muquit","James Murray-Smith","Tanya Murshed","Richard O'Sullivan","David Owusu-Yianoma","Paul Richmond","Mark Roscoe","Pamela Rose","Jessica Russell-Mitra","Shiraz Rustom","Monwara Shah","Moira Sofaer","Sarah Steinhardt","Mark Stephens","David Stephenson","Neelim Sultan","Anna Watterson","Jeffrey Yearwood","Amir Zaidi");

// Array for thumbnail image/page prefix

var a = new Array ("allen_s","benson_j","canter_s","chipperfield_j","crinion_c","daniells-smith_r","delany_f","dulay_r","edwards_i","fessal_i","fielden_c","gifford_a","hawkin_b","howell_j","jaisri_s","jones_j","judge_p","katz_a","levinson_j","lule_j","mackinnon_t","mansoor_p","mcintosh_j","mullen_p","munro_c","muquit_s","murray-smith_j","murshed_t","osullivan_r","owusu-yianoma_d","richmond_p","roscoe_m","rose_p","russell-mitra_j","rustom_s","shah_m","sofaer_m","steinhardt_s","stephens_m","stephenson_d","sultan_n","watterson_a","yearwood_j","zaidi_a");

// Generate first set random thumbnails

i = 0;
s = "+"; // Thumbnails in the sequence
while (i<n) {

// Generate a random index

	r = rand(a.length);

// If this index is not in the current set add to the set

	if(s.indexOf("+"+r+"+") < 0) {
		m[i]=r;
		s = s + r +"+";
		i++;
	}
}

// Write the thumbnail set with layout

document.write("<table cellpadding=0 cellspacing=0 border=0 class='tabThumbnails' align=center title='Thumbnails table'>");
document.write("<tr>");
document.write("<td><a id='lnk0' href='/people/"+a[m[0]]+".htm' class='lnkThumbnail' title='"+b[m[0]]+"'><img id='img0' src='/assets/photos.thumbnails/"+a[m[0]]+".jpg' class='imgThumbnail'></a></td>");
document.write("<td><a id='lnk1' href='/people/"+a[m[1]]+".htm' class='lnkThumbnail' title='"+b[m[1]]+"'><img id='img1' src='/assets/photos.thumbnails/"+a[m[1]]+".jpg' class='imgThumbnail'></a></td>");
document.write("</tr>");
document.write("<tr>");
document.write("<td><a id='lnk2' href='/people/"+a[m[2]]+".htm' class='lnkThumbnail' title='"+b[m[2]]+"'><img id='img2' src='/assets/photos.thumbnails/"+a[m[2]]+".jpg' class='imgThumbnail'></a></td>");
document.write("<td><a id='lnk3' href='/people/"+a[m[3]]+".htm' class='lnkThumbnail' title='"+b[m[3]]+"'><img id='img3' src='/assets/photos.thumbnails/"+a[m[3]]+".jpg' class='imgThumbnail'></a></td>");
document.write("</tr>");
document.write("</table>");

// Set time for first change

t = setTimeout("ChangeThumbnail()",p);


function ChangeThumbnail() {
// ****************************************************************************
// Description
// -----------
// Changes the next thumbnail in the set.  Works by selecting an thumbnail
// and checks to see if it is already in the sequence.  If so, select again.
// Once a new thumbnail has been selected, make it the background image of the
// current thumbnail link and then fade out the current thumbnail.  Once faded
// set the new image as the thumbnail image.
//
// Author(s)
// ---------
// SJB Steven Bradley steve.bradley@ctsltd.net
//
// Revision History
// ----------------
// 08/07/11 SJB Created
//
// Copyright 2011 Chambers Technology Support Ltd. http://www.ctsltd.net
// ****************************************************************************

// Form list of current thumbnails

	s = "+";
	for(i=0;i<n;i++) s = s + m[i] +"+";

// Randonly select a new thumbail checking it isn't already in the set

	do {r = rand(a.length);}
	while (s.indexOf("+"+r+"+") >= 0);
	m[c] = r;
	
// Set the link and title for the new thumbnail

	document.getElementById('lnk'+c).href = "/people/"+a[m[c]]+".htm";
	document.getElementById('lnk'+c).title = b[m[c]];

// Set the bg of the current thumbnail using the image for the new
	
	bg = document.getElementById('lnk'+c);
	bg.style.backgroundImage = "url("+"/assets/photos.thumbnails/"+a[m[c]]+".jpg)";

// Fade out the current thumbnail to show the bg the make the bg the new thumbnail
	
	$('#img'+c).fadeTo(800,0, function() { 
		document.getElementById('img'+c).src = "/assets/photos.thumbnails/"+a[m[c]]+".jpg";
		$('#img'+c).fadeTo(800,1);
	});

// Increment the thumbnail count and reset when required
	c++;
	if(c==n) c=0;

// Set time for next transition

	t = setTimeout("ChangeThumbnail()",p);
	
}

function rand(n) {
// ****************************************************************************
// Description
// -----------
// Returns a random number between 0 and n
//
// Author(s)
// ---------
// SJB Steven Bradley steve.bradley@ctsltd.net
//
// Revision History
// ----------------
// 08/07/11 SJB Created
//
// Copyright 2011 Chambers Technology Support Ltd. http://www.ctsltd.net
// ****************************************************************************
    return (Math.floor(Math.random()*n));
}

