//
// Global textFader management -- BTF (Bugimus Text Fader)
//

bugBase.prototype.attachBTF = function() {
	this.BTFnum = 0
	this.BTFobj = new Array()
}
window.bugimus.attachBTF()

//
// Local textFader functionality
//
function textFader( refObj, rate ) {
	this.objname = "window.bugimus.BTFobj[" + window.bugimus.BTFnum + "]"
	window.bugimus.BTFobj[window.bugimus.BTFnum++] = this
	this.sliderate = rate
	this.faderate = 100
	this.itemindex = -1
	this.numitems = 0
	this.goingup = false
	this.firstFlag = true
	this.timer = null
	this.slidetimer = null
	this.item = new Array()

	// ------------------------------------------------------------
	// Create strip div appended to body.
	refObj.appendChild(this.itemObj=document.createElement("DIV"))
	this.itemObj.appendChild( this.itemNode = document.createTextNode("%%%") )
	this.itemObj.appendChild( document.createElement("BR") )

	// Define the default color transition sequence
	this.trans = new Array()
	this.trans[0] = "#000"
	this.trans[1] = "#333"
	this.trans[2] = "#666"
	this.trans[3] = "#999"
	this.trans[4] = "#ccc"
	this.trans[5] = "#fff"

	// define array to hold user colors if default not needed
	this.isTransC = false
	this.numColors = 0
	this.transC = new Array()
}

// transitions item's color from top to bottom
// input delay in ms
textFader.prototype.fall = function( delay ) {
	this.isTransC ? upper = this.transC.length : upper = this.trans.length
	for( k=0; k<upper; k++ ) {
		timeoutTime = k*this.faderate + delay
		this.timer=setTimeout( this.objname+".setItemColor('"+k+"')", timeoutTime )
	}
	return timeoutTime
}

textFader.prototype.rise = function( delay ) {
	m=0
	this.isTransC ? upper = this.transC.length : upper = this.trans.length
	for( k=upper-1; k>=0; k-- ) {
		timeoutTime = m++*this.faderate + delay
		this.timer=setTimeout( this.objname+".setItemColor('"+k+"')", timeoutTime )
	}
	return timeoutTime
}

textFader.prototype.setItemColor = function( itemnum ) {
	if(this.isTransC)
		this.itemObj.style.color = this.transC[itemnum]
	else
		this.itemObj.style.color = this.trans[itemnum]
}

textFader.prototype.nextItem = function() {
	clearTimeout(this.timer)
	delay = 0
	delay = this.fall(delay)
	this.incItem()
	delay += 100
	this.timer = setTimeout( this.objname+".showItem('"+this.itemindex+"')", delay )
	delay += 500
	this.rise(delay)
}

textFader.prototype.showItem = function( itemnum ) {
	if( itemnum<=this.numitems ) {
		this.itemNode.nodeValue = this.item[itemnum]
	}
	this.itemindex=itemnum
}

textFader.prototype.loadItem = function( itemstring ) {
	this.item[this.numitems] = itemstring
	if(this.numitems==0) this.showItem(this.numitems)
	this.numitems++
}

textFader.prototype.loadColor = function( colorstring ) {
	this.isTransC = true
	this.transC[this.numColors++] = colorstring
}

textFader.prototype.incItem = function() {
	if( this.itemindex >= this.numitems-1 ) this.itemindex=0
	else this.itemindex++
}

textFader.prototype.decItem = function() {
	if( this.itemindex <= 0 ) this.itemindex=this.numitems-1
	else this.itemindex--
}

textFader.prototype.loop = function() {
	if(!this.firstFlag) this.nextItem()
	else this.firstFlag = false
	this.slidetimer = setTimeout( this.objname+".loop()", this.sliderate )
}

textFader.prototype.list = function( refObj ) {
	refObj.appendChild( olObj=document.createElement("OL") )
	for( k=0; k<this.numitems; k++ ) {
		olObj.appendChild( liObj=document.createElement("LI") )
		liObj.appendChild( document.createTextNode(this.item[k]) )
	}
	
	refObj.appendChild( olObj=document.createElement("BR") )
	refObj.appendChild( olObj=document.createElement("BR") )
	refObj.appendChild( olObj=document.createElement("BR") )
	
	refObj.appendChild( olObj=document.createElement("OL") )

	if(this.isTransC) {
		for( k=0; k<this.transC.length; k++ ) {
			olObj.appendChild( liObj=document.createElement("LI") )
			liObj.appendChild( document.createTextNode(this.transC[k]) )
		}
	} else {
		for( k=0; k<this.trans.length; k++ ) {
			olObj.appendChild( liObj=document.createElement("LI") )
			liObj.appendChild( document.createTextNode(this.trans[k]) )
		}
	}
}



