var playerstr = "";
var t;
var playId = new Array();
playId[1] = "";
playId[2] = "";
playId[3] = "";
var DataTable;
var _ModelId;

function SelectPlay(_app, _modelId, _act, _id) {
    var v = [];
    var a = get_tags(DianCMS_$(_id), 'input');

    for (var i = 0; i < a.length; i++) {
        if (_act == 'all') { a[i].checked = true; }
        else if (_act == 'un') { a[i].checked = (a[i].checked) ? false : true; }
        else { if (0 < a[i].value && a[i].checked) { v.push(a[i].value); } }
    }
    if (_act == "play") {
        if (0 == v.length) {
            do_alert('请先选择音乐，再进行连播！');
            setTimeout('close_alert()', 3000); return;
        }
        Win_Open("" + _app + "/PlaySong.aspx?ModelId=" + _modelId + "&Id=" + v.join('_') + "", "", "750", "500");
    }
}


function get_tags(parentobj, tag) {
    if (typeof parentobj.getElementsByTagName != 'undefined') {
        return parentobj.getElementsByTagName(tag);
    } else if (parentobj.all && parentobj.all.tags) {
        return parentobj.all.tags(tag);
    } else { return new Array(); }
}

function do_alert(msg) {
    var _height = document.documentElement.clientHeight || document.body.clientHeight;
    var _top = (_height) ? (_height - 39) / 2 + document.documentElement.scrollTop : 0;
    var msgObj = document.createElement('div');
    msgObj.setAttribute('id', 'msgInfo');
    msgObj.setAttribute('align', 'center');
    msgObj.style.position = 'absolute';
    msgObj.style.top = _top + 'px';
    msgObj.style.left = '0';
    msgObj.style.width = '100%';
    msgObj.style.height = '33px';
    msgObj.style.zIndex = '999';
    msgObj.innerHTML = '<span style=\"display: block;padding: 9px;width: 250px;height: 26px;font-weight: bold;font-size: 14px;border: 3px solid #f9d455;background: #fef8d9;\">' + msg + '</span>';
    document.body.appendChild(msgObj);
}
function close_alert() {
    document.body.removeChild(DianCMS_$('msgInfo'));
}

function LoadPlayList(ModelId, Id) {
    _ModelId = ModelId;
    playerstr = Id;
    D.Music.B_Common.GetPlayList(ModelId, Id, CallBackPlayList)
}

function CallBackPlayList(res) {
    DataTable = res.value;
    var strTbody = "<table width=330px border='0' cellpadding='0' cellspacing='0' id='TableList'>";
    strTbody += "<tr><td class=\"plmbg3\">歌曲名称</td><td width='80' class=\"plmbg3\">歌手名称</td><td width='50px' class=\"plmbg3\">操作</td></tr>";
    strTbody += "<tr><td colspan='3' bgcolor='#e0e0e0' height='1'></td></tr>";

    for (var p = 0; p < playerstr.split(",").length; p++) {
        for (var i = 0; i < res.value.Rows.length; i++) {
            if (res.value.Rows[i]["Id"] == playerstr.split(",")[p]) {
                strTbody += "<tr class='playlist' id=tr_" + res.value.Rows[i]["Id"] + " ondblclick=\"DesMusic('" + res.value.Rows[i]["Id"] + "')\" title=\"双击该行选择音乐\">";
                strTbody += "<td style=\"padding-left:10px;\">" + res.value.Rows[i]["MusicName"] + "</td>";
                strTbody += "<td style=\"padding-left:10px;\">" + res.value.Rows[i]["MusicUser"] + "</td>";
                strTbody += "<td><img src='user/images/play/delete.gif' alt='删除该首歌曲' style='cursor:hand;margin-left:10px' onclick=\"Delete('" + res.value.Rows[i]["Id"] + "')\"></td>";
                strTbody += "</tr>";
                break;
            }
        }
    }

    strTbl = strTbody + "</table>";
    var obj = document.getElementById("tableDiv");
    obj.innerHTML = strTbl;
    t = setInterval('SetPlayer()', 200);
}

function SetPlayer() {
    if (DianCMS_$("MediaPlayer").PlayState == "undefined") {
        alert("该浏览器不支持播放");
        clearInterval(t);
    }
    //读取状态
    GetPlayerState();
    switch (DianCMS_$("MediaPlayer").PlayState) {
        case 0:
            playId[2] = playerstr.split(",")[0];
            Play(playId[2]);
            break;
        case 1:
            DianCMS_$("MediaPlayer").url = "";
            clearInterval(t);
            break;
        case 10:
            Play(playId[3]);
            break;
    }
}

