function pause()
{
  clearTimer();
  document.getElementById('imgPlay').src = IMG_PLAY_0;
  if (isIE) pauseAt = player.getPosition();
  if (isIE && player.canPause()) player.doPause();
  isPaused = true;
  msg('暂停');
}

var pauseAt = -1;
var isPaused = false;
var isStopped = false;
function checkSetsWithSound()
{
  if (!isIE) return false;
  setsWithSound = document.getElementById('soundChk').checked;
  setsWithSoundChanged = true;
  if (setsWithSound)
  {
    if (isPlaying && !isPaused)  // 单元向声音靠拢
    {
      clearTimer();
      curIndex = getIndexFromSound();
      displayCurSet();
      timer = getAutoPlayTimer(timeList[curIndex] - player.getPosition());
      curIndex = nextIndex(curIndex);
    }
  }
  else
  {
    if (isPlaying && !isPaused) clearTimer();  //只 取消 自动播放的 timer
    if (isPlaying && !isPaused) timer = window.setTimeout('playBack();', soundLength - player.getPosition());
  }
}
function playBack()
{
  if (!isIE) return false;
  if (isIE&&player.canStop()) player.doStop();
  msg("播放声音");
  player.setPosition(0);
  window.setTimeout('if(isIE&&player.canPlay())player.doPlay();', 100);
  timer = window.setTimeout('playBack();', soundLength);
}
function getAutoPlayTimer(ti)
{
  return window.setTimeout('autoPlay();', ti);
}
function resumeAfterPause()
{
  clearTimer();
  var startPlayHere = false;

  if (setsWithSound) //音画同步
  {
    if (isIE&&player.canPlay())
    {
      startPlayHere = true;
      player.doPlay();  //无论如何，暂停以后再按play键，都会播放声音
    }
    if (preOrNextPressed)  //已经按了pre or next 按钮，声音向单元靠拢
    {
      if (!isIE) return;
      if (startPlayHere) player.setPosition(getSetStartTime(preIndex));
      var posi = timeList[preIndex]-player.getPosition();
      timer = getAutoPlayTimer(posi);//timeList[preIndex]-player.getPosition());
    }
    else
    {
      timer = isIE?getAutoPlayTimer(timeList[preIndex] - player.getPosition()):getAutoPlayTimer(getTime(preIndex));
    }
    msg('自动播放');
  }
  else
  {
    if (isIE&&player.canPlay()) player.doPlay();
    if (preOrNextPressed && !setsWithSoundChanged) player.setPosition(getSetStartTime(preIndex));
    if (isPlaying && !isPaused) timer = window.setTimeout('playBack();',soundLength-player.getPosition());
    msg('播放声音');// resumeAfterPause not setsWithSound '+preOrNextPressed);
  }
  isPaused = false;
  preOrNextPressed = false;
  setsWithSoundChanged = false;
  document.getElementById('imgPlay').src = IMG_PLAY_1;
}
function resumeAfterStop()
{
  if (isIE && player.canPlay()) player.doPlay();
  if (isIE)player.setPosition(0);
  curIndex = 0;
  if (setsWithSound)
  {
    displayCurSet();
    timer = getAutoPlayTimer(timeList[0]);
    curIndex = 1;
    msg('自动播放');
  }
  else
  {
    msg('播放声音');
    if (turnBack)
    {
      timer = window.setTimeout('resumeAfterStop();', soundLength);
    }
  }
  isPlaying = true;
  document.getElementById('imgPlay').src = IMG_PLAY_1;
}
function play()
{
  if (!(fullDown && isImagePreloaded()))
  {
    alertDowning();
    return;
  }
  if (preOrNextPressed)
  {
    alert('现在处于手工翻页状态，如果要自动播放，请先按停止键！');
    return false;
  }

  if (isPlaying)
  {
    if (isPaused)
    {
      resumeAfterPause();
    }
    else
    {
      pause();
    }
  }
  else
  {
    resumeAfterStop();
  }
}
var preOrNextPressed = false;
function displayPreSet()
{
  curIndex = getPreIndex(preIndex);
  displayCurSet();
  if (setsWithSound && isPlaying && !isPaused)
  {
    clearTimer();
    timer = getAutoPlayTimer(getTime(curIndex));
  }
  curIndex = nextIndex(curIndex);
}
function playSoundOfPreSet()
{
  if (setsWithSound)
  {
    var index = getPreIndex(preIndex);
    if (isPlaying && !isPaused)
    {
      setTimeout('playSoundAt('+getSetStartTime(index)+');', 100);
    }
    else
    {
      clearTimer();
      setTimeout('playSoundAt('+getSetStartTime(index)+');', 100);
      timer = window.setTimeout('pauseSoundAt('+timeList[index]+');', 150);
    }
  }

}
var turnTimer = null;
function pre()
{
  if (!(fullDown && isImagePreloaded()))
  {
    alertDowning();
    return;
  }
  document.getElementById('imgPlay').src = IMG_PLAY_0;
  if (setsWithSound) clearTimer();
  preOrNextPressed = true;
  setsWithSound = false;
  setsWithSoundChanged = true;
  playSoundOfPreSet();
  displayPreSet();
  msg('手工翻页');
  if (isIE && isPlaying && turnBack)
  {
    if (turnTimer != null)
    {
      window.clearTimeout(turnTimer);
      turnTimer = null;
    }
    turnTimer = setTimeout('player.doplay();player.setPosition(0);',(timeList[setsLength-1]-player.getPosition()));
  }
}
function playSoundOfNextSet()
{
  if (setsWithSound)
  {
    var index = curIndex;
    if (isPlaying && !isPaused)
    {
      setTimeout('playSoundAt('+getSetStartTime(index)+');', 100);
    }
    else
    {
      clearTimer();
      setTimeout('playSoundAt('+getSetStartTime(index)+');', 50);
      timer = window.setTimeout('pauseSoundAt('+timeList[index]+');', 100);
    }
  }

}
function displayNextSet()
{
  displayCurSet();
  if (setsWithSound && isPlaying && !isPaused)
  {
    clearTimer();
    timer = getAutoPlayTimer(getTime(preIndex));
  }
  curIndex = nextIndex(curIndex);

}
function next()
{
  if (!(fullDown && isImagePreloaded()))
  {
    alertDowning();
    return;
  }
  document.getElementById('imgPlay').src = IMG_PLAY_0;
  if (setsWithSound) clearTimer();
  preOrNextPressed = true;
  setsWithSound = false;
  setsWithSoundChanged = true;
  if (isIE) playSoundOfNextSet();
  if (curIndex == setsLength) curIndex = 0;
  displayNextSet();
  msg('手工翻页');
  if (isIE && isPlaying && turnBack)
  {
    if (turnTimer != null)
    {
      window.clearTimeout(turnTimer);
      turnTimer = null;
    }
    turnTimer = setTimeout('player.doplay();player.setPosition(0);',(timeList[setsLength-1]-player.getPosition()));
  }
}
var isStopped = false;
function stop()
{
  if (!(fullDown && isImagePreloaded()))
  {
    alertDowning();
    return;
  }
  preOrNextPressed = false;
  clearTimer();
  if (isIE && player.canPause()) player.doPause();
  isPlaying = false;
  isPaused = false;
  isStopped = true;
  setsWithSound = true;
  setsWithSoundChanged = true;
  curIndex = 0;
  displayCurSet();
  msg('停止');
  document.getElementById('imgPlay').src = IMG_PLAY_0;
}
function getSetStartTime(index)
{
  return index == 0 ? 0 : timeList[index-1];
}


