extend(Ship, createjs.Container);

function Ship(_kind, _direct, _sh)
{	
	this.initialize();
	var th = this
//--------------------------------//
	var interval, _parent, _viewmod
	var direct = _direct
	var start_time = 0
	var alpha = false
	var sc
	var cur_kind_vis = _kind + 'ship'
	var _ship = new createjs.Sprite(jsons["JSON1"])
	_ship.gotoAndStop(cur_kind_vis)
	this.mouseChildren = false
	// dragAndDrop(this)

	this.addChild(_ship)

	init()
// Получение ориентации
	this.getDirect = function(){
		return direct
	}

// Установка корабля в ячейку(i, j)
	this.setToCell = function(i, j){
		if(_kind == 1)
			setCoor(this, field_params.startX_1 * __koefDisplay + (field_params.dx * __koefDisplay * j) + (field_params.dx * __koefDisplay * i), field_params.startY_1 * __koefDisplay + (-field_params.dy * __koefDisplay * j) + (field_params.dy * __koefDisplay * i))
		if(_kind == 2)
			setCoor(this, field_params.startX_2 * __koefDisplay + (field_params.dx * __koefDisplay * j) + (field_params.dx * __koefDisplay * i), field_params.startY_2 * __koefDisplay + (-field_params.dy * __koefDisplay * j) + (field_params.dy * __koefDisplay * i))		
		if(_kind == 3)
			setCoor(this, field_params.startX_3 * __koefDisplay + (field_params.dx * __koefDisplay * j) + (field_params.dx * __koefDisplay * i), field_params.startY_3 * __koefDisplay + (-field_params.dy * __koefDisplay * j) + (field_params.dy * __koefDisplay * i))
	}
// Установка родителя и viewmodel
	this.setParent = function(parent, viewmod){
		_parent = parent
		_viewmod = viewmod
		sc = _viewmod.getData().scale_field
		startListener()
	}
// 
	this.changeDir = function(){
		changeDirection()
	}
//
	this.setAlpha = function(v){
		var cur = v ? cur_kind_vis : cur_kind_vis + "_alpha"
		alpha = v ? false : true
		_ship.gotoAndStop(cur)
	}
//
	this.setGraphics = function(v){
		this.removeAllChildren()
		cur_kind_vis = v
		direct = direct == "goriz" ? "vert" : "goriz"
		changeDirection()
		if(!alpha)
			_ship.gotoAndStop(cur_kind_vis)
		else
			_ship.gotoAndStop(cur_kind_vis + "_alpha")
		this.addChild(_ship)
	}

	var eventListener = false
	function startListener(){
		if(eventListener)
			return
		eventListener = true
// Изменение положения на поле
	dragAndDrop(th, function(e){
						if(start_time > 1){
								var cur_cell = {i: 0, j: 0}
								var min = distXY(e.x, e.y, field_params.startX_1 + (field_params.dx * 0) + (field_params.dx * 0), field_params.startY_1 + (-field_params.dy * 0) + (field_params.dy * 0))
								for(var a=0; a<_viewmod.getDimension(); a++)
									for(var b=0; b<_viewmod.getDimension(); b++){
										var m = distXY(e.x, e.y, field_params.startX_1 + (field_params.dx * b) + (field_params.dx * a), field_params.startY_1 + (-field_params.dy * b) + (field_params.dy * a))
										if(m < min){
											min = m
											cur_cell = {i: a, j: b}
										}
									}
								_viewmod.setToCell(_sh, cur_cell.i, cur_cell.j)
							}else{
								var rot = changeDirection()
								_viewmod.rotateShip(_sh, rot)
							}
							_parent.addDragAndDrop()
							},
					function(){
						_parent.removeDragAndDrop()
						t_delInTicker(interval)
						start_time = 0
						var ca = function(){
							start_time++
							interval = t_delayOn(.1, ca)
						}
						ca()
					}, null, null, sc)
	this.addEventListener("pressup", changeDirection)
	}

// Изменение ориентации (вертикальная/горизонтальная)
	function changeDirection(){
		if(direct == "goriz"){
			direct = "vert"
			_ship.scaleX = -1
			if(_kind == 1)
				setCoor(_ship, 73, 2)
			if(_kind == 2)
				setCoor(_ship, 147-51, 33-1)
			if(_kind == 3)
				setCoor(_ship, 178-26, 63-2)			// тройной корабль в вертикали
		}else{
			direct = "goriz"
			_ship.scaleX = 1
			setCoor(_ship, 0, 0)
		}
		return direct
	}
	changeDirection()

// Инициализация
	function init(){
		if(_kind == 1){
			th.regX = 14/__koefDisplay
			th.regY = 89/__koefDisplay
		}
		if(_kind == 2){
			th.regX = 34/__koefDisplay
			th.regY = 174/__koefDisplay
		}
		if(_kind == 3){
			th.regX = 32/__koefDisplay
			th.regY = 190/__koefDisplay
		}
	}
}