function Play(Id) {
    DianCMS_$("PlayImg").src = "user/images/play/Pause_1.gif";
    playId[1] = playId[2];
    playId[2] = Id;

    lrcwt13.innerText = "";
    lrcwt12.innerText = "";
    lrcwt11.innerText = "";
    lrcwt1.innerText = "";
    lrcwt2.innerText = "";
    lrcwt3.innerText = "";
    lrcwt4.innerText = "";
    lrcwt5.innerText = "";
    lrcwt6.innerText = "";
    lrcwt7.innerText = "";
    lrcwt8.innerText = "";
    lrcbox.innerText = "";
    lrcbc.style.width = 0;

    //歌词加载
    MediaLrc(Id);


    if (playId[1] != "") {
        DianCMS_$("tr_" + playId[1] + "").className = "playlist";
    }
    DianCMS_$("tr_" + Id + "").className = "playlist2";

    //设置滚动条所在位置
    SetscrollTop(Id);
    DianCMS_$("MediaPlayer").url = "common/song.aspx?ModelId=" + _ModelId + "&Id=" + Id + "";

    for (var i = 0; i < playerstr.split(",").length; i++) {
        if (playerstr.split(",")[i] == Id) {
            if (i != playerstr.split(",").length - 1) {
                playId[3] = playerstr.split(",")[i + 1]
            }
            else {
                playId[3] = playerstr.split(",")[0]
            }
        }
    }
}

function GetPlayerState() {
    switch (DianCMS_$("MediaPlayer").PlayState) {
        case 0:
            DianCMS_$("PlayerState").innerHTML = "未定义";
            break;
        case 1:
            DianCMS_$("PlayerState").innerHTML = "停止";
            break;
        case 3:
            SetJinDu();
            DianCMS_$("PlayerState").innerHTML = DianCMS_$("MediaPlayer").controls.currentPositionString + "|" + DianCMS_$("MediaPlayer").currentMedia.durationString;
            break;
        case 6:
            DianCMS_$("PlayerState").innerHTML = "正在缓冲";
            break;
        case 9:
            DianCMS_$("PlayerState").innerHTML = "正在连接";
            break;
        case 10:
            DianCMS_$("PlayerState").innerHTML = "准备就绪";
            break;
    }
}

function SetJinDu() {
    //    $("TextBox1").value = $("MediaPlayer").settings.volume;
    DianCMS_$("SoundNum2").style.width = DianCMS_$("MediaPlayer").settings.volume;


    DianCMS_$("PlayerJinDu2").style.width = (DianCMS_$("MediaPlayer").controls.currentPosition / DianCMS_$("MediaPlayer").currentMedia.duration) * DianCMS_$("PlayerJinDu").offsetWidth;
    DianCMS_$("PlayerDown").style.width = (DianCMS_$("MediaPlayer").network.downloadProgress.toString() / 100) * DianCMS_$("PlayerJinDu").offsetWidth;
}


function PlayerDown(x, y) {
    if (DianCMS_$("MediaPlayer").PlayState == 1 || DianCMS_$("MediaPlayer").PlayState == 2 || DianCMS_$("MediaPlayer").PlayState == 3) {
        DianCMS_$("MediaPlayer").controls.currentPosition = (x / y * DianCMS_$("MediaPlayer").currentMedia.duration).toFixed(2);
        DianCMS_$("PlayerJinDu2").style.width = x / y * 200;
    }
}

function SoundDown(x, y) {
    s = (x / y * 100).toFixed(0);
    DianCMS_$("MediaPlayer").settings.volume = s;
    DianCMS_$("SoundNum2").style.width = s;
    DianCMS_$("SoundNum").title = s;

}

function CloseSound() {
    if (DianCMS_$("MediaPlayer").settings.mute) {
        DianCMS_$("SoundImg").alt = "静音";
        DianCMS_$("SoundImg").src = "user/images/play/sound_1.gif";
        DianCMS_$("MediaPlayer").settings.mute = false;
    } else {
        DianCMS_$("SoundImg").alt = "声音";
        DianCMS_$("SoundImg").src = "user/images/play/sound_2.gif";
        DianCMS_$("MediaPlayer").settings.mute = true;
    }
}

