var myslideshow = null;
/*
 * The main use is to call startSlideshow() to display a slideshow.
 * The Slideshow class also provides a list() method to show a static display of the slides.
 *
 */

/* usage:
 * 	   myslideshow = new Slideshow('changeImg', 'galleryText', 'myslideshow');
 * 
 * imgId = the id attribute of the slideshow's <img> element
 * captionId = the id attribute of the element to display gallery caption text
 *
 */

function Slideshow(imgId, captionId)
{
	var img_id = imgId;
	var caption_id = captionId;
	var img_element = document.getElementById(imgId);
	var caption_element = document.getElementById(captionId);
	var imagefiles = new Array;
	var images = new Array;
	var captions = new Array;
	var index = 0;
	var intervalId = 0;
	var intervalDelay = 4000;
	var shuffleFlag = 1;
	var captionFlag = 0;
	var shuffleSequence = new Array;

	this.run = function run()
	{
		if (shuffleFlag) {
			if (shuffleSequence.length == 0) {
				initShuffleSequence(images.length);
			}
			index = shuffleSequence.shift();
		}
		update();
		intervalId = setInterval("update()", intervalDelay);
	}
	
	this.inspect = function inspect()
	{
		str = "ImageId: " + img_id + "<br />";
		str += "ImageEl: " + img_element + "<br />";
		str += "CaptionId: " + caption_id + "<br />";
		str += "CaptionEl: " + caption_element + "<br />";
		return str;
	}
	
	this.list = function list()
	{
		var str = "<div class='envelope'>Number of images in slideshow: " + images.length + "</div>";
		var i  = 0;
		while (i < images.length) {
			str += "<div class='envelope'>"
				+ "[" + i + "] <a href='" + imagefiles[i] + "'>" + imagefiles[i] + "</a>"
				+ "<div class='slide'><img src='" + images[i].src + "' alt='' /><br />"
				+ captions[i] + "</div></div>\n";
			++i;
		}
		return str;
	}

	this.add = function add(imgname, caption) 
	{
		if (caption == null || caption == undefined || caption == '') {
			caption = '<br />';
		}
		imagefiles.push(imgname);
		captions.push(caption);
	}
	
	this.curr_caption = function curr_caption()
	{
		if (captionFlag && index < captions.length) {
			return captions[index];
		}
		return "";
	}
	
	this.curr_img = function curr_img()
	{
		if (index < images.length) {
			return images[index].src;
		}
		return "";
	}
	
	this.update = function update()
	{
		img_element.src = curr_img();
		caption_element.innerHTML = curr_caption();
		if (shuffleFlag) {
			if (shuffleSequence.length == 0) {
				initShuffleSequence(images.length);
			}
			index = shuffleSequence.shift();
		} else {
			if (++index >= images.length) {
				index = 0;
			}
		}
	}

	this.preload = function preload()
	{
		//caption_element.innerHTML = "buffering " + imagefiles.length + " images...";
		for (i = 0; i < imagefiles.length; ++i) {
			images[i] = new Image();
			images[i].src = imagefiles[i];
		}
		//caption_element.innerHTML = "";
	}
	
	// returns a shuffled list of integers from 0 through {limit-1}
	this.initShuffleSequence = function (limit) {
		var seq = new Array; // sequential population source
		shuffleSequence.splice(0,shuffleSequence.length); // make sure it's empty
		var i = 0;
		while (i < limit) {seq.push(i++);}
		while (i-- > 0) {
			var j = Math.floor(Math.random() * seq.length);
			shuffleSequence.push(seq.splice(j,1)[0]); // decrements srcarr.length
		}
	}

	return this;
}

			
function cycleImg() { startSlideshow(); } // make an alias for startSlideshow

/*
 * This method instantiates the myslideshow object, populates it with images and captions, 
 * preloads the images, and finally starts the thing running.
 *
 */
