﻿var loaderInterval;
var tmpScroll;
var tmpScrollId;
var perLength;
var tmpMatchLrcLine = 0;
var preMatchLrcLine = -1;
var count = 0;
var tmpLrcContent = "";
//****************fix firefox bug
var isFireFox = (navigator.userAgent.indexOf("Firefox")!=-1) ;
var preStyleObj ,nowStyleObj;
if(!isFireFox)
{
	preStyleObj = document.styleSheets[0].rules[0];
	nowStyleObj = document.styleSheets[0].rules[1];
}
else
{
	preStyleObj = document.styleSheets[0].cssRules[0];
	nowStyleObj = document.styleSheets[0].cssRules[1];
}
//***************
var tmpLrcs = new Array();
var lineCount = new Array();
var perLineCount = new Array();
var preLrcContent = new Array();
var nextLrcContent = new Array();
var color = new Array(21);
color[0] = "#000000";
color[1] = "#0d0500";
color[2] = "#1a0a00";
color[3] = "#270f00";
color[4] = "#341400";
color[5] = "#411900";
color[6] = "#4e1e00";
color[7] = "#5b2300";
color[8] = "#682800";
color[9] = "#752d00";
color[10] = "#823200";
color[11] = "#8f3700";
color[12] = "#9c3c00";
color[13] = "#a94100";
color[14] = "#b64600";
color[15] = "#c34b00";
color[16] = "#d05000";
color[17] = "#dd5500";
color[18] = "#ea5a00";
color[19] = "#f75f00";
color[20] = "#ff5a00";

function lrcInterface(pID, lrcID)
    {
    var tmpOutput = document.getElementById('LrcShower_div');
    count = 0;
    tmpOutput.innerHTML
        = "<br><br><br><br><br><br><span style='font-size:12px;margin-left:12px'>&nbsp;正在加载歌词信息，请您稍等片刻....";
    var tmp = setInterval(getObject, 200);

    function getObject()
        {
        bdLRC = new bdSyncLRC();
              
          
        if (bdLRC != null)
            {
            clearInterval(tmp);
            bdLRC.setPlayer(pID);//获取播放器对象
            bdLRC.setURL(lrcID);//歌词来源于的文件的位置
            bdLRC.setOutput("LrcShower_div");
            bdLRC.Exchange();
            bdLRC.begin();
            }
        else
            {
            if (count == 25)
                {
                clearInterval(tmp);
                tmpOutput.innerHTML
                    = "<br><br><br><br><br><br><span style='font-size:12px;margin-left:12px'>&nbsp;可能是因为网络的原因，系统没有找到合适的歌词。请稍后重试"
                }
            else
                count++
            }
        }
    };
function bdSyncLRC()
    {
    var playerObj = null;
    var playerTypeStr = null;
    var lrcURL = "";
    var lrcShower = "";
    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";
		//得到Lrc 地址
        this.lrcURL = "/bdlrc/" + lrcDir + "/" + lrcFileName
		//this.lrcURL = "http://localhost/top100web/audition/LRC歌词/M0044456002.lrc";
		this.lrcURL = lrcFileName;//"/w王力宏 - 在梅边.lrc";
        }
    };

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.lrcShower = showerId
    } ;

bdSyncLRC.prototype.getOutput = function()
    {
    if (this.lrcShower != "")
        {
        arg = this.lrcShower;

        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, "Over...");
    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] = "<br>" + "<br>" + "<br>" + "<br>" + "<br>" + "<br>" + "<br>";
        nextLrcContent[i] = "";

        for (j = 0; j < i - 1; j++)
            preLrcContent[i] += "<span style='font-size:12px;margin-left:12px'>&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:12px'>&nbsp;<font color=#000000>" + replaceStr(
                                                                                                    tmpLrcs[j].lrcContent)
                       + "</font></span><br>";

        nextLrcContent[i] += "<br><br><br><br><br><br><br>"
        }
    }

;
bdSyncLRC.prototype.Exchange = function()
    {
    var str=PlayAJAX.GetSynedLrc(this.getURL());
    var thisObj=this;
    thisObj.setContent(str.value);
    
   /* 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, false);
        xmlhttp.onreadystatechange = function()
            {
            if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
                {
                tmpStr = bdBytes2Str(xmlhttp.responseBody);
                thisObj.setContent(tmpStr)
                }
            };

        xmlhttp.send(null)
        }
    catch (ee)
        {
        }*/
    };

bdSyncLRC.prototype.begin = function()
    {
    thisObj = this;
    tmpPlayerObj = thisObj.getPlayer();
    var tmpOutput = document.getElementById('LrcShower_div');
    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:12px'>&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);
                tmpOutput.innerHTML
                    = "<br><br><br><br><br><br><span style='font-size:12px;margin-left:12px'>&nbsp;可能是因为网络的原因，系统没有找到合适的歌词。请稍后重试"
                }
            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(typeof tmpPlayer == 'object')
	{
		//return tmpPlayer.controls.currentPositionString
		//return tmpPlayer.controls.currentPosition / 1000 ;
		return tmpPlayer.CurrentPosition;//tmpPlayer.controls.currentPosition;
	}
	/*if (playerTypeStr == "MediaPlayer1")
        	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 class=prelrc>&nbsp;" + replaceStr(tmpLrcs[j - 1].lrcContent) + "</span><br>";

        tmpLrcContent += "<span class=nowlrc>&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()
                    {
                    tmpScrollId.scrollTop += 2 * (1 + lineCount[preMatchLrcLine]);
                    count++;

                    if (count == 10)
                        clearInterval(loaderInterval)
                    }

                ;
                loaderInterval = setInterval(scrollAdd, 25)
                }
            };

        preMatchLrcLine = tmpMatchLrcLine
        }
    catch (e)
        {
        }
    }

;
