// 以下是playState状态值的说明:
// 0(未定义) 1(已停止播放) 2(已暂停播放) 3(正在播放中) 4(向前搜索) 5(向后搜索)
// 6(缓冲处理中) 7(等待中) 8(已播放完毕) 9(转换曲目中) 10(就绪状态)

// 以下是openState状态值的说明:
// 0(未定义) 8(转换媒体中) 9(寻找媒体中) 10(联机媒体中) 11(加载媒体中)
// 12(开启媒体中) 13(媒体已开启) 20(等待播放中) 21(正在开启不明的连结)


var Player = {	
	
	WMPlayer: null,
	Playlist: new Array(),

	ActIdx:	0,	//当前正在播放歌曲的id
	ActMusicId:	0,
	ActMusicTitle: null,
	ActMusicKey: null,
	ActMusicFileID:null,
	//events
	beforePlay:	null,
	afterPlay:	null,
	startPlay:	null,
	nextPlay:	null,
	
	rndPlay: null,
	
	DivStatus:	null,
	DivTitle:	null,
	DivTime:	null,
	
	tidErr:	null,
	tidMsg:	null,
	tidTLab:null,
	tidBuf: null,
	
	intErrCnt:	0,

	//是否随机播放
	blnRndPlay:	false,
	//是否循环播放
	blnLoopTrk:	true,
	
	//是否自动播放媒体档案：
	blnAutoStart:	false,
	//是否重复播放
	blnRept:	false,
	//自动连续播放
	blnAutoProc:	true,
	//正常方式(Elapse)抑或倒数方式(Lapse)显示时间
	blnElaps:	true,

	//每首曲目之间的延迟时间(msec)
	intDelay:	500,

	//vmax 代表最大音量(100), vmin 代表最小音量(0), vdep 代表调校音量的间隔(建议设为5至20之间)
	vmax:	100,
	vmin:	0,
	vdep:	10,
	
	init:	function(d2) {
		this.DivTime = d2;
		this.DivStatus = d2;
		
		var wmps = this.WMPlayer.playState;
		if(wmps == 3){	setInterval('Player.showTLab()',1000); return; }
		
		//init wmp control
		var wmps = this.WMPlayer.settings;
		wmps.autoStart = true;
		wmps.balance = 0;
		wmps.enableErrorDialogs = false;
		wmps.invokeURLs = false;
		wmps.mute = false;
		wmps.playCount = 1;
		wmps.rate = 1;
		this.WMPlayer.enabled = true;
		//init wmp end
		
		this.showTLab();
		
		if(this.blnAutoStart)	this.start();
	},	
	
	add: 	function(id,title,URL,Key,FileID) {  	
		if(this.Playlist == null){this.Playlist=new Array(); idx=0;}
		else {idx=this.Playlist.length;}
		
		this.SelMmCnt++;
		//setTimeout('Player.dtest()',1000);

		this.Playlist[idx] = new this.music(id, title,URL,Key,FileID);
	},	
	
	music:	function(id, title,URL,Key,FileID){
			this.id = id;
			this.url = URL;
			this.title = title;
			this.selected = true;
			this.Key = Key;
			this.FileID=FileID;
	},
	
	//选取或不选取播放清单项目
	toggle:	function(idx){
		this.Playlist[idx].selected = !this.Playlist[idx].selected;
	},	
		
	//clear all music
	clearAll:	function(){
		this.Playlist = new Array();
	},

	clearOne:	function(idx){
		this.toggle(idx);
	},

	isPlaying:	function(){
		return 	this.WMPlayer.playState==3||this.WMPlayer.playState==2;
	},
	
	getMusicIdx:	function(){
		return this.ActIdx;	
	},
	
	getMusicID:	function(){
		return this.ActMusicId;	
	},
	
	getMusicTitle:	function(){
		return this.ActMusicTitle;	
	},
	
	nextIdx:	function(step){
		//下一首歌曲
		if(this.nextPlay != null){	return this.fireEvent(this.nextPlay);	}
		
		if(this.Playlist.length==0)	return -1;
		
		var st, count=this.Playlist.length;		

		for(st=this.ActIdx + step; count>0; st+=step){
			if(st<0)	st=this.Playlist.length-1;
			if(st>=this.Playlist.length)	st=0;
			if(this.Playlist[st].selected){
				return st;
			}else{
				count--;
			}
		}
		return -1;
	},
	
	playFrom:	function(idx){
		this.play(idx);
	},

	playOne:	function(id,title,URL,Key){		
		//this.showStatus("播放 - " + title);
		this.stop();
		
		this.ActMusicId = id;
		this.ActMusicTitle = title;
		this.ActMusicKey=Key;

		this.WMPlayer.URL = URL;
		
		this.WMPlayer.controls.play();
	},
	
	//在暂停播放和继续播放之间进行切换
	togglePause:	function(){
		var wmps = this.WMPlayer.playState;
		var wmpc = this.WMPlayer.controls;
		clearInterval(this.tidTLab);
		clearTimeout(this.tidMsg);
		if(wmps==2){wmpc.play();}
		if(wmps==3){wmpc.pause();}
		return;
	},
	
	//开始播放
	start:	function(){		
		var wmps = this.WMPlayer.playState;
		
		if(wmps==2){	this.WMPlayer.controls.play(); return;}
		if(wmps==3){ return;}
	
		this.intErrCnt=0;
		if(this.blnRndPlay){
			this.rndPlay();
		}else {
			this.ActIdx=-1;
			this.play(this.nextIdx(1));
		}
	},
	
	playPrev: function(){
		var wmps = this.WMPlayer.playState;
		if(wmps==2 || wmps==3){	this.WMPlayer.controls.stop();}
		
		this.intErrCnt=0;
		if(this.blnRndPlay){this.rndPlay();}
		else {			
			this.play(this.nextIdx(-1));
		}
	},

	playNext: function(){
			var wmps = this.WMPlayer.playState;
			if(wmps==2 || wmps==3){	this.WMPlayer.controls.stop();}
			
			this.intErrCnt=0;
			if(this.blnRndPlay){this.rndPlay();}
			else {
				this.play(this.nextIdx(1));
			}
	},
	
	playAuto: function(){		
		if(this.blnRept){this.retryPlay();return;}
		if(!this.blnAutoProc){this.stop();return;}
		if(this.blnRndPlay){this.rndPlay();}
		else{			
			this.play(this.nextIdx(1));
		}
	},

	
	playRandom: function(){
			intErrCnt=0;
			var idx=Math.floor(Math.random() * intActMmCnt);
			this.play(arrActMm[idx]);
	},

	play: function(idx){
		if(idx == -1 || !this.Playlist[idx]){	this.stop(); return; }
		
		clearTimeout(this.tidErr);
		this.ActIdx=idx;	
		this.ActMusicId=this.Playlist[idx].id;		
		this.ActMusicTitle=this.Playlist[idx].title;
		this.ActMusicKey=this.Playlist[idx].Key;
		this.ActMusicFileID=this.Playlist[idx].FileID;
		try{
			parent.ContentFrame.showInfo();//happyli addd
		}catch(e){
			;
		}
		
		var strURL=this.Playlist[idx].url;
		//alert(strURL);
		this.WMPlayer.URL=strURL;
		this.WMPlayer.controls.play();
		parent.ContentFrame.TipURL.innerHTML=strURL;//.substr("http://music.funnyai.com/".length);
	},
				
	stop: function(){
		this.intErrCnt=0;
		clearTimeout(this.tidErr);
		clearInterval(this.tidTLab);	
		clearInterval(this.tidBuf);
		this.WMPlayer.controls.stop();
		this.WMPlayer.close();
		this.showTLab();
	}

};

