Fetion.Web.Chat.defined("Fetion.Web.Chat.Audio");
Fetion.Web.Chat.Audio.list = [];
Fetion.Web.Chat.Audio.create = function (o)
{
    var frm = Fetion.Web.Chat.newCtrl("cardfrm");
    frm.setSize(182);
    frm.foot.style.display = "none";
    var loadingImg = "<img src='chat/img/chat/loading.gif'>";
    if (!o.path)
        frm.updateProgress();
    else
        loadingImg = "<span></span>";
    frm.progress = Fetion.Web.Chat.newCtrl("progress");
    frm.progress.update({ size: o.time - -1 });
    frm.body.style.padding = "6px";
    var html = "";
    html += "<table cellPadding='0' cellSpacing='0' style='width:100%;margin-bottom:2px'><tr>";
    html += "<td style='background:url(chat/img/chat/audio_none.png);width:30px;height:26px'></td>";
    html += "<td style='width:100%;background:url(chat/img/chat/audio_bg.png)' align='center'>" + loadingImg + "</td>";
    html += "<td style='background:url(chat/img/chat/audio_tail.png);width:24px;height:26px;font-size:12px'></td>";
    html += "</tr></table>";
    frm.body.innerHTML = html;
    frm.row = frm.body.childNodes[0].rows[0];
    frm.btn = frm.row.cells[0];
    frm.btn.ctrl = frm;
    frm.loading = frm.row.cells[1];
    frm.cell = frm.row.cells[1];
    frm.time = frm.row.cells[2];
    frm.play = false;

    if (o.path)
        this.update(frm, o);

    if (o.parent)
        o.parent.appendChild(frm);

    this.list.push(frm);

    return frm;
};
Fetion.Web.Chat.Audio.update = function (ctrl, data)
{
    var msg = null;
    if (data.status == "overdue")
    {
        ctrl.updateProgress({ size: 100, value: 0 });
        ctrl.foot.progress.text.innerHTML = "<span style='color:red'>文件已过期</span>";
        ctrl.loading.innerHTML = "";
        return;
    }
    else if (data.status == "failure")
    {
        ctrl.updateProgress({ size: 100, value: 0 });
        ctrl.foot.progress.text.innerHTML = "<span style='color:red'>接收失败</span>";
        ctrl.loading.innerHTML = "";
        ctrl.retry = ctrl.addFootButton({
            title: "重试", onclick: function ()
            {
                if (!window.external.DownLoadObj(this.ctrl.parentNode.id))
                    return;
                ctrl.foot.progress.text.innerHTML = "0%";
                ctrl.loading.innerHTML = "<img src='chat/img/chat/loading.gif'>";
                ctrl.removeFootButton(ctrl.retry);
            }
        });
        return;
    }

    if (data.path)
        data.progress = 100;

    if (ctrl.retry)
    {
        ctrl.removeFootButton(ctrl.retry);
        ctrl.retry = null;
    }

    if (ctrl.foot.progress)
        ctrl.updateProgress({ size: 100, value: data.progress });

    if (!ctrl.foot.progress || ctrl.foot.progress.value == ctrl.foot.progress.size)
    {
        ctrl.btn.style.background = "url(chat/img/chat/audio_play.png)";
        ctrl.btn.data = data;

        ctrl.cell.removeChild(ctrl.cell.childNodes[0]);
        ctrl.cell.align = "left";
        ctrl.cell.style.paddingRight = "5px";
        ctrl.cell.appendChild(ctrl.progress);
        ctrl.time.innerHTML = data.time + "'";

        ctrl.removeProgress();
        ctrl.removeFootButton();

        if (data.op == false)
            return;

        ctrl.btn.style.cursor = "hand";
        ctrl.btn.onclick = function ()
        {
            var ctrl = this.ctrl;
            var data = this.data;
            ctrl.play = !ctrl.play;
            if (ctrl.play)
            {
                if (window.preAudio)
                    preAudio.btn.click();

                this.style.background = "url(chat/img/chat/audio_stop.png)";
                ctrl.progress.update({ value: 0 });
                window.preAudio = ctrl;
                ctrl.time = data.time;
                ctrl.curTime = 0;
                ctrl.intervalID = setInterval(function ()
                {
                    ctrl.progress.update({ value: ctrl.progress.value + 1 });
                    if (!ctrl.play || ctrl.progress.value == ctrl.progress.size)
                        ctrl.btn.click();
                }, 1000);

                //调客户端接口，播放声音
                window.external.OperateSoundMsg(data.path, 1);
            }
            else
            {
                if (ctrl.intervalID)
                    clearInterval(ctrl.intervalID);
                ctrl.progress.update({ value: 0 });
                window.preAudio = null;
                this.style.background = "url(chat/img/chat/audio_play.png)";
                //调客户端接口，停止声音
                window.external.OperateSoundMsg(data.path, 2);
            }
        };

        ctrl.addFootButton({
            title: "另存为",
            data: data,
            onclick: function ()
            {
				if((isIE&&window.event.button==1)||(!isIE&&window.event.button==0)){  
					var data = this.data;
					window.external.SaveFileAs(data.path);
				}
            }
        });
		//if(!(window.funType&&window.funType==6)){
			ctrl.addFootButton({
				title: "转发",
				data: data,
				onclick: function ()
				{
					if((isIE&&window.event.button==1)||(!isIE&&window.event.button==0)){  
						var ctrl = this.ctrl;
						var parent = ctrl.parentNode;
						/*window.external.ForwardingMsg(parent.id, parent.jsonStr);*/
						var p ;
						if(window.point){
							p = window.point;
						}else{
							p = mousePosition(window.event, null);
						}
					window.external.onClickMenu(parent.id, parent.jsonStr,p.y+10, p.x-10);
					}
				}
			});
		//}
        return;
    }
};
Fetion.Web.Chat.Audio.stop = function (path)
{
    var ctrl = null;
    for (var i = 0; i < this.list.length; i++)
    {
        var tmpCtrl = this.list[i];
        if (tmpCtrl.data.path == path && tmpCtrl.play)
        {
            ctrl = tmpCtrl;
            break;
        }
    }

    if (ctrl == null)
        return;

    ctrl.progress.update({ value: 0 });
    if (ctrl.intervalID)
        clearInterval(ctrl.intervalID);
    window.preAudio = null;
    ctrl.btn.style.background = "url(chat/img/chat/audio_play.png)";
    ctrl.play = !ctrl.play;
    //ctrl.btn.click();     // 不再发出停止播放，否则声音会停止
};