'use strict';
var play;
var title;
var helper;
var bg;
var sponsors;
var definicja;
var definicja_audio;
var definicja_audio2;
var inDefinition;
var definition_text;
Game1.Menu = function() {};
 
  Game1.Menu.prototype = {
   
    create: function() {
        
    inDefinition = 0;    
    definicja_audio = new Audio('./sound/OB_g_I_1_1.mp3');
    definicja_audio2 = new Audio('./sound/OB_g_I_1_2.mp3');     
    this.strona = new Audio("./sound/page.mp3");
        
    this.strona.load();     
      var style = { font: '70px Arial', fill: '#0080ca', align: 'center'};
      bg = this.game.add.sprite(0, 0, 'bg');
        this.boo = this.game.add.sprite(0,0,"book");
        //bg.scale.setTo(1/2,1/2);
        sponsors = this.game.add.sprite(0,458*2,'sponsors');
       // sponsors.scale.setTo(1/2,1/2);
      //this.sprite.anchor.setTo(0.5, 0.5);
    //this.sprite.scale.setTo(0.9,0.9);
   definicja = this.game.add.sprite(1154,727, 'definicja');
            definicja.inputEnabled =true;
        definicja.name = "definicjaButton";
        definicja.events.onInputOver.add(this.over,this);
        definicja.events.onInputDown.add(this.down, this);
        definicja.events.onInputOut.add(this.out,this);    
   title = this.game.add.sprite(422, 158, 'title');
        //title.scale.setTo(1/2,1/2);
        helper = this.game.add.sprite(1366, 347, 'pomocnik');
       // helper.scale.setTo(1/2,1/2);
    //this.sprite.anchor.setTo(0.5, 0.5);
            
    //var button = new Phaser.Button(this.game,1302,516,'play_button', this.buttonCallback, this, 1,0,2);
      play = this.game.add.sprite(488,630,'play_button');
        play.inputEnabled =true;
        play.name = "play";
        play.events.onInputOver.add(this.over,this);
        play.events.onInputDown.add(this.down, this);
        play.events.onInputOut.add(this.out,this);
        // play = this.game.add.button(446/2,686/2,'play_button', this.actionOnClick, this,'','','');
        //play.scale.setTo(1/2,1/2);
        // play.onInputOver.add(1, this);
          //  play.onInputOut.add(2,this);
        
    //play =  this.game.add.sprite(446, 686, 'play_button');
    play.inputEnabled = true;
    play.animations.add('off',[0], true);
    play.animations.add('on',[1], true);
        
      
    //this.sprite.anchor.setTo(0.5, 0.5);
    
        //this.sprite = this.game.add.sprite(1302, 516, 'play_select');
    //this.sprite.anchor.setTo(0.5, 0.5);   
        
    //this.titleText = this.game.add.text(this.game.world.centerX, 400, 'Odkrywca świata', style);
    //this.titleText.anchor.setTo(0.5, 0.5);

    //this.instructionsText = this.add.text(this.game.world.centerX+200, 450                                            , 'tytul gry', { font: '40px Arial', fill: '#0080ca', align: 'right'});
    //this.instructionsText.anchor.setTo(0.5, 0.5);

      //this.sprite.angle = -20;
      //this.game.add.tween(this.sprite).to({angle: 20}, 1000, Phaser.Easing.Linear.NONE, true, 0, 1000, true);
    },
      
    showDefinition: function () {
        inDefinition = 1;
        play.inputEnabled = false;
        definicja.inputEnabled = false;
        definicja_audio.play();
       //this.StopInput();
        this.guiHelp = this.game.add.sprite(1210, 350, 'guiPomocnik_good');
        //this.guiHelp.scale.setTo(0.8,0.8);
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(95, 96, 'Finish');
        this.guiBubble.scale.setTo(1.3,1.3); 
        this.guiBubble.fixedToCamera = true;
       definition_text = this.game.add.text(200, 330, 'Produkty pierwszej potrzeby to te artykuły,\nktóre są niezbędne do zaspokojenia najważniejszych\npotrzeb człowieka, są konieczne w codziennym życiu.\nPrzykładem takich produktów są podstawowe\nartykuły spożywcze (jedzenie, woda)\noraz podstawowe środki higieny (jak mydło).', {
            font: "36px Arial",
            fill: "#000000",
        });
        definition_text.fixedToCamera = true;
       
    },

    hideDefinition: function () {
       inDefinition = false;
        play.inputEnabled = true;
        definicja.inputEnabled = true;
      //this.ReStartInput();
        this.guiHelp.visible = false;
        this.guiBubble.visible = false;
        definition_text.visible = false;
        //this.lektor.hide();
    },  
    //button.onInputOver.add(over, this);
   // button.onInputOut.add(out, this);  

onInputOver: function(sprite, pointer){
    
},
down: function(sprite, pointer) {
    if (sprite.name == 'play'){
    this.strona.play();
    this.game.state.start('Game');
    }
    else
    {
      this.showDefinition();  
    }
    ////console.log('button down: '+sprite.name);
},      
over: function(sprite, pointer) {
    play.animations.play('on',1,true,false);
    //console.log('button over'+sprite.name);
    if (!this.game.device.desktop)
        this.game.state.start('Game');
},

out: function(sprite, pointer) {
     play.animations.play('off',1,true,false);
    //console.log('button out'+sprite.name);
},
      update: function() {
          if ((!isPlaying(definicja_audio) || definicja_audio.ended) && inDefinition == 1) {
                definicja_audio2.play();
                definition_text.setText('Produkty potrzeb dalszych to produkty,\nktóre zaspokajają dodatkowe potrzeby człowieka,\nsprawiają przyjemność, ale nie są nam niezbędne do życia.\nPrzykładem mogą być słodycze, ozdoby,\nzabawki, nietypowe produkty spożywcze, książki,\ntelewizor, gry, komputer, itp.');
               // this.hideInstruction();
                inDefinition = 2;
            }
            if ((!isPlaying(definicja_audio2) || definicja_audio2.ended) && inDefinition == 2) {
      //inDefinition = false;          
      //definicja_audio2.play();
                this.hideDefinition();
               // isReadyToRead = true;
            }

     // this.game.debug.text('this.camera.width: '+ this.camera.width+' this.camera.height: '+this.camera.height+ ' bg.width: '+bg.width,  20, 20);
         // if (play.input.pointerDown()){
        //   this.game.state.start('Game');
      // }
         /* if (play.input.pointerOver()){
          
              play.animations.play('on',1,true,false);
          }*/
 /*             else if  (!play.input.pointerOver()){
           play.animations.play('off',1,true,false);
                     }*/
 //  if (play.pointerDown()){actionOnClick()}
    },

actionOnClick: function() {
 this.game.state.start('Game');

}
  };