Player.showStatus = function(msg) {
	try{
		this.DivStatus.innerHTML = msg;
	}catch(ex){}			
};

Player.showTime = function(msg) {
		try{ 
       this.DivTime.innerHTML = msg;
    }catch(ex){}
};

Player.showTitle = function(msg) {
		try{
			this.DivTitle.innerHTML = msg;
		}catch(ex){}
};

//计算时间长度
Player.wmpTime = function (dur){
	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;
};

//对媒体档案进行缓冲处理(Buffering)的动作
Player.evtWmpBuff = function(f){		
	if(f){
		this.showBuffering();
		clearInterval(this.tidBuf);
		this.tidBuf = setInterval('Player.showBuffering()',1000);		
	}
};
Player.showBuffering = function(){		
	if(this.WMPlayer.network.bufferingProgress < 100)
		this.showStatus("缓冲中..." + this.WMPlayer.network.bufferingProgress + "%");	
}

//当无法联机到媒体档案时，显示错误讯息
Player.evtWmpError = function(){
	
	this.intErrCnt++;
	this.WMPlayer.Error.clearErrorQueue();

	if(this.intErrCnt<3){
		this.showStatus("重试连接(" + this.intErrCnt + ")");
		//alert("无法播放地址："+this.WMPlayer.URL);
		link = this.WMPlayer.URL.toUpperCase();		
		ext = link.substring(link.lastIndexOf('.'));
		/*
		backup = "/mp3s/" + this.ActMusicId + ext;
		
		//check playlist or single
		//playlist
		if(this.ActIdx != -1 && this.Playlist[this.ActIdx] && this.Playlist[this.ActIdx].id == this.ActMusicId){
			if(this.intErrCnt==2) this.Playlist[this.ActIdx].url = backup;
			this.tidErr=setTimeout('Player.retryPlay()',1000);
		}else{	//single
			//this.tidErr=setTimeout('Player.playOne(' + this.ActMusicId + ',"' + this.ActMusicTitle + '","' + backup + '")',1000);
			this.tidErr=setTimeout('Player.WMPlayer.URL="' + backup + '"',1000);
		}
		*/
	} else {
		clearTimeout(this.tidErr);
		this.intErrCnt=0;
		setTimeout('Player.playAuto()',1000);
	}
};

