//
// Rotate Images - rotate.js
// Adam Mark Finlayson, North Western University
//


//====== Dependencies ======//

// Prelod (images) in layout.js
document.write('<script language="JavaScript" type="text/javascript" src="http://www.coogeetech.com/js/layout.js"></script>\n') ;




//====== Usage ======//
// 
// Specify all rotating images (including the original) using function
// add_rotating_image.  The id passed to that function is the id of the <img> to
// perform the rotation on.  Call rotate_image with the number of seconds 
// between steps to begin rotating.  Optionally delay the rotation start by 
// delay_seconds to stagger multiple rotations.
// 
// This script does not (and may never) dynamically resize the images.  All the 
// images should be the same size or they will look stretched/squished when they
// are fitted to the original image.
// 
// Example:
//     <img id="rotatingphoto" src="images/photo0.jpg" width="160" height="100" alt=""  />
//
//     add_rotating_image("rotatingphoto", "images/photo0.jpg") ;
//     add_rotating_image("rotatingphoto", "images/photo1.jpg") ;
//     add_rotating_image("rotatingphoto", "images/photo2.jpg") ;
//     
//     rotate_image("rotatingphoto", 5) ;
//
// 



//====== Variables ======//

var rotation = new Array() ;
var rotationIndex = new Array() ;



//====== Functions ======//

function add_rotating_image(id, src) {
	if(rotation[id] == null) {
		var newArray = new Array() ;
		rotation[id] = newArray ;
	}
	
	rotation[id][rotation[id].length] = src ;
	preload(src) ;
}

function rotate_image(id, rotate_seconds, delay_seconds) {
	if(delay_seconds != null && delay_seconds>0) {
		var rotateCall = "rotate_image_milliseconds('"+ id +"',"+ (rotate_seconds*1000) +")"
		setTimeout(rotateCall, delay_seconds*1000) ;
	} else {
		rotate_image_milliseconds(id, (rotate_seconds*1000)) ;
	}
}



//====== Internal Functions ======//

function rotate_image_milliseconds(id, milliseconds) {
	if(!document.getElementById) return ;
	
	var image = document.getElementById(id) ;
	if(image==null) return ;
	
	if(rotationIndex[id] == null) {
		rotationIndex[id] = 0 ;
	}
	
	image.src = rotation[id][rotationIndex[id]] ;
	
	rotationIndex[id]++ ;
	if(rotationIndex[id] >= rotation[id].length) rotationIndex[id] = 0;
	
	var rotateCall = "rotate_image_milliseconds('"+ id +"',"+ milliseconds +")"
	setTimeout(rotateCall, milliseconds) ;
}

