var BarNum, Timer1;
Dragging=false;
urls=new Array();
urlid=0;
playnext=false;

if (typeof(WMP_PLAYING)=="undefined") WMP_PLAYING="正在播放……";
if (typeof(WMP_STOPPED)=="undefined") WMP_STOPPED="已停止";

function getobj(n) {
	if (document.getElementById)
		return document.getElementById(n);
	else
		return eval("document.all."+n);
}

function clearTimer() {
	clearInterval(Timer1);
}

function setTimer() {
	clearTimer();
	Timer1=setInterval("playing()", 1000);
}

function showtip(h) {
	var tip=getobj("mediaInfo");
	if (tip) tip.innerHTML=h;
}

function openStateChange1(NewState) {
	switch (NewState) {
    case 10:
        showtip("正在连接到媒体...");
        break;
    case 12:
        showtip("正在打开媒体...");
        break;
    case 16:
        showtip("正在下载许可证...");
        break;
	}
}

function playStateChange1(NewState) {
	switch (parseInt(NewState)) {
		case 0:
		    showtip('未知状态');
		    break;
		case 1:
				showtip(WMP_TITLE+WMP_STOPPED);
		    window.setTimeout("playnextmedia()", 1000);
		    break;
		case 2:
		    showtip('已暂停');
				clearTimer();
		    break;
		case 3:
				showtip(WMP_TITLE+WMP_PLAYING);
				setTimer();
		    break;
		case 4:
		    showtip('快进...');
		    break;
		case 5:
		    showtip('快退...');
		    break;
		case 6:
		    window.setTimeout('buffering()',900);
		    break;
		case 7:
		    showtip('等待中...');
		    break;
		case 8:
		    showtip('播放结束');
				clearTimer();
				playnext=true;
		    break;
		case 9:
		    showtip('正在打开媒体...');
		    break;
		case 10:
		    showtip('准备就绪');
				clearTimer();
		    break;
		case 11:
		    showtip('正在尝试重新连接');
		    break;
		default:
		    break;
	}
}

function buffering() {
  if (document.WMPlayer.playState==6) {
  	var bp=getobj("WMPlayer").network.bufferingProgress;
  	if (bp==100) bp=99;
  	showtip("缓冲处理: 完成 "+bp+"%");
		bptimer = window.setTimeout("buffering()",900);
	}
}

function playing() {
	var cp=WMPlayer.controls.currentPosition.toFixed(0);
	var sl=WMPlayer.currentMedia.duration.toFixed(0);
	var t=getobj("ctime");
	var p=getobj("ProgressBar1");
	var pb=getobj("pbar");
	var pn=getobj("pbutton");
	if (t) {
		if (sl==0)
			t.innerText=s2hms(cp);
		else
			t.innerText=s2hms(cp)+"/"+s2hms(sl);
	}
	if (sl>0 && p) {
		var w=cp*310/sl;
		p.style.width=w.toFixed(0)+"px";
	}
	if (sl>0 && pb && pn && !Dragging) {
		var percent = Math.round((cp/sl)*(pb.clientWidth-pn.clientWidth));
		if (!isNaN(percent))
			pn.style.left = percent+"px";
	}
}

function s2hms(s) {
	var h=(s-s%3600)/3600;
	s%=3600;
	if (h<10) h="0"+h;
	var m=(s-s%60)/60;
	if (m<10) m="0"+m;
	s%=60;
	if (s<10) s="0"+s;
	if (h>0)
		return(h+":"+m+":"+s);
	else
		return(m+":"+s);
}

function volumeplus(n) {
	setvolume(WMPlayer.settings.Volume+n);
	var o=document.getElementById("volumeInfo");
	if (o) o.innerHTML="音量"+WMPlayer.settings.Volume+"%";
}

function volumeminus(n) {
	setvolume(WMPlayer.settings.Volume-n);
	var o=document.getElementById("volumeInfo");
	if (o) o.innerHTML="音量"+WMPlayer.settings.Volume+"%";
}

