// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('kinderspiel', ['ionic', 'ngCordova','ngAnimate', 'ngDraggable', 'kinderspiel.controllers', 'kinderspiel.services'])

.run(function($ionicPlatform, $state, backgroundsounds_mediahandler) {

  $ionicPlatform.ready(function() {
    
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }

    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
    
    /*
    * Bildschirm in Landscape modus drehen und halten
    */
    screen.lockOrientation('landscape');
    
    /*
    * Bildschirm aktiv halten einschalten
    */
    window.plugins.insomnia.keepAwake();
    /*
    * Bildschirm aktiv halten ausschalten
    */
    //window.plugins.insomnia.allowSleepAgain();
    
    /*
    * Homebutton und Backgroundinfos abrufen
    */
    document.addEventListener("resume", onResume, false);
    function onResume() {
      setTimeout(function() {
            backgroundsounds_mediahandler.setbackgroundstate(false);
            backgroundsounds_mediahandler.playbackgroundmusic();
            //console.log("The application is resuming from the background" + backgroundsounds_mediahandler.getbackgroundstate());
      }, 0);
    }

    document.addEventListener("pause", onPause, false);
    function onPause() {
      setTimeout(function() {
            backgroundsounds_mediahandler.setbackgroundstate(true);
            backgroundsounds_mediahandler.stopbackgroundmusic();
            //console.log("The application is going to the background" + backgroundsounds_mediahandler.getbackgroundstate());
      }, 0);
    }

    /*
    * Backbutton Handling
    */
    $ionicPlatform.registerBackButtonAction(function (event) {
      /*
      * Startseite
      */
      if($state.current.name=="home"){
        backgroundsounds_mediahandler.stopbackgroundmusic();
        /*
        * App-Beenden
        */
        navigator.app.exitApp(); 
      }
      else {
        
        navigator.app.backHistory();
      }
    }, 100);


  });
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  /*
  * Routen setzen
  */
  .state('home', {
    url: '/home',
    abstract: false,
    templateUrl: 'templates/home.html',
    controller: 'home'
  })
  .state('suchspiel', {
    url: '/suchspiel',
    templateUrl: 'templates/suchspiel.html',
    controller: 'Suchspiel'
  })
  .state('minispiel1', {
    url: '/minispiel1',
    templateUrl: 'templates/minispiel1.html',
    controller: 'Minispiel1'
  })

  /*
  * Standard-Route setzen
  */
  $urlRouterProvider.otherwise('/home');

});