// ------------------------------------
// ブラウザ判定
// ------------------------------------
var ua = navigator.userAgent;
var msPlayer = false;
if((ua.indexOf("Netscape")>-1) || (ua.indexOf("MSIE")>-1)){
	msPlayer=true;
}

// ------------------------------------
// Volume変数
// ------------------------------------
var wmpVolArrayIndex = 4;
var wmpVolArray = new Array('0','10','25','40','60','80','100');
var wmpVol = wmpVolArray[wmpVolArrayIndex];

var MGNvoldown = 'ctrl_vol_down';
var MGNvoldisp = 'ctrl_vol_disp';
var MGNvolup = 'ctrl_vol_up';

function BTNstate(BTN,BTNreq) {
//	var vol1=document.all && document.all('volbar') || document.getElementById && document.getElementById('volbar');
	// ボリュームダウンボタン
	if (BTN == MGNvoldown) {
		//alert('voldown:' + BTNreq);
			if (BTNreq == '-do'){
				if(wmpVolArrayIndex != 0){
					wmpVolArrayIndex--;
					wmpVol = wmpVolArray[wmpVolArrayIndex]; 
					JVolume(wmpVol);
					$('mc_vsli2').src = '/img/mov_control/vol_' + wmpVol + '.gif';
					$('mc_vsli2').alt = wmpVol + '%';
				}
			}
		}
	// ボリュームダウンボタン
	if (BTN == MGNvolup) {
		//alert('volup:' + BTNreq);
		if (BTNreq == '-do'){
			if(wmpVolArrayIndex < wmpVolArray.length-1){
				wmpVolArrayIndex++;
				wmpVol = wmpVolArray[wmpVolArrayIndex]; 
				JVolume(wmpVol);
				$('mc_vsli2').src = '/img/mov_control/vol_' + wmpVol + '.gif';
				$('mc_vsli2').alt = wmpVol + '%';
			}
		}
	}
}

// ボリューム
function JVolume(value) {
//	var vol1=document.all && document.all('volbar') || document.getElementById && document.getElementById('volbar');
//	var vol2=document.all && document.all('vol') || document.getElementById && document.getElementById('vol');
//	var vx = vol1.scrollWidth * val / 100 - ( vol2.scrollWidth / 2 ) + 2;
//	vx = parseInt(vx,10);
//	if (vx > 88) vx = 83;
//	vol2.style.left = vx + 'px';
	//音量変更
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.settings.volume = value;
//	mute.style.background = 'url(/img/player/mute_off.gif) 4px 0px no-repeat';

//	if (!wmpMute) {
//		$('Viewer').settings.volume = value;
		//wmpMute = false;
		//btnStateMuteWait();
//	}
}

// 初期化
function initSlider(){
	// スライダー関連マウスイベントの定義
//	$('sldPosClick').onmousedown = sldPos_dragstart;
//	$('sldPosClick').onmousemove = sld_dragmove;
//	$('sldPosClick').onmouseup = sld_dragend;
//	$('sldPosClick').onmouseout = sld_out;
//	$('sldPosClick').onmouseover = sld_over;
//	sldPosMove(0);
	$('moviecontrol').src = '/img/mov_control/vol_' + wmpVolArrayIndex + '.gif';
	$('moviecontrol').alt = wmpVol;
}

