// (C) Simon Slavin 2009, written
// for the Department of Psychology, Lancaster University.
//
// Instructions:
//
// Have a <div> in the page you're calling this script from, and call 'fadeListInit'.  That
// routine takes care of everything else: making the image elements and setting up the timer
// for the fading.

globalFadeListSpecArray = []
globalFadeListCurrentItem = 0
globalFadeListNextItem = 1
globalFadeListTimeout = 'no timeout'

// parameters:
//
// fadeDiv: the empty <div> that you want the fader to put the image and caption in.
//
// specString: a long string with all the data needed for fader.  The format of specString is
//
// pic1,caption1,page1;pic2,caption2,page2;pic3,caption3,page3,...
// 
//  example: studentsLearning_fs.jpg:Students learning in the Levy Lab.:/us/generalinfo.html;studentlearning2_fs.jpg:2:/us/generalinfo.html;studentlearning3_fs.jpg:3:/us/generalinfo.html;socialPschology_fs.jpg:Social:/res/social.html

function fadeListInit(fadeDiv,specString) {
	fadeDiv.id = 'fadeListDiv'
	fadeDiv.align = 'center'

// slice and dice specString
	globalFadeListSpecArray = specString.split(';')

	var temp = globalFadeListSpecArray[0].split(':')

	var theAnchor = document.createElement('a')
	theAnchor.id = 'fadeListAnchor'
	theAnchor.name = specString
	theAnchor.href = '/'+temp[2]

	var theImageTable = document.createElement('table')
	var theImageRow = document.createElement('tr')
	var theImageCell = document.createElement('td')
	theImageCell.style.padding = '0px'
	theImageCell.style.borderWidth = '0px'
	theImageCell.id = 'fadeListImageCell'
	var temp2 = globalFadeListSpecArray[1].split(':')
	theImageCell.style.backgroundImage="url("+'images/'+temp2[0]+")"

	var theImage = document.createElement('img')
	theImage.id = 'fadeListImage'
	theImage.style.borderWidth = '0px'
	theImage.src = 'images/'+temp[0]
	theImageCell.appendChild(theImage)

	theImageRow.appendChild(theImageCell)
	theImageTable.appendChild(theImageRow)
	theAnchor.appendChild(theImageTable)

	var thePara = document.createElement('p')
	thePara.id = 'fadeListCaption'
	thePara.innerHTML = temp[1]
	theAnchor.appendChild(thePara)

	fadeDiv.appendChild(theAnchor)

	fadeListStartTimer(specString)
}

function fadeListStartTimer(specString) {
	globalFadeListSpecArray = specString.split(';')
	globalFadeListCurrentItem = 0
	globalFadeListNextItem = 1
	globalFadeListLoopNumber = 0 
	globalFadeListTimeout = setInterval("fadeListTimerEvent()", 20)
}

function fadeListTimerEvent() {
	globalFadeListLoopNumber = globalFadeListLoopNumber + 20

	if (globalFadeListLoopNumber > 4000) {
		var thisPart = document.getElementById('fadeListImage')
		thisPart.style.opacity = ((5000 - globalFadeListLoopNumber) / 1000)
	}

	if (globalFadeListLoopNumber == 4100) {
		var thisPart = document.getElementById('fadeListCaption')
		thisPart.style.visibility = 'hidden'
	}

	if (globalFadeListLoopNumber == 4500) {
		var temp2 = globalFadeListSpecArray[globalFadeListNextItem].split(':')
		var thisPart = document.getElementById('fadeListCaption')
		thisPart.innerHTML = temp2[1]

		var thisPart = document.getElementById('fadeListAnchor')
		thisPart.href = '/'+temp2[2]
	}

	if (globalFadeListLoopNumber == 4900) {
		var thisPart = document.getElementById('fadeListCaption')
		thisPart.style.visibility = 'visible'
	}

	if (globalFadeListLoopNumber == 5000) {
		var fadeDiv = document.getElementById('fadeListImage')
		globalFadeListCurrentItem = globalFadeListNextItem
		if (++ globalFadeListNextItem >= globalFadeListSpecArray.length) globalFadeListNextItem = 0

		var temp = globalFadeListSpecArray[globalFadeListCurrentItem].split(':')
		thisPart = document.getElementById('fadeListImage')
		thisPart.src = 'images/'+temp[0]
		thisPart.style.opacity = 1

		var temp2 = globalFadeListSpecArray[globalFadeListNextItem].split(':')
		var thisPart = document.getElementById('fadeListImageCell')
		thisPart.style.backgroundImage="url("+'images/'+temp2[0]+")"

		globalFadeListLoopNumber = 0
	}
}

function fadeListKillTimer() {
	if (globalFadeListTimeout != 'no timeout') clearTimeout(globalFadeListTimeout)
	globalFadeListTimeout = 'no timeout'
}

