/**
 * 設定當播放器加載頁面時，是否自動播放媒體文件
 * true = 自動播放
 * false = 不要自動播放，等待使用者啟動播放器
 */
var blnAutoStart = true;
/**
 * 設定曲目的預設播放順序，是否隨機(隨機數/Random Playing)播放媒體
 * true = 隨機播放
 * false = 循序播放
 */
var blnRndPlay = false; 
var intVolume = 80;	//默認音量
var blnAbs1stTrack = false;
var blnStatusBar = false; 	//是否在任務欄顯示標題
var blnUseSmi = false;	//使用字幕功能,預留接口
var blnLoopTrk = true;	//默認循環播放
var blnShowMmInfo =false;

var objMmInfo = null;
var intMmCnt = 0;
var intSelMmCnt=0;
var intActMmCnt=0;
var intCurrActIdx=0;
var strCurrActTit="nontitle";
var strCurrMediaUrl="";
var strMmInfo ="SMusic Media Information";

var blnLonelyPlay=false;
var blnEnabled= false;
var blnEOT=false;
var arrSelMm=null;
var arrActMm=null;
var intSMusicStat=0;
var tidTLab=null;
var tidErr=null;
var tidMsg=null;
var intErrCnt=0;
var blnRept=false;
var blnExtMmUsed=false;
var strExtTitle="nontitle"

var intSupremeTrkIdx=0;
var blnAutoProc = true;
var blnElapse=true; 
var intDelay =500;
var disp2 = document.getElementById('disp2');
var musicobj;
var volSliderObj;
var dTitle;

for(var i=0; i<urlarr.length; i++) {
	mkList(urlarr[i][0], urlarr[i][1]);	//創建播放列表
}
/**
 * 初始化Windows Media Player 鏈接庫建立環境設定
 */
function wmpInit() {
	musicobj = document.getElementById('SMusic');
	var wmpEnabled=musicobj.enabled;
	if(wmpEnabled) {
		intSMusicStat=0;
	} else {
		intSMusicStat=3;	//初始化Media Player OLE 錯誤
		blnEnabled=false;
	}

	var wmps = musicobj.settings;
	var wmpc = musicobj.ClosedCaption;
	

	wmps.autoStart=true;
	wmps.balance=0;
	wmps.enableErrorDialogs=false;
	wmps.invokeURLs = false;
	wmps.mute=false;
	wmps.playCount=1;
	wmps.rate=1;
	wmps.volume = intVolume;

	if(blnUseSmi) {
		wmpc.captioningID="capText";
		document.getElementById("capText").style.display="";
	}
}

/**
 * 創建 Multi-object-contents 的新數組
 */
function mkMmPath(u, t, f,s){
	this.mmUrl = u;
	var mmChkStr = u;
	mmChkStr = mmChkStr.toLowerCase();
	mmChkStr = mmChkStr.substring(mmChkStr.length - 4 , mmChkStr.length);
	switch(mmChkStr){
		case ".asx":
			this.mmeta="t";
			break;
		case ".wax":
			this.mmeta="t";
			break;
		case ".wvx":
			this.mmeta="t";
			break;
		case ".m3u":
			this.mmeta="t";
			break;
		default:
			this.mmeta="f";
 	}

	this.mmTit =t;
	this.mmDur =0;
	this.selMm=f;
	this.actMm=f;
	if(blnUseSmi) { this.mmSmi=s; }
}

/**
 * 給 Multi-object-contents數組附值
 */
function mkList(u,t,s,f){
	var cu=u;
	var ct=t;
	var cs=s;
	var cf=f;
	var idx=0;
	if(objMmInfo == null) {
		objMmInfo=new Array();
		idx=0;
	} else {
		idx=objMmInfo.length;
	}
	if(u=="" || u==null) { cu=""; }
	if(t=="" || t==null) { ct="nontitle"; }
	if(f=="f" || f=="F") {
		cf="f";
	} else{
		cf="t";
		intSelMmCnt=intSelMmCnt+1;
	}
	blnUseSmi ? objMmInfo[idx]=new mkMmPath(cu,ct,cf,cs):objMmInfo[idx]=new mkMmPath(cu,ct,cf);
	intActMmCnt=intSelMmCnt;
	intMmCnt=objMmInfo.length;
}

/**
 * 建立已選取播放項目(Selected Media)的數組
 */
function mkSel(){
//	arrSelMm=null;
	intSelMmCnt=0;
	var selidx = 0;

	var wmpEnabled=musicobj.enabled;
	if(!wmpEnabled){intSMusicStat=3; blnEnabled=false;return;} //Media Play OLE 出錯
	if(intMmCnt<=0){intSMusicStat=1; blnEnabled=false; return;} //空的播放列表

	arrSelMm=new Array();
	for(var i=0; i<intMmCnt;i++) {
		if(objMmInfo[i].selMm =="t") {
			arrSelMm[selidx]=i;
			selidx=selidx+1;
		}
	}
	intSelMmCnt=arrSelMm.length;

	if(intSelMmCnt<=0) {
		blnEnabled=false;
		intSMusicStat=2;
		arrSelMm=null;
		return;
	} else{
		blnEnabled=true;
		mkAct();	//激活播入列表
	} 
}

