var errorTimerID;
var timerID;
var ms=-1;
var stop=1;
window.onload = function(){readplaying(); readinfo();}

function dispLoading()
{
  var elm = document.getElementById("loading");
  if( elm )
  {
    elm.innerHTML = '<img src="./img/indicator.white.gif">&nbsp;';
  }
}

function hideLoading()
{
  var elm = document.getElementById("loading");
  if( elm )
  {
    elm.innerHTML = "";
  }
}

function errorDispStart()
{
  clearTimeout(errorTimerID);
  errorTimerID = setTimeout("eraseError();", 10*1000);
}

function eraseError()
{
  var elm = document.getElementById("error");
  if( elm )
  {
    elm.innerHTML = "";
    elm.style.border = "";
  }
  clearTimeout(errorTimerID);
}

function countdownstart()
{
  clearTimeout(timerID);
  update();
}

function update()
{
  if( ms > 0 && !stop )
  {
    ms--;
    th = Math.floor(ms / 3600);
    tm = Math.floor(ms / 60);
    ts = Math.floor(ms % 60);
    var timestring = (th<10?"0":"")+th+":"+(tm<10?"0":"")+tm+":"+(ts<10?"0":"")+ts;
    var clock = document.getElementById("clock");
    if( clock )
    {
      clock.innerHTML = "残り演奏時間 : "+timestring;
    }
    if( ms <= 0 )
    {
      stop = 1;
      timerID = setTimeout("readplaying();",1000);
    }
    else
    {
      timerID = setTimeout("update();",1000);
    }
  }
  else
  {
    clearTimeout(timerID);
  }
}

function readinfo( arg )
{
  dispLoading();
  sendRequest(onload_main,arg,'GET','./info.php',true,true);
}

function readmain( loc )
{
  dispLoading();
  sendRequest(onload_main,'','GET',loc,true,true);
}

function readmain2( arg, loc )
{
  dispLoading();
  sendRequest(onload_main,arg,'GET',loc,true,true);
}

function readhistory()
{
  dispLoading();
  sendRequest(onload_history,'','GET','./songhistory.php',true,true);
}

function searchmain( arg )
{
  dispLoading();
  sendRequest(onload_main,arg,'GET','./search.php',true,true);
}

function readplaying()
{
  clearTimeout(timerID);
  dispLoading();
  sendRequest(onload_top,'','GET','./playing.php',true,true);
}

function request( arg )
{
  dispLoading();
  sendRequest(onload_top,arg,'GET','./playing.php',true,true);
}

function onload_main( oj )
{
  hideLoading();
  document.getElementById("main").innerHTML = oj.responseText;
}

function onload_history( oj )
{
  hideLoading();
  var main = document.getElementById("main");
  if( !main ) return;
  var msg = '<SPAN class="info">Song History Last 1 hour</SPAN><BR>\n';
  msg += '<P class="history">\n';
  var r = eval(oj.responseText);
  var l = r.length-1;
  for( i=0; i<l; )
  {
    msg += r[i++] + ' ' + decodeURIComponent(r[i++]);
    msg += (r[i++]?' *':'') + ((i>3)?'<BR>\n':'&nbsp;<SPAN class="red"><- Current Song</SPAN><BR>\n');
  }
  msg += '</P>';
  main.innerHTML = msg;
}

function onload_top( oj )
{
  hideLoading();
  if( oj.responseText == "" )
  {
    clearTimeout(timerID);
    timerID = setTimeout("readplaying();",5000);
    return;
  }
  var r = eval(oj.responseText);
  ms = r[0];
  stop = ((ms>0)?0:1);
  var top = document.getElementById("top");
  if( !top ) return;
  var elm;
  var msg = "";
  if( r[1] != "" )
  {
    var current = decodeURIComponent(r[1]);
    var arr = current.indexOf("[Arrange]");
    var title = "";
    var song = "";
    if( arr != -1 )
    {
      title = current.substring(arr+10);
      song = title.split(" - ");
    }
    else
    {
      title = current.split(" - ");
      song = title[1].split(" / ");
    }
    elm = document.getElementById("playing");
    if( elm )
    {
      elm.innerHTML = current;
    }
    elm = document.getElementById("ad");
    if( elm )
    {
      var str = '<iframe src="http://rcm-jp.amazon.co.jp/e/cm?t=pachisloch-22&o=9&p=10&l=st1&mode=music-jp&search=';
      str += encodeURIComponent( song[0] );
      str += '&fc1=000000&lt1=_blank&lc1=3366FF&bg1=FFFFFF&f=ifr" marginwidth="0" marginheight="0" width="120" height="450" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe>';
      elm.innerHTML = str.replace("&amp;","&");
    }
  }
  if( r[2] != "" )
  {
    var tmp = decodeURIComponent(r[2]);
    var tag = tmp.split("<>");
    msg += '<DIV class="tag">関連タグ：\n';
    for( i=0; i<tag.length; i++ )
    {
      if( tag[i].length == 0 )
      {
        continue;
      }
      msg += '<a href="javascript:void(0)" onclick="searchmain({tag:\'' + tag[i] + '\'})">' + tag[i] + '</a>&nbsp;\n';
    }
    if( r[1] != "" )
    {
      var title = decodeURIComponent(r[1]);
      var stat = encodeURIComponent('RT @PachisloCh #NowPlaying ' + title + ' http://pachisloch.crz.jp/');
      msg += '<a href="http://twitter.com/home?status=' + stat + '"';
      msg += ' title="Twitterでつぶやく" target="_blank"><img src="img/tweetthis-teal16.png" border="0"></a>\n';
    }
    msg += '</DIV>\n';
  }
  msg += "<BR>";
  if( r[3] == "" )
  {
    msg += '<SPAN class="normal">※ ランダム演奏中</SPAN>\n';
  }
  else
  {
    var tmp = decodeURIComponent(r[3]);
    msg += '<SPAN class="info">Request Queue</SPAN><BR>\n';
    msg += '<DIV class="queue">\n' + tmp + '</DIV>\n';
  }
  top.innerHTML = msg;
  elm = document.getElementById("listener");
  if( elm )
  {
    if( r[4] > 0 )
    {
      elm.innerHTML = r[4] + " listener" + ((r[4]==1)?", now":"s, now");
    }
    else
    {
      elm.innerHTML = "";
    }
  }
  if( r[5] != "" )
  {
    elm = document.getElementById("error");
    if( elm )
    {
      elm.innerHTML = decodeURIComponent(r[5]);
      elm.style.border = "1px solid #ff0000";
    }
    errorDispStart();
  }
  if( stop == 0 )
  {
    countdownstart();
  }
}

