
/**
 * Makes all players in the disabled mode.
 *
 */
function disableAllPlayers(showWarning){
    if (showWarning){
        alert("Your browser doesn\'t fully support the audio-tag, which is a part of html5. " +
            "\nMaybe there is an update for your browser, otherwise you can change to one that fully supports html5." +
            "\nHowever you can still download our songs.");
    } 
    var elements = document.getElementsByName("songplayer");
    elements.item(i);
    var i;
    var el = null;
    for (i = 0; i < elements.length; i++){
        el = elements.item(i);
        el.className = "songplayer_disabled";
        debug("changing class on songplayer: " + el.id);
    }
    //ie doesnt always return the correct list of elements from
    //document.getElementsByName method so we have to iterate of the potential
    //ids to disable players.
    //also ie tends to call the <script> tag content from internal of audio tag
    //before the rest of the page is loaded, so we should wait a small amount
    //before changing the classes.
    //
    if (elements.length == 0){
        setTimeout(function (){
            for (i = 1; ; i++){
                el = document.getElementById("play." + i);
                if (!el){
                    break;
                }
                el.className = "songplayer_disabled";
                debug("changing class on songplayer: " + el.id);
            }
        }, 100);
    }
}

function checkPlaySupport(){
    //it seems like we are not able to just add some script inside the audio tag
    //since alot of browsers tend to run that script even if the support exists.
    //so we do a little trick...
    setTimeout(function(){
        if (!document.getElementById("audioPlayer").play){
            disableAllPlayers(false);
        }
    }, 100);

}


function resetOtherPlayers(than){
    var elements = document.getElementsByName("songplayer");

    elements.item(i);
    var i;
    var el = null;
    for (i = 0; i < elements.length; i++){
        el = elements.item(i);
        if (el.id != than){
            el.className = "songplayer";
        }
    }
}

/**
 * Plays the song at the given url, if the url ends with mp3 it is changed to .ogg
 * @param songurl the song to play.
 * @param firedBy the html-element that started the song.
 */
function play(songurl, firedBy){
    var audioElement = document.getElementById("audioPlayer");
    audioElement.setAttribute("src", songurl);
    info("src on " + audioElement + " is now: " + audioElement.src);

    var className = "";
    var firedByElement = document.getElementById(firedBy);

    if (firedByElement == null){
        warn("no element found by the id: " + firedBy + " will return.");
    } else{
        className = firedByElement.className;
    }

    if (className == "songplayer_disabled"){
        info("trying to play without html5 support.");
        return;
    }

    if (!audioElement.load){
        disableAllPlayers(true);
        return;
    }
    if (className == "songplayer_pause"){
        audioElement.pause();
        className = "songplayer";
    } else{
        audioElement.addEventListener("canplay", function(){
            audioElement.play();
        }, true);
        audioElement.load();
        className = "songplayer_pause";
        resetOtherPlayers(firedBy);
    }
    
    firedByElement.className = className;
     
}


