$(function () {
  // 开始直播
  // DWLive.onLiveStart = function (j) {
  //   console.log(j);
  // }

  window._onStart = function () {
    console.log('直播中---_onStart-')
  }

  // 停止直播
  DWLive.onLiveEnd = function (j) {
    console.log('websdk log: DWLive.onLiveEnd', j)
    DWLive.hangupInteraction()
    console.log('websdk log: DWLive.hangupInteraction')

    setTimeout(function () {
      $('li[name="interaction"]').hide()
    }, 1500)
  }
  // 清晰度回调
  DWLive.onHDReceivedVideoQuality = function (data) {
    console.log('webdk log: DWLive.onHDReceivedVideoQuality', data)
    var qualityHtml = data.map(function (data) {
      var itemHTML = '<button id="quality' + data.quality + '" onclick="changeQuality(' + data.quality + ')">' + data.desc + '</button>'
      return itemHTML
    }).join('') 
    $('#streamQuality').html(qualityHtml)
    $('#quality' + DWLive.getQualityIndex()).addClass('btn')
  }

  window.changeQuality = function (quality) {
    DWLive.changeQuality(quality, function (data) {
      console.log('websdk log: DWLive.changeQuality', data)
    })
    $('#streamQuality button').removeClass('btn')
    $('#quality' + DWLive.getQualityIndex()).addClass('btn')
    console.log('websdk log: DWLive.getQualityIndex', DWLive.getQualityIndex())
  }
  // 在线人数
  DWLive.onUserCountMessage = function (j) {
    console.log('websdk log: DWLive.onUserCountMessage', j);
  }

  // 开始直播后显示公告
  DWLive.onAnnouncementShow = function (j) {
    console.log('websdk log: DWLive.onAnnouncementShow', j)
    console.log('公告:', j)
  }

  // 修改公告,发布公告
  DWLive.onAnnouncementRelease = function (j) {
    console.log('websdk log: DWLive.onAnnouncementRelease', j)
    console.log(j)
  }

  // 删除公告
  DWLive.onAnnouncementRemove = function (j) {
    console.log('websdk log: DWLive.onAnnouncementRemove', j)
    console.log(j)
  }

  // 广播信息添加
  DWLive.onBroadcastMsg = function (data) {
    console.log('websdk log: DWLive.onBroadcastMsg', data)
    var h = '<li class="msg-admin" id="' + data.id + '">系统消息：' + data.content + ' </li>'
    $('#chat-list').append(h)

    $('#chat-list').parent().scrollTop($('#chat-list').height())
  }


  // 广播信息删除
  DWLive.removeBroadcastMsg = function (data) {
    console.log('websdk removeBroadcastMsg', data)
    $('#' + data.id).remove();
  };

  // 设置音量
  $('.set-sound').click(function () {
    var soundValue = $('#soundInput').val()
    DWLive.setSound(soundValue)  // 设置音量(0-1)
    console.log('websdk log: DWLive.setSound', soundValue)
  })

  // 弹幕开关
  $('.toggle-barrage').click(function (e) {
    if (e.target.innerText === '打开弹幕') {
      DWLive.openBarrage(true)
      console.log('websdk log: DWLive.openBarrage', true)
      $('.toggle-barrage').text('关闭弹幕')
    } else {
      DWLive.openBarrage(false)
      console.log('websdk log: DWLive.openBarrage', false)
      $('.toggle-barrage').text('打开弹幕')
    }
  })

  // 隐藏控制条
  $('.toggle-control').click(function (e) {
    if (e.target.innerText === '隐藏控制条') {
      DWLive.showControl(false)  // 隐藏控制条
      console.log('websdk log: DWLive.showControl', false)
      $('.toggle-control').text('显示控制条')
    }else {
      DWLive.showControl(true)  // 显示控制条
      console.log('websdk log: DWLive.showControl', true)
      $('.toggle-control').text('隐藏控制条')
    }
  })

  // 接收公聊
  DWLive.onPublicChatMessage = function (j, type) {
    console.log('websdk log: DWLive.onPublicChatMessage', j, type)
    var o = JSON.parse(j)
    var d = ''
    var _time = 0
    if (type !== 'live')  _time = 3000
    setTimeout(() => {
      if (o.userid != DWLive.viewerid) {
        d += '<li status="' + o.status + '" chatId="' + o.chatId + '" class="clearfix" name="' + o.username + '" uid="' + o.userid + '">'
      } else {
        d += '<li  class="clearfix" name="' + o.username + '" uid="' + o.userid + '">'
      }
      d += '<div class="peo-infos">'
        + '<p class="peo-names">'
        + '<span class="p-n-names"><span class="name-tip">' + o.username + '</span></span>'
        + '<i class="peo-icons "></i>'
        + '</p>'
        + '<div class="hide" name="tipBtn">'
        + '<ul class="btnul">'
        + '<li name="whisper">私聊</li>'
        + '</ul>'
        + '<span class="btnul-arrow"></span>'
        + '</div>'
        + '</div>'
        + '<div class="peo-chat">'
        + '<i class="icons"></i>'
        + '<p class="chat-content">' + showEm(o.msg) + '</p>'
        + '</div>'
        + '</li>'
      if (o.groupId == DWLive.groupId || !o.groupId || !DWLive.groupId) {
        $('#chat-list').append(d)
  
        $('#chat-list').parent().scrollTop($('#chat-list').height())
  
        if (o.userid === DWLive.viewerid) {
          $('#chat-list li[uid = ' + o.userid + ']').addClass('me')
        }
        if (type == 'live') {
          DWLive.barrage(o.msg) // 发送弹幕
          console.log('websdk log: DWLive.barrage', o.msg)
        }
      } 
    }, _time);
  }

  DWLive.onSilenceUserChatMessage = function (o) {
    console.log('websdk log: DWLive.onSilenceUserChatMessage', o)
    var d = ''
    var _time = 0
    setTimeout(() => {
      if (o.userid != DWLive.viewerid) {
        d += '<li status="' + o.status + '" chatId="' + o.chatId + '" class="clearfix" name="' + o.username + '" uid="' + o.userid + '">'
      } else {
        d += '<li  class="clearfix" name="' + o.username + '" uid="' + o.userid + '">'
      }
      d += '<div class="peo-infos">'
        + '<p class="peo-names">'
        + '<span class="p-n-names"><span class="name-tip">' + o.username + '</span></span>'
        + '<i class="peo-icons "></i>'
        + '</p>'
        + '<div class="hide" name="tipBtn">'
        + '<ul class="btnul">'
        + '<li name="whisper">私聊</li>'
        + '</ul>'
        + '<span class="btnul-arrow"></span>'
        + '</div>'
        + '</div>'
        + '<div class="peo-chat">'
        + '<i class="icons"></i>'
        + '<p class="chat-content">' + showEm(o.msg) + '</p>'
        + '</div>'
        + '</li>'
      if (o.groupId == DWLive.groupId || !o.groupId || !DWLive.groupId) {
        $('#chat-list').append(d)
  
        $('#chat-list').parent().scrollTop($('#chat-list').height())
  
        if (o.userid === DWLive.viewerid) {
          $('#chat-list li[uid = ' + o.userid + ']').addClass('me')
        }
      } 
    }, _time);
  }

  //隐藏部分聊天
  DWLive.onPublicChatLogManage = function (j) {
    console.log('websdk log: DWLive.onPublicChatLogManage', j)
    var chatInfo = JSON.parse(j)
    var status = chatInfo.status
    var chatIds = chatInfo.chatIds
    $.each(chatIds, function (index, data) {
      var cId = '[chatId=' + data + ']'
      $(cId).attr('status', status)
    })
    $('#chat-list').parent().scrollTop($('#chat-list').height())
  }
  // 接收私聊
  DWLive.onPrivateChatMessage = function (j) {
    console.log('websdk log: DWLive.onPrivateChatMessage', j)
    var o = JSON.parse(j)
    var d = '<li name="' + o.fromusername + '">'
      + '<div class="peo-infos">'
      + '<p class="peo-names">'
      + '<span class="p-n-names">' + o.fromusername + '&nbsp;对&nbsp;' + o.tousername + '&nbsp;说</span>'
      + '<i class="peo-icons "></i>'
      + '</p>'
      + '</div>'
      + '<div class="peo-chat">'
      + '<i class="icons"></i>'
      + '<p class="chat-content pchat">' + showEm(o.msg) + '</p>'
      + '</div>'
      + '</li>'
    $('#chat-list').append(d)

    $('#chat-list').parent().scrollTop($('#chat-list').height())

    if (o.fromusername == DWLive.viewername) {
      $('#chat-list li[name = ' + o.fromusername + ']').addClass('me')
    }
  }

  // 接收私聊回复
  DWLive.onPrivateAnswer = function (j) {
    console.log('websdk log: DWLive.onPrivateAnswer', j)
    var o = JSON.parse(j)
    var d = '<li name="' + o.fromusername + '">'
      + '<div class="peo-infos">'
      + '<p class="peo-names">'
      + '<span class="p-n-names">' + o.fromusername + '&nbsp;对&nbsp;' + o.tousername + '&nbsp;说</span>'
      + '<i class="peo-icons "></i>'
      + '</p>'
      + '</div>'
      + '<div class="peo-chat">'
      + '<i class="icons"></i>'
      + '<p class="chat-content pchat">' + showEm(o.msg) + '</p>'
      + '</div>'
      + '</li>'
    $('#chat-list').append(d)

    $('#chat-list').parent().scrollTop($('#chat-list').height())

  }


  // 发布问题
  DWLive.onQaPublish = function (data) {
    console.log('websdk log: DWLive.onQaPublish', data)
    var $q = $('#' + data.value.questionId).attr('isPublish', '1')
    // 只看自己的问答
    if (!$('#only-me i').hasClass('active')) {
      $q.show()
    }
  }
  var isBanChat = false;
  DWLive.onBanChat = function (j) {
    console.log('websdk log: DWLive.onBanChat', j)
    console.log('您已被禁言' + JSON.stringify(j))
    isBanChat = true;
  }
  DWLive.onUnBanChat = function (j) {
    console.log('websdk log: DWLive.onUnBanChat', j)
    console.log('您已解禁' + j)
    isBanChat = false;
  }
  // 提问
  DWLive.onQuestion = function (j) {
    console.log('websdk log: DWLive.onQuestion', j)
    var o = JSON.parse(j)
    console.log('-提问------提问---提问--', o)
    var qid = o.value.id
    var question = o.value
    // 提问者
    var isFromMe = Viewer.isMe(o.value.userId)
    // 只看自己的问答
    var isOnlyMyOwnQas = $('#only-me i').hasClass('active')
    // 问题已发布
    var isPublish = o.value.isPublish === 1

    var d = '<li id="' + qid + '" isPublish self>'
      + '<div class="peo-infos">'
      + '<p class="peo-names">'
      + '<span class="p-n-names">' + o.value.userName + '</span>'
      + '<i class="peo-icons"></i>'
      + '</p>'
      + '</div>'
      + '<div class="peo-chat">'
      + '<p class="chat-content">' + o.value.content + '</p>'
      + '</div>'
      + '</li>'
    if (question.groupId == DWLive.groupId || !question.groupId || !DWLive.groupId) {
      $('#question-main').append(d)
      $('#question-main').parent().scrollTop($('#question-main').height())

      if (o.value.userName !== DWLive.viewername) {
        $('#' + qid).addClass('not-mines')
      }
    }
  }

  // 接收回答
  DWLive.onAnswer = function (j) {
    console.log('websdk log: DWLive.onAnswer', j)
    console.log(j)
    var o = JSON.parse(j)
    var answer = o.value
    // 私密回答只能自己看
    if (answer.questionUserId !== DWLive.viewerid && answer.isPrivate) {
      return
    }
    var qid = o.value.questionId
    var d = '<div class="peo-repeat">'
      + '<p class="teacher-name">'
      + '<i></i>' + o.value.userName + ''
      + '</p>'
      + '<p class="repeat-content">' + o.value.content + '</p>'
      + '</div>'
    if (answer.groupId == DWLive.groupId || !answer.groupId || !DWLive.groupId) {
      $('#' + qid).append(d).show()
      $('#question-main').parent().scrollTop($('#question-main').height())
    }
  }

  var winPrizeTime
  var lotteryId
  DWLive.onLottery = function (data) {
    console.log('websdk log: DWLive.onLottery', data)
    console.log('onLottery-->', data)
    $('.luckey-draw').hide()
    $('.win-prize-box').hide()
    $('.luckey-draw-result').hide()
    $('.submit-msg-tips').hide()
    if (data.haveLottery) { // 有抽奖
      lotteryId = data.lotteryId
      if (data.lotteryStatus === 0) { // 抽奖ing
        $('.luckey-draw').show()
        $('.luckey-drawing').show()
      } else if (data.lotteryStatus === 1) { // 取消抽奖
        $('.luckey-draw').show()
        $('.luckey-drawing').hide()
        $('.tips-box-start').show()
      } else if (data.lotteryStatus === 2) { // 抽奖结束
        $('.luckey-draw-result').show()
        $('.luckey-drawing').hide()
        if (data.LotteryWinInfo.isWinner) {  // 中奖
          winPrizeTime = new Date()
          luckeyCloseLock = true
          $('.luckey-draw').show()
          $('.win-prize-box').show()
          $('.not-win-prize-box').hide()
          $('.luckey-draw-result').show()
          $('.win-name').html(data.LotteryWinInfo.ownUserInfo.userName)
          $('.win-prize-num').html(data.LotteryWinInfo.ownUserInfo.prizeCode)
          if (data.LotteryWinInfo.LotteryCollectTemplate.length === 0) {
            luckeyCloseLock = false
            $('.luckey-draw-btn').hide().html('提交')
            $('.cash-prize-tips').hide()
          } else {
            $('.luckey-draw-btn').show().html('提交')
            $('.cash-prize-tips').show()
          }
          var submmitHTML = data.LotteryWinInfo.LotteryCollectTemplate.map(function (item, index) {
            var itemHTML
            if (index === 0) {
              itemHTML = '<li class="collect-list" collect-id="' + item.index + '"><label for="name">' + item.title + '</label><input  type="text" placeholder="请输入姓名"></li>'
            } else if (index === 1) {
              itemHTML = '<li class="collect-list" collect-id="' + item.index +  '"><label for="name">' + item.title + '</label><input  type="text" placeholder="请输入电话号码"></li>'
            } else {
              itemHTML = '<li class="collect-list" collect-id="' + item.index +  '"><label for="name">' + item.title + '</label><input  type="text" placeholder="' + item.tips + '"></li>'
            }
            return itemHTML
          }).join('')
          $('.luckey-draw-info-list').html(submmitHTML)
        } else { // 未中奖
          $('.luckey-draw').show()
          $('.win-prize-box').hide()
          $('.not-win-prize-box').show()
          $('.no-win-name').html('【'+HTMLEncode2(data.LotteryWinInfo.LotteryPrize.name)+'】')
        }
        var userInfoHTML = data.LotteryWinInfo.LotteryUserInfo.map(function (item) {
          var itemHTML = '<li> <div><img src="https://view.csslcloud.net/images/lottery-v2/normal.png"></div> <p>' + item.userName + '</p></li>'
          return itemHTML
        }).join('')
        $('.win-prize-list').html(userInfoHTML)
      }
    } else { // 没有抽奖，隐藏抽奖相关
      $('#luckey-draw').hide()
    }
  }

  $('#getChatTop').bind('click', function () {
    DWLive.hdGetChatTop(function (res) {
      console.log('websdk log: DWLive.hdGetChatTop', res)
      console.log('当前聊天置顶消息---', res)
    })
    DWLive.getChatTop({
      success: (res) => {
        console.log('websdk log: DWLive.getChatTop', res)
      }
    })
  })

  DWLive.onChatTop = function (data) {
    console.log('聊天置顶---', data)
  }

  DWLive.onChatTopCancel = function (data) {
    console.log('取消聊天置顶---', data)
  }

  // v2.0中奖提交用户信息
$('.luckey-draw-btn').click(function () {
  var collectList = $('.luckey-draw-info-list li')
  var collectData = []
  for (var i = 0; i < collectList.length; i++) {
    var collectIndex = collectList.eq(i).attr('collect-id')
    var collectValue = collectList.eq(i).find('input').val()
    collectData.push({
      index: collectIndex * 1,
      value: collectValue
    })
  }
  var collectBody = {
    lotteryId: lotteryId,
    collectInfos: collectData
  }
  var nowTime = new Date()
  var reduceTime = nowTime - winPrizeTime;
  if(reduceTime > 1800000){
    $('.luckey-draw-success').hide()
    $('.luckey-draw-error').show()
    $('.luckey-draw-error p').html('已超过提交时间，提交失败')//已超过提交时间，提交失败
    luckeyCloseLock = false;
    setTimeout( function() {
      $('.luckey-draw-error').hide()
    }, 1000);
  }else{
    DWLive.commitLottery(collectBody, {
      onSuccess: function (result) {
        $('.luckey-draw').hide()
        console.log('websdk log: DWLive.commitLottery:: reuslt',collectBody, result)
      },
      onError: function (err) {
        $('.submit-msg-tips').show()
        $('.submit-msg-tips').html(err)
        console.log('websdk log: DWLive.commitLottery:: err', err)
      }
    })
  }
})


  // 中奖名单
var isWinPrize = false;
$('.winning-list-title').click(function(){
  isWinPrize = !isWinPrize;
  if(isWinPrize){//展开
    $('.winning-list-icon').removeClass('down')
    $('.winning-list-icon').addClass('up')
    $('.win-prize-wrap').removeClass('win-prize-min-height')
  }else{//收起
    $('.winning-list-icon').removeClass('up')
    $('.winning-list-icon').addClass('down')
    $('.win-prize-wrap').addClass('win-prize-min-height')
  }
})

//抽奖结果关闭
var luckeyCloseLock = false; //点击关闭提示弹窗
$('.luckey-draw-result-close').click(function (){
  if(luckeyCloseLock){
    //展示弹窗
    $('.tips-box-end').show()
    $('.tips-end-desc').html('您还没有提交信息，确定要关闭么？')//您还没有提交信息，确定要关闭么？
    $('.tips-cancel').show()
  }else{
    //不展示弹窗
    $('.luckey-draw').hide()
  }
})
//抽奖中关闭
$('.luckey-drawing-close').click(function (){
  $('.luckey-draw').hide()
})

//抽奖中提示弹窗确定按钮
$('.tips-start-submit').click(function(){
  $('.tips-box-start').hide()
  $('.luckey-draw').hide()
})
//抽奖提示弹窗确定按钮
$('.tips-end-submit').click(function(){
  $('.tips-box-end').hide()
  $('.luckey-draw').hide()
})
//抽奖提示弹窗取消按钮
$('.tips-cancel').click(function(){
  $('.tips-box-end').hide()
})

  function HTMLEncode2(html) {
    var temp = document.createElement("div");
    (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
    var output = temp.innerHTML;
    temp = null;
    return output;
  }

  // <li> <div><img src="/images/lottery-v2/normal.png"></div> <p>131</p></li>
  $('.luckey-drawing-close').on('click', function () {
    $('#luckey-draw').hide()
    $('#luckey-drawing').hide()
  })

  var Viewer = {
    isMe: function (viwerId) {
      return viwerId === DWLive.viewerid
    }
  }
  //随堂测功能
  DWLive.onPracticePublishStop = function (data) {
    console.log('onPracticePublishStop 随堂测停止答题', data)
    DWLive.getPracticeRanking(data.practiceId, (data) => {
      console.log('随堂测排行数据', data)
    })
  }
  //关闭随堂测功能
  DWLive.onPracticeClose = function (data) {
    console.log('onPracticeClose 随堂测关闭', data)
    DWLive.getPracticeStatisInfo(data.practiceId, (data) => {
      console.log('随堂测统计数据', data)
    })
  }
  //发布随堂测功能
  DWLive.onPracticePublish = function (j) {
    console.log('websdk log: DWLive.onPracticePublish', j)
    console.log('接收到随堂测发布消息', j)
    var pid = j.practiceId
    // 获取随堂测信息
    DWLive.getPracticeInfomation(pid, function(data) {
      console.log('websdk log: DWLive.getPracticeInfomation', pid, data)
      console.log('获取随堂测信息', data)
      $('#practiceId').val(data.datas.practice.id)
      $('#practiceOpt').val(data.datas.practice.options[0].id)
    })
  }
  // 发布奖杯
  DWLive.onPrizeSend = function (j) {
    console.log('奖杯信息：' + j)
  }
  // 禁言
  DWLive.onInformation = function (j) {
    console.log('websdk log: DWLive.onInformation', j)
    var chat = $('#chat-content'),
      chatX = chat.offset().left,
      chatY = chat.offset().top
    tips(chatX, chatY, '您已经被禁言！')
    return
  }

  var chatTime = 0

  function chatSend() {
    if (chatTime > 0) {
      return
    } else {
      chatTime = 10
      var t = setInterval(function () {
        $('#chat-submit').html(chatTime)
        chatTime--
        if (chatTime <= 0) {
          $('#chat-submit').html('发送')
          clearInterval(t)
        }
      }, 1000)
    }

    var msg = Tools.formatContent($.trim($('#chat-content').val()))

    // DWLive.barrage(msg)
    DWLive.docBarrage(msg)
    console.log('websdk log: DWLive.docBarrage', msg)
    if (!msg) {
      var chat = $('#chat-content'),
        chatX = chat.offset().left,
        chatY = chat.offset().top
      tips(chatX, chatY, '请输入您的聊天内容！')
      return
    }

    if (msg.length > DWLive.chatCharLimit) {
      tips(chatX, chatY, `聊天不能超过${ DWLive.chatCharLimit }个字符`)
      return
    }

    var nmsg = ''
    $.each(msg.split(' '), function (i, n) {
      var ur = /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
      if (ur.test(n)) {
        nmsg += '[uri_' + n + '] '
      } else {
        nmsg += n + ' '
      }
    })

    var touserid = $('.select-current').attr('id')
    var tousername = $('.select-current').html()
    if (touserid == 'all') {
      DWLive.sendPublicChatMsg(nmsg) // 发送公聊
    } else {
      DWLive.sendPrivateChatMsg(touserid, tousername, nmsg) // 发送私聊
    }

    $('#chat-content').val('').focus()
  }

  $('#chat-submit').click(function () {
    chatSend()
  })

  $('#chat-content').bind('keypress', function (e) {
    if (e.keyCode == 13) {
      chatSend()
    }
  })

  function qaSend() {
    var qa = $('#question-content')
    var msg = Tools.formatContent($.trim(qa.html()))
    var chatX = qa.offset().left,
      chatY = qa.offset().top

    if (!msg) {
      tips(chatX, chatY, '请输入您的问题！')
      return
    }
    if (msg.length > 300) {
      tips(chatX, chatY, '问题不能超过300个字符')
      return
    }
    console.log('当前的引用对象msg-->' + msg)

    DWLive.sendQuestionMsg(msg) // 发送问题
    console.log('websdk log: DWLive.sendQuestionMsg', msg)

    $('#question-content').html('').focus()
  }

  $('#question-submit').click(function () {
    qaSend()
  })

  $('#question-content').bind('keypress', function (e) {
    if (e.keyCode == 13) {
      qaSend()
    }
  })

  function tips(chatX, chatY, msg) {
    $('#input-tips').find('p').text(msg)
    $('#input-tips').css({ 'left': chatX, 'top': (chatY - 42) }).stop(1, 1).fadeIn(200).delay(1500).fadeOut(200)
  }

  //对老师说
  $('#to-teacher').bind('click', function () {
    if (!$(this).find('i').hasClass('active')) {
      $(this).find('i').addClass('active')
      $(this).attr('for', 'teacher')
    } else {
      $(this).find('i').removeClass('active')
      $(this).attr('for', 'all')
    }
  })


  // 私聊
  $('#private-name').on('click', 'li', function () {
    var uid = $(this).attr('id'),
      uname = $(this).html()
    $('.select-current').attr('id', uid).html(uname)
  })

  $('.select-span').click(function (e) {
    $('#private-name').toggle()
    $(this).toggleClass('select-active')
    $(document).one('click', function () {
      $('#private-name').hide()
      $('.select-span').removeClass('select-active')
    })
    e.stopPropagation()
  })

  $('#btn-clip').click(function (e) {
    var base = DWLive.clipVideoImage()
    console.log('websdk log: DWLive.clipVideoImage')
    // var url = URL.createObjectURL(dataURLtoBlob(base))
    $('#clipImage_down').attr('href', base)
    $('#clipImage_down')[0].click()

  })

  function dataURLtoBlob(dataurl) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new Blob([u8arr], { type: mime })
  }

  // 聊天页面浮出框
  $(document).on('click', 'ul[id="chat-list"] li span.name-tip', function (e) {
    var $thisTipBtn = $(this).parent().parent().next('div[name="tipBtn"]')
    if (!$thisTipBtn.is(':hidden')) {
      $thisTipBtn.hide()
      e.stopPropagation()
      return
    }

    $('div[name="tipBtn"]').hide()
    var $li = $(this).parent().parent().parent().parent()
    if (!$li.find('div[name="tipBtn"]').length) {
      return
    }

    if ($(this).html() == DWLive.viewername) {
      return
    }

    $li.find('div[name="tipBtn"]').show()
    $(document).one('click', function () {
      $('div[name="tipBtn"]').hide()
    })

    e.stopPropagation()
  })

  // 点击浮出屏蔽按钮
  $(document).on('click', 'ul[id="chat-list"] li li', function () {
    var $t = $(this)
    var action = $t.attr('name')
    var $li = $(this).parent().parent().parent().parent()
    var uid = $li.attr('uid')
    var username = $li.find('.name-tip').html()

    tipBtnHanle(action, uid, username)

    $('div[name="tipBtn"]').hide()
  })

  function tipBtnHanle(action, uid, uname) {
    if (action == 'whisper') {
      $('#private-name').prepend('<li id="' + uid + '" title="' + uname + '">' + uname + '</li>')
      $('.select-current').attr('id', uid).html(uname)
      $('#chat-content').focus()

      var option = $('#private-name').find('li[id="' + uid + '"]')
      if (option.length > 1) {
        option.eq(0).remove()
      }

    }
  }

  // 签到
  DWLive.onStartRollCall = function (data) {
    console.log('websdk log: DWLive.onStartRollCall', data)
    if (window.ROLLCALL_INTERVAL_TIMER > 0) {
      clearInterval(window.ROLLCALL_INTERVAL_TIMER)
      window.ROLLCALL_INTERVAL_TIMER = -1
    }

    var rid = data.rollcallId,
      pid = data.publisherId,
      time = data.duration
    $('.signtxt').html('签到倒计时: <span id="signtime">00:00</span>').css('margin-top', '75px')
    $('.signbtn button').show()
    $('#signtime').text(timeFormat(time))
    $('.sign').show()
    $('.signbtn button').click(function () {
      DWLive.answerRollcall(rid, pid)
      console.log('websdk log: DWLive.answerRollcall', rid, pid)
      $('.sign').hide()

      if (window.ROLLCALL_INTERVAL_TIMER > 0) {
        clearInterval(window.ROLLCALL_INTERVAL_TIMER)
        window.ROLLCALL_INTERVAL_TIMER = -1
      }
    })

    window.ROLLCALL_INTERVAL_TIMER = setInterval(function () {
      if (time > 1) {
        time--
        $('#signtime').text(timeFormat(time))
      } else {
        $('.signtxt').html('签到结束').css('margin-top', '90px')
        $('.signbtn button').hide()
        setTimeout(function () {
          $('.sign').hide()
        }, 2000)
        if (window.ROLLCALL_INTERVAL_TIMER > 0) {
          clearInterval(window.ROLLCALL_INTERVAL_TIMER)
          window.ROLLCALL_INTERVAL_TIMER = -1
        }
      }
    }, 1000)
  }

  function timeFormat(time) {
    var t = parseInt(time),
      h, i, s
    h = Math.floor(t / 3600)
    h = h ? (h + ':') : ''
    i = h ? Math.floor(t % 3600 / 60) : Math.floor(t / 60)
    s = Math.floor(t % 60)
    i = i > 9 ? i : '0' + i
    s = s > 9 ? s : '0' + s
    return (h + i + ':' + s)
  }


  // 开始抽奖
  var win = false,
    stop = false
  DWLive.onStartLottery = function (data) {
    console.log('websdk log: DWLive.onStartLottery', data)
    stop = false
    $('.lottery').show()
    $('.lotterybox').hide()
    if (win == true) {
      $('.lotteryh3').html('恭喜您中奖啦')
    } else {
      $('.lotteryh3').html('正在抽奖')
    }
  }

  // 中奖
  DWLive.onWinLottery = function (data) {
    console.log('websdk log: DWLive.onWinLottery', data)
    var code = data.lotteryCode,
      name = data.viewerName
    if (data.viewerId == DWLive.viewerid) {
      $('.lotterynum').html(code)
      $('.lotterybox').hide()
      $('.lotterynum, .lotterytext, .lottery').show()
      $('.lotteryh3').html('恭喜您中奖啦')
      $('.lotterynum, .lotterytext').css('z-index', 9)
      win = true
    } else {
      $('.lotteryname').html(name)
      $('.lotterybox, .lottery').show()
      if (win == true) {
        $('.lotteryh3').html('恭喜您中奖啦')
      } else {
        $('.lotteryh3').html('哎呀，就差一点')
      }
      setTimeout(function () {
        $('.lotterybox').hide()
        if (win == true) {
          $('.lotteryh3').html('恭喜您中奖啦')
        } else {
          $('.lotteryh3').html('正在抽奖')
        }
      }, 2000)
    }
  }

  $('.lotteryclose').click(function () {
    if (win == true) {
      if (stop == true) {
        $('.lottery').hide()
      }
      $('.lotterynum, .lotterytext').hide()
      $('.lotteryh3').html('正在抽奖')
      win = false
    } else {
      $('.lottery').hide()
    }
  })

  // 结束抽奖
  DWLive.onStopLottery = function (data) {
    console.log('websdk log: DWLive.onStopLottery', data)
    stop = true
    if (win == false) {
      setTimeout(function () {
        $('.lottery').hide()
      }, 2000)
    }
  }


  // 答题
  var option = ''
  window.voteOptions = []
  DWLive.onStartVote = function (data) {
    console.log('websdk log: DWLive.onStartVote', data)
    option = ''
    window.voteOptions = []

    var voteid = data.voteId
    var pid = data.publisherId
    var count = data.voteCount
    var voteType = data.voteType

    if (count == 5) {
      $('.vote-option').append('<li>A<span></span></li><li>B<span></span></li><li>C<span></span></li><li>D<span></span></li><li>E<span></span></li>')
      $('.vote-option').css('padding-left', 10)
      $('.vote-option li').css('margin-right', 7)
      $('.vote-option li:last-child').css('margin-right', 0)
      $('#vote1').append('<div class="votebtn"><button>提交</button></div>')
    }
    if (count == 4) {
      $('.vote-option').append('<li>A<span></span></li><li>B<span></span></li><li>C<span></span></li><li>D<span></span></li>')
      $('.vote-option').css('padding-left', 28)
      $('.vote-option li').css('margin-right', 15)
      $('#vote1').append('<div class="votebtn"><button>提交</button></div>')
    }
    if (count == 3) {
      $('.vote-option').append('<li>A<span></span></li><li>B<span></span></li><li>C<span></span></li>')
      $('.vote-option').css('padding-left', 50)
      $('.vote-option li').css('margin-right', 25)
      $('#vote1').append('<div class="votebtn"><button>提交</button></div>')
    }
    if (count == 2) {
      $('.vote-option').append('<li>A<span></span></li><li>B<span></span></li>')
      $('.vote-option li').eq(0).addClass('vote-r')
      $('.vote-option li').eq(1).addClass('vote-w')
      $('.vote-option').css('padding-left', 80)
      $('.vote-option li').css('margin-right', 40)
      $('#vote1').append('<div class="votebtn"><button>提交</button></div>')
    }
    $('.vote-option li:last-child').addClass('last-item')
    $('.vote').show()
    $('#vote1').show()
    $('#vote2').hide()
    $('.vote-list').empty()

    $('.vote-option li').click(function () {
      var index = $('.vote-option li').index(this)

      if (voteType == 1) { // 多选题
        if ($(this).hasClass('active')) {
          $(this).removeClass('active')
        } else {
          $(this).addClass('active')
        }

        window.voteOptions = []
        $('.vote-option li').each(function (index, data) {
          if ($(data).hasClass('active')) {
            window.voteOptions.push(index)
          }
        })
      } else {
        $(this).addClass('active').siblings().removeClass('active')
      }

      $('.votebtn button').css('opacity', 1)
      option = index
    })
    $('.votebtn button').click(function () {
      if ($('.vote-option li').hasClass('active')) {
        if (voteType == 1) { // 多选题
          DWLive.replyVote(voteid, voteOptions, pid)
          console.log('websdk log: DWLive.replyVote', voteid, voteOptions, pid)
        } else {
          DWLive.replyVote(voteid, option, pid)
          console.log('websdk log: DWLive.replyVote', voteid, option, pid)
        }

        $('.vote').hide()
        $('.vote-option').empty()
        $('.votebtn button').css('opacity', 0.6)
        $('.votebtn').remove()
      }
    })
    $('.vote-close').click(function () {
      $('.vote').hide()
      $('.vote-option, .vote-list').empty()
      $('.votebtn button').css('opacity', 0.6)
      option = ''
      window.voteOptions = []
      $('.votebtn').remove()
    })
  }

  // 结束答题
  DWLive.onStopVote = function (data) {
    console.log('websdk log: DWLive.onStopVote', data)
    $('.vote').hide()
    $('.vote-option, .vote-list').empty()
    $('.votebtn button').css('opacity', 0.6)
    $('.votebtn').remove()
  }

  // 答题统计
  DWLive.onVoteResult = function (data) {
    console.log('websdk log: DWLive.onVoteResult', data)
    // 选项个数
    var count = data.voteCount
    // 正确答案 数字或者数组
    var coption = data.correctOption
    // 多选题目
    var isMultiple = !((typeof coption) === 'number')
    // 判断题目
    var isJudge = count == 2

    var statisics = data.statisics
    var zcount = ''

    // 总回答人数
    var answerCount = data.answerCount

    // 正确内容
    var correctAnswer = ''
    var selfAnswer = ''

    if (isMultiple) {
      var ccs = []
      $.each(coption, function (index, o) {
        if (o >= 0 && o < 26) {
          ccs.push(String.fromCharCode(65 + o))
        }
      })
      correctAnswer = ccs.join(',')

      var ccs2 = []
      $.each(voteOptions, function (index, o) {
        if (o >= 0 && o < 26) {
          ccs2.push(String.fromCharCode(65 + o))
        }
      })
      selfAnswer = ccs2.join(',')
    } else if (isJudge) {
      if (coption == 0) {
        correctAnswer = '<i class="vote-ricon"></i>'
      } else if (coption == 1) {
        correctAnswer = '<i class="vote-wicon"></i>'
      }

      if ($.isNumeric(option) && option == 0) {
        selfAnswer = '<i class="vote-ricon"></i>'
      } else if ($.isNumeric(option) && option == 1) {
        selfAnswer = '<i class="vote-wicon"></i>'
      }
    } else {
      if ($.isNumeric(coption) && coption >= 0 && coption < 26) {
        correctAnswer = String.fromCharCode(65 + coption)
      }
      if ($.isNumeric(option) && option >= 0 && option < 26) {
        selfAnswer = String.fromCharCode(65 + option)
      }
    }

    switch (count) {
      case 2:
        var li = '<li>'
          + '<span class="spanl">√:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[0].count + '人</span>'
          + '<span>(' + statisics[0].percent + '%)</span>'
          + '</li>'
          + '<li class="last-item">'
          + '<span class="spanl">X:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[1].count + '人</span>'
          + '<span>(' + statisics[1].percent + '%)</span>'
          + '</li>'
        $('.vote-list').append(li)
        zcount = statisics[0].count + statisics[1].count
        break
      case 3:
        var li = '<li>'
          + '<span class="spanl">A:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[0].count + '人</span>'
          + '<span>(' + statisics[0].percent + '%)</span>'
          + '</li>'
          + '<li>'
          + '<span class="spanl">B:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[1].count + '人</span>'
          + '<span>(' + statisics[1].percent + '%)</span>'
          + '</li>'
          + '<li class="last-item">'
          + '<span class="spanl">C:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[2].count + '人</span>'
          + '<span>(' + statisics[2].percent + '%)</span>'
          + '</li>'
        $('.vote-list').append(li)
        zcount = statisics[0].count + statisics[1].count + statisics[2].count
        break
      case 4:
        var li = '<li>'
          + '<span class="spanl">A:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[0].count + '人</span>'
          + '<span>(' + statisics[0].percent + '%)</span>'
          + '</li>'
          + '<li>'
          + '<span class="spanl">B:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[1].count + '人</span>'
          + '<span>(' + statisics[1].percent + '%)</span>'
          + '</li>'
          + '<li>'
          + '<span class="spanl">C:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[2].count + '人</span>'
          + '<span>(' + statisics[2].percent + '%)</span>'
          + '</li>'
          + '<li class="last-item">'
          + '<span class="spanl">D:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[3].count + '人</span>'
          + '<span>(' + statisics[3].percent + '%)</span>'
          + '</li>'
        $('.vote-list').append(li)
        zcount = statisics[0].count + statisics[1].count + statisics[2].count + statisics[3].count
        break
      case 5:
        var li = '<li>'
          + '<span class="spanl">A:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[0].count + '人</span>'
          + '<span>(' + statisics[0].percent + '%)</span>'
          + '</li>'
          + '<li>'
          + '<span class="spanl">B:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[1].count + '人</span>'
          + '<span>(' + statisics[1].percent + '%)</span>'
          + '</li>'
          + '<li>'
          + '<span class="spanl">C:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[2].count + '人</span>'
          + '<span>(' + statisics[2].percent + '%)</span>'
          + '</li>'
          + '<li>'
          + '<span class="spanl">D:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[3].count + '人</span>'
          + '<span>(' + statisics[3].percent + '%)</span>'
          + '</li>'
          + '<li class="last-item">'
          + '<span class="spanl">E:</span>'
          + '<div class="vote-bar">'
          + '<div class="vote-in"></div>'
          + '</div>'
          + '<span class="color1">' + statisics[4].count + '人</span>'
          + '<span>(' + statisics[4].percent + '%)</span>'
          + '</li>'
        $('.vote-list').append(li)
        zcount = statisics[0].count + statisics[1].count + statisics[2].count + statisics[3].count + statisics[4].count
        break
    }

    for (var i = 0; i < statisics.length; i++) {
      $('.vote-in').eq(i).css('width', statisics[i].percent * 1.2)
    }

    if (isMultiple) {
      // 为了兼容老客户端，单选题和判断的回答人数扔计算得出
      zcount = answerCount
    }

    $('#vote-count').html(zcount)
    // 自己选择的答案
    $('#yansw em').html(selfAnswer)
    // 正确答案
    $('#cansw em').html(correctAnswer)

    if ($('#yansw em').html() == $('#cansw em').html()) {
      $('#yansw').addClass('vote-righta')
    } else {
      $('#yansw').removeClass('vote-righta')
    }

    $('.vote').show()
    $('#vote2').show()
    $('#vote1').hide()
    $('.votebtn').remove()

    option = ''
    window.voteOptions = []
    $('.vote-close').click(function () {
      $('.vote').hide()
      $('.vote-list').empty()
      option = ''
      window.voteOptions = []
    })
  }


  // 请求互动
  $('li[name="interaction"]').click(function (e) {
    var $t = $(this)
    if ($t.hasClass('disable')) {
      e.preventDefault()
      return
    }

    var type = $t.attr('t')
    var isVideo = type == 'video'
    var isAudio = !isVideo
    var $a = $t.find('a')

    if ($a.hasClass('applying')) {
      return
    }

    if (window.VIDEO_INTERACTION_MSG_TIME_OUT >= 0) {
      clearTimeout(window.VIDEO_INTERACTION_MSG_TIME_OUT)
      $('#videoInteraction .call-tit').text('')
    }

    if (window.INTERACTION_MSG_TIME_OUT >= 0) {
      clearTimeout(window.INTERACTION_MSG_TIME_OUT)
      $('#interactionMsg').text('')
    }

    // 请求互动
    if ($a.hasClass(type)) {
      $('li[name="interaction"]').addClass('disable').hide()
      $t.removeClass('disable').show()

      $a.removeClass('video audio applying calling').addClass('applying')

      if (isVideo) {
        isAudio = true

        $('#videoInteraction').show()
        $('#videoInteraction .call-tit').show().text('视频通话申请中...')
      }
      $('#interactionMsg').show().text('通话申请中...')
      var requestInteractionParams = {
        video: isVideo,
        audio: isAudio
      }
      DWLive.requestInteraction(requestInteractionParams)
      console.log('websdk log: DWLive.requestInteraction', requestInteractionParams)
    } else if ($a.hasClass('calling')) { // 挂断互动
      DWLive.hangupInteraction()
      console.log('websdk log: DWLive.hangupInteraction')
      $('li[name="interaction"]').removeClass('disable').show();
      $('li#interactionVideo').find('a').removeClass('applying calling').addClass('video')
      $('li#interactionAudio').find('a').removeClass('applying calling').addClass('audio')
    }
    e.preventDefault()
  })


})


