var loaderInterval;
var tmpScroll;
var tmpScrollId;
var perLength;
var tmpMatchLrcLine=0;
var preMatchLrcLine=-1;
var count=0;
var tmpLrcContent="";
var tmpLrcs=new Array();
var lineCount=new Array();
var perLineCount=new Array();
var preLrcContent=new Array();
var nextLrcContent=new Array();

function lrcInterface(pID,lrcID){
	//-------------------------
	if(loaderInterval) clearInterval(loaderInterval);
	tmpScroll=null;
	tmpScrollId=null;
	perLength=null;
	tmpMatchLrcLine=0;
	preMatchLrcLine=-1;
	count=0;
	tmpLrcContent="";
	tmpLrcs=new Array();
	lineCount=new Array();
	perLineCount=new Array();
	preLrcContent=new Array();
	nextLrcContent=new Array();
	//-------------------------
	var tmpOutput=document.getElementById('divLRC');
	count=0;
	var tmp=setInterval(getObject,200);
	function getObject(){
		bdLRC=new bdSyncLRC();
		if(bdLRC!=null){
			clearInterval(tmp);
			bdLRC.setPlayer(pID);
			bdLRC.setURL(lrcID);
			bdLRC.setOutput("divLRC");
			bdLRC.Exchange();
			bdLRC.begin()
		};
		else{
			if(count==25){
				clearInterval(tmp);
			};
			else count++
		}
	}
};

function bdSyncLRC(){
	var playerObj=null;
	var playerTypeStr=null;
	var lrcURL="";
	var divLRC="";
	var lrcContent="";
	var lrcObjArray=new Array();
	this.preLRC=new Array();
	this.offsetTime=0;
	this.scrollMoveLen=20;
	if(arguments.length>=1)this.setPlayer(arguments[0]);
	if(arguments.length>=2)this.setURL(arguments[1]);
	if(arguments.length>=3)this.setOutput(arguments[2])
};

bdSyncLRC.prototype.setPlayer=function(){
	if(arguments.length>=1){
		arg=arguments[0];
		playerTypeStr=String(arg);
		if(typeof(arg)=="string") this.playerObj=document.getElementById(arg);
		else if(typeof(arg)=="object") this.playerObj=arg
	}
};

bdSyncLRC.prototype.getPlayer=function(){
	if(typeof(document.getElementById("this.playerObj"))=='object') return this.playerObj;
	else return null
};

bdSyncLRC.prototype.setURL=function(){
	if(arguments.length>=1){
		var lrcDir,lrcFileName;
		lrcDir=String(Math.floor((parseInt(arguments[0])/100)));
		lrcFileName=arguments[0]+".lrc";
		this.lrcURL="/html/lrc/"+lrcDir+"/"+lrcFileName
	}
};

bdSyncLRC.prototype.getURL=function(){
	return this.lrcURL
};
		
bdSyncLRC.prototype.setContent=function(){
	if(arguments.length>=1)this.lrcContent=arguments[0]
};

bdSyncLRC.prototype.getContent=function(){
	return this.lrcContent
};

bdSyncLRC.prototype.setLRC=function(){
	if(arguments.length>=1)this.lrcObjArray=arguments[0]
};

bdSyncLRC.prototype.getLRC=function(){
	return this.lrcObjArray
};

bdSyncLRC.prototype.setOutput=function(showerId){
	this.divLRC=showerId
};

bdSyncLRC.prototype.getOutput=function(){
	if(this.divLRC!=""){
		arg=this.divLRC;
		if(typeof(arg)=="string") return document.getElementById(arg);
		else if(typeof(arg)=="object") return arg
	};
	return null
};

bdSyncLRC.prototype.multiLRC=function(lrcLine){
	thisObj=this;
	tmpVar=lrcLine.split("]");
	if(tmpVar.length>=2){
		lrcText=tmpVar[tmpVar.length-1];
		for(j=0;j<tmpVar.length-1;j++){
			lrcTime=tmpVar[j]+"]";
			thisObj.preLRC.push(lrcTime+""+lrcText)
		}
	}
};

