extend(TopInterface, createjs.Container);

function TopInterface(viewmod, pod_money, money)
{	
	this.initialize();
	var th = this
//--------------------------------//
	var cur_time_part = 0
	var timer = false;
	var f_timer;

	var s = new createjs.Sprite(jsons["JSON1"])
	s.gotoAndStop("interface")

	var life = new createjs.Sprite(jsons["JSON1"])
	life.gotoAndStop("icon_life")
	setCoor(life, 36, 3)

	var life_num = new Text(jsons["JSON2"], "numbers_green_small_", "Center", 15)
	setCoor(life_num, 106+14, 64-7)
	// dragAndDrop(life_num)

	var random = new createjs.Sprite(jsons["JSON1"])
	random.gotoAndStop("btn_random")
	setCoor(random, 38, -1)
	random.addEventListener("click", function(){viewmod.randomClick()})

	var ps = new createjs.Shape()
	ps.graphics.beginFill("#000000").drawRect(0, 0, 100/__koefDisplay, 100/__koefDisplay)


	var pause = new createjs.Sprite(jsons["JSON1"])
	pause.gotoAndStop("btn_pause")
	setCoor(pause, 136, 4)
	pause.hitArea = ps
	pause.addEventListener("click", function(){viewmod.pauseClick()})

	var ship = new createjs.Sprite(jsons["JSON1"])
	setCoor(ship, 426, -3)
	ship.addEventListener("click", function(){viewmod.shipClick()})

	var ready = new createjs.Sprite(jsons["JSON1"])
	ready.gotoAndStop("btn_ok_on")
	setCoor(ready, 516, 4)

	var ready_sh = new createjs.Shape()
	ready_sh.graphics.beginFill("#000000").drawRect(0,0, 66/__koefDisplay, 56/__koefDisplay)
	setCoor(ready_sh, 530, 27)
	ready_sh.alpha = .01
	ready_sh.addEventListener("click", function(){viewmod.readyClick()})

	

	// dragAndDrop(pod_money)


	var loupe = new Button({pod:"btn_loop", i:"icon_loop_minus", i2:"icon_loop_plus", iconX: -2, iconY: 1})//, podScale: .7
	// var curIcon = 1
	// money.scaleX = money.scaleY = .7
	setCoor(loupe, 539-3, 104-20)
	loupe.addEventListener("click", function(){
		viewmod.clickOnLoupe()
	})

	var time = new Timer()
	setCoor(time, 261, 5)

	// dragAndDrop(time)
	// dragAndDrop(random)
	// dragAndDrop(pause)
	// dragAndDrop(ship)
	// dragAndDrop(ready)
	// dragAndDrop(money)
	// dragAndDrop(loupe)
	// dragAndDrop(life)
	// dragAndDrop(pod_money)
	ship.gotoAndStop("btn_transperent_off")
	th.addChild(life)
			th.addChild(random)
			th.addChild(pause)
			th.addChild(ship)
			th.addChild(ready)
			th.addChild(time)
			th.addChild(life_num)
			th.addChild(ready_sh)

	viewmod.registerObservers(th)

	setLvlTimer(0)
	
	this.onlyreadyEnable = function(){
		for(var a = 0; a< this.getNumChildren(); a++)
			this.getChildAt(a).mouseEnabled = false
		ready_sh.mouseEnabled = true
		pause.mouseEnabled = true
		// readyActive()
	}

	this.enableAll = function(){
		for(var a = 0; a< this.getNumChildren(); a++)
			this.getChildAt(a).mouseEnabled = true
	}

// 	Обновление
	this.updateTopIface = function(){
		var step = viewmod.getData()
		setStep(step.cur_step)
	}

	this.setLife = function(lif){		
		var l = lif ? lif : 0
		life_num.setText(l)
	}

	this.setVisibleRandom = function(v){
		random.visible = v		
	}

	this.setStep = function(v){
		setStep(v)
	}
// Private //
	function setStep(v){
		th.removeAllChildren()
		var p = viewmod.getData()
		if(v == 1 || v == 2 || v == 3) {
			// th.addChild(s)
			th.addChild(life)
			th.addChild(random)
			th.addChild(pause)
			th.addChild(ship)
			th.addChild(ready)
			th.addChild(time)
			th.addChild(life_num)
			th.addChild(ready_sh)
			th.addChild(loupe)
			life_num.visible = false
			life.visible = false
			random.visible = true
			pause.visible = true
			ship.visible = true
			ready.visible = true
			ready_sh.visible = true
			money.visible = true
			pod_money.visible = true
			if(p.cur_ship == "transp")
				ship.gotoAndStop("btn_transperent_on")
			if(p.cur_ship == "real")
				ship.gotoAndStop("btn_transperent_off")
			if(p.ready){
				ready_sh.mouseEnabled = true
				ready.gotoAndStop("btn_ok_on")
				if(!timer)
					readyActive()
			}else{
				ready_sh.mouseEnabled = false
				ready.gotoAndStop("btn_ok_off")
			}
			// console.log(p.user_money)
			money.setText(p.user_money)
			pod_money.x = -133/__koefDisplay + p.user_money.toString().length*20/__koefDisplay
			life_num.setText(p.life_user)

			if(p.loupe_active)
				loupe.setIcon(2)
			else
				loupe.setIcon(1)
		}
		if(v == 1){
			money.visible = false
			pod_money.visible = false
		}
		if(v == 2){
			random.visible = false
			life.visible = true
			life_num.visible = true
			if(p.ready){
				ready_sh.mouseEnabled = true
				ready.gotoAndStop("btn_ok_on")
				if(!timer)
					readyActive()
			}else{
				ready_sh.mouseEnabled = false
				ready.gotoAndStop("btn_ok_off")
			}
		}
		if(v == 3){
			life_num.visible = true
			life.visible = true
			random.visible = false
		}
		setLvlTimer(p.time)
	}

	function setLvlTimer(v){
		// console.log(v)
		if(v == -1)
			v = 0
		cur_time_part = v
		time.setTime(v)
	}

	function readyActive(){
		t_delInTicker(f_timer)
		timer = true
		ready.gotoAndStop("btn_ok_on")

		var t = 0
		var f = function(){
			if(t>6 || !ready_sh.mouseEnabled){
				timer = false
				ready.visible = true
				return
			}
			t++
			ready.visible = ready.visible == true ? false : true
			f_timer = t_delayOn(.2, f)
		}
		f()

	}
};