//
// Global imageSwapper management -- BIS (Bugimus Image Swapper)
//

bugBase.prototype.attachBIS = function() {
	this.BISnum = 0
	this.BISobj = new Array()
	this.BIS_FORWARD = 0
	this.BIS_BACKWARD = 1
}
window.bugimus.attachBIS()

//
// Local imageSwapper functionality
//
function imageSwapper( rate ) {
//	this.browser = new sniffer()
	this.objname = "window.bugimus.BISobj[" + window.bugimus.BISnum + "]"
	window.bugimus.BISobj[window.bugimus.BISnum++] = this
	this.rate = rate
	this.imageindex = -1
	this.numimages = 0
	this.goingup = false
	this.direction = window.bugimus.BIS_FORWARD
	this.stopall = false
	this.timer = null
	this.img = new Array()
}

imageSwapper.prototype.fall = function( placement, delay ) {
	for( k=1; k<this.numimages; k++, delay++ ) {
		this.timer=setTimeout( this.objname+".showimage('"+placement+"','"+k+"')", delay*this.rate );
		this.imageindex=k;
	}
	return delay;
}

imageSwapper.prototype.rise = function( placement, delay ) {
	for( k=this.numimages-2; k>=0; k--, delay++ ) {
		this.timer=setTimeout( this.objname+".showimage('"+placement+"','"+k+"')", delay*this.rate );
		this.imageindex=k;
	}
	return delay;
}

imageSwapper.prototype.showimage = function( placement, imagenum ) {
	if( imagenum<this.numimages ) document.getElementById(placement).src=this.img[imagenum].src;
	this.imageindex=imagenum;
}

imageSwapper.prototype.loadimage = function( source ) {
	this.img[this.numimages] = new Image()
	this.img[this.numimages].src = source
	this.numimages++
}

imageSwapper.prototype.incimage = function() {
	if( this.imageindex >= this.numimages-1 ) this.imageindex=0;
	else this.imageindex++;
}

imageSwapper.prototype.decimage = function() {
	if( this.imageindex <= 0 ) this.imageindex=this.numimages-1;
	else this.imageindex--;
}

imageSwapper.prototype.nextimage = function() {
	if (this.direction == window.bugimus.BIS_BACKWARD) {
		this.decimage()
	} else {
		this.incimage()
	}
}

imageSwapper.prototype.setdirection = function( direction ) {
	this.direction = direction
}

imageSwapper.prototype.stop = function() {
//	this.stopall=true
	clearTimeout(this.timer)
}

imageSwapper.prototype.loop = function( placement ) {
	this.nextimage()
	if (!this.stopall) {
		this.showimage(placement, this.imageindex);
		clearTimeout(this.timer)
		this.timer = setTimeout(this.objname + ".loop('" + placement + "')", this.rate);
	} else {
		this.stopall = false;
	}

	document.getElementById("info").value = "dir = " + this.direction 
											+ ", img = " + this.imageindex
											+ ", rate = " + this.rate/1000 + " sec"
											+ ", ? = " + 0
}

imageSwapper.prototype.bounce = function( placement ) {

//alert('hello')

	if( this.goingup && this.imageindex >= this.numimages-1 ) this.goingup=false;
	else if( !this.goingup && this.imageindex <= 0 ) this.goingup=true;
	if( this.goingup ) this.incimage(); else this.decimage();
	this.showimage( placement, this.imageindex );
	if( !this.stopall ) this.timer=setTimeout( this.objname+".bounce('"+placement+"')", this.rate );
	else this.stopall=false;
}