window.ALLOW_SPEAK_INTERACTION = false
DWLive.onRoomSetting = function (data) {
  window.ALLOW_SPEAK_INTERACTION = data.allow_speak_interaction == 'true'
  if (window.RECONNECTING && window.ALLOW_SPEAK_INTERACTION) {
    window.RECONNECTING = false
    return
  }

  if (!live.interaction.isSupportInteraction()) {
    // console.log(!live.interaction.isSupportInteraction());
    return
  }

  var $audioInteraction = $('li[name="interaction"][t="audio"]')
  var $videoInteraction = $('li[name="interaction"][t="video"]')

  $audioInteraction.removeClass('disable').find('a').removeClass('audio applying calling').addClass('audio')
  $videoInteraction.removeClass('disable').find('a').removeClass('video applying calling').addClass('video')

  $('#interactionMsg').text('')

  // 允许语音通话
  if (data.allow_speak_interaction == 'true') {
    $audioInteraction.show()
    $videoInteraction.show()
  } else {
    // 断开语音通话
    DWLive.hangupInteraction()
    console.log('websdk log: DWLive.hangupInteraction')

    $audioInteraction.hide()
    $videoInteraction.hide()
  }
}


function on_cc_live_interaction_interval(p, t) {
  if (t < 0) {
    return
  }

  var s = t % 60
  s = s < 10 ? ('0' + s) : s

  var m = parseInt(t / 60, 10)
  m = m < 10 ? ('0' + m) : m


  $('#interactionMsg').text('通话中 ' + m + ':' + s)
}

