/**
 * TabScrollerUI
 * Copyright (c) 2010 Matthew Norris (http://www.avatar-generator.com)
 */
var TabScrollerUI = Class.create({
	
	STEP_DIV_WIDTH: 1000,
	
	initialize: function(destStep, onComplete) {
		this.initStepScrolling();
		this.scrolling = true;
		this.onComplete = (onComplete) ? onComplete : null;
		var contentContainer 	= $("content_container");
		var sourcePosition 		= contentContainer.scrollLeft;
		var destPosition 		= (destStep) * this.STEP_DIV_WIDTH
		var pixelSpan			= destPosition - sourcePosition;
		
		var xRel = 0; // this goes from 0 to 1
		var i = 0;
		var scrollLoop = function() {
 			if (!this.scrolling) // happens when the scroller gets cancelled.
				return;
			xRel = this.range[i];
			if (xRel >= 1) {
				contentContainer.scrollLeft = destPosition;
				if (this.onComplete != null)
					this.onComplete()
			} else {
				contentContainer.scrollLeft = sourcePosition + (xRel * pixelSpan);
				setTimeout(scrollLoop, 10);
				// carry on
				i++;
			}
		}.bind(this);
		scrollLoop();
	},
	initStepScrolling: function() {
		// build the bouncy range array
		var divver = 1.2;
		var rangeBit = [];
		var result = 1;
		do {
			rangeBit.push(result);
			result = result / divver; 
		} while (result > 0.001);
		this.range = [];
		for (i = 0; i < rangeBit.length; i++)
			this.range.push(1 - rangeBit[i]);
		this.range.push(1);
	},
	cancel: function() {
		this.scrolling = false;
	}
});
