curr_ko=null;

var playObj = null;
var DataInfo = null;
var DataList = null;
var DataTimeOut = null
var ko = null;
var httpObj = null;
var volume = 50;
var volNew = 0;
var errNum;

var DataTimeOut;

//
var isLRC=true,isGeci=true,curLyrictype=0,curLyricid=0;
//window.focus();
var scrollSpeed=1;
var nonLRCHandle=null;
function nonLRC(){
var tempTop=scrollId.scrollTop;
scrollId.scrollTop=tempTop+scrollSpeed;
nonLRCHandle=window.setTimeout("nonLRC()",400);
}

function LRCClass(){ 
	var XMLHTTP	= null;
	var FORMAT_LRC	= 1;
	var FORMAT_SMI	= 2;
	var FORMAT_DEF  = 3;
	var player	= null;
	var url 	= "";
	var output	= "";
	var content	= "";
	
	var LRC		= new Array();
	this.ddeLRC = new Array();
	var format	= FORMAT_LRC;
	this.offset = 0;
	this.fixTime = 0;
	this.lrctimer 	= null;
	this.flag		= "";
	this.dispaly=1;
	this.scObj="scrollId";
	this.scLength=15;
	this.playerver = 1; 
	if(arguments.length>=1)	this.setPlayer(arguments[0]);
	if(arguments.length>=2)	this.setURL(arguments[1]);
	if(arguments.length>=3)	this.setOutput(arguments[2]);
	if(arguments.length>=4)	this.setDisplayType(arguments[3]);
	if(arguments.length>=5)	this.setScrollId(arguments[4]);
	if(arguments.length>=6)	this.setScLength(arguments[5]);
}

LRCClass.prototype.setPlayer=function(){
	if(arguments.length>=1){
		arg	= arguments[0];
		if(typeof(arg)=="string")	this.player	= document.getElementById(arg);
		else if(typeof(arg)=="object")	this.player	= arg;
	}
	this.playerver = this.getPlayerVer();//ver
}

LRCClass.prototype.getPlayerVer=function(){
	var  p = this.getPlayer();
	if (p==null){return null;}
	var c =String(p.classid);
	c = c.toLowerCase();
	if (c=='clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95'){return 0;}
	if (c=='clsid:6bf52a52-394a-11d3-b153-00c04f79faa6'){return 1;}
}

LRCClass.prototype.getPos=function(){
		return (this.playerver==0)?this.player.CurrentPosition:this.player.controls.CurrentPosition;

}

LRCClass.prototype.getPlayer=function(){
	if( typeof(this.player)=='object')	return this.player;
	else	return null;
}



LRCClass.prototype.setURL=function(){
	if(arguments.length>=1){
		this.url = arguments[0];
		this.LRC="";
		this.LRC = new Array();
		this.ddeLRC = new Array();
	}
}

LRCClass.prototype.getURL=function(){
	return this.url;
}

LRCClass.prototype.setDisplayType=function(dtype){
	this.display=dtype;
}
LRCClass.prototype.getDisplayType=function(){
	return this.display;
}

LRCClass.prototype.setScrollId=function(sid){
if(typeof(sid)=="string")	this.scObj	= document.getElementById(sid)
	else if(typeof(sid)=="object")	this.scObj	= sid;
}
LRCClass.prototype.getScrollId=function(){
	return this.scObj;
}

LRCClass.prototype.setScLength=function(sLength){
	this.scLength=sLength;
}
LRCClass.prototype.getScLength=function(){
	return this.scLength;
}


LRCClass.prototype.setContent=function(){
	if(arguments.length>=1){
		this.content = arguments[0];
	}
}

LRCClass.prototype.getContent=function(){
	return this.content;
}

LRCClass.prototype.setLRC=function(){
	if(arguments.length>=1){
		this.LRC = arguments[0];
	}
}

LRCClass.prototype.getLRC=function(){
	return this.LRC;
}

LRCClass.prototype.setOutput=function(str){
		this.output	= str;
}

LRCClass.prototype.getOutput=function(){
    if(this.output!=""){
        arg = this.output;
        if(typeof(arg)=="string")   return document.getElementById(arg);
        else if(typeof(arg)=="object")  return arg;
    }
	return null;
}