/**
 * 获取本地流信息
 * */
function on_cc_live_interaction_local_media(p, stream) {
  // 视频+音频
  if (p.video) {
    $('#videoInteractio').show()
    var $lv = $('#interactionLocalVideo')[0]
    // $lv.src = URL.createObjectURL(stream); // 加载流信息
    $lv.srcObject = stream
    $lv.volume = 0 // 静音
  } else {

  }
}

/**
 * 远程互动流
 *
 * */
window.on_cc_live_interaction_remote_media = function (data) {
  // 订阅远端流
  if (data && data.video) {
    interactionDom = document.getElementById('videoInteractions')
    interactionDom.style.display = 'block'
    let localVideoDom = document.getElementById('videoInteraction')
    localVideoDom.style.display = 'none'
  } 
}

// 断开语音通话
window.on_cc_live_interaction_disconnect = function (data, type) {
  console.log('websdk log: window.on_cc_live_interaction_disconnect', data, type)
  DWLive.hangupInteraction()
  console.log('websdk log: DWLive.hangupInteraction')
  live.interaction.clearCallingTimer()
  live.interaction.disconnectInteraction(data.disconnectid)
  // 订阅远端流
  let interactionDom = document.getElementById('videoInteractions')
  interactionDom.style.display = 'none'
  let localVideoDom = document.getElementById('videoInteraction')
  localVideoDom.style.display = 'none'
  $('li[name="interaction"]').removeClass('disable').show();
  $('li#interactionVideo').find('a').removeClass('applying calling').addClass('video')
  $('li#interactionAudio').find('a').removeClass('applying calling').addClass('audio')
  if (live.interaction.local.type.video) {
    DWLive.livePlayerInit()
  }
}