function NextImg() {
    Play(playId[3]);
}

function PreviousImg() {
    for (var i = 0; i < playerstr.split(",").length; i++) {
        if (playerstr.split(",")[i] == playId[2]) {
            if (i != 0) {
                Play(playerstr.split(",")[i - 1]);
            }
            else {
                Play(playerstr.split(",")[0]);
            }
        }
    }
}

function StopImg() {
    DianCMS_$("MediaPlayer").controls.stop();
    DianCMS_$("PlayImg").src = "user/images/play/play_1.gif";
    clearInterval(t);
}

function PlayEventImg(state) {
    switch (DianCMS_$("MediaPlayer").PlayState) {
        case 1: //停止
            switch (state) {
                case "1":
                    DianCMS_$("PlayImg").src = "user/images/play/play.gif";
                    break;
                case "2":
                    DianCMS_$("PlayImg").src = "user/images/play/play_1.gif";
                    break;
                case "3":
                    DianCMS_$("PlayImg").src = "user/images/play/Pause_1.gif";
                    t = setInterval('SetPlayer()', 200);
                    DianCMS_$("MediaPlayer").controls.play();
                    break;
            }
            break;
        case 2: //暂停
            switch (state) {
                case "1":
                    DianCMS_$("PlayImg").src = "user/images/play/play.gif";
                    break;
                case "2":
                    DianCMS_$("PlayImg").src = "user/images/play/play_1.gif";
                    break;
                case "3":
                    DianCMS_$("MediaPlayer").controls.play();
                    DianCMS_$("PlayImg").src = "user/images/play/play_1.gif";
                    break;
            }
            break;
        case 3: //播放
            switch (state) {
                case "1":
                    DianCMS_$("PlayImg").src = "user/images/play/pause.gif";     //
                    break;
                case "2":
                    DianCMS_$("PlayImg").src = "user/images/play/Pause_1.gif";    //
                    break;
                case "3":
                    DianCMS_$("MediaPlayer").controls.pause();
                    DianCMS_$("PlayImg").src = "user/images/play/Pause_1.gif";
                    break;
            }
            break;
    }
}

function Delete(Id) {
    if (playId[2] != Id) {
        var _playerstr = "";
        for (var i = 0; i < playerstr.split(",").length; i++) {
            if (playerstr.split(",")[i] == Id) {
                DianCMS_$("TableList").deleteRow(DianCMS_$("tr_" + Id + "").sectionRowIndex);
                if (playId[3] == playerstr.split(",")[i]) {
                    if (i != playerstr.split(",").length - 1) {
                        playId[3] = playerstr.split(",")[i + 1]
                    }
                    else {
                        playId[3] = playerstr.split(",")[0]
                    }
                }
            }
            else {
                _playerstr += playerstr.split(",")[i] + ",";
            }
        }

        if (_playerstr.length > 0) {
            _playerstr = _playerstr.substring(0, _playerstr.length - 1);
        }

        playerstr = _playerstr;
    }
}

function DesMusic(Id) {
    Play(Id);
}

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.cnane;
    //以上几个属性是为了判断是否还在上次显示的时间范围，以减少循环次数


    if (/\[offset\:(\-?\d+)\]/i.test(tt))		//取offset余补时间
        this.oTime = RegExp.$1 / 1000;
    this.gsh = this.gsh.replace("~1~", (/\[ar:([^\[\]:]+)\]/i.test(tt)) ? RegExp.$1 : "----");
    this.gsh = this.gsh.replace("~2~", (/\[ti:([^\[\]:]+)\]/i.test(tt)) ? RegExp.$1 : "----");
    this.gsh = this.gsh.replace("~3~", (/\[al:([^\[\]:]+)\]/i.test(tt)) ? RegExp.$1 : "----");
    this.gsh = this.gsh.replace("~4~", (/\[by:([^\[\]:]+)\]/i.test(tt)) ? RegExp.$1 : "----");
    //lrcxx.innerText = this.gsh;
    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;
        }
        lrcbc.innerHTML = "<font>" + this.inr[ii].n.replace(/&/g, "&").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/%=%/g, "</font><font>") + " </font>";
        var fall = lrcbc.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 = lrcbc.innerText;
    }


    this.print("");
    lrcwt13.innerText = "";
    lrcwt12.innerText = "";
    lrcwt11.innerText = "";
    lrcwt1.innerText = "";
    lrcwt2.innerText = "";
    lrcwt3.innerText = "";
    lrcwt4.innerText = "";
    lrcwt5.innerText = "";
    lrcwt6.innerText = "";
    lrcwt7.innerText = "";
    lrcwt8.innerText = "";
    lrcbc.style.width = 0;
}


