EasyPlayer = function()
{
};

EasyPlayer.prototype.lastId = 0;
EasyPlayer.prototype.isPlaying = null;

EasyPlayer.prototype.init = function()
{
    var as = document.getElementsByTagName("a");
    for (var i = 0; i < as.length; ++i)
    {
        var a = as[i];
        if (a.href.substr(a.href.lastIndexOf(".") + 1) == "mp3")
        {
            this.wrapLink(a);
        }
    }
};

EasyPlayer.prototype.wrapLink = function(anchor)
{
    var that = this;
    var id = "easySound_" + (++this.lastId);
    soundManager.createSound(id, anchor.href);
    var div = document.createElement("div");
    div.className = "easySound";
    div.soundId = id;
    var button = document.createElement("div");
    button.className = "easyButton";
    div.appendChild(button);
    var text = document.createElement("div");
    text.className = "easyText";
    text.appendChild(document.createTextNode(anchor.innerHTML));
    div.appendChild(text);
    anchor.parentNode.insertBefore(div, anchor);
    div.appendChild(anchor);
    anchor.style.display = "none";
    div.onclick = function(e)
    {
        if (that.isPlaying == this)
        {
            soundManager.stop(that.isPlaying.soundId);
            that.isPlaying.className = "easySound";
            that.isPlaying = null;
        }
        else
        {
            if (that.isPlaying)
            {
                soundManager.stop(that.isPlaying.soundId);
                that.isPlaying.className = "easySound";
            }
            that.isPlaying = this;
            that.isPlaying.className = "easySoundActive";
            soundManager.play(that.isPlaying.soundId, {
                onfinish: function()
                {
                    that.isPlaying.className = "easySound";
                }
            });
        }
    };
};

var easyPlayer = new EasyPlayer();

soundManager.debugMode = false;
soundManager.consoleOnly = true;
soundManager.flashVersion = 8;
soundManager.useHighPerformance = true;
soundManager.useFlashBlock = true;
soundManager.onready(function()
{
    easyPlayer.init();
});

