;



function truckClass(options){

	var _this = this;

	this.port = options.port;	

	this.init();
}

truckClass.prototype = {
	trucksCreated: 0,
	movingEasing: 'easeInOutQuad',
	start: -1000,
	speed: {
		forward: 120, /* проходит за 1 сек */
		backward: 60 /* проходит за 1 сек */
	},
	parkingArea: {
		from: 75,
		to: 120
	},
	init: function(){ // Инициализация крана
		this.createTruck(); // Создаем грузовик
		this.$truck.data('class',this);
		this.$truck.hide().appendTo('#port-animation');
		this.width = this.$truck.width();
		this.start = 0 - this.port.globalOffset - this.$truck.width();
		this.$truck.css('left', this.start);
	},
	createTruck: function(){ // Создаем DOM и jQuery объекы нового грузовика
		var _this = this;
		truckClass.prototype.trucksCreated++; // Увеличиваем общее число грузовиков
		this.id = 'truck'+this.trucksCreated; // Формируем идентификатор нового грузовик
		// Грузовик
		this.$truck = $("<div/>", {'class': 'truck truck-holder', 'id': this.id,
			click: function(){
				_this.stop();
			}});
		// Держатель груза
		this.$truckImageHolder = $("<div/>", {'class': 'truck-image-holder'}).appendTo(this.$truck);
		// Держатель груза
		this.$cargoHolder = $("<div/>", {'class': 'cargo-holder'}).appendTo(this.$truck);
		// Свет стопов
		this.$stopLight = $("<div/>", {'class': 'truck-stop-light'}).appendTo(this.$truck);
		// Свет заднего хода
		this.$reverseLight = $("<div/>", {'class': 'truck-reverse-light'}).appendTo(this.$truck);
		return this.$truck; // Возвращаем ссылку на грузовик
	},
	show:function(callback){
		var _this = this;
		var start = parseInt(this.$truck.css('left'));
		var finish = Math.randomFromTo( this.parkingArea.from , this.parkingArea.to );
		if(this.$truck.is(':hidden')) this.$truck.show();
		this.$truck.find('.cargo').remove();
		var speed = this.port.getSpeed(finish - start, this.speed.backward);
		this.$reverseLight.show(); // Зажигаем задний свет
		setTimeout(function(){
			_this.$stopLight.show(); // Зажигаем стопы
		}, speed / 1.7);

		var callback2 = function(){ // Действие выполняемое после остановки
			_this.$reverseLight.hide(); // Тушим задний ход
			setTimeout(function(){
				_this.$stopLight.hide(); // Тушим стопы
			}, _this.port.speed(2400) );
			if(typeof callback == 'function') callback();
		}
		this.$truck.animate({left: finish}, speed, this.movingEasing, callback2);
	},
	hide: function(callback){
		var _this = this;
		var start = parseInt(this.$truck.css('left'));
		var finish = 0-this.port.globalOffset-200;
		var timeout = 500;
		this.$reverseLight.hide(); // Тушим задний ход
		setTimeout(function(){
			_this.$truck.animate({left: finish}, _this.port.getSpeed(finish - start, _this.speed.forward), _this.movingEasing, callback);
		}, _this.port.speed(timeout));
	},
	stop: function(){
		this.$truck.stop();
		this.$stopLight.hide();
		this.$reverseLight.hide();
	},
	dropCargo: function($cargo){
	}
};