/**
 * 激活已選擇的播放列表
 */
function mkAct(){
	arrActMm=null;
	intActMmCnt=0;
	var selidx=0;
	var actidx=0;

	if(blnEnabled){
		arrActMm=new Array();
		for(var i=0; i<intSelMmCnt;i++){
			selidx=arrSelMm[i];
			if(objMmInfo[selidx].actMm=="t"){arrActMm[actidx]=selidx; actidx=actidx+1;}
		}
		intActMmCnt=arrActMm.length;
	} else { return; }
	if(intActMmCnt<=0) {
		blnEOT=true;
		arrActMm=null;
	} else { blnEOT=false; }
}

/**
 * 全部選取所有的播放清單項目
 */
function chkAllSel() {
	for(var i=0; i<intMmCnt; i++){
		objMmInfo[i].selMm="t";
		objMmInfo[i].actMm="t";
	}
	mkSel();
	dspList();
}

/**
 * 取消所有的媒體選擇
 */
function chkAllDesel() {
	for(var i=0; i<intMmCnt; i++){
		objMmInfo[i].selMm="f";
		objMmInfo[i].actMm="f";
	}
	mkSel();
	dspList();
}
/**
 * 反向選擇
 */
function reverseSel() {
	for(var i=0; i<intMmCnt; i++) {
		if(objMmInfo[i].selMm =="t") {
			objMmInfo[i].selMm="f";
			objMmInfo[i].actMm="f";
		} else {
			objMmInfo[i].selMm="t";
			objMmInfo[i].actMm="t";
		}
	}
	mkSel();
	dspList();
}
/**
 * 設置選取或不選取播放清單項目（反向選擇）
 */
function chkItemSel(idx) {
	if(objMmInfo[idx].selMm =="t") {
		objMmInfo[idx].selMm="f";
		objMmInfo[idx].actMm="f";
	} else {
		objMmInfo[idx].selMm="t";
		objMmInfo[idx].actMm="t";
	}
	mkSel();
}

/**
 * 將某個已啟用播放項目(Activated Media)凍結
 */
function chkItemAct(idx){
	objMmInfo[idx].actMm="f";
	mkAct();
}

/**
 * 激活未選擇到播放列表的媒體
 */
function mkSelAct(){
	var idx=0;
	for(var i=0; i<intSelMmCnt; i++){
		idx=arrSelMm[i];
		objMmInfo[idx].actMm="t";
 	}
	mkAct();
}

/**
 * 將已選取播放項目(Selected Media)加入到已啟用播放項目(Activated Media)
 */
function inLink(trk){
	var wmps= musicobj.playState;
	if(wmps==2 || wmps==3){musicobj.controls.stop();}
	blnExtMmUsed=false;

	if(isNaN(parseInt(trk))){
		etcMsg(6,trk);
		return;
	} else {
  		var innerIdx = parseInt(trk) - 1;
  		if(innerIdx<0 || innerIdx>=intMmCnt){
			etcMsg(6,trk);	return;
  		} else { 
			blnLonelyPlay=true; 
			intErrCnt=0;
			selMmPlay(innerIdx);
		}
	}
}

/**
 * 給Media Player附媒體地址
 * @param string url: 音樂的URL地址
 * @param string tit: 音樂標題
 */
function outLink(url , tit){
	var wmps= musicobj.playState;
	if(wmps==2 || wmps==3){musicobj.controls.stop();}
	blnExtMmUsed=true;

	var wmpEnabled=musicobj.enabled;
	var wmps=musicobj.settings;
	if(!wmpEnabled) {
		intSMusicStat=3;	//OLE錯語(return void)
		waitMsg();
	} else {
		blnLonelyPlay=true;
		intErrCnt=0;
		strCurrMediaUrl=url;
		if(url==null || url=="") {etcMsg(5);return;} //Media URL Empty Error
		if(tit==null || tit==""){
			strExtTitle="nontitle";
		} else{ strExtTitle=tit; }
		strCurrActTit=strExtTitle;
		musicobj.URL=strCurrMediaUrl;
		if(!wmps.autoStart) { musicobj.controls.play(); }
		disp1.innerHTML= strCurrActTit;
		if(blnStatusBar){window.status=(strCurrActTit);}	//在狀態欄顯示標題
	}
}
/**
 * 初始化Media Player播放器
 */
function initMPlayer(){
	var re = new RegExp("(msie[^;^)^(]*)", "ig");
	if(navigator.userAgent.search(re) == -1) {
		alert("請使用IE瀏覽器訪問，可能無法使用播放器功能！");
	}
	wmpInit();
	mkSel();
	blnLonelyPlay=false;
	blnExtMmUsed=false;
	blnRept ? imgChange('rept',1) : imgChange('rept',0);
	blnLoopTrk ? imgChange('tloop',1) : imgChange('tloop',0);
	blnRndPlay ? imgChange('pmode',1) : imgChange('pmode',0);
 	showTLab();
 	dTitle = document.getElementById('disp1');
 	dTitle.innerHTML="X-Space Music/Video Player";
 	if(blnStatusBar){window.status=('X-Space Music/Video Player');}
 	if(blnAutoStart){ startSMusic(); }
}

