var time=0;
var total=0;
var position=0;
var refresh=0;
var np_artist="";
var np_track="";
var np_release="";
var np_year="";
var np_label="";
var np_length="";
var np_listeners="";


function writeInto()
{
    refreshRequest();
}

function refreshRequest()
{
    if(refresh>=30)
    {
        Request();
        refresh=0;    
    }
    else
    {
        refresh = parseInt("1", 10)+parseInt(refresh, 10);
    }
    if(total-time>0)
    {
        time = parseInt(1,10)+parseInt(time, 10);
    }
    else
    {   
        setTimeout("Request()",Math.floor(Math.random()*8));
    }
    if(total==0)
    {
        position = 0;
    }
    else 
    {
        position = Math.round((1-((total-time)/total))*450);
    }
    if ( position == NaN )
    {
        position = 0;
    }
    //document.getElementById('slider').innerHTML=time+"/"+total+"="+position +"("+refresh+")";
    percent = Math.round(((time/total)*100))+"%";

    remaining = total - time;
    rem_mins_secs = formatTime(remaining,1);
    tot_mins_secs = formatTime(total,1);
    pos_mins_secs = formatTime(time,1);
 
    posPx = position+"px";
    parem_posPx = 450-position;
    parem_posPx = parem_posPx+"px";

    document.getElementById('slider').innerHTML=pos_mins_secs;
    document.getElementById('slider-parem').innerHTML=rem_mins_secs;
    window.document.getElementById("slider").style.width=posPx;
    window.document.getElementById("slider-parem").style.width=parem_posPx;

    document.getElementById('np-length').innerHTML=tot_mins_secs;

    var timeout = 1000;

    setTimeout("refreshRequest()",timeout);
}

function formatTime(time_in_secs, mode)
{
    tot_mins = Math.floor(time_in_secs/60);
    tot_secs = time_in_secs % 60;
    if (tot_mins < 10)
    {
        tot_mins = "0"+tot_mins;
    }
    if (tot_secs < 10)
    {
        tot_secs = "0"+tot_secs;
    }
    if(mode == 1)
    {
        total_formatted = tot_mins +":"+tot_secs;
    }
    if(mode == 2)
    {
        total_formatted = tot_mins+" min. "+tot_secs+" sec.";
    }
    return total_formatted;
}

function writeNpInfo(dom)
{
    var x = dom.documentElement.getElementsByTagName('currentsong')[0];
    np_artist = x.getElementsByTagName("artist")[0].firstChild.nodeValue;
    np_track = x.getElementsByTagName("track")[0].firstChild.nodeValue;
    np_release = x.getElementsByTagName("release")[0].firstChild.nodeValue;
    np_year = x.getElementsByTagName("year")[0].firstChild.nodeValue;
    np_label = x.getElementsByTagName("label")[0].firstChild.nodeValue;
    np_listeners = x.getElementsByTagName("listeners")[0].firstChild.nodeValue;

    if(np_year == "0000")
    {
        np_year = "";
    }

    document.getElementById('np-artist').innerHTML=np_artist;
    document.getElementById('np-track').innerHTML=np_track;
    document.getElementById('np-release').innerHTML=np_release;
    document.getElementById('np-year').innerHTML=np_year;
    document.getElementById('np-label').innerHTML=np_label;
    document.getElementById('np-listeners').innerHTML=np_listeners;
}

function writeComingUp(dom)
{
    var x = dom.documentElement.getElementsByTagName('comingup')[0].childNodes;
    for (i=0;i<x.length;i++)    
    {
    //Display only element nodes
        if (x.item(i).nodeType==1)
        {
           order = x.item(i).getAttribute('id');
           request = x.item(i).getAttribute('request');
           p_id = "comingup_"+order;
           songname = x.item(i).firstChild.nodeValue;
           if(songname == "")
                songname = "---";
           if(request == 1)
                songname = '<span class="comingup_added">[added]</span> '+songname;
           document.getElementById(p_id).innerHTML=songname;
        }
    }
}

function Request()
{
    var urlVar = "/js/song.php";
    urlVar = urlVar + '&' + Math.random();
    var aj = new Ajax.Request("js/song.php", { method:'get', onComplete: writeData } );
}

function writeData(oReq)
{
    var response = oReq.responseXML; 
    time = response.documentElement.getElementsByTagName('time')[0].firstChild.nodeValue;
    total = response.documentElement.getElementsByTagName('total')[0].firstChild.nodeValue;
    writeNpInfo(response);
    writeComingUp(response);
}        