LRCClass.prototype.transformLRC=function(){
	try{	
	if(this.getContent().length>0){
    		isGeci=true;
    	}else{
    		isGeci=false;
    	}
	}catch(e){}
	
	if( this.getContent()==null || this.getContent()=="" ){return;}
	if( this.format	== this.FORMAT_LRC ){
	  if(/\[offset\:(\-?\d+)\]/i.test(this.getContent()))		//取offset余补时间
		this.fixTime = RegExp.$1/1000;

		lines	= this.getContent().split("\n");
		
		arr	= new Array();
		str="";
		for(i=0;i<lines.length;i++){
			this.pushLine(trim(lines[i]));					
		}
		
		this.ddeLRC.sort();	

        var mm = "aaa";		
        for(var i=this.ddeLRC.length-1;i>=0;i--){
        	if(this.ddeLRC[i]==mm){
        		mm = this.ddeLRC[i];
        		this.ddeLRC.splice(i,1);
        	}else{
        		mm = this.ddeLRC[i];
        	}
        }		
		
		if(this.ddeLRC.length>0){//LRC
			isLRC=true;
			
			if(nonLRCHandle) clearTimeout(nonLRCHandle);
			
			if(typeof(scrollId)!='undefined'){
				scrollId.style.overflow='hidden';
				scrollId.scrollTop=0;
			}
			
		}else{
			isLRC=false;
			if(typeof(scrollId)!='undefined'){
				scrollId.style.overflow='auto';
				scrollId.scrollTop=0;
			}
			//nonLRC();
		}
		lasttime = 0;
		idx	= 0;

		for(i=0;i<this.ddeLRC.length;i++){
			if(this.ddeLRC[i].length>9){
				_time = getLyrcTime(this.ddeLRC[i]);
				_lrc  = getLyrc(this.ddeLRC[i]);
				if(_time<lasttime) {
					continue;
				}
				lasttime = _time;
				var dde = new LRCItem(_time,_lrc);
				arr[idx++]	= dde;
				//str+="arr["+(idx-1)+"]="+this.ddeLRC[i]+"...."+dde.time+"::"+dde.content+"\r\n";
			}
		}

		var dde = new LRCItem(3600,'本歌词由 <a href="http://www.51t.com" target="_blank" class="bluelink">www.51t.com</a> 友情提供');
		arr[idx++]= dde;
		this.setLRC(arr);
	}else{
		return;
	}
}

function trim(str)
{
     return str.replace(/^\s*|\s*$/g,"");
}

LRCClass.prototype.pushLine=function(line){
	supper = this;
    mm      = line.split("]");
	if(mm.length>=2){
		  text  = mm[mm.length-1];
		  for(j=0;j<mm.length-1;j++){				
				time    = formatTime(mm[j]+"]");				
				supper.ddeLRC.push(time+""+text);
		  }
	}
}

function formatTime(str){
    return str;
}

function printDdeLRC(){
	s	= "";
	for(i=0;i<supper.ddeLRC.length;i++){
		s += supper.ddeLRC[i]+"\r\n";
	}
}

LRCClass.prototype.syncLRC=function(){
	
}
LRCClass.prototype.setLrcData=function()
{

}
LRCClass.prototype.start=function(){
	this.flag = "start";
	supper	= this;
	supper.output.innerHTML="我要听音乐网……";
	pp	= supper.getPlayer();
	this.lrctimer	= window.setTimeout("detectSpeed(supper.getPlayer(),supper)",500);	
}

LRCClass.prototype.destroy=function(){
	this.flag = "stop";
	if(this.lrctimer) clearTimeout(this.lrctimer);
	this.url = "";
	this.content="";
	this.LRC="";
	this.LRC = new Array();
	this.LRC.length=0;
	this.ddeLRC = new Array();
	this.ddeLRC.length=0;
}

LRCClass.prototype.stop=function(){
	this.flag = "stop";
	if(this.lrctimer) clearTimeout(this.lrctimer);
}