// retryPlay() 函式: 再次尝试联机到媒体档案
Player.retryPlay = function(){
	this.play(this.ActIdx);
}
	
Player.showTLab = function(){
	var ps = this.WMPlayer.playState;	
	if(ps==2 || ps==3){
		var cp=this.WMPlayer.controls.currentPosition;
		var cps=this.WMPlayer.controls.currentPositionString;
		var dur=this.WMPlayer.currentMedia.duration;
		var durs=this.WMPlayer.currentMedia.durationString;
		if(this.blnElaps)
			this.showTime(cps + " | " + durs);
		else{
			var laps = dur-cp;
			var strLaps = wmpTime(laps);
			this.showTime(strLaps + " | " + durs);
		}		
	}
	else if(ps==1 || ps==0 || ps==10){
		this.showTime("00:00 | 00:00");
	}
}

//打开媒体档案的动作
Player.evtOSChg = function(f){
	// 以下是状态值 (f) 的说明:
	// 0(未定义) 8(转换媒体中) 9(寻找媒体中) 10(联机媒体中) 11(加载媒体中)
	// 12(开启媒体中) 13(媒体已开启) 20(等待播放中) 21(正在开启不明的连结)
	switch(f){
		case 12:
			this.fireEvent(this.beforePlay);	
			this.showStatus("正在连接...");
			break;
		case 13:
			this.fireEvent(this.startPlay);
			break;
	}
}

//切换播放程序的动作
Player.evtPSChg = function(f){
	// 以下是状态值 (f) 的说明:
	// 0(未定义) 1(已停止播放) 2(已暂停播放) 3(正在播放中) 4(向前搜索) 5(向后搜索)
	// 6(缓冲处理中) 7(等待中) 8(已播放完毕) 9(转换曲目中) 10(就绪状态)
	switch(f){
		case 1:	//stop
			clearTimeout(this.tidErr);
			clearInterval(this.tidTLab);
			clearInterval(this.tidBuf);
			this.intErrCnt=0;
			break;
		case 2:	//pause
			clearInterval(this.tidTLab);
			break;
		case 3:
			this.tidTLab=setInterval('Player.showTLab()',1000);		
			break;
		case 8:
			this.fireEvent(this.afterPlay);
			setTimeout('Player.playAuto()', this.intDelay);
		break;
	}
};

Player.fireEvent = function(evt){
		if (typeof evt=="function"){
 			return evt();
		}
		else if(evt!=null && evt.length>0){
 			return eval(evt);
		}				
};

// wmpVolUp() 函式: 增加音量(Volume Up)
Player.volUp = function(){
	var ps = this.WMPlayer.settings;
	if(ps.mute){ps.mute=false;}
	else {
		if(ps.volume >= this.vmax-this.vdep){
			ps.volume = this.vmax;
		}
		else 
			ps.volume = ps.volume + this.vdep;
	}
	return ps.volume;
};

Player.volDn = function(){
	var ps = this.WMPlayer.settings;
	if(ps.mute){ps.mute=false;}
	else {
		if(ps.volume <= this.vdep){
			ps.volume = this.vmin;
		}
		else 
			ps.volume = ps.volume - this.vdep;
	}
	return ps.volume;
};

Player.setVol = function(v){
	var ps = this.WMPlayer.settings;
	if(ps.mute){ps.mute=false;}
	else {
		ps.volume=(this.vmax-this.vmin)*v + this.vmin;
	}
	return ps.volume;		
};

Player.getVol = function(){
	var ps = this.WMPlayer.settings;
	return ps.volume/this.vmax;
};

Player.mute = function(){
	var ps = this.WMPlayer.settings;
	ps.mute = !ps.mute;
	return 0;
};

Player.CachedInfo = new Array();

Player.hasInfo = function(id){
    var exists = false;
    for (var i in this.CachedInfo) {
			if (i == id && this.CachedInfo[i] != null) {
	    	exists = true;
	    	break;
			}
    }
    return exists;
};

Player.setInfo = function(id, info){
	this.CachedInfo[id] = info;
};	

Player.getInfo = function(id){
	return this.CachedInfo[id];
};