﻿
/*bylewi at 2009-2-22 1:33:04*/

function ShowLrc(lrc) {
  this.mediaPlayer;
  this.instance;
  this.seesaw;
  this.curRowDate;
  this.curRowLength;
  this.curRow = -1;
  this.curRowDateV = -1;
  this.curRowDateK = -1;
  this.offsetTime = 0;
  this.playptr = 0;
  this.gsh = "歌手:~1~\n曲名:~2~\n专辑:~3~\n编者:~4~";

  this.rowCollection = [];
  if (/\[offset\:(\-?\d+)\]/i.test(lrc)) {
    this.offsetTime = RegExp.$1 / 1000;
  }
  this.gsh = this.gsh.replace("~1~", (/\[ar:([^\[\]:]+)\]/i.test(lrc)) ? RegExp.$1 : "----");
  this.gsh = this.gsh.replace("~2~", (/\[ti:([^\[\]:]+)\]/i.test(lrc)) ? RegExp.$1 : "----");
  this.gsh = this.gsh.replace("~3~", (/\[al:([^\[\]:]+)\]/i.test(lrc)) ? RegExp.$1 : "----");
  this.gsh = this.gsh.replace("~4~", (/\[by:([^\[\]:]+)\]/i.test(lrc)) ? RegExp.$1 : "----");
  lrc = lrc.replace(/\[\:\][^$\n]*(\n|$)/g, "$1");
  lrc = lrc.replace(/\[[^\[\]\:]*\]/g, "");
  lrc = lrc.replace(/\[[^\[\]]*[^\[\]\d]+[^\[\]]*\:[^\[\]]*\]/g, "");
  lrc = lrc.replace(/\[[^\[\]]*\:[^\[\]]*[^\[\]\d\.]+[^\[\]]*\]/g, "");
  lrc = lrc.replace(/<[^<>]*[^<>\d]+[^<>]*\:[^<>]*>/g, "");
  lrc = lrc.replace(/<[^<>]*\:[^<>]*[^<>\d\.]+[^<>]*>/g, "");
  while (/\[[^\[\]]+\:[^\[\]]+\]/.test(lrc)) {
    lrc = lrc.replace(/((\[[^\[\]]+\:[^\[\]]+\])+[^\[\r\n]*)[^\[]*/, "\n");
    var zzzt = RegExp.$1;
    /^(.+\])([^\]]*)$/.exec(zzzt);
    var ltxt = RegExp.$2;
    var eft = RegExp.$1.slice(1, -1).split("][");
    for (var i = 0; i < eft.length; i++) {
      var sf = eft[i].split(":");
      var tse = parseInt(sf[0], 10) * 60 + parseFloat(sf[1]);
      var sso = { t: [], w: [], n: ltxt }
      sso.t[0] = tse - this.offsetTime;
      this.rowCollection[this.rowCollection.length] = sso;
    }
  }
  this.rowCollection = this.rowCollection.sort(function(a, b) { return a.t[0] - b.t[0]; });
  for (var i = 0; i < this.rowCollection.length; i++) {
    while (/<[^<>]+\:[^<>]+>/.test(this.rowCollection[i].n)) {
      this.rowCollection[i].n = this.rowCollection[i].n.replace(/<(\d+)\:([\d\.]+)>/, "%=%");
      var tse = parseInt(RegExp.$1, 10) * 60 + parseFloat(RegExp.$2);
      this.rowCollection[i].t[this.rowCollection[i].t.length] = tse - this.offsetTime;
    }
    LrcBg.innerHTML = "<font>" + this.rowCollection[i].n.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/%=%/g, "</font><font>") + " </font>";
    var fall = LrcBg.getElementsByTagName("font");
    for (var wi = 0; wi < fall.length; wi++) {
      this.rowCollection[i].w[this.rowCollection[i].w.length] = fall[wi].offsetWidth;
    }
    this.rowCollection[i].n = LrcBg.innerText;
  }
  this.print(this.gsh);
  LrcLine1.innerText = "";
  LrcLine2.innerText = "";
  LrcLine3.innerText = "";
  LrcLine4.innerText = "";
  LrcLine5.innerText = "";
  LrcLine6.innerText = "";
  LrcLine7.innerText = "";
  LrcLine8.innerText = "";
  LrcLine9.innerText = "";
  LrcLine10.innerText = "";
  LrcLine11.innerText = "";
  LrcBg.style.width = 0;
}
ShowLrc.prototype.run = function() {
  try {
    this.runing(this.mediaPlayer.controls.currentPosition + this.playptr, this.mediaPlayer.currentMedia.duration);
  } catch (hh) { }
}
ShowLrc.prototype.runing = function(tme, plen) {
  if (tme < this.curRowDateV || tme >= this.curRowDateK) {
    var i;
    for (i = this.rowCollection.length - 1; i >= 0 && this.rowCollection[i].t[0] > tme; i--) { }
    if (i < 0) return;
    this.curRowDate = this.rowCollection[i].t;
    this.curRowLength = this.rowCollection[i].w;
    this.curRowDateV = this.rowCollection[i].t[0];
    this.curRowDateK = (i < this.rowCollection.length - 1) ? this.rowCollection[i + 1].t[0] : plen;
    LrcLine1.innerText = this.retxt(i - 6);
    LrcLine2.innerText = this.retxt(i - 5);
    LrcLine3.innerText = this.retxt(i - 4);
    LrcLine4.innerText = this.retxt(i - 3);
    LrcLine5.innerText = this.retxt(i - 2);
    LrcLine6.innerText = this.retxt(i - 1);
    LrcLine7.innerText = this.retxt(i + 1);
    LrcLine8.innerText = this.retxt(i + 2);
    LrcLine9.innerText = this.retxt(i + 3);
    LrcLine10.innerText = this.retxt(i + 4);
    LrcLine11.innerText = this.retxt(i + 5);
    this.print(this.retxt(i));
    if (this.curRow == i - 1) {
      clearTimeout(this.seesaw);
      this.changeLrcScroll(0);
    }
    this.curRow = i;
  }
  var bbw = 0;
  var ki;
  for (ki = 0; ki < this.curRowDate.length && this.curRowDate[ki] <= tme; ki++) {
    bbw += this.curRowLength[ki];
  }
  var kt = ki - 1;
  var sc = ((ki < this.curRowDate.length) ? this.curRowDate[ki] : this.curRowDateK) - this.curRowDate[kt];
  var tc = tme - this.curRowDate[kt];
  bbw -= this.curRowLength[kt] - tc / sc * this.curRowLength[kt];
  if (bbw > LrcBox.offsetWidth) {
    bbw = LrcBox.offsetWidth;
  }
  LrcBg.style.width = Math.round(bbw);
}
ShowLrc.prototype.retxt = function(i) {
  return (i < 0 || i >= this.rowCollection.length) ? "" : this.rowCollection[i].n;
}
ShowLrc.prototype.print = function(txt) {
  LrcBox.innerText = txt;
  LrcBg.innerText = txt;
}
ShowLrc.prototype.changeLrcScroll = function(s) {
  LrcScroll.style.top = 25 - (s++) * 5;
  LrcLine1.filters.alpha.opacity = 90 - s * 18;
  LrcLine11.filters.alpha.opacity = s * 18 + 10;
  if (s <= 5) {
    this.seesaw = setTimeout(this.instance + ".changeLrcScroll(" + s + ")", 120);
  }
}
var LrcObj;
function LrcRun(m) {
  LrcObj = new ShowLrc(m);
  LrcObj.instance = "LrcObj";
  LrcObj.mediaPlayer = document.getElementById("MusicPlayer");
  setInterval("LrcObj.run();", 100);
}