/*******************************************************************************

FILE: mud_ShiftContent.js
REQUIRES: mud_API.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 1.0 - initial public release
DATE: 07/22/2005

--------------------------------------------------------------------------------

This file is part of MudShiftContent.

	MudShiftContent is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	MudShiftContent is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Foobar; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*******************************************************************************/

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) {
		this.onShiftStart();
		// 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;
		this.onShiftEnd();
	}
}

MudShiftContent.prototype.onShiftStart = function() {
	hide("bagtext-"+this.unit);
};

MudShiftContent.prototype.onShiftEnd = function() {
	show("bagtext-"+this.unit);
};