extend(VisualGame, createjs.Container);

function VisualGame(viewmod, dim)
{	
	this.initialize();
	var th = this
//--------------------------------//
	var startX, startY, delay, timer, pause, mainEvent

	var _field = new Field(this, viewmod, dim)

	var minX
	var maxX
	var minY
	var maxY

	var sh = new createjs.Shape()
	sh.graphics.beginFill("#000000").drawRect(0, 0, WIDTHGAME, HEIGHTGAME)
	sh.alpha = .01

	this.addChild(sh) 
	this.addChild(_field) 
	viewmod.registerObservers(this)
	// tapEvent()

	this.updateField = function(){
		var p = viewmod.getData()
		// console.log(p.game_data.current_stage, p.cur_step)
		if(p.cur_step == 3 && p.game_data.current_stage == "bot")
			tapEvent()
		else
			th.removeAllEventListeners()

		var sc = p.scale_field
		if(dim == 16){
			minX = sc == 1 ? -700 : -40
			maxX = sc == 1 ? 38 : 70
			minY = sc == 1 ? -402 : 88
			maxY = sc == 1 ? 136 : 170
		}
		if(dim == 12){
			minX = sc == 1 ? -405 : -2
			maxX = sc == 1 ? 38 : 175
			minY = sc == 1 ? -315 : 38
			maxY = sc == 1 ? 36 : 180
		}
		if(dim == 10){
			minX = sc == 1 ? -227 : 17
			maxX = sc == 1 ? 38 : 292
			minY = sc == 1 ? -238 : 12
			maxY = sc == 1 ? -13 : 182
		}
		// if(viewmod.getData().modal)
		// 	disableEvents()
		// else
		 if(!viewmod.getData().only_ready)
			enableEvents()
		else
			disableEvents()
	}

	this.removeEvents = function(dont_change_flag){
		if(!dont_change_flag)
			mainEvent = false
	
		_field.removeAllEventListeners()
	}

	this.addEvents = function(dont_change_flag){
		if(!dont_change_flag)
			mainEvent = true
	
		_field.addEventListener("mousedown", function(e){
			startX = e.stageX
			startY = e.stageY
		})

		_field.addEventListener("pressmove", function(e){
			var p = viewmod.getData()
			var sc = 1// p.scale_field
			tw_removeTween(_field)

			if(_field.x + (e.stageX - startX)/sc < maxX/__koefDisplay && _field.x + (e.stageX - startX)/sc > minX/__koefDisplay){
				_field.x += (e.stageX - startX)/sc
				startX = e.stageX
			}
			if(_field.y + (e.stageY - startY)/sc < maxY/__koefDisplay && _field.y + (e.stageY - startY)/sc > minY/__koefDisplay){
				_field.y += (e.stageY - startY)/sc
				startY = e.stageY
			}

			// console.log(_field.x, _field.y)
		})
	}
	
	this.addEvents()


	function enableEvents(){
		th.mouseEnable = true
		th.mouseChildren = true
	}

	function disableEvents(){		
		th.mouseEnable = false
		th.mouseChildren = false
	}

	function tapEvent(){
		th.removeAllEventListeners()
		th.addEventListener("mousedown", function(){
			t_delInTicker(timer)
			delay = -1
			var fun = function(){
				var p = viewmod.getData()
				delay++
				if(delay >= 1){
					if(pause){
						t_delAllInPause()
						if(!mainEvent)
							th.removeEvents()
						pause = false
						console.log("unPause")
					}else if(p.cur_step == 3){
						t_allInPause()
						th.addEvents(true)
						pause = true
						console.log("pause")
					}
				}else
					timer = t_delayOn(1, fun)
			}
			fun()
		})

		th.addEventListener("pressup", function(){
			t_delInTicker(timer)
		})		
	}
};