function startCheckPlayer() {
	window.setTimeout("checkPlayer()",1000);
	window.setInterval("checkPlayerStatus()",1000);
}
function checkPlayerStatus() {
	var player = document.getElementById("MediaPlayer");
	var playerStatus = parseInt(player.playState);
	pos = player.Controls.currentPosition;
	if(playerStatus > 0) {
		if(playerStatus == 3) {
			dur = parseInt(player.currentMedia.duration);
			min = parseInt(dur / 60);
			sec = parseInt(dur % 60);
			if (min < 10) { min="0"+min; }
			if (sec < 10) { sec="0"+sec; }
			document.getElementById('state').innerHTML="Видео - <b>" + min + ":" + sec;
			pos = player.Controls.currentPosition;
			prc = parseInt(dur - pos);
			min = parseInt(prc / 60);
			sec = parseInt(prc % 60);
			if (min < 10) { min="0"+min; }
			if (sec < 10) { sec="0"+sec; }
			storePos();
			document.getElementById('state').innerHTML+="</b> | Осталось <b>" + min + ":" + sec + "</b>";
			document.getElementById('play').style.backgroundPosition='0 -17';
			document.getElementById('pause').style.backgroundPosition='0 0';
			document.getElementById('stop').style.backgroundPosition='0 0';
		} else {
			if(playerStatus == 2) {
				document.getElementById('state').innerHTML="Пауза...";
				document.getElementById('play').style.backgroundPosition='0 0';
				document.getElementById('pause').style.backgroundPosition='0 -17';
				document.getElementById('stop').style.backgroundPosition='0 0';
			}
			if(playerStatus == 6) {
				document.getElementById('state').innerHTML="Пожалуйста, подождите";
				document.getElementById('play').style.backgroundPosition='0 0';
				document.getElementById('pause').style.backgroundPosition='0 -17';
				document.getElementById('stop').style.backgroundPosition='0 0';
			}
			if(playerStatus == 8 || playerStatus == 1) {
				document.getElementById('state').innerHTML="Окончено";
				document.getElementById('play').style.backgroundPosition='0 0';
				document.getElementById('pause').style.backgroundPosition='0 0';
				document.getElementById('stop').style.backgroundPosition='0 -17';
				resetPos();
			} else {
				document.getElementById('state').innerHTML="Пожалуйста, подождите";
				document.getElementById('play').style.backgroundPosition='0 0';
				document.getElementById('pause').style.backgroundPosition='0 -17';
				document.getElementById('stop').style.backgroundPosition='0 0';
			}
		}
	} else {
		document.getElementById('state').innerHTML="Нет поддержки ActiveX!";
		document.getElementById('play').style.backgroundPosition='0 -17';
		document.getElementById('pause').style.backgroundPosition='0 0';
		document.getElementById('stop').style.backgroundPosition='0 0';
	}
}
function checkPlayer() {}

function BRControls () {
	if (bitrate === "900") {
		document.getElementById('lores').style.backgroundPosition='0 0';
		document.getElementById('hires').style.backgroundPosition='0 -17';
	} else {
		document.getElementById('lores').style.backgroundPosition='0 -17';
		document.getElementById('hires').style.backgroundPosition='0 0';
	}
}

function setBR(id,br) {
	var player = document.getElementById("MediaPlayer");
	tm = parseInt(start) + parseInt(pos);
	player.URL="http://vlast.rtvi.com/bin/asx.pl?id="+id+"&br="+br+"&tm="+tm;
	bitrate = br;
	BRControls();
}

function setURL(id,tm) {
	var player = document.getElementById("MediaPlayer");
	start = tm;
	player.URL="http://vlast.rtvi.com/bin/asx.pl?id="+id+"&tm="+tm+"&br="+bitrate;
}

function setTIME(tm) {
	var player = document.getElementById("MediaPlayer");
	player.Controls.currentPosition=parseInt(tm);
}

function restart() {
    document.getElementById("MediaPlayer").Controls.play();
}

function rewind() {
    document.getElementById("MediaPlayer").Controls.stop();
    window.setTimeout("restart()",1000);
}

function storePos() {
        var curCookie = "pos=" + parseInt(pos);
        document.cookie = curCookie;
}

function restorePos() {
    l = (document.location).toString().split("?");
    r = (document.referrer).toString().split("?");
    if (l[0] === r[0]) getPos();
}
function getPos() {
	var prefix = 'pos=';
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1) {
	    pos = 0;
    	    return null;
	}
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
	if (cookieEndIndex == -1)
	    cookieEndIndex = document.cookie.length
	    pos = parseInt(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function resetPos() {
	document.cookie='pos=0';
}