// 接受语音/连麦互动
window.on_cc_live_interaction_accept = function (p) {
  console.log('websdk log: window.on_cc_live_interaction_accept', p)
  if (p.video) {
    $('li[name="interaction"][t="video"] a').removeClass('audio applying calling').addClass('calling')
    $('#videoInteraction .call-tit').hide()
  } else {
    $('li[name="interaction"][t="audio"] a').removeClass('audio applying calling').addClass('calling')
  }
  $('#interactionMsg').text('通话中...')

  $('#btn-network').addClass('wl-disable')
}



/**
 * 请求互动60s内没有接受，超时断开
 *
 * @param p
 */
function on_cc_live_interaction_request_timeout(p) {
  // 音频通话
  if (p.audio) {
    $('li[name="interaction"][t="audio"] a').removeClass('audio applying calling').addClass('audio')
  }

  // 视频通话
  if (p.video) {
    $('li[name="interaction"][t="video"] a').removeClass('audio applying calling').addClass('video')
    $('#videoInteraction').hide()
  }

  $('#interactionMsg').hide().text('')
  $('li[name="interaction"]').removeClass('disable').show()

  $('#btn-network').removeClass('wl-disable')

  if (!window.ALLOW_SPEAK_INTERACTION) {
    $('li[name="interaction"]').hide()
  }
}

