;
function trainClass(options){

	var _this = this;
	
	this.$train = {};
	this.$trainHolder = {};
	
	this.port = options.port;
	
	this.id = (typeof options.id != 'undefined') ? options.id : false ;

	this.offsetTop = (typeof options.offsetTop != 'undefined') ? options.offsetTop : 0 ;

	this.cargoType = (typeof options.cargoType != 'undefined') ? options.cargoType : 4 ;
	
	this.places = (typeof options.places != 'undefined') ? options.places : 4 ;

	this.cargoTarget = (typeof options.cargoTarget != 'undefined') ? options.cargoTarget : 'ship' ;

	this.hasCargo = (typeof options.hasCargo != 'undefined') ? options.hasCargo : false ;

	this.typeSize = this.port.sizesByType[this.cargoType];
	
	this.placesInVagon = Math.ceil(4 / this.typeSize);


//	this.vagons = this.places * this.placesInVagon;
	this.vagons = Math.ceil(this.places * this.typeSize / 4 );

	this.cargoShipped = false;
	
	this.init();
}

trainClass.prototype = {
	trainsCreated: 0,
	movingEasing: 'easeInOutQuad',
	start: -1000,
	speed: {
		forward: 120, /* проходит за 1 сек */
		backward: 60 /* проходит за 1 сек */
	},
	parkingArea: {
		from: 175,
		to: 400
	},
	init: function(){ // Инициализация крана
		this.createTrain(); // Создаем поезд
		this.$trainHolder.data('class',this);
		this.$trainHolder.hide().appendTo('#port-animation');
		this.$trainHolder.css('left', this.start);
	},
	createTrain: function(){ // Создаем DOM и jQuery объекы нового грузовика
		var _this = this;
		trainClass.prototype.trainsCreated++; // Увеличиваем общее число грузовиков

		if(!this.id) this.id = 'train'+this.trainsCreated; // Формируем идентификатор нового поезда если он не указан
		
		this.$trainRoad = $("<div/>", {'class': 'train-road', 'css':{'top': this.offsetTop}}).prependTo('body'); // Создаем железнодорожное полотно
		this.$trainHolder = $("<div/>", {'class': 'train-holder', 'id': this.id, 'css':{'top': this.offsetTop - this.port.offset.top}}).appendTo(this.$trainRoad);
	},
	show:function(callback){
		var _this = this;
		var start = parseInt(0 - this.port.globalOffset - this.width);
		var finish = Math.randomFromTo( this.parkingArea.from , this.parkingArea.to );
		var speed = this.port.getSpeed(finish - start, this.speed.backward);
/*		
		var f1 = function(){
			_this.$train.animate({left: '-=900px'}, 6000);
			if(typeof callback == 'function') callback();
		}
*/
		this.$trainHolder.show().animate({left: finish}, speed, this.movingEasing, callback);
	},
	hide: function(callback){
		var _this = this;
		var start = parseInt(this.$trainHolder.css('left'));
		var finish = 0-this.port.globalOffset-this.width;
		var timeout = 500;
		setTimeout(function(){
			_this.$trainHolder.animate({left: finish}, _this.port.getSpeed(finish - start, _this.speed.forward), _this.movingEasing, callback);
		}, _this.port.speed(timeout));
	},
	stop: function(){
		this.$train.stop();
	},
	dropCargo: function($cargo){
	},
	equip: function(data){
		var _this = this;
		this.$trainHolder.empty(); // Удаляем прошлый состав и формируем новый
		var lastVagon = {};
		var shipped = false;
		if(typeof data == 'object'){
			if(typeof data.places == 'number') this.places = data.places;
			if(typeof data.size == 'number') this.typeSize = data.size;
			if(typeof data.cargo == 'object'){
				if(this.places < data.cargo.length) this.places = data.cargo.places; // Увеличиваем количество мест если их меньше чем груза
				shipped = true;
				var total_size = 0;

				_this.$train = $("<div/>", {'class': 'train'}).appendTo(_this.$trainHolder);

				for (c = 0; c < data.cargo.length; c++){
					var cargo = data.cargo[c];
					var cargo_size = this.port.sizesByType[cargo[0]];
					var cargo_position = false;
					var cargo_target = (typeof cargo[1] != 'undefined') ? cargo[1] : false;
					total_size += cargo_size;
					if(cargo_size == 4){
						lastVagon = _this.$trainVagon = $("<div/>", {'class': 'train-vagon'}).appendTo(_this.$trainHolder);

						_this.$cargoHolder = $("<div/>", {'class': 'cargo-holder type'+cargo[0]+' place1', 'cargo-type':this.cargoType}).appendTo(_this.$trainVagon);

						if(typeof cargo[2] == 'number'){
							var $cargo = _this.port.createCargo(cargo[0],cargo[1],cargo[2]);
						}else{
							var $cargo = _this.port.createCargo(cargo[0],cargo[1]);
						}

						if(cargo_target) this.port.crane.stack.push($cargo); // Набираем стек
						_this.$cargoHolder.append($cargo);
					}
				}
				
				this.width = this.$trainHolder.width();
				this.parkingArea.to = 900 - this.width;
				this.start = 0 - this.port.globalOffset - this.width;

				return this;

//				this.vagons = Math.ceil(this.places * this.typeSize / 4 );
			}
		}else{
			return this;
		}
		this.width = this.$trainHolder.width();
		this.parkingArea.to = 900 - this.width;
		this.start = 0 - this.port.globalOffset - this.width;
		return this;
	}
};