function mousedown(n) {
	BarNum=n;
	var fa, son;
	if (BarNum==0) {
		fa=getobj("pbar");
		son=getobj("pbutton");
	}
	else if (BarNum==1) {
		fa=getobj("vbar");
		son=getobj("vbutton");
	}
  Dragging = true;
  var l=(window.event.srcElement.id!=fa.id)?son.offsetLeft:(window.event.x-Math.floor(son.clientWidth/2));
  l=(l<0)?0:l;
  l=((l+son.clientWidth)>fa.clientWidth)?(fa.clientWidth-son.clientWidth):l;
 	son.style.left=l;
}

function mousemove()
{
  if (BarNum==0)
  {
		fa=getobj("pbar");
		son=getobj("pbutton");
  }
	else if (BarNum==1) {
		fa=getobj("vbar");
		son=getobj("vbutton");
	}
	if (Dragging) {
		var l=window.event.clientX - fa.offsetLeft - son.clientWidth/2;
	  l=(l<0)?0:l;
  	l=((l+son.clientWidth)>fa.clientWidth)?(fa.clientWidth-son.clientWidth):l;
		son.style.left = l;
	}
	if (BarNum==1 && Dragging) {
		getobj("WMPlayer").settings.Volume=100*(parseInt(getobj("vbutton").style.left)/(getobj("vbar").clientWidth-getobj("vbutton").clientWidth));
		mute(0);
	}
}

function mouseup()
{
  if (BarNum==0) {
  	if (getobj("WMPlayer").currentMedia.Duration>0 && Dragging)
			getobj("WMPlayer").controls.CurrentPosition=getobj("WMPlayer").currentMedia.Duration*(parseInt(getobj("pbutton").style.left)/(getobj("pbar").clientWidth-getobj("pbutton").clientWidth));
  }
	if (BarNum==1 && Dragging) {
		getobj("WMPlayer").settings.Volume=100*(parseInt(getobj("vbutton").style.left)/(getobj("vbar").clientWidth-getobj("vbutton").clientWidth));
		mute(0);
	}
  Dragging=false;
}

function setvolume(vol) {
	if (vol<0) vol=0;
	if (vol>100) vol=100;
	var o=getobj("WMPlayer");
	if (o) o.settings.Volume=vol;
	var vb=getobj("vbutton");
	var o=getobj("vbar");
	if (vb && o)
		vb.style.left=vol*(o.clientWidth-vb.clientWidth)/100;
}

function setsource(url) {
	var p=getobj("WMPlayer");
	if (p) {
		p.URL=url;
		p.controls.play();
	}
}

function playnextmedia() {
	if ((playnext==true) && urls && (urls.length>0)) {
		playnext=false;
		urlid++;
		if (urlid>=urls.length) urlid=0;
		setsource(urls[urlid]);
	}
}

function dostop() {
	var o=getobj("WMPlayer");
	if (o) o.controls.stop();
}

function doplay() {
	var o=getobj("WMPlayer");
	if (o) o.controls.play();
}

function dopause() {
	var o=getobj("WMPlayer");
	if (!o) return;
	if (o.controls.isAvailable('Pause')) o.controls.pause();
}

function fullscreen() {
	var o=getobj("WMPlayer");
	if (!o) return;
	if (o.playState==3) o.fullScreen="true";
}

function mute(n) {
	var o=getobj("WMPlayer");
	if (!o) return;
	var om=o.settings.mute;
	switch(n) {
		case -1: o.settings.mute=true; break;
		case 0: o.settings.mute=false; break;
		case 1: o.settings.mute=!om; break;
	}
	var nm=o.settings.mute;
	o=getobj("muteButton");
	if (o && muteclass && demuteclass) {
		if (nm && o.className!=muteclass)
			o.className=muteclass;
		else if (!nm && o.className!=demuteclass)
			o.className=demuteclass;
	}
}

window.document.onmousemove = mousemove;
window.document.onmouseup = mouseup;