/**
 * 開始播放媒體
 */
function startSMusic() {
	var wmps= musicobj.playState;
	if(musicobj.settings.rate != 1.0){musicobj.settings.rate=1.0; return;}
	if(wmps==2){musicobj.controls.play(); return;}	//如果狀態為中止，則激活播放
	if(wmps==3){return;}	//如是在播放狀態，則直接退出

	blnLonelyPlay=false;
	if(!blnEnabled) { waitMsg();return; }
	if(blnEOT) { mkSelAct(); }
	if(intErrCnt>0) {
		intErrCnt=0;
		tidErr=setTimeout('retryPlay(),1000');
		return;
	}
	if(isNaN(intSupremeTrkIdx) || intSupremeTrkIdx >= intMmCnt) { intSupremeTrkIdx=0; }
	if(blnRndPlay) {
		rndPlay();
	} else {
		if(objMmInfo[intSupremeTrkIdx].selMm=="t" && objMmInfo[intSupremeTrkIdx].actMm=="t") {
			intCurrActIdx=intSupremeTrkIdx;
			selMmPlay(intSupremeTrkIdx);
		} else {
			intCurrActIdx=arrActMm[0];
			selMmPlay(intCurrActIdx);
		}
	}
}

/**
 * 處理播入媒體標題，如果媒體路徑為空則跳到下一個媒體播放
 */
function selMmPlay(idx){
	clearTimeout(tidErr);
	if(intSMusicStat==3){blnEnabled=false;waitMsg();return;} // WindowsMedia OLE 錯誤
	blnExtMmUsed=false;
	intCurrActIdx=idx;
 	var trknum=idx+1;
 	var ctit =objMmInfo[idx].mmTit;
 	if(ctit=="nontitle") {ctit="正在獲取標題或歌手";}
 	if(blnUseSmi){musicobj.ClosedCaption.SAMIFileName = objMmInfo[idx].mmSmi;}
 	strCurrMediaUrl=objMmInfo[idx].mmUrl;
	//如果媒體路徑為空，則播放下一個媒體
 	if(strCurrMediaUrl=="" || strCurrMediaUrl==null){ etcMsg(4);setTimeout('playAuto()', 3000);return; }
 	musicobj.URL = strCurrMediaUrl;
 	var wmps=musicobj.settings;
 	if(!wmps.autoStart) { musicobj.controls.play(); }
 	if(blnAbs1stTrack && intCurrActIdx==intSupremeTrkIdx) {
 		strCurrActTit="[SupremeTrack:" + trknum + "] " + ctit;
 	} else { strCurrActTit= " "+ trknum + ". " + ctit; }
	dTitle.innerHTML = strCurrActTit;
	if(blnStatusBar) { window.status=(strCurrActTit); }
	chkItemAct(intCurrActIdx); 
}

/**
 * 使用wmp-obj播放媒體
 */
function wmpPlay() { musicobj.controls.play(); }

/**
 * 停止或等待播放
 */
function wmpStop(){
	intErrCnt=0;
	clearTimeout(tidErr);
	clearInterval(tidTLab);
	imgChange("stopt",1);
	imgChange("pauzt",0);
	showTLab();
	mkSelAct();
	musicobj.controls.stop();
	musicobj.close();
	dTitle.innerHTML="等待播放";
	if(blnStatusBar) {window.status=('等待播放');return true;}
}

/**
 * 暫停wmp-obj播放媒體
 */
function wmpPause() { musicobj.controls.pause(); }

/**
 * 暫停或播放媒體
 */
function wmpPP(){
	var wmps = musicobj.playState;
	var wmpc=musicobj.controls;
	clearInterval(tidTLab);
	clearTimeout(tidMsg);
	if (wmps == 2) {wmpc.play();}
	if (wmps == 3) {
		wmpc.pause();
 		disp2.innerHTML="暫停";
 		tidMsg=setTimeout('rtnTLab()',1500);
 	}
 	return;
}
/**
 * 後退
 */
function fastRew() {
	clearInterval(tidTLab);
	var wmpfr=musicobj.controls.isAvailable("FastReverse");
	if(wmpfr){musicobj.controls.fastReverse();
		disp2.innerHTML="FastRWD"
	} else {
		disp2.innerHTML="noReward";
	}
}
/**
 * 前進
 */
function fastFwd() {
	clearInterval(tidTLab);
	var wmpff=musicobj.controls.isAvailable("FastForward");
	if(wmpff){musicobj.controls.fastForward();
		disp2.innerHTML="FastFWD"
	} else {
		disp2.innerHTML="noForward"
	}
}

