angular.module('kinderspiel.services', [])

.factory("backgroundsounds_mediahandler", function($ionicPlatform, $cordovaMedia) {
    
    /*
    * Checkvariable ob App im Hintergrund ist
    */
    var inbackground=false;

    /*
    * Soundfiles Variablen
    */    
    var backgroundmusic='';

    var backgroundmusicsoundfile='';

    $ionicPlatform.ready(function() {
      backgroundmusicsoundfile='/android_asset/www/sounds/background/background4.mid';
      var mybackground_music = new Media(backgroundmusicsoundfile, loopbackgroundmusic ,null);
      backgroundmusic=mybackground_music;
    });


    function loopbackgroundmusic(){
      if (inbackground == false){
        //backgroundmusic.release();
        //backgroundmusic=mybackground_music;
        backgroundmusic.play();
        backgroundmusic.setVolume('0.3');
      }
    }


    return {
        playbackgroundmusic: function() {
          if (inbackground == false){
            backgroundmusic.play();
            backgroundmusic.setVolume('0.3');
          }
        },
        setbackgroundsoundfile: function(soundfile) {
            $ionicPlatform.ready(function() {
                backgroundmusicsoundfile=soundfile;
                backgroundmusic.release();
                var mybackground_music = new Media(backgroundmusicsoundfile, loopbackgroundmusic ,null);
                backgroundmusic=mybackground_music;
                mybackground_music.release();
            });
        },
        stopbackgroundmusic: function() {
            backgroundmusic.stop();
        },
        setbackgroundstate: function(mybool) {
            inbackground=mybool;
        },
        getbackgroundstate: function() {
            return inbackground;
        }
    };
});