var player = null;
function start()
{
  checkImagePreload();
  if (!isIE)
  {
    fullDown = true;
    return false;
  }

  if (isIE && player == null) player = new Player('mediaPlayer');//document.getElementById('player');
  if (isIE)
  {
    setTimeout('mgo()',500);
  }
/*  if (isIE) player.setVolume(70);
  init();
*/
}
function mgo()
{
  if (isIE)
  {
    player.setVolume(70);
    //player.doPlay();
    init();
  }
}


function init()
{
  if (!isIE)
  {
    fullDown = true;
    return false;
  }
  if (player.getPlayState() == 3 || player.getPlayState() == 10)   // is playing
  {
    soundLength = player.getLength();
    //timeList[setsLength] = soundLength;
    player.setPosition(0);
    player.doStop();
    fullDown = true;
    curIndex = 0;

    if (isImagePreloaded())
    {

      isPlaying = true;
      player.setVolume(70);
      document.getElementById('indexLabel').innerHTML = '页次：';
      document.getElementById('soundMsg').innerHTML = '自动播放';
      document.getElementById('indexMsg').innerHTML = 1 + ' / ' + setsLength;
     hidePreloadDiv();
   }
    else checkImagePreload();
  }
  else
  {
    cnt++;
    timer = setTimeout("init()", 150);
    var m = document.getElementById("soundMsg").innerHTML+'.';
    if(m.length >= 10) m = "语音缓冲 ";
    msg(m+player.getPlayState());
  }
}
function isImagePreloaded()
{
  return imagePreloaded == imageCount;
}
var iii=0;
function checkImagePreload()
{
  if (!isImagePreloaded()) setTimeout('checkImagePreload();', 150);
  else
  {

    isPlaying = true;
    if (isIE)
    {
      //if (player == null) player = document.getElementById('player');
      //if (player != null) player = null;
      //player = new Player('mediaPlayer');
      if (isIE && player == null) player = new Player('mediaPlayer');
      player.setVolume(70);
    }
    document.getElementById('indexLabel').innerHTML = '页次：';
    document.getElementById('soundMsg').innerHTML = '自动播放';
    document.getElementById('indexMsg').innerHTML = 1 + ' / ' + setsLength;
    hidePreloadDiv();
  }
}
function msg(m)
{
  document.getElementById("soundMsg").innerHTML = m;
}