function detectSpeed(player,ko){
  try{
	KO	= ko;
	var pp	= player;
	if(typeof(pp)=="string")	pp	= document.getElementById(pp);
	if( ko.flag == "stop" ){
		where   = ko.getOutput();
		if(where) where.innerHTML="我要听音乐网……";
	}else{
		this.lrctimer = window.setTimeout("detectSpeed(pp,KO)",1000);
	}
	
		if(ko.getDisplayType()==0){
			syncShowLyrc(KO);
		}else{
			syncVShowLyrc(KO);
		}
 }catch(E){document.title="Error..."+E.toString();}
}

function syncShowLyrc(ko){
try{
	loader2	= ko.getPlayer();
	where	= ko.getOutput();
	if(loader2){
		curtime	= ko.getPos();
		lrcs	= ko.getLRC();
		if( typeof(lrcs)=='undefined' || lrcs.length==0 ){
			ko.transformLRC();
			lrcs	= ko.getLRC();
		}

		for(j=0;j<lrcs.length;j++){
			var lrc	= lrcs[j];
			var nextlrc	= lrcs[(j<lrcs.length-1)?j+1:j];
			t1=lrc.time+ko.fixTime;
			t2=nextlrc.time+ko.fixTime;
			if(t1<=curtime&&curtime<t2){
				if(lrc.content)
					where.innerHTML=lrc.content;
					return;
			}
		}
	}
	}catch(E){document.title="syncShowLyrc="+E.toString();}	
}

var last_match_line_seq = -1;
function syncVShowLyrc(ko){
//LRC播放
try{
		loader2	= ko.getPlayer();
		where	= ko.getOutput();
		scid= ko.getScrollId();
		var tmp_seq = null;
		var t1=0,t2=0;
		curtime	= ko.getPos();
		
		sLength=ko.getScLength();
		if(loader2){			
			lrcs	= ko.getLRC();
			if( typeof(lrcs)=='undefined' || lrcs.length==0 ){
				ko.transformLRC();
				lrcs	= ko.getLRC();
			}
			if(isLRC&&nonLRCHandle) clearTimeout(nonLRCHandle);
			var temp="";
			var tmp_seq=0;
			for(j=0;j<lrcs.length;j++){
				var lrc	= lrcs[j];
				var nextlrc	= lrcs[(j<lrcs.length-1)?j+1:j];
				t1=lrc.time+ko.fixTime;
				t2=nextlrc.time+ko.fixTime;
				if(t1<=curtime && curtime<t2){
					tmp_seq = j;
					temp+="<span>&nbsp;<span class='red1'>"+trim(lrcs[j].content)+"</span></span><br>"
					if(typeof(scrollId)!='undefined') scrollId.scrollTop=j*sLength;
				}else{
					temp+="<span style='margin-left:22px'>"+lrc.content+"</span><br>"
				}	
			}
			if(where&&isGeci&&isLRC) {
				if(last_match_line_seq != tmp_seq){where.innerHTML=temp}
			}
			last_match_line_seq = tmp_seq;
		}
	}catch(E){
		//document.title="syncVShowLyrc="+E.toString();
	}
}

function LRCItem(){
	var time = -1;
	var content = "";
	if(arguments.length>=2){
		this.time 	= arguments[0];
		this.content	= arguments[1];
	}
}


function getLyrc(str){
var _ar
		try{
				_ar	= str.split("]");
				return _ar[_ar.length-1];
		}catch(E){
			debug(e.toString());
		}
		return "";
}

function getLyrcTime(str){
var t,mm,min
		try{
			t	= str.split("]")[0].split("[")[1];
			mm	= t.split(":");
			if(mm.length<2) return 0;		
			min	= mm[1].split(".")[0];				
			t	= parseInt(mm[0])*60+parseFloat(min);				
			if(!isNaN(t)) return t;
		}catch(E){
			debug(e.toString());
		}
		return 0;
}

function debug(v)
{
	document.title = v;
}

function ShowLRC(lrcUrl,o)
{	
	var playObj = getObject('Exobud')
	var  ko = new LRCClass();
	ko.setPlayer(playObj);
	ko.setURL(lrcUrl);
	ko.setOutput("contentLRC");
	ko.setDisplayType(1);
	ko.setScrollId("scrollId");
	ko.setScLength(16)
	ko.setContent(getObject(o).value);
	ko.start();
}