//
// Global bugSwarm management -- BBS (Bugimus Bug Swarm)
//

bugBase.prototype.attachBBS = function() {
	this.BBSnum = 0
	this.BBSobj = new Array()
}
window.bugimus.attachBBS()

//
// Local bugSwarm functionality
//

// Define swarm object
function swarm(numbugs,widthdivisor,heightdivisor,speed) {
	this.objname = "window.bugimus.BBSobj[" + window.bugimus.BBSnum + "]"
	window.bugimus.BBSobj[window.bugimus.BBSnum++] = this

	this.speed = speed
	this.x = 0
	this.y = 0	
	this.wd = widthdivisor
	this.hd = heightdivisor
	this.scroll = 0
	
	this.distance = 0
	this.alpha = 0
	this.qzone = 2

	this.bug = new Array()
	for(i=0; i<numbugs; i++) {
		this.bug[i] = new swarmBug(this,i)
	}
	this.cursorChase(this.x,this.y)
}

swarm.prototype.cursorChase = function ( x, y ) {
    step = 2
	dx = this.x - x + 0
	dy = this.y - y + 0
	this.distance = parseInt(Math.sqrt(dx*dx+dy*dy))
	if( dx!=0 ) this.alpha = Math.atan( dy/dx )
	else this.alpha = Math.PI/2
	if( dx>0 ) this.alpha+=Math.PI
	xstep = step*Math.cos(this.alpha)
	ystep = step*Math.sin(this.alpha)
	dspeed = 0.5 + this.distance/75
	if( Math.abs(dx) > this.qzone ) {
		this.x+=xstep*dspeed
	} 
	if( Math.abs(dy) > this.qzone ) {
		this.y+=ystep*dspeed
	}
	this.scroll=bugimus.BMIscrollY()
	setTimeout( this.objname+".cursorChase("+bugimus.BMImousex+","+(bugimus.BMImousey+this.scroll)+")", this.speed )
}

// Define bug object
function swarmBug(swarmObj,bugnum) {
	size=10
	this.bugnum = bugnum
	this.swarmobj = swarmObj
	this.currStep = 0
	// ------------------------------------------------------------
	// Create strip div appended to body.
	document.body.appendChild(this.obj=document.createElement("DIV"))
	this.obj.getx = getoX
	this.obj.gety = getoY
	this.obj.setx = setoX
	this.obj.sety = setoY
	this.obj.show = showoDiv
	this.obj.hide = hideoDiv
	with(this.obj.style) {
		position="absolute"
		width=size  
		height=size 
		border="solid 1px #f00"
		backgroundColor="#999" 
		clip="rect(0,"+size+","+size+",0)"
		overflow="hidden"
	}
	this.swarmMove(this.obj.getx(),this.obj.gety())
}

swarmBug.prototype.swarmMove = function ( x, y ) {
	/* The code that produces the swarm movement is based on code from Doc Ozone (www.ozones.com) who got it from 
	   fullerene.com originally.  Thanks to both of these sites for allowing the Swarm to thrive!
	*/
	IExoffset=30
	IEyoffset=30
	width=bugimus.BSNwinWidth
	height=bugimus.BSNwinHeight+this.swarmobj.scroll
	step = 0.3
	j = this.bugnum
	xBase = height/this.swarmobj.wd
	yBase = width/this.swarmobj.hd

    this.obj.setx( x + Math.sin((20*Math.sin(this.currStep/30))+j*70)*xBase*(Math.sin(10+this.currStep/(10+j))+0.2)*Math.cos((this.currStep + j*55)/10) )
    this.obj.sety( y + Math.cos((20*Math.sin(this.currStep/(30+j)))+j*70)*yBase*(Math.sin(10+this.currStep/10)+0.2)*Math.cos((this.currStep + j*55)/10) )
	this.currStep += step
	if( bugimus.IE5 ) {
		this.obj.show()
		if( this.obj.getx() < 0 ) {
			this.obj.setx(0)
			this.obj.hide()
		} else if( this.obj.getx() > width-IExoffset ) { 
			this.obj.setx( width-IExoffset )
			this.obj.hide()
		}
		if( this.obj.gety() < 0 ) {
			this.obj.sety(0)
			this.obj.hide()
		} else if( this.obj.gety() > height-IEyoffset ) {
			this.obj.sety( height-IEyoffset )
			this.obj.hide()
		}
	}
	setTimeout( this.swarmobj.objname+".bug["+this.bugnum+"].swarmMove("+this.swarmobj.x+","+this.swarmobj.y+")", this.swarmobj.speed )
}


