//
// Global bugStripSlider management -- BSS (Bugimus Strip Slider)
//

bugBase.prototype.attachBSS = function() {
	this.BSSnum = 0
	this.BSSobj = new Array()
}
window.bugimus.attachBSS()

//
// Local bugStripSlider functionality
//

// Define strip slider object
function stripSlider(refObj, xinit, yinit, orientation) {
	this.objname = "window.bugimus.BSSobj[" + window.bugimus.BSSnum + "]"
	window.bugimus.BSSobj[window.bugimus.BSSnum++] = this

	this.numpix=0
	if(!(this.orientation=orientation)) this.orientation="h" // set (h)orizontal/(v)ertical
	this.fade=true         // fade on/off
	this.pixgap=5          // default
	this.pixwidth=100      // default
	this.pixheight=100     // default
	this.sweetspot=0       // default
	this.a=200             // amplitude
	this.s=50              // scale factor

	this.bgcolor="transparent" // container div background color
	this.border="0px"          // container div border style
	this.imgborder="0px"       // image border style

	// only update vertical pos
	this.constrainX = (this.orientation=="v") ? true : false
	// only update horizontal pos
	this.constrainY = (this.orientation=="h") ? true : false

	this.pixObj = new Array()
	this.hx=xinit
	this.hy=yinit
	this.timer=null
	this.speed=10
	this.active=false
	this.decay = 5   // basically the speed of the chasing object, the higher the slower

	// ------------------------------------------------------------
	// Create strip div appended to body.
	refObj.appendChild(this.stripObj=document.createElement("DIV"))

	this.stripObj.getx = getoX
	this.stripObj.gety = getoY
	this.stripObj.setx = setoX
	this.stripObj.sety = setoY
	this.stripObj.setw = setoWidth
	this.stripObj.seth = setoHeight
	this.stripObj.warp = warpoTo
	this.stripObj.setz = setoZindex
	with(this.stripObj.style) {
		position="absolute"
		left=this.hx+"px"
		top=this.hy+"px"
		width=this.pixwidth+2*this.pixgap+1+"px"
		height=this.pixheight+2*this.pixgap+1+"px"
		border=this.border  //"solid 1px #666"
		backgroundColor=this.bgcolor   //"#999" 
		overflow="visible"
	}

}

//-----------------------------------------------------------------
stripSlider.prototype.addPic = function(picname) { 

//alert(this.pixwidth+" "+this.pixheight)

	// Create each pic on the strip.
	this.stripObj.appendChild(this.pixObj[this.numpix]=document.createElement("DIV"))
	this.pixObj[this.numpix].id="P"+this.numpix
	this.pixObj[this.numpix].getx = getoX
	this.pixObj[this.numpix].gety = getoY
	this.pixObj[this.numpix].setopacity = setoOpacity
//debug this.pixObj[this.numpix].appendChild(document.createTextNode(this.pixObj[this.numpix].id))
//debug this.pixObj[this.numpix].appendChild(document.createElement("BR"))
	var imgString = "<img src='"+picname+"'"
					+" width='"+this.pixwidth+"'"
					+" height='"+this.pixheight+"'"
					+" alt='' />"
	this.pixObj[this.numpix].appendChild(document.createElement(imgString))
	with(this.pixObj[this.numpix].style) {
		position="absolute"
		width=this.pixwidth+"px"      // IE5 and IE6 are at variance here.
		height=this.pixheight+"px"     // IE5 and IE6 are at variance here.
		if(this.orientation=="h") {
			top=this.pixgap+"px"
			left=(this.pixwidth*this.numpix)+(this.pixgap*(this.numpix+1))+"px"
		} else {
			top=(this.pixheight*this.numpix)+(this.pixgap*(this.numpix+1))+"px"
			left=this.pixgap+"px"
		}
		border=this.imgborder
		backgroundColor="#900"
		filter="alpha(opacity=100)" 
	}

	this.numpix++
	if(this.orientation=="h") {
		this.stripObj.setw(this.numpix*(this.pixwidth+this.pixgap)+this.pixgap)
		this.stripObj.seth(this.pixheight+2*this.pixgap)
	} else {
		this.stripObj.setw(this.pixwidth+2*this.pixgap)
		this.stripObj.seth(this.numpix*(this.pixheight+this.pixgap)+this.pixgap)
	}
	
	this.stripObj.warp(this.hx,this.hy)
	if(this.fade)this.doAlpha()
}

//-----------------------------------------------------------------
stripSlider.prototype.chase = function ( x, y ) {
	if(this.active)clearTimeout(this.timer)
	dx=x-this.stripObj.getx()
	dy=this.stripObj.gety()-y
	distance=Math.round(Math.sqrt(dx*dx+dy*dy))
//alert("hello   dx="+this.stripObj.getx()+" dy="+dy+" distance="+distance )
	if(dx==0) angle=Math.PI*((dy>0)?.5:1.5)
	else angle=Math.atan(dy/dx)+(Math.PI*((dx<0)?1:(dy<0)?2:0))
	xstep=(dx>1)?Math.ceil(distance*Math.cos(angle)/this.decay):Math.floor(distance*Math.cos(angle)/this.decay)
	ystep=(dy<1)?Math.ceil(-distance*Math.sin(angle)/this.decay):Math.floor(-distance*Math.sin(angle)/this.decay)
	if(!this.constrainX) {
		this.stripObj.setx(this.stripObj.getx()+xstep)
	}
	if(!this.constrainY) {
		this.stripObj.sety(this.stripObj.gety()+ystep)
	}
	if(this.fade)this.doAlpha()
	if( distance>1 ) {
		this.active=true
		this.timer=setTimeout(this.objname+".chase("+x+","+y+")", this.speed)
	} else {
		clearTimeout(this.timer)
		this.stripObj.warp(x,y)
		this.active=false
	}
}

stripSlider.prototype.gotoPic = function(which) {
	if(which>=this.numpix)which=this.numpix-1
	if(which<0)which=0
	if(this.orientation=="h") {
		this.chase( this.sweetspot-this.pixObj[which].getx(), this.hy)
	} else {
		this.chase( this.hx, this.sweetspot-this.pixObj[which].gety() )
	}
}

stripSlider.prototype.doAlpha = function() {
	if(this.orientation=="h") {
		for(i=0; i<this.numpix; i++) {
			x=Math.abs( this.sweetspot-this.stripObj.getx()-this.pixObj[i].getx() )
			o=this.a*Math.exp((-x^2)/this.s) 
			this.pixObj[i].setopacity(Math.abs(o))
		}
	} else {
		for(i=0; i<this.numpix; i++) {
			y=Math.abs( this.sweetspot-this.stripObj.gety()-this.pixObj[i].gety() )
			o=this.a*Math.exp((-y^2)/this.s) 
			this.pixObj[i].setopacity(Math.abs(o))
		}
	}
}



