/******************************************************************************/
/* mud_ShiftContent.js                                                               
/* REQUIRES mud_API.js
/* author: Takashi Okamoto mud(tm) - http://mudcorp.com
/******************************************************************************/

MudShiftContent.MOVE_COORDS = new Array(0, -1, -4, -7, -11, -17, -23, -30, -38, -47, -56, -66, -75, -84, -92, -99, -100);

// CONSTRUCTOR
function MudShiftContent(id, unitX, unitTotal) {
	this.id = id;
	this.posX = 0;
	this.scale = unitX / 100;
	this.dir = "next";
	this.moving = false;
	this.frame = 0;
	this.frameTotal = MudShiftContent.MOVE_COORDS.length;
	this.MOVE_COORDS = new Array(this.frameTotal);
	this.timerID = null;
	this.unit = 0;
	this.unitTotal = unitTotal;
}

// returns array
MudShiftContent.prototype.calcX = function(dir, currX) {
	switch (dir) {
		case "next":
			for (var i = 0; i < this.frameTotal; i++) {
				this.MOVE_COORDS[i] = currX + MudShiftContent.MOVE_COORDS[i] * this.scale;
			}
			break;
			
		case "prev":
			for (var i = 0; i < this.frameTotal; i++) {
				this.MOVE_COORDS[i] = currX - MudShiftContent.MOVE_COORDS[i] * this.scale;
			}
			break;

		case "start":
			for (var i = 0; i < this.frameTotal; i++) {
				this.MOVE_COORDS[i] = currX - MudShiftContent.MOVE_COORDS[i] * this.scale * this.unit;
			}
			break;
			
		case "end":
			for (var i = 0; i < this.frameTotal; i++) {
				this.MOVE_COORDS[i] = currX + MudShiftContent.MOVE_COORDS[i] * this.scale * (this.unitTotal-1);
			}
	}
}

MudShiftContent.prototype.moveTo = function(x) {
	getObject(this.id).left = x + "px";
}

MudShiftContent.prototype.move = function(dir) {
	this.setDir(dir);
	this.run();
}

MudShiftContent.prototype.setDir = function(dir) {
	this.dir = dir;
}

MudShiftContent.prototype.run = function() {
	if (this.timerID) {
		window.clearTimeout(this.timerID);
		this.timerID = null;
	}
	if (!this.moving) {
		// onstart method
		this.onStart();
		// calc the coords
		if (this.dir == "next") {
			if (this.unit < this.unitTotal-1) {
				this.calcX("next", this.posX);
				this.unit++;
			}
			else if (this.unit == this.unitTotal-1) {
				this.calcX("start", this.posX);
				this.unit = 0;
			}
			else return;
		}
		else if (this.dir == "prev") {
			if (this.unit > 0) {
				this.calcX("prev", this.posX);
				this.unit--;
			}
			else if (this.unit == 0) {
				this.calcX("end", this.posX);
				this.unit = this.unitTotal-1;
			}
			else return;
		}
		else return;
		this.moving = true;
	}
	if (this.frame < this.frameTotal) {
		this.posX = this.MOVE_COORDS[this.frame];
		this.frame++;
		this.moveTo(this.posX);
		// set timeout
		this.timerID = window.setTimeout(this.id + ".run()", 20);
	}
	else {
		this.moving = false;
		this.frame = 0;
		window.clearTimeout(this.timerID);
		this.timerID = null;
		// onEnd method
		this.onEnd();
	}
}

MudShiftContent.prototype.onStart = function() {
	hide("caption-"+this.unit);
}
MudShiftContent.prototype.onEnd = function() {
	show("caption-"+this.unit);
}