function endFwd(){
	if(musicobj.settings.rate >1.0) {
  		musicobj.settings.rate=1.0;
	} else {
		tidTLab=setInterval('showTLab()',1000);
	}
}

function endRew(){
	if(musicobj.settings.rate <1.0) {
		musicobj.settings.rate=1.0;
	} else {
		tidTLab=setInterval('showTLab()',1000);
	}
}

/**
 * 隨機播放(Random Play)模式
 */
function rndPlay(){
	if(!blnEnabled) {waitMsg();return;}
	intErrCnt=0;
	if(blnAbs1stTrack){
		if(objMmInfo[intSupremeTrkIdx].selMm=="t" && objMmInfo[intSupremeTrkIdx].actMm=="t") {
			intCurrActIdx=intSupremeTrkIdx;
			selMmPlay(intSupremeTrkIdx);
		} else {
			var idx=Math.floor(Math.random() * intActMmCnt);
		 	intCurrActIdx= arrActMm[idx];
			selMmPlay(intCurrActIdx);
		}
 	} else {
		var idx=Math.floor(Math.random() * intActMmCnt);
	 	intCurrActIdx= arrActMm[idx];
		selMmPlay(intCurrActIdx);
	} 
}

/**
 * 對已啟用播放項目進行「自動連續播放」的處理
 * 這是根據上面 blnAutoProc 的設定值而決定的動作。
 */
function playAuto(){
	var wmps=musicobj.playState;
	if(wmps>1 && wmps<10){return;}

	if(!blnAutoProc){wmpStop();return;}
	if(blnLonelyPlay){wmpStop(); return;} 
	if(!blnEnabled){wmpStop();return;}
	if(blnEOT) {
		if(blnLoopTrk) {
			startSMusic();
		} else { wmpStop(); }
 	} else {
		if(blnRndPlay) {
			rndPlay();
		} else {
			intErrCnt=0;
			var idx=intCurrActIdx;
	 		var blnFind=false;
			for(var i=0;i<intSelMmCnt;i++) {
				if(intCurrActIdx==arrSelMm[i]) {
					idx=i+1;
					blnFind=true;
				}
			}
			if(!blnFind) { return; }
			if(idx>=intSelMmCnt) {
				idx=0;
				intCurrActIdx=arrSelMm[idx];
			} else { intCurrActIdx=arrSelMm[idx]; }
			selMmPlay(intCurrActIdx); 
		}

	}
}

/**
 * 播放使用者在播放清單上所點選的單一曲目
 */
function selPlPlay(idx){
	var wmps= musicobj.playState;
	if(wmps==2 || wmps==3) { musicobj.controls.stop(); }
 	blnLonelyPlay=false; 
	intErrCnt=0;
	selMmPlay(idx);
}
/**
 * 播放上一首
 */
function playPrev(){
	var wmps= musicobj.playState;
	if(wmps==2 || wmps==3) { musicobj.controls.stop(); }
	blnLonelyPlay=false;
	if(!blnEnabled) { waitMsg();return; }
	if(blnEOT) { mkSelAct(); }
	intErrCnt=0;
	if(blnRndPlay) {
		rndPlay();
	} else {
		var idx=intCurrActIdx;
 		var blnFind=false;
		for(var i=0;i<intSelMmCnt;i++) {
			if(intCurrActIdx==arrSelMm[i]) {
				idx=i-1;
				blnFind=true;
			}
		}
		if(!blnFind) { startSMusic();return; }
		if(idx<0) {
			idx=intSelMmCnt-1;
			intCurrActIdx=arrSelMm[idx];
		} else { intCurrActIdx=arrSelMm[idx]; }
		selMmPlay(intCurrActIdx);
	}
}
/**
 * 播放下一首媒體
 */
function playNext(){
	var wmps= musicobj.playState;
	if(wmps==2 || wmps==3) { musicobj.controls.stop(); }
	blnLonelyPlay=false;
 	if(!blnEnabled){waitMsg();return;}
 	if(blnEOT){mkSelAct();}
	intErrCnt=0;
	if(blnRndPlay) {
		rndPlay();
	} else{
		var idx=intCurrActIdx;
 		var blnFind=false;
		for(var i=0;i<intSelMmCnt;i++) {
			if(intCurrActIdx==arrSelMm[i]) {
				idx=i+1;
				blnFind=true;
			}
		}
		if(!blnFind) { startSMusic();return; }
		if(idx>=intSelMmCnt) {
			idx=0;
			intCurrActIdx=arrSelMm[idx];
		} else {
			intCurrActIdx=arrSelMm[idx];
		}
		selMmPlay(intCurrActIdx);
	}
}

/**
 * 重試當前播放媒體
 */
function retryPlay(){
	var wmps=musicobj.settings;
	musicobj.URL=strCurrMediaUrl;
	if(!wmps.autoStart) { musicobj.controls.play(); }
}

/**
 * 重複播入當前媒體
 */
