function RandomTrain(timeout, timeout_rate){
	/***************************
	* Variables
	****************************/
	var box = $("#random_train");
	var ms_per_step;
	var px_per_step;
	var offset = 730;
	var rear_width = 0;
	var delta_rear_width = 0;
	var direction;
	var image_stack = [];
	var rear_car;
	var rear_ready = true;

	this.step = function(){
		/***************************
		* add to rear
		****************************/
		if (rear_ready && delta_rear_width >= rear_width && image_stack.length !== 0){
			rear_car = image_stack.pop();
			if (direction == 'left'){
				box.append(rear_car);
			}else if (direction == 'right'){
				box.prepend(rear_car);
			}
			rear_ready = false;
		}
		if (!rear_ready && rear_car.width() !== 0){
			rear_width = rear_car.width();
			delta_rear_width = 0;
			rear_ready = true;
		}

		/***************************
		* remove from front
		****************************/
		if (offset < 0 && box.children().length !== 0){
			var front_car;
			if (direction == 'left'){
				front_car = box.children(":first-child");
			}else if (direction == 'right'){
				front_car = box.children(":last-child");
			}
			offset = offset + front_car.width();
			front_car.remove();
		}

		/***************************
		* train is done
		****************************/
		if (box.children().length === 0){
			var train = new RandomTrain(timeout*timeout_rate, timeout_rate);
			return;
		}

		/***************************
		* move
		****************************/
		if (rear_ready){
			if (direction == 'left'){
				box.css("left", offset+"px");
				box.css("right", "");
			}else if (direction == 'right'){
				box.css("right", offset+"px");
				box.css("left", "");
			}
			offset = offset - px_per_step;
			delta_rear_width = delta_rear_width + px_per_step;
		}

		/***************************
		* step again after timeout
		****************************/
		var thisObj = this;
		setTimeout(function(){thisObj.step();}, ms_per_step);
	};

	/***************************
	* load train cars
	****************************/
	this.init = function(){
		$.ajax({
				type: "GET",
				async: false,
				url: "/random_train",
				dataType: "xml",
				success: function(xml){
					ms_per_step = parseInt($("ms_per_step", xml).text(), 10);
					px_per_step = parseInt($("px_per_step", xml).text(), 10);
					direction = $("direction", xml).text();
					$("car", xml).each(function(i, car){
							var img = $(new Image());
							img.attr("src", $(car).text());
							image_stack.push(img);
						}
					);
				}
			}
		);
		this.step();
	};
	var thisObj = this;
	setTimeout(function(){thisObj.init();}, timeout);
}