function startSlideshow()
{
	myslideshow = Slideshow('ChangeImg', 'imgCaptionText'); // assumes of course that elements with these ids exist
	myslideshow.add('/photos/2011/LentenRose-200px.jpg', 'Spring has Spring in Milton<br />&quot;<span style=\'font-style:oblique;\'>Lenten Rose</span>&quot;');
	myslideshow.add('/photos/2011/IMG_3612rev0-200px.jpg', 'Earth Day 2011');
	myslideshow.add('/photos/2011/IMG_3576rev0-200px.jpg', 'Milton Youth love Earth Day');
	myslideshow.add('/photos/2011/IMG_3611rev0-200px.jpg', 'Milton Grows Green Volunteers Making a Difference - Earth Day 2011');
	myslideshow.add('/photos/2011/IMG_3569rev0-200px.jpg', 'Mayor Lockwood joins in on the Earth Day festivities!');
	myslideshow.add('/photos/2011/IMG_3606rev0-200px.jpg', 'Ann Coggins of MDAC joins Julie and her mom at Earth Day');
	myslideshow.add('/photos/2011/IMG_3555rev0-200px.jpg', 'Wagon Rides were a hit at Earth Day');
	myslideshow.add('/photos/2007/EarthWeek2007/3-thumb.jpg', 'Earth Week 2007');
	myslideshow.add('/photos/2007/ArborDay/92-thumb.jpg', 'Arbor Day');
	myslideshow.add('/photos/2011/IMG_4129-2rev0-200px.jpg', 'Julie with NMS students');
	myslideshow.add('/photos/2011/IMG_4134-2rev0-200px.jpg', 'Teaching our youth is a real joy!');
	myslideshow.add('/photos/2011/IMG_4059rev0-200px.jpg', 'Positively impacting the future; one student at a time.');
	myslideshow.add('/photos/2011/IMG_4146-2rev0-200px.jpg', 'A love of teaching!');
	myslideshow.add('/photos/2011/IMG_3492rev0-200px.jpg', '');
	myslideshow.add('/photos/2011/IMG_4171-2rev0-200px.jpg', 'Memorial Day 2011');
	myslideshow.add('/photos/2011/IMG_4167-2rev0-200px.jpg', 'Colonel Murphy Neal Jones, his family, Julie and her son, Zach');
	myslideshow.add('/photos/2011/IMG_4168-2rev0-200px.jpg', 'Colonel Murphy Neal Jones joins Milton for Memorial Day');
	myslideshow.add('/photos/2011/IMG_4165-2rev0-200px.jpg', 'Memorial Day - Milton Student, Destiny Coggins, participates in Memorial Day Services');
	myslideshow.add('/photos/2011/MiltonHSCelebratesAmerica-200px.jpg', 'Milton High School celebrates America');
	myslideshow.add('/photos/2006/JulieandFamily/JZBandFamily-022_thumb.jpg', 'Julie and her Family');
	myslideshow.add('/photos/2008/MGG/IMG_6608-thumb.jpg', '');
	myslideshow.add('/photos/2008/MGG/IMG_6693-thumb.jpg', '');
	myslideshow.add('/photos/2008/PetDay/IMG_6182-thumb.jpg', '');
	myslideshow.add('/photos/2008/MGG/green-committee-thumb.jpg', 'Milton Grows Green committee');
	myslideshow.add('/photos/2009/GreatAmericanCleanup/IMG_7299-1rev0-200px.jpg', 'Great American Cleanup 2009');
	myslideshow.add('/photos/2009/GreatAmericanCleanup/IMG_7274-1rev0-200px.jpg', 'Great American Cleanup 2009');
	myslideshow.add('/photos/2008/RiversAlive/IMG_6110-thumb.jpg', 'RiversAlive!');
	myslideshow.add('/photos/2007/RiversAlive/29-thumb.jpg', 'RiversAlive!');
	myslideshow.add('/photos/2007/RiversAlive/84-thumb.jpg', 'RiversAlive!');
	myslideshow.add('/photos/2007/RiversAlive/94-thumb.jpg', 'RiversAlive!');
	myslideshow.add('/photos/2007/RothParty/5-thumb.jpg', '');
	myslideshow.add('/photos/2007/RothParty/39-thumb.jpg', '');
	myslideshow.add('/photos/2007/Inauguration/Julie-Tina-thumb.jpg', '');
	myslideshow.add('/photos/2007/OldSoldiersDayParade2007/10-thumb.jpg', 'Old Soldiers Day Parade');
	myslideshow.add('/photos/2007/OldSoldiersDayParade2007/21-thumb.jpg', 'Old Soldiers Day Parade');
	myslideshow.add('/photos/2007/OldSoldiersDayParade2007/26-thumb.jpg', 'Old Soldiers Day Parade');
	myslideshow.add('/photos/2007/OldSoldiersDayParade2007/28-thumb.jpg', 'Old Soldiers Day Parade');
	myslideshow.add('/photos/2007/OldSoldiersDayParade2007/46-thumb.jpg', 'Old Soldiers Day 2007');
	myslideshow.add('/photos/2007/OldSoldiersDayParade2007/56-thumb.jpg', 'Old Soldiers Day 2007');
	myslideshow.add('/photos/2007/FireStation43/17-thumb.jpg', 'Fire Station 43');
	myslideshow.add('/photos/2007/Police/5-thumb.jpg', 'City of Milton Police');
	myslideshow.add('/photos/2006/JulieandFamily/JZBandFamily-017_thumb.jpg', 'Julie and her Family');
	myslideshow.add('/photos/2007/CogburnElementary/5-thumb.jpg', 'Groundbreaking for Cogburn Elementary');
	myslideshow.add('/photos/2007/MiracleLeague/73-thumb.jpg', '');
	myslideshow.add('/photos/2007/EnvironmentalEducation/77-thumb.jpg', 'Environmental Education');
	myslideshow.add('/photos/2007/Inauguration/7b-thumb.jpg', 'Inauguration');
	myslideshow.add('/photos/2007/SummitHill/22-thumb.jpg', 'Summit Hill');
	myslideshow.add('/photos/2007/RibbonCuttings/47-thumb.jpg', 'Ribbon cutting');
	myslideshow.add('/photos/2007/RibbonCuttings/62-thumb.jpg', '');
	myslideshow.add('/photos/2006/TomPrice/TomPrice2006-006_thumb.jpg', '');
	myslideshow.add('/photos/2011/OldSoldiersDay/IMG_5968-2rev1-thumb.jpg', 'Old Soldiers Day 5K Race 2011');
	myslideshow.add('/photos/2011/OldSoldiersDay/IMG_5986-2rev1-thumb.jpg', 'Old Soldiers Day Parade 2011');
	myslideshow.add('/photos/2011/OldSoldiersDay/IMG_5987-2rev0-thumb.jpg', 'Old Soldiers Day Parade 2011');
	myslideshow.add('/photos/2011/OldSoldiersDay/IMG_5989-2rev1-thumb.jpg', 'Old Soldiers Day Parade 2011');
	myslideshow.add('/photos/2011/OldSoldiersDay/IMG_6015-2rev1-thumb.jpg', 'Old Soldiers Day Parade 2011');
	myslideshow.add('/photos/2011/OldSoldiersDay/web_1_rev1-thumb.jpg', 'Old Soldiers Day Parade 2011');
	myslideshow.add('photos/2011/IMG_0748rev0-thumb.jpg', '');
	myslideshow.preload();
	myslideshow.run();
}

