/*******************

Beggars Audio Player
Version 1.1
Created by David Emery

*******************/

var next_timer = 0;
var should_loop = 0;
var retry_count = 0;
var current_track_name = "";
var current_track_url = "";

function play(track_name, track_url) {
    
    play_span = document.getElementById(track_name);
    
    if( play_span.getAttribute("class") == "stop_button" || play_span.getAttribute("className") == "stop_button")    {        play_span.setAttribute("class","");
        play_span.setAttribute("className","");
        player_id = track_name + "_player";
        player_instance = document.getElementById(player_id);
        if(isReal(track_url))
        {
            player_instance.DoStop();
        }
        else
        {
            player_instance.Stop();
        }
        play_span.innerHTML="";
        play_span.innerHTML="<a href=\"javascript:play(\'"+ track_name + "\', \'" + track_url + "\')\" class=\"play_button\">Play</a>";
        next_timer = 0;
        set_nothing_playing();    }    else    {
        next_timer = 0;
        var all_players = document.getElementsByTagName('embed');
        for(var i = 0; i < all_players.length; i++)
        {
            if(isReal(all_players.item(i).getAttribute('src')))
            {
                if(all_players.item(i).CanStop())
                {
                    all_players.item(i).DoStop();
                }
            }
            else
            {
                all_players.item(i).Stop();
            }
            new_play_span = all_players.item(i).parentNode;
            new_play_span.setAttribute("class","");
            new_play_span.setAttribute("className","");
            new_href = new_play_span.getElementsByTagName('a').item(0).getAttribute('href');
            new_play_span.innerHTML="<a href=\"" + new_href + "\" class=\"play_button\">Play</a>";
        }
                play_span.setAttribute("class","stop_button");
        play_span.setAttribute("className","stop_button");
        play_span.innerHTML="";
        if(isReal(track_url))
        {
            play_span.innerHTML="<a href=\"javascript:play(\'"+ track_name + "\', \'" + track_url + "\')\" class=\"play_button\">Stop</a><embed src='" + track_url + "' id='" + track_name + "_player\' hidden=true autostart=true enablejavascript=true type='audio/x-pn-realaudio-plugin'>";
        }
        else
        {
            play_span.innerHTML="<a href=\"javascript:play(\'"+ track_name + "\', \'" + track_url + "\')\" class=\"play_button\">Stop</a><embed src='" + track_url + "' id='" + track_name + "_player\' hidden=true autostart=true enablejavascript=true>";
        }

        
        set_now_playing(track_name, track_url);
        
        player_id = track_name + "_player";
        player_instance = document.getElementById(player_id);

        end_time = 0;
        
        if(isReal(track_url))
        {
            try {
                end_time = player_instance.GetLength();
            }
            catch(err) {
                try {
                    end_time = player_instance.GetDuration();
                }
                catch(err) {
                    end_time = 0;
                }
                
            }
        }
        else
        {
            try {
                end_time = player_instance.GetDuration();
            }
            catch(err) {
                try {
                    end_time = player_instance.GetLength();
                }
                catch(err) {
                    end_time = 0;
                }
            }
        }

        if(end_time == 0)
        {
            retry_count = 0;
            next_timer = window.setTimeout('retry_length()', 2000);
        }
        else
        {
            next_timer = window.setTimeout('next_song()', end_time);
        }    }
    
}

