/**
 * servicesOpen() to reveal
 */
function servicesOpen() {
	var services = document.getElementById('services');
	
	if (0 == mover.target) {
		//open the panel by default
		mover.target = servicesOpen.height;
		mover.animate(5, function(height) {services.style.height = height + 'px';});
	} else {
		//otherwise close it again
		mover.target = 0;
		mover.animate(5, function(height) {services.style.height = height + 'px';});
	}
		
}

function servicesClose() {
	var services = document.getElementById('services');
	mover.target = 0;
	mover.animate(5, function(height) {services.style.height = height + 'px';});
}
	

/**
 * move() for showing/hiding services panel
 */
function move(position, target) {
	this.position = position;
	this.target = target;
	this.velocity = 0;
	this.interval = null;
}

move.prototype.update = function() {
	if (this.velocity < 0) {
		if (this.target > this.position - this.velocity * (this.velocity - 1) / 2) {
			this.velocity ++;
		} else if (this.target <= this.position - (this.velocity - 1) * (this.velocity - 2) / 2) {
			this.velocity--;
		}
	} else {
		if (this.target < this.position + this.velocity * (this.velocity + 1) / 2) {
			this.velocity--;
		} else if (this.target >= this.position + (this.velocity + 1) * (this.velocity + 2) / 2) {
			this.velocity++;
		}
	}
	this.position += this.velocity;
	return this.position;
}

move.prototype.stopped = function() {
	return (this.position == this.target && 0 == this.velocity);
}

move.prototype.animate = function(interval, updater, stopper) {
	if (this.interval) window.clearInterval(this.interval);
	
	this.interval = window.setInterval(this.closure(updater, stopper), interval);
}

move.prototype.closure = function(updater, stopper) {
	var that = this;
	return function() {
		that.update();
		updater(that.position, that);
		if (that.stopped()) {
			window.clearInterval(that.interval);
			that.interval = null;
			if (stopper) {
				stopper(that);
			}
		}
	}
}
var mover = new move(0, 0);
