var Hive = {
	// divide page into columns of 256px (5 columns at max width of 1280px)
	cell: function(c) {
		var pos = c.positionedOffset();
		return [(pos.left / 256).floor()];
	}
}
var Bee = {
	// off go the bees
	gone: function() {
		Effect.multiple(['bee1', 'bee2', 'bee3', 'bee4'], Effect.Fade, { speed: 2 });
		new Effect.Appear($$('#bees p')[0], { delay: 7 });
	},
	// and back they come
	back: function() {
		new Effect.Fade($$('#bees p')[0], { duration: 0 });
		Effect.multiple(['bee4', 'bee3', 'bee2', 'bee1'], Effect.Appear);
	},
	// always twitching
	twitch: function() {
		bee1 = new Effect.Move('bee1', { y: -25, transition: Effect.Transitions.Bounce, duration: 0.1, afterFinish: function() {
			new Effect.Move('bee1', { y: 25, transition: Effect.Transitions.Bounce, duration: 0.1, afterFinish: function() {
				new Effect.Move('bee1', { y: -40, transition: Effect.Transitions.Bounce, duration: 0.2, delay: 5, afterFinish: function() {
					new Effect.Move('bee1', { y: 40, transition: Effect.Transitions.Bounce, duration: 0.2 });
				} });
			} });
		} });
		//bee3 = new Effect.Move('bee3', { y: -25, transition: Effect.Transitions.Bounce, duration: 0.1, delay: 0.4, afterFinish: function() {
		//	new Effect.Move('bee3', { y: 25, transition: Effect.Transitions.Bounce, duration: 0.1 });
		//} });
		//bee2 = new Effect.Move('bee2', { y: -25, transition: Effect.Transitions.Bounce, duration: 0.1, delay: 1.6, afterFinish: function() {
		//	new Effect.Move('bee2', { y: 25, transition: Effect.Transitions.Bounce, duration: 0.1 });
		//} });
		//bee4 = new Effect.Move('bee4', { y: -25, transition: Effect.Transitions.Bounce, duration: 0.1, delay: 2, afterFinish: function() {
		//	new Effect.Move('bee4', { y: 25, transition: Effect.Transitions.Bounce, duration: 0.1 });
		//} });
		//bee3b = new Effect.Move('bee3', { y: -40, transition: Effect.Transitions.Bounce, duration: 0.2, delay: 3, afterFinish: function() {
		//	new Effect.Move('bee3', { y: 40, transition: Effect.Transitions.Bounce, duration: 0.2 });
		//} });
	},
	// dragging and dropping
	drag: function(beeId) {
		$(beeId).setStyle('cursor: move;');
		var range = $R(0, 4); // set the range to 5 columns accepting bees...
		$(beeId).isOut = function() { // allows us to check if the bee is in this range
			var pos = Hive.cell(this);
			return !(range.include(pos[0]));
		};
		new Draggable(beeId, {
			revert: $(beeId).isOut.bind($(beeId)), // set revert method for cases where the bee ends up out of range...
			reverteffect: function(b, top_offset, left_offset) { // send it back the way it came, quickly and springily
				var secs = Math.sqrt((top_offset^2).abs() + (left_offset^2).abs()) * 0.02;
				new Effect.Move(b, { x: -left_offset, y: -top_offset, duration: secs, queue: { scope: '_draggable', position: 'end' }, transition: Effect.Transitions.spring });
			},
			onDrag: function() { // if the bee is actively dragged, stop it twitching temporarily
				if (beeId=='bee1') { bee1.cancel(); }
				if (beeId=='bee2') { bee2.cancel(); }
				if (beeId=='bee3') { bee3.cancel(); bee3b.cancel(); }
				if (beeId=='bee4') { bee4.cancel(); }
			},
			onEnd: function(d) {
				if (d.element.isOut()) return; // if the bee is out of 5 column range send it back as above
				var pos = Hive.cell(d.element); // at this point we're in range, so we check the bee's position
				pos = (pos[0] + 1); // then convert this into a column number between 1 and 5 for simplicity
				if (pos == '5') { // if the column number is 4, we're in the bee column, a little more exciting...
					new Effect.Move(beeId, { y: -140, transition: Effect.Transitions.Bounce, duration: 0.2, delay: 0.2, afterFinish: function() {
						new Effect.Move(beeId, { y: 140, transition: Effect.Transitions.Bounce, duration: 0.2 });
					} });
				} else { // otherwise we're in one of the other columns, still exciting but less so...
					new Effect.Move(beeId, { y: -70, transition: Effect.Transitions.Bounce, duration: 0.2, delay: 0.2, afterFinish: function() {
						new Effect.Move(beeId, { y: 70, transition: Effect.Transitions.Bounce, duration: 0.2 });
					} });
				}
			}
		});
	}
}
// once the dom has loaded...
document.observe('dom:loaded', function() {
	Bee.twitch.delay(5); // let's start those bees twitching
	new PeriodicalExecuter(Bee.twitch, 15); // and keep 'em twitching!
	//new PeriodicalExecuter(function(buzz) { Sound.play('sounds/buzz.mp3' ); }, 54);
});