bdSyncLRC.prototype.preSyncLRC=function(){
	if(this.getContent()==null||this.getContent()=="")return;
	lrcLines=this.getContent().split("\n");
	tmpArr=new Array();
	var tmpItem;
	for(i=0;i<lrcLines.length;i++) this.multiLRC(replaceStr(lrcLines[i]));
	this.preLRC.sort();
	var tmpVar="aaa";
	for(i=this.preLRC.length-1;i>=0;i--)
		if(this.preLRC[i]==tmpVar){
			tmpVar=this.preLRC[i];
			this.preLRC.splice(i,1)
		};
		else tmpVar=this.preLRC[i];
	preTime=0;
	indexLRC=1;
	tmpArr[0]=new LRCItem(0,"");
	for(i=0;i<this.preLRC.length;i++){
		if(this.preLRC[i].length>9){
			tmpTime=getLyrcTime(this.preLRC[i]);
			tmpLrc=getLyrc(this.preLRC[i]);
			if(tmpTime<preTime||tmpLrc=="")continue;
			if(tmpTime==0){
				tmpArr[0].lrcContent=tmpLrc;
				continue
			};
			preTime=tmpTime;
			tmpItem=new LRCItem(tmpTime,tmpLrc);
			tmpArr[indexLRC++]=tmpItem
		}
	};
	var tmpItem=new LRCItem(3600,"[حي]");
	tmpArr[indexLRC]=tmpItem;
	this.setLRC(tmpArr);
	getLineCount(tmpArr);
	getLrcContent(tmpArr);
	tmpLrcs=this.getLRC();
	tmpScroll=this.getOutput();
	tmpScrollId=this.getOutput();
	perLength=this.scrollMoveLen
};
	
function replaceStr(tmpStr){
	return tmpStr.replace(/^\s*|\s*$/g,"")
};

function getLyrc(tmpStr){
	try{
		tmpArray=tmpStr.split("]");
		return tmpArray[tmpArray.length-1]
	}catch(e){};
	return ""
};

function getLyrcTime(tmpStr){
	try{
		tmpChar=tmpStr.split("]")[0].split("[")[1];
		tmpVar=tmpChar.split(":");
		if(tmpVar.length<2)return 0;
		min=tmpVar[1].split(".")[0];
		tmpInt=parseInt(tmpVar[0])*60+parseFloat(min);
		if(!isNaN(tmpInt))return tmpInt
	}catch(e){};
	return 0
};

function LRCItem(){
	var lrcTime=-1;
	var lrcContent="";
	if(arguments.length>=2){
		this.lrcTime=arguments[0];
		this.lrcContent=arguments[1]
	}
};

function getLineCount(tmpLrcs){
	for(i=0;i<tmpLrcs.length;i++){
		lineCount[i]=Math.floor(tmpLrcs[i].lrcContent.length/67);
		perLineCount[i]=0;
		for(j=0;j<i;j++)perLineCount[i]+=lineCount[j]
	}
};

function getLrcContent(tmpLrcs){
	for(i=0;i<tmpLrcs.length;i++){
		preLrcContent[i]="";
		nextLrcContent[i]="";
		for(j=0;j<i-1;j++)preLrcContent[i]+="<span style='font-size:12px;margin-left:4px'>&nbsp;<font color=#000000>"+replaceStr(tmpLrcs[j].lrcContent)+"</font></span><br>";
		for(j=i+1;j<tmpLrcs.length;j++)nextLrcContent[i]+="<span style='font-size:12px;margin-left:4px'>&nbsp;<font color=#000000>"+replaceStr(tmpLrcs[j].lrcContent)+"</font></span><br>";
		nextLrcContent[i]+="<br><br>"
	}
};
	
bdSyncLRC.prototype.Exchange=function(){
	var xmlhttp=null;
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest()
	};
	else{
		if(window.ActiveXObject){
			try{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
			}catch(ee){
				xmlhttp=new ActiveXObject("MSXML.XMLHTTP")
			}
		}
	};
	var tmpURL=this.getURL();
	thisObj=this;
	var tmpStr="";
	try{
		xmlhttp.open("GET",tmpURL,true);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				if(xmlhttp.status==200){
					//tmpStr=bdBytes2Str(xmlhttp.responseBody);
					document.getElementById('lrcTitle').style.display="block";
					document.getElementById('divLRC').style.display="block";
					document.getElementById('divLyric').style.height="252px";
					document.getElementById('downLRC').href="/downlrc.php?p=" + tmpURL + "&n=" + mn_ms;
					document.getElementById('downLRC').style.color="red";//#F1F2DF
					document.getElementById('downLRC').style.display="inline";
					tmpStr=xmlhttp.responseText;
					thisObj.setContent(tmpStr)
				}else{
					document.getElementById('lrcTitle').style.display="none";
					document.getElementById('divLRC').style.display="none";
					document.getElementById('divLyric').style.height="336px";
				}
			}
		};
		xmlhttp.send(null)
	}catch(ee){}
};