function nextIndex(index)
{
  return (index >= setsLength) ? 0 : index+1;
}
function getPreIndex(index)
{
  return (index == 0) ? setsLength-1 : index-1;
}
function clearTimer()
{
  if (timer != null)
  {
    window.clearTimeout(timer);
    timer = null;
  }
}
function getTimeout(action)
{
  return window.setTimeout(action, getTime(curIndex));
}
function checkTurn()
{
  turnBack = document.getElementById('turnChk').checked;
  if (isIE && turnBack)
  {
    clearTimer();
    timer = getAutoPlayTimer(timeList[preIndex]-player.getPosition());
  }
}
function autoPlay()
{
//alert(curIndex+'   '+setsLength+'   '+preIndex)
  if (!isPlaying || !fullDown)
    return;
  document.getElementById("imgPlay").src = IMG_PLAY_1;
  if (curIndex == setsLength)
  {
    if (isIE&&player.canStop()) player.doStop();
    if (isIE)player.setPosition(0);
    if (turnBack) setTimeout('autoPlay();', 100);
    else
    {
      isPlaying = false;
      document.getElementById("imgPlay").src = IMG_PLAY_0;
      msg('停止');
    }

    curIndex = 0;
    return;
  }
  else if (isIE&&player.canPlay()) player.doPlay();
  displayCurSet();
  if (setsWithSound) timer = getTimeout('autoPlay();');
  curIndex = curIndex + 1;
}

