/* HANDLES PHYSICS FOR THE VARIOUS SLIDING/EXPANDING ELEMENTS */

var NumberSlider = Class.create();
NumberSlider.prototype = {

	initialize: function (options) {
		this.options = Object.extend({
			startValue:		0,
			resolution:		3
		}, options || {});
		this.value = this.options.startValue;
		this.target = this.value;
		this.lastChange = 0;
		this.moving = false;
	},
	
	slideTo: function (target) {
		this.target = target;
		this.lastChange = 0;
		this.moving = true;
	},
	
	jumpTo: function (target) {
		this.target = target;
		this.moving = false;
		this.value = target;
	},
	
	step: function () {
		var change = (this.target - this.value) / this.options.resolution;
		if (Math.abs(change) < 1 && change != 0)
			change = this.target - this.value;
		change = parseInt(change);
		
		this.value += change;
		this.lastChange = change;
		this.moving = (this.value != this.target);
		
		return !this.moving;
	}

};





/* modules for home page */
var currentModule = null;

var Module = Class.create();
Module.modules = [];
Module.resolution = 50;
Module.step = 2;
Module.started = false;

Module.prototype = {

	initialize: function (element) {
		this.element = $(element);
	
		this.bodyElement = this.element.down('.body');		
		this.contentElement = this.element.down('.content');
		this.footerLink = this.element.down('.footer').down('a');

		this.contentWidth = this.contentElement.getWidth();
		
		this.width = new NumberSlider({resolution: Module.step});

		this.horizOffset = 0;
		
		this.prepared = false;
		this.modulesAfter = [];
		
		var self = this;
		Module.modules.each(function (module) { module.modulesAfter.push(self); });
		Module.modules.push(this);
	},
	
	prepare: function () {
		this.prepared = true;
		Position.absolutize(this.element);
		this.element.style.margin = '0';
		this.offsetLeft = Position.positionedOffset(this.element)[0];
		Event.observe(this.element, 'mouseover', this.expand.bind(this));
	},
	
	position: function () {
		this.element.style.left = this.offsetLeft + this.horizOffset + 'px';
	},
	
	updateBody: function () {
		change = this.width.lastChange;
		
		this.modulesAfter.each(function (module) {
			module.horizOffset += change;
			module.position();
		});
		
		var targets = [this.element, this.bodyElement, this.footerLink];
		if (change < 0)
			targets.reverse();
			
		targets.each(function (elem) {
			elem.style.width = elem.getWidth() + change + 'px';
		});
	},
	
	expand: function () {
		if (this.prepared && Module.started) {
			var skip = this;
			Module.modules.each(function (module) {
				if (module != skip)
					module.contract(); 
			});
			this.resizeTo(this.contentWidth);
		}
	},
	
	contract: function () {
		if (this.prepared) {
			this.resizeTo(0);
		}
	},
	
	resizeTo: function (width) {
		if (this.width.target != width) {
			this.width.slideTo(width);
			Module.setTimer();
		}
	},
	
	tick: function () {
		this.width.step();
		this.updateBody();

		return (!this.width.moving);
	}
	
};

Module.setTimer = function () {
	if (!Module.timer)
		Module.timer = setTimeout(Module.tick, Module.resolution);
};

Module.tick = function () {
	var finished = true;
	Module.modules.reverse(false).each(function (module) {
		if (!module.tick())
			finished = false;
	});
	
	Module.timer = null;
	if (!finished)
		Module.setTimer();
};

Module.prepare = function () {
	Module.element = $('modules');
	Position.relativize(Module.element);
	Module.modules.reverse(false).invoke('prepare');
};

Module.start = function () {
	var modulesOffset = Position.cumulativeOffset(Module.element)[0];
	Module.targetOffset = Position.cumulativeOffset($('modulewrap'))[0];
	if (modulesOffset != Module.targetOffset) {
		Module.offset = new NumberSlider();
		Module.offset.slideTo(Module.targetOffset - modulesOffset);
		Module.slideLeft();
	} else {
		Module.started = true;
		Module.modules.first().expand();
	}
};

Module.slideLeft = function () {
	Module.offset.step();
	Module.element.style.left = Module.offset.value + 'px';
	if (Module.offset.moving)
		setTimeout(Module.slideLeft, Module.resolution);
	else {
		Module.started = true;
		Module.modules.first().expand();
	}
};

Event.observe(window, 'load', function (e) {
	$$('.module').each(function (element) { new Module(element) });
	Module.prepare();
	setTimeout(Module.start, 1000);
});