function isReal(track_url)
{
    if(track_url.indexOf('.rm') != -1)
    {
        return true;
    }
    else if(track_url.indexOf('.ram') != -1)
    {
        return true;
    }
    else if(track_url.indexOf('.rpm') != -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function retry_length()
{
    var track_url = "";
    var player_instance = "";
    var all_players = document.getElementsByTagName('embed');
    for(var i = 0; i < all_players.length; i++)
    {
        if(all_players.item(i).parentNode.getAttribute("class") == "stop_button" || all_players.item(i).parentNode.getAttribute("className") == "stop_button")
        {
            track_url = all_players.item(i).getAttribute('src');
            player_instance = all_players.item(i);
        }
    }
    
    if(track_url != "")
    {
        end_time = 0;
        
        if(isReal(track_url))
        {
            try {
                end_time = player_instance.GetLength();
            }
            catch(err) {
                try {
                    end_time = player_instance.GetDuration();
                }
                catch(err) {
                }
                
            }
        }
        else
        {
            try {
                end_time = player_instance.GetDuration();
            }
            catch(err) {
                try {
                    end_time = player_instance.GetLength();
                }
                catch(err) {
                }
            }
        }
        

        if(end_time == 0)
        {
            
            if(retry_count > 50)
            {
                next_timer = window.setTimeout('next_song()', 5000);
            }
            else
            {
                retry_count += 1;
                next_timer = window.setTimeout('retry_length()', 1000);
            }
        }
        else
        {
            next_timer = window.setTimeout('next_song()', end_time);
        }
    }
}

function next_song()
{
    next_timer = 0;
    var track_url = "";
    var player_instance = "";
    var all_players = document.getElementsByTagName('embed');
    for(var i = 0; i < all_players.length; i++)
    {
        if(all_players.item(i).parentNode.getAttribute("class") == "stop_button" || all_players.item(i).parentNode.getAttribute("className") == "stop_button")
        {
            track_url = all_players.item(i).getAttribute('src');
            player_instance = all_players.item(i);
        }
    }
    
    if(track_url != "")
    {
        if(isReal(track_url))
        {
            if(player_instance.GetPlayState() == 0)
            {
                
                play_next_song(player_instance);
            }
            else
            {
                // If it's still playing, try again in 2secs
                next_timer = window.setTimeout('next_song()', 2000);
            }
        }
        else
        {
            if(player_instance.GetTime() >= player_instance.GetDuration())
            {
                play_next_song(player_instance);
            }
            else
            {
                // If it's still playing, try again in 2secs
                next_timer = window.setTimeout('next_song()', 2000);
            }
        }
    }
}

function play_next_song(player_instance)
{
    next_timer = 0;
    var found_values = new Array();
            
            all_list = player_instance.parentNode.parentNode.parentNode.getElementsByTagName('li');
            for(var k = 0; k < all_list.length; k++)
            {
                if(all_list.item(k) == player_instance.parentNode.parentNode)
                {
                    next_song_li = "";
                    if((k+1) == all_list.length)
                    {
                        if(should_loop == 1)
                        {
                            //loop back to start
                            next_song_li = all_list.item(0);
                        }
                        else
                        {
                            play(player_instance.parentNode.getAttribute('id'), player_instance.getAttribute('src'));
                            return;
                        }
                    }
                    else
                    {
                        next_song_li = all_list.item(k+1);
                    }
                    
                    all_li_a = next_song_li.getElementsByTagName('a');
                    for(var j = 0; j < all_li_a.length; j++)
                    {
                        if(all_li_a.item(j).getAttribute('class') == "play_button" || all_li_a.item(j).getAttribute('className') == "play_button")
                        {
                            //we've found the next song
                            link_href = all_li_a.item(j).getAttribute('href');
                            
                            found_values = link_href.split('\'');
                            
                        }
                    }
                }
            }
            
            // Stop the last track
            //play(player_instance.parentNode.getAttribute('id'), player_instance.getAttribute('src'));
            //play the next one
            play(found_values[1], found_values[3]);
}

function play_previous_song(player_instance)
{
    next_timer = 0;
    var found_values = new Array();
            
            all_list = player_instance.parentNode.parentNode.parentNode.getElementsByTagName('li');
            for(var k = 0; k < all_list.length; k++)
            {
                if(all_list.item(k) == player_instance.parentNode.parentNode)
                {
                    next_song_li = "";
                    if((k-1) < 0)
                    {
                        if(should_loop == 1)
                        {
                            //loop back to start
                            next_song_li = all_list.item((all_list.length -1));
                        }
                        else
                        {
                            play(player_instance.parentNode.getAttribute('id'), player_instance.getAttribute('src'));
                            return;
                        }
                    }
                    else
                    {
                        next_song_li = all_list.item(k-1);
                    }
                    
                    all_li_a = next_song_li.getElementsByTagName('a');
                    for(var j = 0; j < all_li_a.length; j++)
                    {
                        if(all_li_a.item(j).getAttribute('class') == "play_button" || all_li_a.item(j).getAttribute('className') == "play_button")
                        {
                            //we've found the next song
                            link_href = all_li_a.item(j).getAttribute('href');
                            
                            found_values = link_href.split('\'');
                            
                        }
                    }
                }
            }
            
            // Stop the last track
            //play(player_instance.parentNode.getAttribute('id'), player_instance.getAttribute('src'));
            //play the next one
            play(found_values[1], found_values[3]);
}

function skip_song(type)
{
    next_timer = 0;
    var track_url = "";
    var player_instance = "";
    var all_players = document.getElementsByTagName('embed');
    for(var i = 0; i < all_players.length; i++)
    {
        if(all_players.item(i).parentNode.getAttribute("class") == "stop_button" || all_players.item(i).parentNode.getAttribute("className") == "stop_button")
        {
            track_url = all_players.item(i).getAttribute('src');
            player_instance = all_players.item(i);
        }
    }
    
    if(track_url != "")
    {
        if(type=="previous")
        {
            play_previous_song(player_instance);
        }
        else
        {
            play_next_song(player_instance);
        }
    }
}

function set_now_playing(track_name, track_url)
{
    play_span = document.getElementById(track_name);
    
    if( document.getElementById("now_playing") )
    {
        all_images = play_span.parentNode.getElementsByTagName('img');
        
        if(all_images.length > 0)
        {
            image = all_images.item(0);
            
            now_playing_img = document.getElementById("now_playing_img");
            now_playing_string = document.getElementById("now_playing_string");
            
            if(now_playing_prefix)
            {
                now_playing_string.innerHTML = now_playing_prefix + image.getAttribute("alt");
            }
            else
            {
                now_playing_string.innerHTML = image.getAttribute("alt");
            }
            now_playing_img.setAttribute("src", image.getAttribute("src"));
            now_playing_img.style.display = "block";
            document.getElementById("now_playing").style.display = "block";
        }
    }   
}

function set_nothing_playing()
{
    if( document.getElementById("now_playing") )
    {
        now_playing_img = document.getElementById("now_playing_img");
        now_playing_string = document.getElementById("now_playing_string");
        
        now_playing_string.innerHTML = "";
        now_playing_img.style.display = "none";
        document.getElementById("now_playing").style.display = "none";
    }
}

function using_IE()
{
    if(document.all && document.getElementById)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function stop() {
    
    next_timer = 0;
    var track_url = "";
    var player_instance = "";
    var all_players = document.getElementsByTagName('embed');
    for(var i = 0; i < all_players.length; i++)
    {
        if(all_players.item(i).parentNode.getAttribute("class") == "stop_button" || all_players.item(i).parentNode.getAttribute("className") == "stop_button")
        {
            track_url = all_players.item(i).getAttribute('src');
            player_instance = all_players.item(i);
        }
    }
    
    current_track_name = player_instance.parentNode.getAttribute('id');
    current_track_url = player_instance.getAttribute('src');
    
    play(player_instance.parentNode.getAttribute('id'), player_instance.getAttribute('src'));
}

function playFirst() {
    
    var track_url = "";
    playspan = document.getElementById("play_1");
    var found_values = new Array();
    all_li_a = playspan.getElementsByTagName('a');
    for(var j = 0; j < all_li_a.length; j++)
    {
        if(all_li_a.item(j).getAttribute('class') == "play_button" || all_li_a.item(j).getAttribute('className') == "play_button")
        {
            // we've found the next song
            link_href = all_li_a.item(j).getAttribute('href');
            
            found_values = link_href.split('\'');
            
        }
    }
    play(found_values[1], found_values[3]);
}

function resume() {
    if((current_track_name != "") && (current_track_url != "")) {
        play(current_track_name, current_track_url);
    } else {
        playFirst();
    }
}

function play_pause(button_id) {
    if(document.getElementsByTagName('embed').length > 0) {
        // we're playing, we need to stop
        stop();
        document.getElementById(button_id).setAttribute("className","");
        document.getElementById(button_id).setAttribute("class","");
    } else {
        // we're not doing anything, we need to play
        resume();
        document.getElementById(button_id).setAttribute("className","stop");
        document.getElementById(button_id).setAttribute("class","stop");
    }
}