function getTime(index)
{
  return index == 0 ? timeList[0] : (timeList[index] - timeList[index-1]);
}
function getIndexFromSound()
{
  if (!isIE) return false;
  var posi=player.getPosition();
  if (posi > 0)
  {
    for (var i = 0; i < setsLength; i++)
    {
      if (timeList[i] > posi)
      {
        posi = i;
        break;
      }
    }
  }
  return posi;
}
function pauseSoundAt(posi)
{
  if (!isIE) return false;
  if (player.getPosition() < posi)
  {
    timer = window.setTimeout('pauseSoundAt(' + posi + ');', 100);
  }
  else
  {
    if (player.canStop()) player.doStop();
  }
}
function playSoundAt(posi)
{
  if (!isIE) return false;
  if (isIE&&player.canPlay()) player.doPlay();
  player.setPosition(posi);
}

function hidePreloadDiv()
{
  if (isIE&&!hide)
  {
     hide = true;
   return;
  }
doHidePreloadDiv();
}

var _volContainer = 'volllll';
var _vol = 'vol'
var _volStart = 0;
var _volY = 0;

function vgetX(obj)
{//return obj.offsetLeft + 8;
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
    //alert(obj.tagName+ ' '+obj.id+ ' ' + obj.offsetLeft)
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft + 8;
}
function vgetY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

var vol = 70;
var vLineTop = 25
var vLineHeight = 60
var volBarHeight = 8

function showVol(vol)
{
	if ( !$( _volContainer ) )
	{
    var vDiv = document.createElement('div');
    vDiv.id = _volContainer;


    var vs = vDiv.style // vStyle
    var vWidth = 50;
    var vHeight = vLineHeight + vLineTop + 10 + volBarHeight
    var volBarWidth = 25;

    vs.width = vWidth + 'px'
    vs.height= vHeight + 'px'
    vs.backgroundColor = '#edf'
    vs.zIndex = '222'
    vs.position = 'absolute'
    vs.textAlign = 'center'
    vs.paddingTop = '5px'
    vs.borderTop = '1px solid #888'
    vs.borderLeft = '1px solid #888'
    vs.borderBottom = '1px solid #000'
    vs.borderRight = '1px solid #000'

    vs.left = (vgetX(window.event.srcElement)-vWidth/2) + 'px'
    vs.top = (vgetY(window.event.srcElement) - vHeight)+'px'

    var vtop = (100-vol) * vLineHeight / 100 + vLineTop

		vDiv.innerHTML = '音量<div id="volLine" style="font-size:0;width:2px;background-color:#888;height:'+(vLineHeight + volBarHeight)+'px;left:'+parseInt(vWidth/2-2)+'px;top:'+vLineTop+'px;position:absolute;"></div><div style="cursor:pointer;background-color:#fef;font-size:0;width:'+volBarWidth+'px;height:'+volBarHeight+'px;border:1px solid #000;border-top:1px solid #aaa;border-left:1px solid #aaa;position:absolute;top:'+vtop+'px;left:'+(vWidth-volBarWidth)/2+'px;" onmousedown="_volStart=1;_volY=window.event.clientY;" onmouseup="_volStart=0;" onmousemove="volMove();" id="'+_vol+'" title="拖动调节音量"></div>';
		document.body.appendChild(vDiv);
	}
}

function volMove()
{
  if ( _volStart )
  {
  	var newY = window.event.clientY;
    var len = newY - _volY
    var vs = $( _vol ).style

    vtop = (parseInt(vs.top) + len)

    if ( vtop < vLineTop )
    {
    	vtop = vLineTop
    }
    else if ( vtop > vLineHeight + vLineTop )
    {
    	vtop = vLineHeight + vLineTop
    }
    vol = 100-(vtop - vLineTop) * 100 / vLineHeight

    vs.top = vtop + 'px'

    player.setVolume(vol);



    _volY = newY
    return false;

  }
	
}
function hideVol()
{
  var clickSrc = window.event.srcElement;
  //alert(vol)
	if ( $( _vol ) && clickSrc != $('volIcon') && clickSrc != $(_volContainer)
   && clickSrc != $(_vol) && clickSrc != $('volLine') )
	{
		document.body.removeChild( $( _volContainer ) );
	}
}

