var music = {};
function initMusic() {
    $('body').append('<audio class="name" id="bgm_recharge" src="/audio/draw.mp3" preload="metadata"  ></audio>');
    $('body').append('<audio class="name" id="bgm_agent" src="/audio/agent.mp3" preload="metadata"  ></audio>');
    $('body').append('<audio class="name" id="bgm_take" src="/audio/save.mp3" preload="metadata"  ></audio>');

    music.recharge = document.getElementById("bgm_recharge");
    music.agent = document.getElementById("bgm_agent");
    music.take = document.getElementById("bgm_take");
    music.currentMusic = '';
    music.nextMusic='';
    music.numCount={};
    music.player = music.recharge;
    music.player.onended = function () {
        stopPlay();
    }
}
function startPlay(){
    try{
        music.currentMusic=music.nextMusic;
        music.nextMusic=null;
        if(music.currentMusic!=undefined && music.currentMusic!=null){
            music.player = music.currentMusic;
            music.player.play();
            music.player.onended=function(){
                stopPlay();
            }
        }else{
            music.player.pause();
        }       
       
    }catch(ex){
        console.log(ex);
    }   
}
function stopPlay(){
    startPlay();
}
function playMusic(name){
    try{
        var current = music.currentMusic;
        music.nextMusic = music[name];
        if (current == undefined || current == null || current == '') {
            startPlay();
        }
    }catch(ex){}
        
}


$(document).ready(function(){
    initMusic();
});
