/*
Copyright 2009 Caleb Begly
Photorotator Object
*/
//this class requires div.js which can be obtained from calebbegly@gmail.com
try
{
	Div();
}
catch(error){
	alert("This module requires the Div Object to be linked.");	//comment out for release version
}

//global context object
var context;

function Photorotator(prefix,millisecondsUntilChange)
{
	this.prefix = prefix;			//the div prefix
	this.pictures = new Array();		//holds all the elements
	this.index = 0;			//current picture beign shown
	this.delay = millisecondsUntilChange;	//delay between pictures change
}

Photorotator.prototype.init = function(context)
{
	window.context = context;		//set the global context object to the passed context
	var i = 0;
	while(this.isValidDiv(i))	//if there is another element
	{
		this.pictures[this.pictures.length] = new Div(this.prefix + i++);	//add it to the array
	}
	this.pictures[0].show();					//display the first item in the array
	this.pictures[0].elementToShow.parentElement.style.visibility = "visible";	//this shows the whole thing.
	
	setInterval("context.rotate();",context.delay + 4000);			//start the tape rolling - the "4000" is used to allow the rest of the page to load.
}
Photorotator.prototype.isValidDiv = function(id)
{
	var tmpDiv = document.getElementById(this.prefix + id);		//get the element
	if((tmpDiv == null))						//it doesn't exist
	{
		return false;					//return false
	}
	else
	{
		return true;					//It exists! Return TRUE
	}
}
	
Photorotator.prototype.rotate = function()
{
	var newIndex = context.index + 1;		//set the index to the next item
	if(context.index == context.pictures.length-1)	//if we are at the last item in the array...
	{
		newIndex = 0;			//we are going to loop back to the first picture
	}

	context.pictures[context.index].hide();		//hide the old
	context.pictures[newIndex].show();		//show the new

	context.index = newIndex;			//set the index to the currently shown picture
}