login.login(function (res) {
   $('.main_container').before(topBack(gotoLink.questionList(), '我问现场', res.data.avatar))
 });
    , "success": function (res) {
      if (res.code == 200) {
        var item = res.data;
        handleQuestionData(item);
        answerList = item.answer || [];
        var html = shareTmp('detail_tpl', {
          item: item
        })
        $('.main_container').html(html).before(
          shareTmp('top_back_tpl', {
            link: gotoLink.questionList()
            , name: item.nickname
            , avatar: item.avatar
            , price: item.price
            , has_delete_btn: item.has_delete_btn
            , id: item.id
          })
        );
        $('.img_item.hide_files_info').height($('.img_item.hide_files_info').width() * 150 / 120);
        $('.main_container a[data-preview-image]').click(function(){
          var index = parseInt($(this).data('answer'));
          wx.previewImage({
              current: $(this).data('href'),
              urls: imageList[index],
          });
        });
        $('.ssz-auth').click(function(event) {
          event.preventDefault();
          var id = $(this).data('id');
          if (confirm('确定申请小丫鉴定吗?')) {
            $.post('/news/update', {id: id, type: 'answer', 'confirm_status': 1}, function(data) {
              if (data.code == 200) {
                alert('申请鉴定成功');
                forceReload();
              } else {
                alert('申请鉴定失败');
                console.log(data);
              }
            });
          }
        });
        $('#for_pub_btn').on('click', function () {
          $(this).hide();
          $('#shishi-auth').show();
        })

        $('#ssz-submit').on('click', function (event) {
          if (this.isSub) return;
          /*if (this.innerHTML.length > 2) {
            return false;
          }*/
          if (!confirm("是否确定提交?")) {
            return false;
          }
          if (uploading) {
            alert('正在上传文件,请稍后发布')
            return;
          }
          var _this = this
          var content_img = [];
          $('.other_upload_wrap .upload_item').each(function (i, ele) {
            content_img.push($(ele).find('img').attr('src'))
          });
          var des = $('#ssz-description').val();
          if (des.length > 300) {
            alert('描述不能超过300字');
            return;
          }
          if (des.length == 0) {
            alert("请填写描述部分。");
            return;
          }
          var params = {
            vote_id: $('#for_pub_btn').attr('data-id')
            , type: 'answer'
            , files: content_img.join(',')
            , description: des
          }
          $.ajax({
            url: '/news/orderpayback'
            , type: 'POST'
            , dataType: 'json'
            , data: params
            , beforeSend: function () {
              _this.isSub = true;
              _this.innerHTML = '正在提交'
            }
            , success: function (res) {
              if (res.code == 200) {
                // location.href = gotoLink.inforDetail(res.data.id)
                // alert(res.msg);
              }
            }
            , error: function () {
              alert('系统错误');
            }
            , complete: function () {
              _this.isSub = false;
              _this.innerHTML = '申请信息失实已提交,请等待审核...';
              $('#shishi-container').hide();
              $(_this).css('background-color', '#f8f7f7');
              $(_this).css('color', 'black');
            }
          })
        });
        // var $eles = $('.other_img_wrap.clearfix a');
        // $eles.height($eles.width());
        // $eles.each(function() {
        //   // if ($(this).children('img').height() < $eles.height()) $(this).children('img').height($eles.height());
        //   $(this).children('img').css('clip', 'rect(0,0,' + $eles.width() + 'px,' + $eles.width() + 'px)');
        // });
        // $eles = $('.hide_img_wrap.clearfix a');
        // $eles.height($eles.width());
        // $eles.each(function() {
        //   // if ($(this).children('img').height() < $eles.height()) $(this).children('img').height($eles.height());
        //   $(this).children('img').css('clip', 'rect(0,0,' + $eles.width() + 'px,' + $eles.width() + 'px)');
        // });
        $('.share_btn').click(function(){
          $('#share_page').show();
        });
        $('#share_page').click(function(){
          $('#share_page').hide();
        });
        $('.show_after_loaded').show();
        $('#intro_btn').click(function(){
          $('#intro_page').show();
          $('body').addClass('modal-open');
        });
        $('#intro_page .icon').click(function(){
          $('#intro_page').hide();
          $('body').removeClass('modal-open');
        });

        var activeAudioCell = null;
        $('.audio_cell .symbol').on('PlayStart', function(){
          if (this.timer) {
            clearInterval(this.timer);
          }
          var $symbol = $(this);
          this.timer = setInterval(function(){
            var text = $symbol.text();
            if (text.length < 3) {
              $symbol.text(text+'>');
            } else {
              $symbol.text('>');
            }
          }, 400);
        }).on('PlayStop', function(){
          if (this.timer) {
            clearInterval(this.timer);
            this.timer = null;
          }
          $(this).text('>>>');
        });

        $('.audio_cell audio').on('play', function(){
          $(this).siblings('.symbol').trigger('PlayStart');
        }).on('pause ended', function(){
          $(this).siblings('.symbol').trigger('PlayStop');
        });

        $('.audio_cell').click(function() {
          var audioDom;
          if (activeAudioCell && activeAudioCell != this ) {
            audioDom = $(activeAudioCell).find('audio')[0];
            audioDom.pause();
            audioDom.currentTime = 0;
          }
          activeAudioCell = this;
          audioDom = $(this).find('audio')[0];
          if (audioDom.paused) {
            audioDom.play();
          } else {
            audioDom.pause();
          }
        });
        
        wxShareConfig(item);
        audioManager = {
          limitCount: 3,
          list: [],
          current: null,
          $els: {
            addButton: $('#audioAddButton'),
            recordWrap: $('#audioRecordWrap'),
            startWidget: $('#audioRecordWrap .start_widget'),
            recordWidget: $('#audioRecordWrap .recording_widget'),
            startRecordButton: $('#audioRecordWrap .start_widget .start'),
            sendButton: $('#audioRecordWrap .recording_widget .send'),
            cancelButton: $('#audioRecordWrap .cancel'),
            durationSpan: $('#audioRecordWrap .recording_widget .duration'),
            audioListWrap: $('#audioListWrap'),
            audioAddWrap: $('#audioAddWrap'),
            ellipsisSpan: $('#audioRecordWrap .recording_widget .ellipsis'),
          },
          timer: null,
          showRecordWrap: function() {
            var $els = this.$els;
            $els.recordWrap.show();
            $els.startWidget.slideDown();
            $els.recordWidget.hide();
          },
          hideRecordWrap: function() {
            var $els = this.$els;
            $els.startWidget.hide();
            $els.recordWidget.hide();
            $els.recordWrap.hide();
            this.removeTimer();
          },
          removeTimer: function() {
            if (!this.timer) return;
            clearInterval(this.timer);
            this.timer = null;
          },
          showRecordWidget: function(){
            var $els = this.$els;
            var _this = this;
            $els.ellipsisSpan.text('. . .');
            $els.durationSpan.text('0');
            $els.startWidget.hide();
            $els.recordWidget.show();
            this.removeTimer();
            var duration = 0;
            this.timer = setInterval(function(){
              duration++;
              if (duration > 60) {
                $els.ellipsisSpan.text('. . .');
                $els.durationSpan.text('60');
                return _this.removeTimer();
              }
              $els.durationSpan.text(duration);
              var ellipsisText = $els.ellipsisSpan.text();
              if (ellipsisText.length < 5) {
                $els.ellipsisSpan.text(ellipsisText + ' .');
              } else {
                $els.ellipsisSpan.text('.');
              }
            }, 1000);
          },
          isVoiceUploading: function() {
            for (var i in this.list) {
              var audio = this.list[i];
              if (!audio.deleted && audio.isUploading) {
                return true;
              }
            }
            return false;
          },
          getVoiceData: function() {
            var arr = [];
            for (var i in this.list) {
              var audio = this.list[i];
              if (!audio.deleted && audio.uploadSuccess && audio.mediaUrl) {
                arr.push({
                  media_url: audio.mediaUrl,
                  media_duration: audio.duration
                });
              }
            }
            return arr;
          },
          addAudioCell: function(audio) {
            if (!audio) return;
            var _this = this;
            this.list.push(audio);
            var index = this.list.length-1;
            var $img = $('<img/>').attr('src', audio.getImageLink());
            var $duration = $('<span/>').addClass('duration').text(audio.duration + '"');
            var $symbol = $('<span/>').addClass('symbol').text('>>>');
            var $delButton = $('<span/>').addClass('button').text('取消');
            var $cell = $('<div/>').addClass('audio_cell').append($img).append($duration).append($symbol).append($delButton);
            this.$els.audioListWrap.append($cell);
            audio.index = index;
            audio.$el = $cell;
            audio.timer = null;
            $cell.on('PlayStart', function(){
              if (audio.timer) {
                clearInterval(audio.timer);
              }
              audio.timer = setInterval(function(){
                var text = $symbol.text();
                if (text.length < 3) {
                  $symbol.text(text+'>');
                } else {
                  $symbol.text('>');
                }
              }, 400);
            }).on('PlayStop', function(){
              if (audio.timer) {
                clearInterval(audio.timer);
                audio.timer = null;
              }
              $symbol.text('>>>');
            });
            $cell.click(function() {
              _this.playOrPauseAudio(audio);
            });
            $delButton.click(function(event){
              event.stopPropagation();
              if (confirm('确定删除?')) {
                _this.removeAudioCell(audio);
              }
            });
            this.onAudioCellChange();
          },
          playOrPauseAudio: function(audio){
            for (var i in this.list) {
              var _audio = this.list[i];
              if (audio != _audio) {
                _audio.stopVoice();
              }
            }
            audio.playOrPause();
          },
          removeAudioCell: function(audio) {
            if (!audio) return;
            audio.deleted = true;
            if (audio.timer) {
              clearInterval(audio.timer);
              audio.timer = null;
            }
            if (audio.localId && !audio.paused) {
              audio.paused = true;
              wx.stopVoice({
                localId: audio.localId,
              });
            }
            if (audio.$el) {
              audio.$el.remove();
              this.onAudioCellChange();
            }
          },
          stopAllVoice: function() {
            for (var i in this.list) {
              var audio = this.list[i];
              if (audio.localId && !audio.paused) {
                audio.paused = true;
                wx.stopVoice({
                  localId: audio.localId,
                });
              }
            }
          },
          getAudioCellCount: function() {
            var total = 0;
            for (var i in this.list) {
              if (!this.list[i].deleted) {
                total ++;
              }
            }
            return total;
          },
          onAudioCellChange: function() {
            var total = this.getAudioCellCount();
            var $els = this.$els;
            var $p = $els.audioAddWrap.children('p');
            if (total <= 0) {
              $els.addButton.detach().appendTo($els.audioAddWrap.siblings('h2'));
              $p.text('');
            } else if (total >= this.limitCount) {
              $els.addButton.detach().prependTo($els.audioAddWrap);
              $p.text('');
            } else {
              $els.addButton.detach().prependTo($els.audioAddWrap);
              $p.text('再说一段');
            }
          },
          init: function() {
            var $els = this.$els;
            var _this = this;
            $els.addButton.click(function() {
              var $p = $els.audioAddWrap.children('p');
              if (_this.getAudioCellCount() < _this.limitCount) {
                _this.showRecordWrap();
                $p.show();
              } else {
                $p.text('最多录'+_this.limitCount+'段语音');
                $p.show();
                $p.fadeOut(2000);
              }
            });
            $els.startRecordButton.click(function() {
              _this.showRecordWidget();
              _this.current = new AudioItem($els);
              _this.current.startRecord();
            });
            $els.cancelButton.click(function() {
              _this.hideRecordWrap();
              var audio = _this.current;
              if ( audio) {
                audio.startTime = null;
                if (audio.state == audioStates.isRecording) {
                  wx.stopRecord();
                }
              }
              _this.current = null;
            });
            $els.sendButton.click(function() {
              var audio = _this.current;
              if (!audio) return;
              _this.hideRecordWrap();
              _this.addAudioCell(audio);
              if (audio.localId) {
                audio.uploadRecord();
              } else {
                audio.stopAndUploadRecord();
              }
            });
          },
        };
        audioManager.init();
        window.onbeforeunload = function(){
          audioManager.stopAllVoice();
        };
      }else if (res.code == 100) {
        loadErr('此信息已被删除')
      } else {
        loadErr(res.msg)
      }
    }