// This script prints out the cycling image on the front page

var rotation = {imagelist: [], index: 0}


// to add an image, copy one of these lines and put in the correct file name
// to remove an image, delete its line
rotation.imagelist.push("images/randomimage_main/WenChyan.jpg")
rotation.imagelist.push("images/randomimage_main/Prom 2009.jpg")
rotation.imagelist.push("images/randomimage_main/Guitarhero.jpg")
rotation.imagelist.push("images/randomimage_main/IreneCai.jpg")
rotation.imagelist.push("images/randomimage_main/HappyMan.jpg")
rotation.imagelist.push("images/randomimage_main/Gocart.jpg")
rotation.imagelist.push("images/randomimage_main/JeremyLai.jpg")
rotation.imagelist.push("images/randomimage_main/sorefeet.jpg")
rotation.imagelist.push("images/randomimage_main/Games.jpg")
rotation.imagelist.push("images/randomimage_main/JonathanDau.jpg")
rotation.imagelist.push("images/randomimage_main/TwoGirls.jpg")
rotation.imagelist.push("images/randomimage_main/Dancing.jpg")
rotation.imagelist.push("images/randomimage_main/Staff.jpg")
rotation.imagelist.push("images/randomimage_main/Kelsey and Ashwin.jpg")

// the time each image is visible, in seconds
rotation['delay'] = 30


function write_image() {
	// default to a random image
	rotation.index = Math.floor(Math.random()*rotation.imagelist.length)
	
	// try to load a number from cookie to rotate through images one by one
	var cindex = read_cookie("imagerotate")
	if (cindex == null) {
		// if we fail, try to write the cookie and continue with the random image
		create_cookie("imagerotate", "0", 1337)
	}
	else {
		rotation.index = parseInt(cindex)
		rotation.index %= rotation.imagelist.length
		cindex++
		cindex %= rotation.imagelist.length
		create_cookie("imagerotate", cindex, 1337)
	}

	document.write('<table border=0 with=293 cellpadding=0 cellspacing=1 bgcolor=#ffffff>')
	document.write('<tr><td>')
	document.write('<img id="rotatingthingy" style="border:1px solid #c0c0c0" src="'+rotation.imagelist[rotation.index]+'" border=0 width=293 alt=null>')
	document.write('</td></tr>')
	document.write('</table>')
}

function create_cookie(name, value, days) {
	if (days) {
		var date = new Date()
		date.setTime(date.getTime() + (days*24*60*60*1000))
		var expires = "; expires=" + date.toGMTString()
	}
	else
		var expires = ""
	document.cookie = name + "=" + value + expires + "; path=/"
}

function read_cookie(name) {
	var nameEQ = name + "="
	var ca = document.cookie.split(';')
	for(var i = 0 ; i < ca.length ; i++) {
		var c = ca[i]
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length)
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length)
	}
	return null
}

function erase_cookie(name) {
	create_cookie(name, "", -1)
}

function image_rotate() {
	rotation.index++
	rotation.index %= rotation.imagelist.length
	create_cookie("imagerotate", rotation.index, 1337)
	document.getElementById("rotatingthingy").src = rotation.imagelist[rotation.index]
}

// start it all
write_image()
setInterval(image_rotate, rotation['delay']*1000)
