var Lrc0;
var Lrc1;
var min;
var LrcData=document.getElementById("LrcData");
var PaPaPlayer=document.getElementById("PaPaPlayer");
var LrcollBox=document.getElementById("LrcollBox");
var Lrcoll=document.getElementById("Lrcoll");
var Lrcw1t=document.getElementById("Lrcwt1");
var Lrcwt2=document.getElementById("Lrcwt2");
var Lrcwt3=document.getElementById("Lrcwt3");
var Lrcwt4=document.getElementById("Lrcwt4");
var Lrcwt5=document.getElementById("Lrcwt5");
var Lrcwt6=document.getElementById("Lrcwt6");
var Lrcwt7=document.getElementById("Lrcwt7");
var Lrcwt8=document.getElementById("Lrcwt8");
var Lrcwt9=document.getElementById("Lrcwt9");
var LrcFilter=document.getElementById("LrcFilter");
var LrcBox=document.getElementById("LrcBox");
var LrcBC=document.getElementById("LrcBC");
Lrcobj = new LrcClass(LrcData.innerHTML.slice(4,-3));

function LrcClass(tt)
{
  this.inr = [];
  this.min = [];

  this.oTime = 0;

  this.dts = -1;
  this.dte = -1;
  this.dlt = -1;
  this.ddh;
  this.fjh;

  LrcBC.style.width = 0;

  if(/\[offset\:(\-?\d+)\]/i.test(tt))
    this.oTime = RegExp.$1/1000;

  tt = tt.replace(/\[\:\][^$\n]*(\n|$)/g,"$1");
  tt = tt.replace(/\[[^\[\]\:]*\]/g,"");
  tt = tt.replace(/\[[^\[\]]*[^\[\]\d]+[^\[\]]*\:[^\[\]]*\]/g,"");
  tt = tt.replace(/\[[^\[\]]*\:[^\[\]]*[^\[\]\d\.]+[^\[\]]*\]/g,"");
  tt = tt.replace(/<[^<>]*[^<>\d]+[^<>]*\:[^<>]*>/g,"");
  tt = tt.replace(/<[^<>]*\:[^<>]*[^<>\d\.]+[^<>]*>/g,"");

  while(/\[[^\[\]]+\:[^\[\]]+\]/.test(tt))
  {
    tt = tt.replace(/((\[[^\[\]]+\:[^\[\]]+\])+[^\[\r\n]*)[^\[]*/,"\n");
    var zzzt = RegExp.$1;
    /^(.+\])([^\]]*)$/.exec(zzzt);
    var ltxt = RegExp.$2;
    var eft = RegExp.$1.slice(1,-1).split("][");
    for(var ii=0; ii<eft.length; ii++)
    {
      var sf = eft[ii].split(":");
      var tse = parseInt(sf[0],10) * 60 + parseFloat(sf[1]);
      var sso = { t:[] , w:[] , n:ltxt }
      sso.t[0] = tse-this.oTime;
      this.inr[this.inr.length] = sso;
    }
  }
  this.inr = this.inr.sort( function(a,b){return a.t[0]-b.t[0];} );

  for(var ii=0; ii<this.inr.length; ii++)
  {
    while(/<[^<>]+\:[^<>]+>/.test(this.inr[ii].n))
    {
      this.inr[ii].n = this.inr[ii].n.replace(/<(\d+)\:([\d\.]+)>/,"%=%");
      var tse = parseInt(RegExp.$1,10) * 60 + parseFloat(RegExp.$2);
      this.inr[ii].t[this.inr[ii].t.length] = tse-this.oTime;
    }
    LrcBC.innerHTML = "<font>"+ this.inr[ii].n.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/%=%/g,"</font><font>") +"</font>";
    var fall = LrcBC.getElementsByTagName("font");
    for(var wi=0; wi<fall.length; wi++)
      this.inr[ii].w[this.inr[ii].w.length] = fall[wi].offsetWidth;
    this.inr[ii].n = LrcBC.innerText;
  }

  for(var ii=0; ii<this.inr.length-1; ii++)
    this.min[ii] = Math.floor((this.inr[ii+1].t[0]-this.inr[ii].t[0])*10);
  this.min.sort(function(a,b){return a-b});
  min = this.min[0]/2;

  this.run = function(tme)
  {
    if(tme<this.dts || tme>=this.dte)
    {
      var ii;
      for(ii=this.inr.length-1; ii>=0 && this.inr[ii].t[0]>tme; ii--){}
      if(ii<0) return;
      this.ddh = this.inr[ii].t;
      this.fjh = this.inr[ii].w;
      this.dts = this.inr[ii].t[0];
      this.dte = (ii<this.inr.length-1)?this.inr[ii+1].t[0]:PaPaPlayer.currentMedia.duration;

      Lrcwt1.innerText = this.retxt(ii-5);
      Lrcwt2.innerText = this.retxt(ii-4);
      Lrcwt3.innerText = this.retxt(ii-3);
      Lrcwt4.innerText = this.retxt(ii-2);
      Lrcwt5.innerText = this.retxt(ii-1);
      LrcFilter.innerText = this.retxt(ii-1);
      Lrcwt6.innerText = this.retxt(ii+1);
      Lrcwt7.innerText = this.retxt(ii+2);
      Lrcwt8.innerText = this.retxt(ii+3);
      Lrcwt9.innerText = this.retxt(ii+4);
      this.print(this.retxt(ii));
      if(this.dlt==ii-1)
      {
        clearTimeout(Lrc0);
        if(Lrcoll.style.pixelTop!=0) Lrcoll.style.top = 0;
        goLrcoll(0);
        clearTimeout(Lrc1);
        LrcFilter.filters.alpha.opacity = 100;
        goLrcolor(0);
      }
      else if(parseInt(Lrcoll.style.top)!=-20)
      {
        clearTimeout(Lrc0);
        Lrcoll.style.top = -20;
        clearTimeout(Lrc1);
        LrcFilter.filters.alpha.opacity = 0;
      }
      this.dlt = ii;
    }
    var bbw = 0;
    var ki;
    for(ki=0; ki<this.ddh.length && this.ddh[ki]<=tme; ki++)
      bbw += this.fjh[ki];
    var kt = ki-1;
    var sc = ((ki<this.ddh.length)?this.ddh[ki]:this.dte) - this.ddh[kt];
    var tc = tme - this.ddh[kt];
    bbw -= this.fjh[kt] - tc / sc * this.fjh[kt];
    if(bbw>LrcBox.offsetWidth)
      bbw = LrcBox.offsetWidth;
    LrcBC.style.width = Math.round(bbw);
  }

  this.retxt = function(i)
  {
    return (i<0 || i>=this.inr.length)?"":this.inr[i].n;
  }

  this.print = function(txt)
  {
    LrcBox.innerText = txt;
    LrcBC.innerText = txt;
  }

  this.print("www.papasite.net");
  Lrcwt1.innerText = "";
  Lrcwt2.innerText = "";
  Lrcwt3.innerText = "";
  Lrcwt4.innerText = "";
  Lrcwt5.innerText = "";
  LrcFilter.innerText = "";
  Lrcwt6.innerText = "";
  Lrcwt7.innerText = "";
  Lrcwt8.innerText = "";
  Lrcwt9.innerText = "";

}

function Lrcrun()
{
  with(PaPaPlayer)
  {
    Lrcobj.run(controls.currentPosition);
  }
  if(arguments.length==0) setTimeout("Lrcrun()",10);
}

function goLrcoll(s)
{
  Lrcoll.style.top = -(s++)*2;
  if(s<=9)
    Lrc0 = setTimeout("goLrcoll("+s+")",min*10);
}

function goLrcolor(t)
{
  LrcFilter.filters.alpha.opacity = 110-(t++)*10;
  if(t<=10)
    Lrc1 = setTimeout("goLrcolor("+t+")",min*10);
}
window.onerror = function()
{return true;}
Lrcrun();