var bBuffering = false;
var timerTime = null;
var timerBuffering = null;
var timerVolume = null;

function objGet(x) {
   if (typeof x != 'string') return x;
   else if (Boolean(document.getElementById))
      return document.getElementById(x);
   else if (Boolean(document.all))
      return eval('document.all.'+x);
   else if (Boolean(document.ids))
      return eval('document.ids.'+x);
   else
      return null;
}

/*****************DETECT WMP*********************/
function detectWMP()
{
	var wmpInfo = {
		installed: false,
		scriptable: false,
		type: null,
		versionInfo: null
	};
	var wmp64 = "MediaPlayer.MediaPlayer.1";
	var wmp7 = "WMPlayer.OCX.7";
	if((window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1) || window.GeckoActiveXObject){
		wmpInfo.type = "ActiveX";
		var player = createActiveXObject(wmp7);
		if(player){
			wmpInfo.installed = true;
			wmpInfo.scriptable = true;
			wmpInfo.versionInfo = player.versionInfo;
			return wmpInfo;
		}else{
			player = createActiveXObject(wmp64);
			if(player){
				wmpInfo.installed = true;
				wmpInfo.scriptable = true;
				wmpInfo.versionInfo = "6.4";
				return wmpInfo;
			}else{
				wmpInfo.versionInfo = "none";
				return wmpInfo;
			}
		}
	}else if(navigator.mimeTypes){
		wmpInfo.type = "NetscapePlugin";
		var player = navigator.mimeTypes['application/x-mplayer2'].enabledPlugin;
		if(player){	
			wmpInfo.installed = true;
			wmpInfo.versionInfo = "PluginVersion";
			return wmpInfo;
		}
		return wmpInfo;
	}
}
function createActiveXObject(id){
  var error;
  var control = null;
  try{
    if (window.ActiveXObject){
      control = new ActiveXObject(id);
    }else if (window.GeckoActiveXObject){
      control = new GeckoActiveXObject(id);
    }
  }
  catch (error){;}
  return control;
}
	

function printResults(){
	var detectionResults = detectWMP();
	var sTemp = "<div>"
	sTemp=sTemp+"Browser architecture supports: " + detectionResults.type + "<br />";
	sTemp=sTemp+"Windows Media Player Installed: " + detectionResults.installed + "<br />";
	if(detectionResults.installed)
	{
		sTemp=sTemp+"Windows Media Scriptable: " + detectionResults.scriptable + "<br />";
		sTemp=sTemp+"Windows Media Version: " + detectionResults.versionInfo + "<br />";
	}
	sTemp=sTemp+"</div>"
	return sTemp
}

detectionResults = detectWMP();


function ShowButtons()
{
	if(detectionResults.scriptable && Boolean(objGet("iRadio").controls))
	{
		objGet("Radio").style.marginLeft='-2000px';
		objGet("Panel").style.marginLeft='0';
		ShowText();
	}
	else
	{
//		standart
	}
}

function ShowText() {

	var currentPosition
	
	if (typeof objGet("iRadio").settings.volume != "undefined")	
	{
	player.SetVariable("volproc", objGet("iRadio").settings.volume);
	}

	currentPosition=FormatTime(objGet("iRadio").controls.currentPosition);
	if(!bBuffering){
		if (typeof currentPosition != "undefined")	
		{
			objGet("player").SetVariable("streaminfo", currentPosition);
		}
	}
	if(objGet("iRadio").network.bandwidth != 0){
		if (typeof objGet("iRadio").network.bandWidth != "undefined")	
		{
			objGet("player").SetVariable("rychlost", objGet("iRadio").network.bandWidth + " Kb/s");
		}
	}
	timerTime=setTimeout("ShowText()",1000);
}


function FormatTime(h) {
  var hodina;
  var minuta;
  var sekunda;
  hodina=parseInt(h/3600);
  h=Math.round(h-hodina*3600);
  minuta=parseInt(h/60);
  h=Math.round(h-minuta*60);
  sekunda=Math.round(h);
  return Nula(hodina)+":"+Nula(minuta)+":"+Nula(sekunda);
}
function Nula(arg)
{
	sText = '00'+arg;
	return (sText.substring((sText.length-2),sText.length));
}

/***********************BUTTON****************************/
function PlayVideo () {
	if (Boolean(objGet("iRadio").controls))
	{
	objGet("iRadio").controls.play();
	}
}
function StopMe () {
	if (Boolean(objGet("iRadio").controls))
	{
		if(detectionResults.versionInfo.substring(0,1)==6){
			objGet("iRadio").stop();
		}else{
			objGet("iRadio").controls.stop();
		}
	}
}
function sVolume (arg) {
	if (Boolean(objGet("iRadio").controls))
	{
	
			objGet("iRadio").settings.volume=arg;

	}
	bBuffering = true;
//	if (typeof objGet("iRadio").settings.volume != "undefined")	
//	{
		objGet("player").SetVariable("streaminfo", objGet("iRadio").settings.volume + '%');
//	}
	timerVolume = setInterval("StopBuffering()", 3000);
}

function Mute()
{
	if (Boolean(objGet("iRadio").settings))
	{
		if(objGet("iRadio").settings.mute)
		{
			objGet("iRadio").settings.mute=false;
		}
		else
		{
			objGet("iRadio").settings.mute=true;
		}
	}
}

function StopBuffering()
{
	bBuffering = false;
	clearTimeout(timerVolume);
}

/******************************/

function UpdateBP()
{
	if (Boolean(objGet("iRadio").network))
	{
		if (typeof objGet("iRadio").network.bufferingProgress != "undefined")	
		{
			objGet("player").SetVariable("streaminfo", "Buffering: " + objGet("iRadio").network.bufferingProgress + "%");
		}
	}
}