lrcClass.prototype.run = function() {
    try {
        this.runing(DianCMS_$("MediaPlayer").controls.currentPosition, DianCMS_$("MediaPlayer").currentMedia.duration);
    } catch (hh) { }
}

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;

        lrcwt13.innerText = this.retxt(ii - 6);
        lrcwt12.innerText = this.retxt(ii - 5);
        lrcwt11.innerText = this.retxt(ii - 4);
        lrcwt1.innerText = this.retxt(ii - 3);
        lrcwt2.innerText = this.retxt(ii - 2);
        lrcwt3.innerText = this.retxt(ii - 1);
        lrcwt4.innerText = this.retxt(ii + 1);
        lrcwt5.innerText = this.retxt(ii + 2);
        lrcwt6.innerText = this.retxt(ii + 3);
        lrcwt7.innerText = this.retxt(ii + 4);
        lrcwt8.innerText = this.retxt(ii + 5);
        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 > lrcbox.offsetWidth)
        bbw = lrcbox.offsetWidth;
    lrcbc.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) {
    lrcbox.innerText = txt;
    lrcbc.innerText = txt;
}

lrcClass.prototype.golrcoll = function(s) {
    lrcwt1.filters.alpha.opacity = 90 - s * 18;
    lrcwt5.filters.alpha.opacity = s * 18 + 10;
    lrcoll.style.top = 25 - (s++) * 5;
    if (s <= 5)
        this.hailang = setTimeout(this.cnane + ".golrcoll(" + s + ")", 120);
}


var lrcobj;
var olt;
function playlrc(m) {
    clearInterval(olt);
    lrcobj = new lrcClass(m);
    lrcobj.cnane = "lrcobj";
    olt = setInterval("lrcobj.run();", 100);
}

function MediaLrc(Id) {
    for (var i = 0; i < DataTable.Rows.length; i++) {
        if (DataTable.Rows[i]["Id"] == Id) {
            try {
                if (DataTable.Rows[i]["lrcSong"].length == 0) {
                    lrcwt3.innerText = "暂无LRC歌词";
                    clearInterval(olt);
                }
                else {
                    lrcwt3.innerText = "LRC歌词加载中... ...";
                    playlrc(DataTable.Rows[i]["lrcSong"]);
                }
            } catch (e)
            { }

            try {
                if (DataTable.Rows[i]["txtSong"].length == 0) {
                    TxtSong.style.lineHeight = "260px";
                    TxtSong.style.Height = "260px";
                    TxtSong.innerText = "暂无文本歌词";
                }
                else {
                    TxtSong.style.lineHeight = "150%";
                    TxtSong.style.Height = "260px";
                    TxtSong.innerText = DataTable.Rows[i]["txtSong"];
                }
            }
            catch (e) { }

            DianCMS_$("MusicName").innerText = DataTable.Rows[i]["MusicName"];
            DianCMS_$("MusicUser").innerText = DataTable.Rows[i]["MusicUser"];
        }
    }
}

function ShowSong(name) {
    switch (name) {
        case "lrc":
            DianCMS_$("Tab_lrc").style.display = "";
            DianCMS_$("Tab_txt").style.display = "none";
            break;
        case "txt":
            DianCMS_$("Tab_lrc").style.display = "none";
            DianCMS_$("Tab_txt").style.display = "";
            break;
    }
}

function SetscrollTop(Id) {
    for (var i = 0; i < playerstr.split(",").length; i++) {
        if (playerstr.split(",")[i] == Id) {
            if (i >= 6 && i * DianCMS_$("tr_" + Id).scrollHeight < DianCMS_$("DivListTable").scrollHeight) {
                DianCMS_$("DivListTable").scrollTop = (i * DianCMS_$("tr_" + Id).scrollHeight) - (5 * DianCMS_$("tr_" + Id).scrollHeight);
            }
            else {
                DianCMS_$("DivListTable").scrollTop = 0;
            }
        }
    }
}