var STYLE_LIGHT = 0;
var STYLE_DARK = 1;
var STYLE_BORDER = 2;
var STYLES = [STYLE_LIGHT, STYLE_DARK, STYLE_BORDER];
var TOP_RANGE = [120, 360];
var WIDTH_RANGE = [80, 240];
var HEIGHT_RANGE = [400, 540];
var NUM_MENU_ITEMS = 2;

var planes = new Array();
var animating = false;
var selectedMenu = 0;

var initialize = function() {
	addEmail('emailAddress0', 'founders');
	var styleCounts = [2, 2, 4];
	for (var curStyle = 0; curStyle < STYLES.length; curStyle++)
		for (var i = 0; i < styleCounts[curStyle]; i++)
			planes.push(drawPlane(PlaneSpec.newRandom(), STYLES[curStyle]));	
};

var addEmail = function(id, nme) {
	var a = document.createElement("a");
	var emailArr = [nme];
	emailArr.push("@");
	emailArr.push("8pl", "anes.com");
	var emailAddress = emailArr.join("");
	a.setAttribute("href", "mailto:" + emailAddress);
	a.setAttribute("class", "link");
	a.innerHTML = emailAddress;
	$(id).appendChild(a);
};

var selectMenu = function(textNo) {
	selectedMenu = textNo;
	animatePlanes();
	for (var i = 0; i < NUM_MENU_ITEMS; i++)
		if (i == textNo) {
			$('content' + i).setStyle('display', 'block');
			$('menu' + i).addClass("selected");
		}
		else {
			$('content' + i).setStyle('display', 'none');
			$('menu' + i).removeClass("selected");
		}
};

var mouseOverMenu = function(menuNo, mouseOver) {
	if (menuNo != selectedMenu) {
		if (mouseOver)
			$('menu' + menuNo).addClass("selected");
		else
			$('menu' + menuNo).removeClass("selected");
	}
};

var animatePlanes = function() {
	if (animating) return;
	animating = true;
	var transitions = new Array();
	var numSteps = 20;
	var curStep = 0;
	var wait = 50;
	for (var i = 0; i < planes.length; i++)
		transitions[i] = new PlaneTransition(planes[i], 20);
	var stepFn = function() {
		for (var i = 0; i < transitions.length; i++)
			transitions[i].step();
		curStep++;
		if (curStep < numSteps)
			stepFn.delay(wait);
		else
			animating = false;
	};
	stepFn.delay(wait);
};

var drawPlane = function(planeSpec, style) {
	var plane = document.createElement("div");
	$(plane).setStyle('position', 'absolute');
	planeSpec.applyTo(plane);
	if (style == STYLE_BORDER)
		$(plane).setStyle('border', '1px solid #CCCCCC');
	else if (style == STYLE_LIGHT) {
		$(plane).setStyle('backgroundColor', '#DDDDDD');
		$(plane).setOpacity(0.5);
	}
	else {
		$(plane).setStyle('backgroundColor', '#CCDDDF');
		$(plane).setOpacity(0.5);
	}
	$('bgDiv').adopt(plane);
	return plane;
};

var PlaneTransition = function(plane, numSteps) {
	this.plane = plane;
	this.begSpec = plane.spec;
	this.endSpec = PlaneSpec.newRandom();
	this.numSteps = numSteps;
	this.curStep = 1;
};

PlaneTransition.prototype.step = function() {
	if (this.curStep > this.numSteps)
		throw new Error();
	var planeSpec = new PlaneSpec(
		this.steppedNum(this.begSpec.left, this.endSpec.left),
		this.steppedNum(this.begSpec.top, this.endSpec.top),
		this.steppedNum(this.begSpec.width, this.endSpec.width),
		this.steppedNum(this.begSpec.height, this.endSpec.height));
	planeSpec.applyTo(this.plane);
	this.curStep++;
};

PlaneTransition.prototype.steppedNum = function(begNum, endNum) {
	return begNum + (endNum - begNum) * 
		(Math.sin((this.curStep / this.numSteps - 0.5) * Math.PI) + 1) / 2;
};

var PlaneSpec = function(left, top, width, height) {
	this.left = left;
	this.top = top;
	this.width = width; 
	this.height = height;
};

PlaneSpec.prototype.applyTo = function(plane) {
	var that = this;
	plane.spec = this;
	$(plane).setStyles({
		left: that.left,
		top: that.top,
		width: that.width,
		height: that.height
	});
};

PlaneSpec.newRandom = function() {
	return new PlaneSpec(
		$random(-80, window.getWidth()),
		$random(TOP_RANGE[0], TOP_RANGE[1]),
		$random(WIDTH_RANGE[0], WIDTH_RANGE[1]),
		$random(HEIGHT_RANGE[0], HEIGHT_RANGE[1]));
};

