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

FILE: mud_ShiftContent.js
REQUIRES: prototype.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 2.1 - update to make it work more like MudFadeGallery, support variable width images
DATE: 04/07/2006

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

This file is part of MudShiftContent.

	MudShiftContent is free for anyone to use, but this header MUST be
	included, and may not be modified.

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

var MudShiftContent = Class.create();

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

MudShiftContent.prototype = {
	
	/* ------------- CALLBACKS ------------- */
	/* ------------- EDITABLE -------------- */
	
	onShiftStart: function() {
		var title = "...";
		var caption = "";
		if ($(this.id+"_title")) $(this.id+"_title").innerHTML = title;
		if ($(this.id+"_caption")) $(this.id+"_caption").innerHTML = caption;
	},

	onShiftEnd: function() {
		var title = (this.imgsArray[this.unit].title) ? this.imgsArray[this.unit].title : "";
		var caption = (this.imgsArray[this.unit].caption) ? this.imgsArray[this.unit].caption : "";
		if ($(this.id+"_title")) $(this.id+"_title").innerHTML = title;
		if ($(this.id+"_caption")) $(this.id+"_caption").innerHTML = caption;
	},
	
	/* ------------- DON'T EDIT PAST HERE ------------- */
	
	initialize: function(id, imgsArray) {
		this.id = id;
		this.posX = 0;
		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.imgsArray = imgsArray;
		this.unit = 0;
		this.unitTotal = imgsArray.length;
		
		this.total_width = this.calcWidth();
	},
	
	calcWidth: function() {
		var width = 0;
		for (var i = 0; i < this.imgsArray.length; i++) {
			width += this.imgsArray[i].width;
		}
		return width;
	},

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

			case "start":
				for (var i = 0; i < this.frameTotal; i++) {
					this.MOVE_COORDS[i] = currX - MudShiftContent.MOVE_COORDS[i] / 100 * (this.total_width - this.imgsArray[this.imgsArray.length-1].width);
				}
				break;
			
			case "end":
				for (var i = 0; i < this.frameTotal; i++) {
					this.MOVE_COORDS[i] = currX + MudShiftContent.MOVE_COORDS[i] / 100 * (this.total_width-this.imgsArray[this.imgsArray.length-1].width);
				}
		}
	},

	moveTo: function(x) {
		$(this.id).style.left = x + "px";
	},

	move: function(dir) {
		this.setDir(dir);
		this.run();
	},

	setDir: function(dir) {
		this.dir = dir;
	},

	run: function() {
		if (this.timerID) {
			window.clearTimeout(this.timerID);
			this.timerID = null;
		}
		if (!this.moving) {
			// run on start callback
			this.onShiftStart();
			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);
			this.timerID = window.setTimeout(this.id + ".run()", 20);
		}
		else {
			this.moving = false;
			this.frame = 0;
			window.clearTimeout(this.timerID);
			this.timerID = null;
			// run on end callback
			this.onShiftEnd();
		}
	}
}