var TrainSimulator = new Class({
	
	Implements: Options,
	
	options: {
		targetContainer: 'page',
		minimumDuration: 15000,
		minimumDelay: 10000,
		minimumWaggons: 4,
		transition: 'linear'
	},
	
	/**
	 * All locomotives need unique names and are represented by an image
	 */
	locomotives: {
		'steam1': ['fileadmin/sys/images/fahrzeuge/dampflok.png', 0],
		'diesel1': ['fileadmin/sys/images/fahrzeuge/diesel-blau.png', -3],
		'diesel2': ['fileadmin/sys/images/fahrzeuge/diesel-rot.png', -5],
		'steam2': ['fileadmin/sys/images/fahrzeuge/dampflok-lang.png', 3],
		'diesel3': ['fileadmin/sys/images/fahrzeuge/diesel-lang.png', 5],
		'electric1': ['fileadmin/sys/images/fahrzeuge/elok.png', 4]
	},
	
	/**
	 * All waggons need unique names and are represented by an image and xoffset
	 */
	waggons: {
		'fasswagen': ['fileadmin/sys/images/fahrzeuge/fasswagen.png', 3],
		'gueter-geschlossen': ['fileadmin/sys/images/fahrzeuge/gueterwagen-geschlossen.png', 0],
		'gueter-schuett': ['fileadmin/sys/images/fahrzeuge/gueterwagen-schuettgut.png', 5],
		'autos': ['fileadmin/sys/images/fahrzeuge/autotransporter.png', 4],
//		'postwagen': ['fileadmin/sys/images/fahrzeuge/postwagen.png', 4],
		'kohle': ['fileadmin/sys/images/fahrzeuge/kohlewagen.png', 5],
		'kesselwagen': ['fileadmin/sys/images/fahrzeuge/tankwagen.png', 1]
	},
	
	/**
	 * Only for internat purpose (@private)
	 */
	currentTrain: null,
	track: null,
	
	/**
	 * Initializes the locomotives, waggons and the track.
	 * Also starts the trainbuilding process
	 */
	initialize: function(options) {
		this.setOptions(options);
		this.locomotives = new Hash(this.locomotives);
		this.waggons = new Hash(this.waggons);
		this.track = new Element('div', {
			styles: {
				'position': 'absolute',
				'top': 0,
				'left': '287px',
				'width': '73px',
				'overflow': 'hidden',
				'height': $(this.options.targetContainer).getCoordinates().height.toInt() - 40
			}
		}).injectInside($(this.options.targetContainer));
		this.nextTrain();
	},
	
	/**
	 * Resets the track and delays the start of the next train.
	 */
	nextTrain: function() {
		if($defined(this.currentTrain)) {
			this.currentTrain.getContainer().dispose();
			delete this.currentTrain;
		}
		this.currentTrain = this.buildRandomTrain();
		var delay = Math.ceil(Math.random() * 10000 + this.options.minimumDelay);
		this.log('train starts in ' + (delay / 1000) + ' seconds');
		this.startRolling.delay(delay, this);
	},
	
	/**
	 * Starts the locomotive engines...
	 */
	startRolling: function() {
		this.log('Woooooot, wooooooooot!');
		this.currentTrain.getContainer().setStyle('margin-top', this.track.getSize().y).injectInside(this.track).set('tween', {
			duration: Math.ceil(Math.random() * 10000 + this.options.minimumDuration),
			'onComplete': this.nextTrain.bind(this),
			'transition': this.options.transition
		}).tween('margin-top', this.currentTrain.getContainer().getCoordinates().height * -1);
	},
	
	/**
	 * Creates a random train from possible locomotives and waggons
	 */
	buildRandomTrain: function() {
		this.log('building new Train');
		var locomotive = this.getRandomElement(this.locomotives);
		var train = new Train( locomotive[0], locomotive[1] );
		for(var i = 0; i < Math.ceil(Math.random() * 3 + this.options.minimumWaggons); i++) {
			var waggon = this.getRandomElement(this.waggons)
			train.addWaggon( waggon[0], waggon[1] );
		}
		return train;
	},
	
	/**
	 * Returns random element from given Hash
	 * @param {Hash} sourceHash
	 */
	getRandomElement: function(sourceHash) {
		var random = sourceHash.getKeys().getRandom();
		this.log('adding ' + random);
		return sourceHash.get(random);
	},
	
	/**
	 * Logs given text to console if present
	 * @param {String} message
	 */
	log: function(message) {
//		try {
//			console.log(message);
//		} catch(e) {
//			console = new Console();
//			console.log(message);
//		}
	}
	
});