function Butterfly ()
{
	this.butterfly = document.getElementById('butterfly');
	if (!this.butterfly) return;
	
	this.bframes = [new Image(), new Image(), new Image(), new Image(), new Image(), new Image()];
	this.bframes[0].src = "/design/baum/butterfly0.png";
	this.bframes[1].src = "/design/baum/butterfly1.png";
	this.bframes[2].src = "/design/baum/butterfly2.png";
	this.bframes[3].src = "/design/baum/butterfly0r.png";
	this.bframes[4].src = "/design/baum/butterfly1r.png";
	this.bframes[5].src = "/design/baum/butterfly2r.png";
	
	var bf = this;
	window.setTimeout(function(){bf.init();}, 3000);
}

Butterfly.prototype.measureCanvas = function ()
{
	var cs = dGetCanvasSize();
	this.maxw = cs[0]-this.bframes[0].width-10;
	this.maxh = cs[1]-this.bframes[0].height-10;
}

Butterfly.prototype.init = function ()
{
	var bf = this;
	
	// Wait until all 3 frames are loaded.
	for (var i = 0; i < 6; i++)
	{
		if (!this.bframes[i].complete)
		{
			var bf = this;
			window.setTimeout(function(){bf.init();}, 1000);
			return;
		}
	}
	
	// Get position
	this.left = -100;
	this.top = -100;
	this.measureCanvas();
	window.setInterval(function(){bf.measureCanvas();}, 1000);
	
	// Wake up call!
	var bf = this;
	var fc = 0;
	var dir = [false,false];
	var ttl = 0;
	this.flyInterval = window.setInterval(function()
	{
		// New decision
		if (ttl <= 0)
		{
			ttl = 15+parseInt(Math.random()*30);
			dir = [Math.random()<=.5 ? true : false, Math.random()<=.5 ? true : false];
			
			if (bf.left < 0 && Math.random() > .25) {dir[0] = true; ttl += 10;}
			if (bf.top < 0 && Math.random() > .25) {dir[1] = true; ttl += 10;}
		}
		
		// Change frame
		bf.setFrame(fc, dir[0]);

		// Move ya!
		bf.left += parseInt(Math.random()*16) * (dir[0] ? 1 : -1);
		bf.top += parseInt(Math.random()*16) * (dir[1] ? 1 : -1);
		
		if (bf.left < -100) {ttl = 0; bf.left = -100;}
		if (bf.top < -100) {ttl = 0; bf.top = -100;}
		if (bf.left > bf.maxw) {ttl = 0; bf.left = bf.maxw;}
		if (bf.top > bf.maxh) {ttl = 0; bf.top = bf.maxh;}
		
		bf.butterfly.style.left = bf.left+"px";
		bf.butterfly.style.top = bf.top+"px";
		
		// Go on and on and on
		fc = (fc+1) % 3;
		ttl--;
	}, 100);
}

Butterfly.prototype.setFrame = function (i, isright)
{
	if (isright) i+= 3;
	this.butterfly.src = this.bframes[i].src;
}

if (typeof(window.dGetWinDims) == "undefined" || typeof(window.dGetCanvasSize) == "undefined")
{
	function dGetWinDims (w)
	{
		if (!w) w = window;
		var x,y;
		if (w.innerHeight) // all except Explorer
		{
			x = w.innerWidth;
			y = w.innerHeight;
		}
		else if (w.document.documentElement && w.document.documentElement.clientHeight)
		{
			// Explorer 6 Strict Mode
			x = w.document.documentElement.clientWidth;
			y = w.document.documentElement.clientHeight;
		}
		else if (w.document.body) // other Explorers
		{
			x = w.document.body.clientWidth;
			y = w.document.body.clientHeight;
		}
		return new Array(x,y);
	}
	
	function dGetCanvasSize (w)
	{
		if (!w) w = window;
		
		var v = dGetWinDims();
		var b = w.document.getElementsByTagName("body")[0];
		if (b.scrollHeight || b.scrollWidth)
		{
			if (v[0] < b.scrollWidth) v[0] = b.scrollWidth;
			if (v[1] < b.scrollHeight) v[1] = b.scrollHeight;
		}
		else
		{
			if (v[0] < b.offsetWidth) v[0] = b.offsetWidth;
			if (v[1] < b.offsetHeight) v[1] = b.offsetHeight;
		}
		return v;
	}
}
