var backgroundMusic;
var musicFile = 'img/theodyssey.mp3';

/* Starts the background music */
function startMusic () {
    backgroundMusic.play({
        volume: 50,
        onfinish: function () {
            this.play( {volume: 50} );
        }
    });
}

/* Stops the background music */
function stopMusic () {
    backgroundMusic.stop();
}

/* Toggles background music */
function toggleMusic () {
    if ( backgroundMusic.playState == 0 ) {
        startMusic();
        createCookie( 'musicOn', 'true', 100 );
    } else {
        stopMusic();
        createCookie( 'musicOn', 'false', 100 );
    }

    return backgroundMusic.playState;
}

/* Background music initialisation */
soundManager.url = 'img';
soundManager.waitForWindowLoad = true;
soundManager.onload = function () {
    backgroundMusic = soundManager.createSound({
        id: 'backgroundMusic',
        url: musicFile
    });
    if ( readCookie( 'musicOn' ) == '' ) {
        startMusic();
        createCookie( 'musicOn', 'true', 100 );
    } else if ( readCookie( 'musicOn' ) == 'true' ) {
        startMusic();
    }
}