/**
 * 请求互动错误
 * */
function on_cc_live_interaction_error(p, error, errMsg) {

}

/**
 * 取消通话
 * */
function on_cc_live_interaction_cancal(p) {
  // 处理逻辑与超时一致
  on_cc_live_interaction_request_timeout(p)
}

DWLive.onOnlineTeachers = function (data) {
  console.log('websdk log: DWLive.onOnlineTeachers', data)
  $('#private-name li[role="teacher"]').remove()

  for (var i = 0; i < data.teachers.length; i++) {
    var teacher = data.teachers[i]
    $('#private-name').prepend('<li role="teacher" id="' + teacher.id + '">' + teacher.name + '</li>')
  }
}

window.UNLOADMARQUEE = true

function on_cc_swf_loading_completed(id) {
  if (window.UNLOADMARQUEE) {
    window.UNLOADMARQUEE = false
    var marquee = $.trim($('#viewerMarquee').text())
    if (marquee) {
      try {
        var mj = JSON.parse(marquee)

        var isHttps = window.location.protocol === 'https:'
        // 当前页面如果不是https，跑马灯突破地址为https协议，则强制将图片协议改为http
        if (!isHttps && mj && mj.image && mj.image.image_url && mj.image.image_url.indexOf('https:') >= 0) {
          var u = mj.image.image_url
          mj.image.image_url = u.replace(/https:/g, 'http:')
        }

        setTimeout(function () {
          DWLive.showMarquee(JSON.stringify(mj))
        }, 1000)
      } catch (e) {
      }
    }
  }
}

