//
// Global bugHoverBug management -- BHB (Bugimus Hover Bug)
// extends BDD
//
// it hovers around coordinates passed to it
// when you pick it up and drag it, it holds still
// as soon as you let it go, it hovers again
//

bugBase.prototype.attachBHB = function() {
	this.BHBnum = 0
	this.BHBobj = new Array()
}
window.bugimus.attachBHB()

//
// Local bugHoverBug functionality
//

// Define main object
function hoverBug(x,y,w,h,bgcolor) {
	this.objname = "window.bugimus.BHBobj[" + window.bugimus.BHBnum + "]"
	this.objnum = window.bugimus.BHBnum
	window.bugimus.BHBobj[window.bugimus.BHBnum++] = this

	this.bddobj = new dragMe(0,0,w,h,bgcolor)

	this.bddobj.obj.appendChild(document.createTextNode("Hover\nBug #"+this.objnum))
	this.bddobj.obj.style.fontSize="11pt"

	this.hoverstep=this.getrand(1,360)
	this.speed = 30
	this.amp = 2
	this.timer = null
	this.x = x
	this.y = y

	this.hover(this.x,this.y)
}

hoverBug.prototype.hover = function ( x, y ) {
	if(this.bddobj.dragging) {
		clearTimeout(this.timer)
		this.x = this.bddobj.obj.getx()
		this.y = this.bddobj.obj.gety()
		this.timer=setTimeout(this.objname+".hover("+this.x+","+this.y+")", this.speed)
	} else {
		if(this.hoverstep>360)this.hoverstep=1; else this.hoverstep++;
		this.bddobj.obj.setx( x + this.amp*Math.sin((20*Math.cos(this.hoverstep/30))) )
		this.bddobj.obj.sety( y + this.amp*Math.cos((30*Math.sin(this.hoverstep/40))) )
		this.timer=setTimeout(this.objname+".hover("+this.x+","+this.y+")", this.speed)
	}
}

// Random generator utility
// found here http://webreference.com/js/tips/990925.html
hoverBug.prototype.getrand = function ( x, y ) {
	var range = y - x + 1;
	return Math.floor(Math.random() * range) + x;
}