function chkRept(){
	var wmps=musicobj.playState;
	if(wmps == 3) { clearInterval(tidTLab); }
	if(blnRept) {
		musicobj.settings.playCount=1;
		blnRept=false;
		imgChange('rept',0);
		disp2.innerHTML="正常播放";
	} else {
		musicobj.settings.playCount=65535;
		blnRept=true;
		imgChange('rept',1);
		disp2.innerHTML="重複播放";
	}
	tidMsg= setTimeout('rtnTLab()',1000);
}
/**
 * 選擇播放模式：隨機、順序播放
 */
function chgPMode(){
	var wmps=musicobj.playState;
	if(wmps == 3) { clearInterval(tidTLab); }
	if(blnRndPlay) {
		musicobj.settings.setMode("shuffle", false);
		blnRndPlay=false;
		imgChange('pmode',0);
		disp2.innerHTML="順序播放";
	} else {
		musicobj.settings.setMode("shuffle", true); 
		blnRndPlay=true;
		imgChange('pmode',1);
		disp2.innerHTML="隨機播放";
	}
	tidMsg=setTimeout('rtnTLab()',1000); 
}
/**
 * 循環播放
 */
function chgTrkLoop(){
	var wmps=musicobj.playState;
	if(wmps == 3) { clearInterval(tidTLab); }
	if(blnLoopTrk) {
		blnLoopTrk=false;
		imgChange('tloop',0);
		disp2.innerHTML="不循環播放";
	} else {
		blnLoopTrk=true;
		imgChange('tloop',1);
		disp2.innerHTML="循環播放";
	}
	tidMsg=setTimeout('rtnTLab()',1000); 
}
/**
 * 0(Undefined) 8(MediaChanging) 9(MediaLocating) 10(MediaConnecting) 11(MediaLoading)
 * 12(MediaOpening) 13(MediaOpen) 20(MediaWaiting) 21(OpeningUnknownURL)
 */
function evtOSChg(f) {
	if(f==8 && blnUseSmi){document.getElementById('capText').innerHTML="";}
	if(f==9){disp2.innerHTML="(Access)"; imgChange("playt",6); if(blnStatusBar){window.status=('(Wait...Media Locating)');}} //display 'buffering' image
	if(f==10){disp2.innerHTML="(Connect)"; imgChange("playt",6); if(blnStatusBar){window.status=('(Wait...Media Connecting)');}} //display 'buffering' image
	if(f==11){disp2.innerHTML="(Loading)"; imgChange("playt",6); if(blnStatusBar){window.status=('(Wait...Media Loading)');}} //display 'buffering' image
	if(f==12){disp2.innerHTML="(Opening)"; imgChange("playt",6); if(blnStatusBar){window.status=('(Wait...Media Opening)');}} //display 'buffering' image
	if(f==20){disp2.innerHTML="(Waiting)"; imgChange("playt",6); if(blnStatusBar){window.status=('(Wait...Media Waiting)');}} //display 'buffering' image
	if(f==21){disp2.innerHTML="(Opening)"; imgChange("playt",6); if(blnStatusBar){window.status=('(Wait...Unknown Media URL)');}} //display 'buffering' image
	if(f==13) {
		var strTitle = musicobj.currentMedia.getItemInfo("title");
		if(strTitle.length <=0) { strTitle = "Unknown" }
		var strAuthor = musicobj.currentMedia.getItemInfo("Author");
		if(strAuthor.length <=0) { strAuthor = "Unknown" }
		var strCopy = musicobj.currentMedia.getItemInfo("Copyright");
		if(strCopy.length <=0) { strCopy = "Unknown" }
		var strType = musicobj.currentMedia.getItemInfo("MediaType");
		var strBitrate = musicobj.currentMedia.getItemInfo("Bitrate");
		var strBandwidth =  musicobj.network.bandwidth;
		var strDur=musicobj.currentMedia.durationString;
		var strUrl =musicobj.currentMedia.sourceURL;


		strMmInfo= "Media Title : " + strTitle + "\n\n"
		strMmInfo= strMmInfo + "Media Author : " + strAuthor + "\n\n"
		strMmInfo= strMmInfo + "Media URL : " +strUrl + "\n\n"
		strMmInfo= strMmInfo + "Media Copyright : " + strCopy +"\n\n" 
		strMmInfo= strMmInfo + "Media Type : " +strType +"\n\n"
		strMmInfo= strMmInfo + "Media Duration : " +strDur +"\n\n"
		strMmInfo= strMmInfo + "Media Bitrate : " + parseInt(strBitrate/1000) + " KBit/sec \n\n"
		strMmInfo= strMmInfo + "Media Bandwidth : " + parseInt(strBandwidth/1000) + " KHz \n\n"
		strMmInfo= strMmInfo + " (C)Copyright SupeSite/X-Space  \n";
		if(blnShowMmInfo){alert(strMmInfo);}

		if(blnExtMmUsed){
			if(strExtTitle=="nontitle"){strExtTitle="LoadTitle";}
			strCurrActTit=strExtTitle + "(Info: " + strAuthor + " - " + strTitle + ")";
			dTitle.innerHTML = strCurrActTit;
			if(blnStatusBar){window.status=(strCurrActTit);}
			return;
		}

		var trknum=intCurrActIdx+1;
		var ctit = objMmInfo[intCurrActIdx].mmTit;
	
		if(ctit=="nontitle" && objMmInfo[intCurrActIdx].mmeta=="f"){ 
			objMmInfo[intCurrActIdx].mmTit = "(Title) " + strAuthor + " - " + strTitle;
			ctit="(Info) " + strAuthor + " - " + strTitle;
			if(blnAbs1stTrack && intCurrActIdx==intSupremeTrkIdx){strCurrActTit= "[SupremeTrack:" + trknum + "] " + ctit;}
			else{strCurrActTit= " "+ trknum + ". " + ctit; }
		}

		if(objMmInfo[intCurrActIdx].mmeta=="t") {
			if(ctit=="nontitle") {
				if(blnAbs1stTrack && intCurrActIdx==intSupremeTrkIdx) {
					strCurrActTit="[SupremeTrack:"+ trknum +"] " + " ASXmode (Title:" + strAuthor + "- " + strTitle + ")" ;
				} else {
					strCurrActTit= " "+trknum + ". " + " ASXmode (Title:" + strAuthor +"- "+strTitle+")";
				}
			} else {
				if(blnAbs1stTrack && intCurrActIdx==intSupremeTrkIdx) {
					strCurrActTit="[SupremeTrack:"+trknum+"] " + ctit + " (Title:" + strAuthor +"- "+strTitle+")";
				} else {
					strCurrActTit= " "+trknum + ". " + ctit + " (Title:" + strAuthor +"- "+strTitle+")";
				}
			}
		}
		dTitle.innerHTML = strCurrActTit;

	}
}
/**
 * 0(Undefined) 1(Stopped) 2 (Paused) 3(Playing) 4(ScanFowrd) 5(ScanReverse)
 * 6(Buffering) 7(Waitng) 8(MediaEnded) 9(Transitioning) 10(Ready)
 */
