/*
=====================================================================
  程序名称：lrc歌词播放（完美版）
  程序件者：海浪
  发行网站：http://kongjianzhan.126.com
  作者信箱：xuhotao@163.com
  制作日期：2005年09月12日

  说明：本lrc歌词播放程序原是《浩海网络多格式播放器》中的一个组件，
        现提取出来加以改进。本程序任何人都可以使用，但无论您转载或
        对本程序作任何修改、美化、翻译等工作，请您*必须*保留此段版
	权宣告的内容。
=====================================================================
*/


//  为了兼容 IE5.0 结果好多正则的语法和处理方式都不能用，所以代码效率不是最高，郁闷。


function lrcClass(tt)		//LRC歌词处理 类
{
  this.gsh="歌手:~1~\n曲名:~2~\n专辑:~3~\n编者:~4~";
  this.inr = [];		//行
  this.oTime = 0;		//余补时间
  this.hailang;
  this.dts = -1;		//当前行显示的s
  this.dte = -1;		//当前行显示的e
  this.dlt = -1;		//当前行
  this.ddh;		//当前行数据
  this.fjh;
  this.haohaiplay;
  this.ff;
  this.oceanx;
  this.cnane;
//以上几个属性是为了判断是否还在上次显示的时间范围，以减少循环次数


  if(/\[offset\:(\-?\d+)\]/i.test(tt))		//取offset余补时间
    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;
    }
   document.getElementById("gck").innerHTML = "<font>"+ this.inr[ii].n.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/%=%/g,"</font><font>") +" </font>";
   var fall = document.getElementById("gck").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 = document.getElementById("gck").innerText;
  }


  this.print("");
  document.getElementById("one").innerText = "";
  document.getElementById("two").innerText = "";
  document.getElementById("three").innerText = "";
  document.getElementById("four").innerText = "";
  document.getElementById("five").innerText = "";
  document.getElementById("six").innerText = "";
  document.getElementById("seven").innerText = "";
  document.getElementById("eight").innerText = "";
  document.getElementById("nine").innerText = "";

  document.getElementById("gck").style.width = 0;
}


lrcClass.prototype.run = function()
{
  var isIE=document.all? true:false;
  try {
    if(this.oceanx==0)
	{
		if(isIE)
		{
         this.runing(this.haohaiplay.controls.currentPosition, this.haohaiplay.currentMedia.duration);
	    }
		else
		{
		 this.runing(this.ff.controls.currentPosition, this.ff.currentMedia.duration);
		}
	}
    else
	{
      this.runing(this.haohaiplay.GetPosition()/1000, this.haohaiplay.GetLength()/1000);
	}
  } catch(hh){}
}
function findPlayer(movieName){
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
	return document.getElementById(movieName);
}


lrcClass.prototype.runing = function(tme, plen)
{
	
  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]:plen;
    document.getElementById("one").innerText = this.retxt(ii-3);
	document.getElementById("two").innerText = this.retxt(ii-2);
    document.getElementById("three").innerText = this.retxt(ii-1);
    document.getElementById("four").innerText = this.retxt(ii+1);
    document.getElementById("five").innerText = this.retxt(ii+2);
	document.getElementById("six").innerText = this.retxt(ii+3);
	document.getElementById("seven").innerText = this.retxt(ii+4);
	document.getElementById("eight").innerText = this.retxt(ii+5);
	document.getElementById("nine").innerText = this.retxt(ii+6);

    this.print(this.retxt(ii));
    if(this.dlt==ii-1)
    {
      clearTimeout(this.hailang);
      this.golrcoll(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>document.getElementById("gcbox").offsetWidth)
    bbw = document.getElementById("gcbox").offsetWidth;
  document.getElementById("gck").style.width = Math.round(bbw);
}

lrcClass.prototype.retxt = function(i)
{ 
  return (i<0 || i>=this.inr.length)?"":this.inr[i].n;
}

lrcClass.prototype.print = function(txt)
{
  document.getElementById("gcbox").innerText = txt;
  document.getElementById("gck").innerText = txt;
}

lrcClass.prototype.golrcoll = function(s)
{
  document.getElementById("gckong").style.top = 25-(s++)*3;
  document.getElementById("one").filters.alpha.opacity = 90-s*18;
  document.getElementById("five").filters.alpha.opacity = s*18+10;
  if(s<=5)
    this.hailang = setTimeout(this.cnane+".golrcoll("+s+")",120);
}

////////////////////////////////////////////////////////////////////

var lrcobj;

function mp3play()
{ 
  var m = document.getElementById("lrcdata").innerHTML.slice(4,-3);
  lrcobj = new lrcClass(m);
  lrcobj.cnane = "lrcobj";
  lrcobj.haohaiplay = document.getElementById("mediaPlayerObj");
  lrcobj.ff = document.getElementById("ffmedia");
  lrcobj.oceanx = 0;
  

  //0为使用 Media Player 控件，1为使用 Real Player 控件

  setInterval("lrcobj.run();",100);

}
//
function isIE(){ //ie?
   if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)
    return true;
   else
    return false;
}

if(!isIE()){ //firefox innerText define
   HTMLElement.prototype.__defineGetter__(     "innerText",
    function(){
     var anyString = "";
     var childS = this.childNodes;
     for(var i=0; i<childS.length; i++) {
      if(childS[i].nodeType==1)
       anyString += childS[i].tagName=="BR" ? '\n' : childS[i].textContent;
      else if(childS[i].nodeType==3)
       anyString += childS[i].nodeValue;
     }
     return anyString;
    }
   );
   HTMLElement.prototype.__defineSetter__(     "innerText",
    function(sText){
     this.textContent=sText;
    }
   );
}
