var WW_movieArray 		= new Array();

function WW_movie(names,anchor,delay,baseImg, loop, todo, order){
	if (todo) this.todo	= todo;
	if (order) this.order	= order;
	this.names				= names;
	this.number				= names.length;
	this.pic					= new Array(this.number);
	this.anchor				= anchor;
	this.delay				= delay;
	this.pos					= 0;
	this.loop					= loop;
	this.running			= false;
	this.toggle				= WW_movieToggle;
	this.start				= WW_movieStart;
	this.stop					= WW_movieStop;
	this.stopAndReset 	= WW_movieStopAndReset;
	this.run					= WW_movieRun;
	this.setAnchor		= WW_movieSetAnchor;
	this.setTodo			= WW_movieSetTodo;
	this.timer				= null;
	this.id						= WW_movieArray.length;
	this.baseImg			= baseImg;
	this.basePic			= new Image();
	this.basePic.src	= baseImg;
	WW_movieArray[this.id] 	= this;
	me = "WW_movieArray["+this.id+"]";
	this.name	= name; 
	WW_addSuperInitFunction(me+".pic=WW_preload("+me+".names)");
}
	
function WW_movieStart(anchor){
	if (this.running) return;
	// WW_debug("WW_movieStart : started")	
	if ((typeof(anchor)!= 'undefined')) this.setAnchor(anchor)
	this.running = true;
	this.run();
}
	
function WW_movieStop(){
	clearTimeout(this.timer);
	this.running = false;
	// WW_debug("WW_movieStop : stopped")	
}

function WW_movieToggle(){
	if(this.running)
		this.stop();
	else
		this.start();
}

function WW_movieStopAndReset(){
	if (!this.running) return;		
	clearTimeout(this.timer);
	this.running = false;
	this.pos = 0;
	this.anchor.setSrc(this.basePic.src);	
	// WW_debug("WW_movieStopAndReset : stopped")	
}

function WW_movieStopAll(){
  for (var thisani in Animations)
    thisani.stopAndReset();
}

function WW_movieSetAnchor(anchor){
	this.anchor = anchor;	
}


function WW_movieRun(){
	if (!this.running) return;		
	// WW_debug("WW_movieRun : at position :" + this.pos);

	if (!this.order) {
		if ((this.pos == this.number) && (!this.loop)) {
			clearTimeout(this.timer);
			eval (this.todo);
			return;
		}
		if (this.pos == this.number) 	
			this.pos = 0;
	  this.anchor.setSrc(this.pic[this.pos].src);
	  this.pos ++;
	} else {
		if ((this.pos == this.order.length) && (this.loop==false)) {
			clearTimeout(this.timer);
			return;
   	}
		if (this.pos == this.order.length) 
			this.pos = 0;
		this.anchor.setSrc(this.pic[this.order[this.pos]].src);
		this.pos ++;
	}
	clearTimeout(this.timer);
	this.timer = setTimeout('WW_movieArray['+this.id+'].run()',this.delay);
}

function WW_movieSetTodo(todo){
	this.todo = todo;
	return true;
}