function evtPSChg(f){
	switch(f){
		case 1:
			evtStop();
			break;
		case 2:
			evtPause();
			break;
		case 3:
			evtPlay();
			break;
		case 7:
			evtWait();
			break;
		case 8:
			setTimeout('playAuto()', intDelay);
			break;
	}
}

function evtWmpBuff(f) {
//	disp1 = document.getElementById('disp1');
	if(f){ disp2.innerHTML = "Buffering";
		var msg="(Buffering) " + strCurrActTit;
		dTitle.innerHTML = msg;
		imgChange("playt",6);
		if(blnStatusBar){window.status=(msg);}
	} else { 
		dTitle.innerHTML = strCurrActTit;
		showTLab();
		imgtog('playt',3);
	}
}


function evtWmpError() {
	intErrCnt=intErrCnt+1;
	musicobj.Error.clearErrorQueue();
	imgChange("pauzt",0);
	imgChange("playt",0);
	if(intErrCnt<=3) {
		strCurrMediaUrl=musicobj.URL;
		disp2.innerHTML="重試("+intErrCnt+")";
		var msg="(重試:" + intErrCnt +") " +strCurrActTit;
		dTitle.innerHTML="<錯誤> " +strCurrActTit;
		if(blnStatusBar){window.status=(msg);}
		tidErr=setTimeout('retryPlay()',1000);
	} else{	
		clearTimeout(tidErr);
		intErrCnt=0;showTLab();
		var msg="正在讀取播放媒體文件……";
		dTitle.innerHTML=msg;
		if(blnStatusBar){window.status=(msg);}	
		if(!blnLonelyPlay && blnAutoProc) { setTimeout('playAuto()',1000); }
	}
}

function evtWait() {
	disp2.innerHTML="(等待)";
	if(blnStatusBar){window.status=('(Wait...Media Waiting)');}
	imgChange("playt",6); //display 'buffering' image
}

function evtStop(){
	clearTimeout(tidErr);
	clearInterval(tidTLab);
	showTLab();
	intErrCnt=0;
	imgtog('vmute',3);
	imgChange("pauzt",0);
	imgChange("playt",0);
	dTitle.innerHTML="播放等待中……";
	if(blnStatusBar){window.status=('播放等待中……');return true;}
}

function evtPause(){
	imgChange("pauzt",1)
	imgChange("playt",0);
	imgChange("stopt",0);
	clearInterval(tidTLab);
	showTLab();
}

function evtPlay(){
	imgChange("pauzt",0)
	imgChange("playt",1);
	imgChange("stopt",0);
	imgtog('vmute',3); //recover abnormal 'mute' image
	tidTLab=setInterval('showTLab()',1000);
}

/**
 * 顯示時間長度Displaying Timer label(Elapse,Lapse)
 */