bdSyncLRC.prototype.begin=function(){
	thisObj=this;
	tmpPlayerObj=thisObj.getPlayer();
	var tmpOutput=document.getElementById('divLRC');
	var tmpI=setInterval(getlrc,100);
	count=0;
	function getlrc(){
		if(typeof(thisObj.getContent())!="undefined"){
			clearInterval(tmpI);
			thisObj.preSyncLRC();
			if(thisObj.getLRC()[0].lrcContent!="")thisObj.getOutput().innerHTML="<br><br><br><br><br><br>"+"<span style='font-size:12px;margin-left:4px'>&nbsp;<font color=#000000>"+replaceStr(thisObj.getLRC()[0].lrcContent)+"</font></span><br>"+nextLrcContent[0];
			else thisObj.getOutput().innerHTML="<br><br><br><br><br><br>"+nextLrcContent[0];
			getPlayingTime(thisObj.getPlayer(),thisObj)
		};
		else{
			if(count==50){
				clearInterval(tmpI);
			};
			else count++
		}
	}
};

function getPlayingTime(playerObj,bdLRCObj){
	try{
		tmpbdLRCObj=bdLRCObj;
		var tmpPlayerObj=playerObj;
		if(typeof(tmpPlayerObj)=="string")tmpPlayerObj=document.getElementById(tmpPlayerObj);
		window.setTimeout("getPlayingTime(tmpPlayerObj,tmpbdLRCObj)",1000);
		bdsyncLyrc(tmpbdLRCObj)
	}catch(e){}
};

function getCurrentPosition(tmpPlayer){
	if(playerTypeStr=="MediaPlayer"||playerTypeStr=="mplayer")return tmpPlayer.CurrentPosition;
	else return tmpPlayer.GetPosition()/1000
};

function findLoc(bdLRCObj){
	var start;
	var end;
	var tmpPlayer=bdLRCObj.getPlayer();
	currentTime=getCurrentPosition(tmpPlayer);
	if(currentTime>=tmpLrcs[tmpMatchLrcLine].lrcTime){
		start=tmpMatchLrcLine;
		end=tmpLrcs.length
	};
	else{
		start=0;
		end=tmpMatchLrcLine+1
	};
	for(j=start;j<end;j++){
		nowLrc=tmpLrcs[j];
		nextLrc=tmpLrcs[(j<end-1)?j+1:j];
		nowTime=nowLrc.lrcTime+bdLRCObj.offsetTime;
		nextTime=nextLrc.lrcTime+bdLRCObj.offsetTime;
		if(nowTime<=currentTime&&currentTime<nextTime)return j
	}
};
	
function bdsyncLyrc(bdLRCObj){
	try{
		j=findLoc(bdLRCObj);
		tmpMatchLrcLine=j;
		tmpLrcContent=preLrcContent[j];
		if(j>0)tmpLrcContent+="<span style='font-size:12px;margin-left:4px;color:#000000'>&nbsp;"+replaceStr(tmpLrcs[j-1].lrcContent)+"</span><br>";
		tmpLrcContent+="<span style='font-size:12px;margin-left:4px;color:#ff5a00;font-weight:bold'>&nbsp;"+replaceStr(tmpLrcs[j].lrcContent)+"</span><br>";
		tmpLrcContent+=nextLrcContent[j];
		if(preMatchLrcLine!=tmpMatchLrcLine){
			tmpScroll.innerHTML=tmpLrcContent;
			if(Math.abs(tmpMatchLrcLine-preMatchLrcLine)>1){
				tmpScrollId.scrollTop=(tmpMatchLrcLine+perLineCount[tmpMatchLrcLine]+1)*perLength;
				tmpScrollId.scrollTop=(tmpMatchLrcLine+perLineCount[tmpMatchLrcLine]+1)*perLength
			};
			else{
				count=0;
				function scrollAdd(){
					if(!tmpScrollId) bdLRCObj.preSyncLRC();/*add for error(tmpScrollId is null or not object)*/
					try{
						if(preMatchLrcLine>1){
							tmpScrollId.scrollTop+=2*(1+lineCount[preMatchLrcLine]);
						}
					}catch(e){}
					count++;
					if(count==10)clearInterval(loaderInterval)
				};
				loaderInterval=setInterval(scrollAdd,25)
			}
		};
		preMatchLrcLine=tmpMatchLrcLine
	}catch(e){}
};