DWLive.onExternalQuestionnairePublish = function (data) {
  console.log('websdk log: DWLive.onExternalQuestionnairePublish', data)
  console.log('第三方问卷:', data)
}



//翻页信息
DWLive.onPageChange = function (data) {
  console.log(data)
}

//改名回调
DWLive.onChangeNickname = function (data) {
  console.log('websdk log: DWLive.onChangeNickname', data)
  console.log(data)
}

//直播防360浏览器录屏
window.onresize = function () {
  //application/vnd.chromium.remoting-viewer 可能为360特有
  var is360 = _mime('type', 'application/vnd.chromium.remoting-viewer')
  if (isChrome() && is360) {
    //alert("检测到是360浏览器");
    var str = document.querySelector('#livePlayer').style.cssText
    var width = $('#livePlayer').width()
    var height = $('#livePlayer').height()
    var winW = $(window).width()
    var winH = $(window).height()

    function getCssText() {
      return $('#livePlayer').css('margin-top') == '0px'
        && $('#livePlayer').css('margin-right') == '0px'
        && $('#livePlayer').css('margin-bottom') == '0px'
        && $('#livePlayer').css('margin-left') == '0px'
    }

    if (width == winW && height == winH && getCssText()) {
      $('body *').remove()
      var fp = '<p style="position: absolute;top: 100px;left: 100px">视频不支持录屏模式下播放<br>请刷新重新观看</p>'
      $('body').append(fp)
      return
    }
    if (str.search('width') != -1 && str.search('height') != -1 && str.search('!important') != -1) {
      $('body *').remove()
      var fp = '<p style="position: absolute;top: 100px;left: 100px">视频不支持录屏模式下播放<br>请刷新重新观看</p>'
      $('body').append(fp)
    }
  }
}