function showTLab(){
	var ps=musicobj.playState;
	disp2 = document.getElementById('disp2');
	if(ps==2 || ps==3){
		var cp=musicobj.controls.currentPosition
		var cps=musicobj.controls.currentPositionString
		var dur=musicobj.currentMedia.duration;
		var durs=musicobj.currentMedia.durationString;
		if(isNaN(dur) || dur==0){durs="(AIR)";}
		if(blnElapse){disp2.innerHTML= cps+" | "+durs;
			var msg=strCurrActTit + " ("+cps+" | "+durs+ ")";
			if(ps==2){msg="(Pause) "+ msg;}
			if(blnStatusBar){window.status=(msg);return true;}
		} else {
			var lapse
			if(isNaN(dur) || dur==0) {
				strLapse="Live";
			} else {
				lapse=dur-cp;
				var strLapse=wmpTime(lapse);
			}
			disp2.innerHTML= strLapse + " | "+durs;
			var msg= strCurrActTit + " (" + strLapse + " | "+durs + ")";
			if(ps==2) { msg="(暫停) "+ msg; }
			if(blnStatusBar) { window.status=(msg);return true; }
		}

	} else {disp2.innerHTML="00:00 | 00:00"; }
}

/**
 * 時間顯示模式
 */
function chgTimeFmt(){
	var wmps=musicobj.playState;
	if(wmps == 3) { clearInterval(tidTLab); }
	if(blnElapse) {
		blnElapse=false;
		disp2.innerHTML="倒計時";
	} else {
		blnElapse=true;
		disp2.innerHTML="順計時";
	}
 	tidMsg=setTimeout('rtnTLab()',1000); 
}

/**
 * 顯示時間指示器
 */
function rtnTLab(){
	clearTimeout(tidMsg);
	var wmps=musicobj.playState;
	wmps == 3 ? tidTLab=setInterval('showTLab()',1000) : showTLab();
}
/**
 * 計算時間長度
 */
function wmpTime(dur){
	if(isNaN(dur) || dur==0){return "Live";}
	var hh, min, sec, timeLabel
	hh = Math.floor(dur/3600);
	min = Math.floor(dur / 60)%60;
	sec = Math.floor(dur % 60);
	if (isNaN(min)){ return "00:00"; }
	if (isNaN(hh) || hh==0) {
		timeLabel="";
	} else {
 		if(hh >9) {
			timeLabel = hh.toString()+":";
		} else { timeLabel="0"+hh.toString() +":"; }
	}
	if ( min > 9 ) {
		timeLabel = timeLabel + min.toString() + ":";
	} else {
		timeLabel = timeLabel + "0" +min.toString() + ":";
	}
	if ( sec > 9 ) {
		timeLabel = timeLabel + sec.toString();
	} else {
		timeLabel = timeLabel + "0" + sec.toString();
	}
	return timeLabel;
}
/**
 * 靜音
 */
function wmpMute() {
	var wmps=musicobj.playState;
	if(wmps == 3) { clearInterval(tidTLab); }
	var ps = musicobj.settings;
	if(!ps.mute) {
		ps.mute = true;
		disp2.innerHTML="靜音";
		imgChange("vmute", 1);
	} else {
		ps.mute = false;
		disp2.innerHTML="取消靜音";
		imgChange("vmute", 0)
	}
	tidMsg=setTimeout('rtnTLab()',1000);
}
/**
 * 顯示音量
 */
function prnVol() { disp2.innerHTML= "音量. " + musicobj.settings.Volume + "%"; }
/**
 * 顯示指定的消息
 */
function waitMsg(){
 var outMsg="";
 if(blnUseSmi){document.getElementById('capText').innerHTML="";}
 switch(intSMusicStat){
	case 1 :
		outMsg="對不起，沒有找到播放媒體列表";
		dTitle.innerHTML=outMsg;
		if(blnStatusBar){window.status=(outMsg);return true;}
		break;
	case 2 :
		outMsg="請選擇待播放的媒體文件";
		dTitle.innerHTML=outMsg;
		if(blnStatusBar){window.status=(outMsg); return true;}
		break;
	case 3 :
		outMsg="警告: 您本機的Windows Media Player有問題，請確認Media Player是否可用";
		dTitle.innerHTML=outMsg;
		if(blnStatusBar){window.status=(outMsg);return true;}
		break;

	default :
		outMsg="SupeSite/X-Space 媒體播放器";
		dTitle.innerHTML=outMsg;
		if(blnStatusBar){window.status=(outMsg);return true;}
 }
}

function etcMsg(f, e1, e2){
	var outMsg="";
	if(blnUseSmi){document.getElementById('capText').innerHTML="";}
	switch(f){
		case 4 :
			outMsg="警告: 媒體 "+ (intCurrActIdx+1) + ". 路徑為空";
			dTitle.innerHTML=outMsg;
			if(blnStatusBar){window.status=(outMsg);return true;}
			break;
	
		case 5 :
			outMsg="警告: 媒體路徑為空，或不正常路徑";
			dTitle.innerHTML=outMsg;
			if(blnStatusBar){window.status=(outMsg);return true;}
			break;
		case 6 :
			outMsg="對不起，找不到媒體 No. " + e1 ;
			dTitle.innerHTML=outMsg;
			if(blnStatusBar){window.status=(outMsg);return true;}
			break;
		default :
			outMsg="SupeSite/X-Space 媒體播放器";
			dTitle.innerHTML=outMsg;
			if(blnStatusBar){window.status=(outMsg);return true;}
	}
}

