extend(VisualInterface, createjs.Container);

function VisualInterface(viewmod)
{	
	this.initialize();
	var th = this
//--------------------------------//
	var _top, _bottom, _tutor, _modal, timer
	var t = false
	var dy = 0
	var max_dSize = (__HEIGHTBG - HEIGHTGAME)/2
	var queue = new Array()
	var cont = new createjs.Container()

	var pod_money = new createjs.Sprite(jsons["JSON1"])
	pod_money.gotoAndStop("area_money")
	setCoor(pod_money, -108, 115)
	var money = new Button({pod:"btn_money", i:"icon_money", iconX: -1, iconY: 1, txt: "0", font: "numbers_green_small_", txtX:99, txtY: 42, refTxt: "Left"})
	setCoor(money, 2-4, 104-20)
	money.addEventListener("click", function(){viewmod.clickOnMoney()})

	_top = new TopInterface(viewmod, pod_money, money)
	_bottom = new BottomInterface(viewmod)
	var frame = new FloatFrame()


	cont.addChild(pod_money)
	cont.addChild(money)

	this.addChild(cont)
	this.addChild(frame)
	this.addChild(_top)
	this.addChild(_bottom)

	viewmod.registerObservers(th)
	listenResize(this)


	this.startTutorial = function(v, d){
		t = true
		if(!_tutor)
			_tutor = new Tutorial()
		_tutor.start(v, dy, d)
		this.addChild(_tutor)
	}

	this.nextHint = function(s){
		var v = _tutor.nextHint()
		if(!v)
			viewmod.setTutorial(false)
		if(v == 1 && s == 1)
			viewmod.clickOnShip(2)
	}

	this.updateIface = function(){
		// console.log(viewmod.getData().modal, _modal)
		// if(_modal)
		// 	console.log("text ", _modal.getText())
		if(viewmod.getData().modal){
			if(_modal && _modal.getText() == viewmod.getData().modal)
				return
			// if(indexOf(queue, viewmod.getData().modal))
			// 	queue.push(viewmod.getData().modal)
			if(_modal && _modal.parent){
				t_delInTicker(timer)
				_modal.removeTweens()
				_modal.parent.removeChild(_modal)
				_modal = null
				// return
			}
			_modal = new Modal(viewmod.getData().modal)
			
			this.addChild(cont)
			this.addChild(_modal)
			this.addChild(frame)
			this.addChild(_top)
			if(t)
				this.addChild(_tutor)
			_modal.y = 0 - dy
			_modal.slideFromTop(function(){
				timer = t_delayOn(2, function(){
						_modal.slideToTop(function(){
						_modal.removeTweens()
						_modal.parent.removeChild(_modal)
						_modal = null	
						viewmod.modalClose()
					})
				})
			})
			// disableEvents()
		}else{
			if(_modal && _modal.parent)
				_modal.parent.removeChild(_modal)
			if(viewmod.getData().only_ready){
				disableEvents()
				enableOnlyReady()
			}else
				enableEvents()
		}
	}

	this.resize = function(w, h, game_w, game_h, rel){
		var rel = (game_h/__HEIGHTBG) * 100
		var full_h = h * 100/rel
		dy = (full_h - HEIGHTGAME)/2
		if(dy > max_dSize)
			dy = max_dSize
		_top.y = 0 - dy
		cont.y = 0 - dy
		_bottom.y = 0 + dy
		if(_modal)
			_modal.y = 0 - dy
		if(_tutor){
			var p = viewmod.getData()
			this.startTutorial(p.cur_step, true)
		}
	}

	function perfQueue(){
		
	}

	function enableEvents(){
		_top.enableAll()
		_top.mouseEnable = true
		_bottom.mouseEnable = true
		_top.mouseChildren = true
		_bottom.mouseChildren = true
	}

	function disableEvents(){
		_top.mouseEnable = false
		_bottom.mouseEnable = false
		_top.mouseChildren = false
		_bottom.mouseChildren = false
	}

	function enableOnlyReady(){
		_top.mouseEnable = true
		_top.mouseChildren = true
		_top.onlyreadyEnable()
	}

	ResizeGame()
};