// ------------------------------------
// カーソル移動間隔制御用変数
// ------------------------------------
//CM用カーソルタイマーオブジェクト
var intervalCm = null;
//本編用カーソルタイマーオブジェクト
var intervalMain = null;
//Status用カーソルタイマーオブジェクト
var intervalStatus = null;
//タイマー解除
function clearTimerObj(){
	clearTimerObjSlidebar();
	if (intervalStatus!=null) {
		clearInterval(intervalStatus);
		intervalStatus=null;
	}
}
function clearTimerObjSlidebar(){
	if (intervalCm!=null) {
		clearInterval(intervalCm);
		intervalCm=null;
	}
	if (intervalMain!=null) {
		clearInterval(intervalMain);
		intervalMain=null;
	}
}
function setTimerCm(interval){
	if (intervalMain!=null) {
		clearInterval(intervalMain);
		intervalMain=null;
	}
	if (intervalCm==null) intervalCM=intervalCm=window.setInterval("PosMove()",interval);
}
function setTimerMain(interval){
	if (intervalCm!=null) {
		clearInterval(intervalCm);
		intervalCm=null;
	}
	if (intervalMain==null) intervalMain=window.setInterval("PosMove()",interval);
}
function setTimerStatus(interval){
	if (intervalStatus==null) intervalStatus = window.setInterval("now_Status()",interval);
}
// ------------------------------------
// 音声スライダー操作ドラッグ状態フラグ（ドラッグ中=1）
var volDrag = 0;
//スライダー操作ドラッグ状態フラグ（ドラッグ中=1）
var sldDrag = 0;
// 現在のステータス初期値
var now_status = '';
//スライドバー初期位置
var startPos = 10;
//スライドカーソルイメージ幅
var sldWidth = 12;
//スライドバー幅
var sliderWidth = 0;
//スライドバーカーソル位置
var pos = 0;
// ------------------------------------
// 画面ロード時の処理
// ------------------------------------
function loader(interval){
	//スライドバー横幅計算
	var progressWidth = 0;
//	progressWidth = screen.availWidth - 125 - 33;
	progressWidth = 640;
	sliderWidth = progressWidth - startPos * 2 - 60;
	document.all["progress"].style.width = progressWidth;
	document.all["prgSlider"].style.width = sliderWidth;
	var obj=document.all && document.all('prgClick') || document.getElementById && document.getElementById('prgClick');
	if (obj) obj.style.width = sliderWidth;
	changeStopPos(1);
	changePlayPos(2);
	changeRewindPos(0);
	changeForwardPos(0);
	changeNextPos(0);
	changePrevPos(0);

	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if(msPlayer){
		obj.settings.autoStart=true;
		obj.settings.volume=50;
		move_volume_x(50);
		obj.settings.mute = false;
		mute.title = "ミュート";
		mute.style.background = 'url(/img/player/mute_off.gif) 4px 0px no-repeat';
		obj.settings.rate=1.0;
	}
	//ステータス表示変更タイマー
	setTimerStatus(interval);
}
// イベント処理割り当て
function wmp_control_posmove(){
	var objT=document.all && document.all('prgClick') || document.getElementById && document.getElementById('prgClick');
	if (objT) objT.onmousedown = Pos_dragstart;
	var vol=document.all && document.all('volbar') || document.getElementById && document.getElementById('volbar');
	vol.onmousedown = Vol_dragstart;
	vol.onmousemove = Vol_dragmove;
	vol.onmouseup = Vol_dragend;
	vol.onmouseout = Vol_dragout;
}
// ------------------------------------
// スライドバー制御
// ------------------------------------
function PosMove() {
	if(!sldDrag){
		var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
		if(obj.controls && obj.currentMedia)
			pos = ( obj.controls.currentPosition / obj.currentMedia.duration ) * 100 ;
		if(!pos) { pos = 1; }
		sld_Move(pos);
	}else{
		return false;
	}
}
function sld_Move(value) {
	var obj=document.all && document.all('spc') || document.getElementById && document.getElementById('spc');
	var ob2=document.all && document.all('prg') || document.getElementById && document.getElementById('prg');
	if (!obj || !ob2) return false;
	var posPix = ( ob2.offsetWidth - ( sldWidth / 2 ) ) * ( value / 100 );
	if (posPix > sliderWidth- sldWidth) {
		posPix = sliderWidth- sldWidth;
	}
	if (posPix < 1) { posPix = 1; }
	obj.width = posPix + 'px';
}
function Pos_dragstart(e){
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if (obj.controls.isAvailable("Next")) {
		sldDrag = 1;
		var obj=document.all && document.all('spc') || document.getElementById && document.getElementById('spc');
		var posPix = window.event.clientX - startPos - sldWidth/2;
		if (posPix < 1) { posPix = 1; }
		if (posPix > sliderWidth- sldWidth) {
			posPix = sliderWidth- sldWidth;
		}
		obj.width = posPix + 'px';
	}
}
function Pos_dragend(){
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if (obj.controls.isAvailable("Next")) {
		if (sldDrag) {
			var obj=document.all && document.all('prg') || document.getElementById && document.getElementById('prg');
			var value = (window.event.clientX - startPos - sldWidth/2)/(obj.offsetWidth - sldWidth/2) * 100;
			mov_Move(value);
		}
	}
	sldDrag = 0;
}
function Pos_dragmove(){
	if (sldDrag) {
		var obj=document.all && document.all('spc') || document.getElementById && document.getElementById('spc');
		var value = window.event.clientX - startPos - sldWidth/2;
		if (value < 1) { value = 1; }
		if (value > sliderWidth- sldWidth) {
			value = sliderWidth- sldWidth;
		}
		obj.width = value;
		return false;
	}
}
function mov_Move(value) {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if (obj.controls.isAvailable("Next")) {
		if (value < 1) { value = 1; }
		pos = obj.currentMedia.duration * ( value / 100 );
		obj.controls.currentPosition = pos;
	}
}
// ------------------------------------
// フルスクリーン解除
// ------------------------------------
function wmp_control_ns() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.fullscreen=false;
}
// ------------------------------------
// 現在のステータス表示
// ------------------------------------
function now_Status(){
	var part=document.all && document.all('spstats') || document.getElementById && document.getElementById('spstats');
	if (!part) return false;
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if (obj.status) now_status = obj.status;
	//もしステータスが'接続しています'のとき
	if (now_status.match(/接続しています/)) {
		now_status += obj.network.downloadProgress + '%';
	}
	//もしステータスが'バッファ処理中'のとき
	else if (now_status.match(/バッファ中/)) {
		now_status += '...' + obj.network.bufferingProgress + '%';
	}
	//もしステータスが'再生中'のとき
	else if (now_status.match(/再生中/)) {
		if (obj.currentMedia.duration==0.0) {
				now_status = obj.currentMedia.name;
		} else {
			if (obj.settings.rate == 1.0) {
				now_status = '再生中&nbsp;(&nbsp;' + obj.controls.currentPositionString + '&nbsp;/&nbsp;' + obj.currentMedia.durationString + '&nbsp;)';
			} else if (obj.settings.rate > 1.0) {
				now_status = obj.settings.rate + '倍速再生&nbsp;(&nbsp;' + obj.controls.currentPositionString + '&nbsp;/&nbsp;' + obj.currentMedia.durationString + '&nbsp;)';
			} else if (obj.settings.rate < 1.0) {
				now_status = '巻戻し中&nbsp;(&nbsp;' + obj.controls.currentPositionString + '&nbsp;/&nbsp;' + obj.currentMedia.durationString + '&nbsp;)';
			}
		}
	}
	else if (now_status.match(/早送り中/)) {
		now_status = '早送り中&nbsp;(&nbsp;' + obj.controls.currentPositionString + '&nbsp;/&nbsp;' + obj.currentMedia.durationString + '&nbsp;)';
	}
	else if (now_status.match(/巻き戻し中/)) {
		now_status = '巻戻し中&nbsp;(&nbsp;' + obj.controls.currentPositionString + '&nbsp;/&nbsp;' + obj.currentMedia.durationString + '&nbsp;)';
	}
	//ステータスの状態を「id="spstats"」へ挿入
	part.innerHTML = now_status;
}
// ------------------------------------
// リモコン部ボタン制御
// ------------------------------------
function wmp_control_play() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if(obj.playState==3 || obj.playState==4 || obj.playState==5){
		obj.controls.pause();
		$('mc_play2').src = '/img/mc_play.gif';
		$('mc_play2').alt = '再生開始';
		$('mc_ff2').src = '/img/mc_ff.gif';
		$('mc_ff2').alt = '早送り';
		$('mc_rew2').src = '/img/mc_rew.gif';
		$('mc_rew2').alt = '巻戻し';
	}else{
		obj.settings.rate = 1.0;
		if(msPlayer){
			obj.controls.play();
		}else{
			obj.play();
		}
		$('mc_play2').src = '/img/mc_onestop.gif';
		$('mc_play2').alt = '一時停止';
	}
}
function wmp_control_fastReverse() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if (obj.controls.isAvailable("FastReverse")) {
		var currentRate = obj.settings.rate;
		changeForwardPos(1);
		if (currentRate == -5.0) {
			changeRewindPos(1);
			obj.settings.rate = 1.0;
			$('mc_rew2').src = '/img/mc_rew.gif';
			$('mc_rew2').alt = '巻戻し';
		} else {
			changeRewindPos(2);
			obj.settings.rate = -5.0;
			$('mc_rew2').src = '/img/mc_rew5.gif';
			$('mc_rew2').alt = '巻戻し中';
			$('mc_ff2').src = '/img/mc_ff.gif';
			$('mc_ff2').alt = '早送り';
		}
	}
}
function wmp_control_fastForward() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	if (obj.controls.isAvailable("FastForward")) {
		var currentRate = obj.settings.rate;
		changeRewindPos(1);
		if (currentRate == 1.0) {
			changeForwardPos(2);
			obj.settings.rate = 1.4;
			$('mc_ff2').src = '/img/mc_ff_14.gif';
			$('mc_ff2').alt = '1.4倍速';
			$('mc_rew2').src = '/img/mc_rew.gif';
			$('mc_rew2').alt = '巻戻し';
		} else if (currentRate == 1.4) {
			changeForwardPos(3);
			obj.settings.rate = 2.0;
			$('mc_ff2').src = '/img/mc_ff_2.gif';
			$('mc_ff2').alt = '2倍速';
			$('mc_rew2').src = '/img/mc_rew.gif';
			$('mc_rew2').alt = '巻戻し';
		} else if (currentRate == 2.0) {
			changeForwardPos(4);
			obj.settings.rate = 5.0;
			$('mc_ff2').src = '/img/mc_ff_5.gif';
			$('mc_ff2').alt = '5倍速';
			$('mc_rew2').src = '/img/mc_rew.gif';
			$('mc_rew2').alt = '巻戻し';
		} else if (currentRate == 5.0) {
			changeForwardPos(1);
			obj.settings.rate = 1.0;
			$('mc_ff2').src = '/img/mc_ff.gif';
			$('mc_ff2').alt = '早送り';
		} else {
			changeForwardPos(2);
			obj.settings.rate = 1.4;
			$('mc_ff2').src = '/img/mc_ff_14.gif';
			$('mc_ff2').alt = '1.4倍速';
			$('mc_rew2').src = '/img/mc_rew.gif';
			$('mc_rew2').alt = '巻戻し';
		}
	}
}
function wmp_control_pause() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.controls.pause();
}
function wmp_control_stop() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.settings.rate = 1.0;
	obj.controls.stop();
}
function wmp_control_next() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.settings.rate = 1.0;
	obj.controls.next();
}
function wmp_control_prev() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.settings.rate = 1.0;
	obj.controls.previous();
}
function wmp_control_top() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	//obj.settings.rate = 1.0;
	obj.controls.stop();
	if(msPlayer){
		obj.controls.play();
	}else{
		obj.play();
	}
}
function changePlayPos(status) {
	var obj=document.all && document.all('play') || document.getElementById && document.getElementById('play');
	if(status==0){
		obj.style.backgroundPositionY = 0;
		document.getElementById('b_play').title = "再生";
	}else if(status==1){
		obj.style.backgroundPositionY = -27;
		document.getElementById('b_play').title = "一時停止";
	}else if(status==2){
		obj.style.backgroundPositionY = -54;
		document.getElementById('b_play').title = "";
	}else{
		obj.style.backgroundPositionY = 0;
		document.getElementById('b_play').title = "再生";
	}
}
function changeStopPos(status) {
	var obj=document.all && document.all('stop') || document.getElementById && document.getElementById('stop');
	if(status){
		obj.style.backgroundPositionY = -22;
	}else{
		obj.style.backgroundPositionY = 0;
	}
}
function changePrevPos(status) {
	if (document.all.prev) {
		var obj=document.all.prev;
		if(status==0){
			obj.style.backgroundPositionY = -22;
		}else if(status==1){
			obj.style.backgroundPositionY = 0;
		}else{
			obj.style.backgroundPositionY = 0;
		}
	}
}
function changeNextPos(status) {
	if (document.all.next) {
		var obj=document.all.next;
		if(status==0){
			obj.style.backgroundPositionY = -22;
		}else if(status==1){
			obj.style.backgroundPositionY = 0;
		}else{
			obj.style.backgroundPositionY = 0;
		}
	}
}
function changeRewindPos(status) {
	if (document.all.rewind) {
		var obj=document.all.rewind;
		if(status==0){
			obj.style.backgroundPositionY = -20;
			obj.title = "";
		}else if(status==1){
			obj.style.backgroundPositionY = 0;
			obj.title = "巻き戻し";
		}else if(status==2){
			obj.style.backgroundPositionY = 0;
			obj.title = "再開（標準速度）";
		}else{
			obj.style.backgroundPositionY = 0;
			obj.title = "巻き戻し";
		}
	}
}
function changeForwardPos(status) {
	if (document.all.forward) {
		var obj=document.all.forward;
		if(status==0){
			obj.style.backgroundPositionY = -20;
			obj.title = "";
		}else if(status==1){
			obj.style.backgroundPositionY = 0;
			obj.title = "1.4倍速早送り";
		}else if(status==2){
			obj.style.backgroundPositionY = 0;
			obj.title = "2 倍速再生";
		}else if(status==3){
			obj.style.backgroundPositionY = -40;
			obj.title = "5 倍速早送り";
		}else if(status==4){
			obj.style.backgroundPositionY = -60;
			obj.title = "再開（標準速度）";
		}else{
			obj.style.backgroundPositionY = 0;
			obj.title = "1.4倍速早送り";
		}
	}
}
// ------------------------------------
// リモコン部音量制御
// ------------------------------------
//音声ミュートの切り替え
function wmp_control_mute() {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	var mute=document.all && document.all('mute') || document.getElementById && document.getElementById('mute');
	if(obj.settings.mute){
		obj.settings.mute = false;
		mute.title = "ミュート";
		mute.style.background = 'url(/img/player/mute_off.gif) 4px 0px no-repeat';
	}else{
		obj.settings.mute = true;
		mute.title = "ミュート解除";
		mute.style.background = 'url(/img/player/mute_on.gif) 4px 0px no-repeat';
	}
}
function move_volume_x(val) {
	//つまみ移動
	var vol1=document.all && document.all('volbar') || document.getElementById && document.getElementById('volbar');
	var vol2=document.all && document.all('vol') || document.getElementById && document.getElementById('vol');
	var vx = vol1.scrollWidth * val / 100 - ( vol2.scrollWidth / 2 ) + 2;
	vx = parseInt(vx,10);
	if (vx > 88) vx = 83;
	vol2.style.left = vx + 'px';
	//音量変更
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.settings.volume=val/83*100;
	mute.style.background = 'url(/img/player/mute_off.gif) 4px 0px no-repeat';
}
function wmp_control_volume(v) {
	var obj=document.all && document.all('player') || document.getElementById && document.getElementById('player');
	obj.settings.volume=Math.round(v*100);
}
//ボリューム調整(マウスが押されたとき)
function Vol_dragstart(e){
	var vol1=document.all && document.all('volbar') || document.getElementById && document.getElementById('volbar');
	var vol2=document.all && document.all('vol') || document.getElementById && document.getElementById('vol');
	var bx = vol1.scrollWidth;
	var ex = event.x;
	var mx = (ex / bx) * 100;
	volDrag = 1;
	mx = parseInt(mx,10);
	move_volume_x(mx);
}
//ボリューム調整(マウスが離れたとき)
function Vol_dragend(e){
	if (volDrag) {
		var vol=document.all && document.all('volbar') || document.getElementById && document.getElementById('volbar');
		var bx = vol.scrollWidth;
		var ex = event.x;
		var mx = (ex / bx) * 100;
		mx = parseInt(mx,10);
		move_volume_x(mx);
	}
	volDrag = 0;
}
//ボリューム調整(マウスのドラッグ中)
function Vol_dragmove(e){
	if (volDrag) {
		var vol1=document.all && document.all('volbar') || document.getElementById && document.getElementById('volbar');
		var vol2=document.all && document.all('vol') || document.getElementById && document.getElementById('vol');
		var bx = vol1.scrollWidth;
		var ex = event.x;
		var mx = (ex / bx) * 100;
		mx = parseInt(mx,10);
		move_volume_x(mx);
		return false;
	}
}
//ボリューム調整(マウスが外れたとき)
function Vol_dragout(e){
	volDrag = 0;
}
// ------------------------------------
// イベント処理(マウス操作共通)
// ------------------------------------
document.onmouseup=moveme_onmouseup;
document.onmousemove=moveme_onmousemove;
document.onmouseout=mouseoutHandler;
document.onmouseover=mouseoverHandler;
function moveme_onmouseup() {
	Pos_dragend();
}
function moveme_onmousemove() {
	Pos_dragmove();
}
function mouseoutHandler() {
	if (sldDrag == 1) {
		if (window.event.srcElement.tagName == 'body') {
			sldDrag = 0;
			PosMove();
		}
	}
}
function mouseoverHandler() {
	mouseoutHandler();
}
// ------------------------------------