function fullScreen(){
	var wmps=musicobj.playState;
	if(wmps==2 || wmps==3 ){ musicobj.fullscreen=true;}
}

/**
 * 當播放程序動作變更時，傳回 playState 的狀態值
 * 0(Undefined) 1(Stopped) 2 (Paused) 3(Playing) 4(ScanFowrd) 5(ScanReverse)
 * 6(Buffering) 7(Waitng) 8(MediaEnded) 9(Transitioning) 10(Ready)
 */
function chkWmpState(){
	return musicobj.playState;
}
/**
 * 當播放程序開啟媒體檔案準備播放時，傳回 openState 的狀態值
 * @return 0(Undefined) 8(MediaChanging) 9(MediaLocating) 10(MediaConnecting) 11(MediaLoading)12(MediaOpening) 13(MediaOpen) 20(MediaWaiting) 21(OpeningUnknownURL)
 */
function chkWmpOState(){
	return musicobj.openState;
}
/**
 * 檢查使用者的聯機狀態
 * @return true(已聯機到因特網) false(沒有聯機到因特網)
 */
function chkOnline() {
	return musicobj.isOnline;
}

/**
 * 音量調節功能
 */
document.onmouseup=function() { blnDragging=false; }
var sliderPosLeft;
var volSliderLength=48;
var blnDragging=false;
var mPosX;
var evtSrcObj;
var eobj;

/**
 * 初始化音量開關
 */
function initVol() {
	var ps=intVolume;
	if(isNaN(ps) || ps<0) { ps=0; }
	else if(ps>=100){ps=100;}
	volSliderObj = document.getElementById('volSlider');
	sliderPosLeft = parseInt(volSliderObj.style.left);
	volSliderObj.style.left = (sliderPosLeft + Math.floor(volSliderLength * ps/100)) + "px";;
}

function volTracking() {
	if (blnDragging) {	// && eobj.button==1
		musicobj.settings.Mute=false;
		imgChange("vmute", 0)
		var sliderLength= volSliderLength;
		var minLimit= 14;
		var maxLimit= 75;
		var mov =  sliderPosLeft + eobj.clientX - mPosX;
		if(mov <= minLimit){
			volSliderObj.style.left=minLimit + 'px';
			musicobj.settings.volume=0;
 			prnVol();
		}
		if(mov > maxLimit){
			volSliderObj.style.left=maxLimit + 'px';;
			musicobj.settings.volume=100;
 			prnVol();
		}
		if ((mov <= maxLimit) &&  (mov > minLimit )){
			volSliderObj.style.left = (sliderPosLeft + eobj.clientX - mPosX) + 'px';
			musicobj.settings.volume=Math.round(((mov-minLimit-2)/(sliderLength))*100);
 			prnVol();
		}

	}
	return false;
}
function setVol() {
	blnDragging=false;
}
function readyDrag(e) {
	if(document.layers){return false;}
	if(typeof e.srcElement != 'undefined') {
		evtSrcObj = e.srcElement.id;
	} else {
		evtSrcObj = e.target.id;
	}
	switch(evtSrcObj){
		case "volSlider" :
			mPosX=e.clientX;
			sliderPosLeft = parseInt(volSliderObj.style.left);
			blnDragging=true;
			eobj = e;
			document.onmouseup = setVol;
			volSliderObj.onmousemove = volTracking;
			break;
		case "procSlider" :
			moveProc();
			break;
		default :
			return false;
	}
}
/**
 * 媒體列表
 */
function playSel(){wmpStop();startSMusic();}

function dspList(){
	musicList = document.getElementById('xspace-mmList');
	if(intMmCnt >0 ){
		var list_num=0;
		document.getElementById('xspace-mmList').innerHTML='';
		for (var i=0; i < intMmCnt; i++) {
			list_num = i + 1;
			if(objMmInfo[i].selMm=="t") {
				elm=' <input type=checkbox  style="cursor:pointer;" onClick=chkItemSel('+ i +'); checked>' ;
			} else {
				elm = ' <input type=checkbox style="cursor:pointer;" onClick=chkItemSel('+ i +');>' ;
			}
			elm = elm + '&nbsp;' + list_num + '. ' 
			elm = elm + '<a href=javascript:;'
			elm = elm + ' onclick=\"selPlPlay(' + i + ');this.blur();\">'
			if(objMmInfo[i].mmTit =="nAnT") {
				elm = elm + "(Info)Tracing Media Author-Titles";
			} else {
				elm = elm + objMmInfo[i].mmTit;
			}
			elm= elm+  '</a><br>';
			musicList.innerHTML=musicList.innerHTML+elm;
		}
	} else { musicList.innerHTML='<div align=center> 對不起，沒有找到任何媒體文件 </div>'; }
}
