// Muestra el reproductor
function showReproductor(id) {
  if(!(id)) id='reproductor';
  if(document.getElementById) {
	var ob=document.getElementById(id);
	ob.style.height="99px";
	ob.style.visibility="visible";
  }
}
// Esconde el reproductor
function hideReproductor(id) {
  if(!(id)) id='reproductor';
  if(document.getElementById) {
	var ob=document.getElementById(id);
	ob.style.height="0px";
	ob.style.visibility="hidden";
  }
}
//mostra els errors
function showErrors(id) {
  if(!(id)) id='errormsg';
  if(document.getElementById) {
	var ob=document.getElementById(id);
	var ob2=document.getElementById('error_desc');
	if(ob2.innerHTML.split("\n").length>2)
		ob.style.height="76px";
	else
		ob.style.height="60px";
	ob.style.visibility="visible";
  }	
}
//amaga els errors
function hideErrors(id) {
  if(!(id)) id='errormsg';
  if(document.getElementById) {
	var ob=document.getElementById(id);
	ob.style.height="0px";
	ob.style.visibility="hidden";
  }
}
player = 0;
errors = 0;
isBusy = false;
//variable AJAX para interacción de estado de directos
if (window.XMLHttpRequest){
	xml = new XMLHttpRequest();
	xml.onreadystatechange=nextStatusRequest;
    }
else {
	try{
		xml = new ActiveXObject("Microsoft.XMLHTTP");
		xml.onreadystatechange=nextStatusRequest;
	}catch(e){}
}
//alterna entre els 2 estats d'errors
function sweepErrors(dir,id) {
  	if(!(id)) id='error_desc';
	if(parseInt(dir) == 0) {
		hideErrors();
	}
	else {
		var err=xml.responseXML.documentElement.getElementsByTagName('errors_html').item(0).firstChild.nodeValue;
		var ob=document.getElementById(id);
		ob.innerHTML = err;
		showErrors();
	}
}
//alterna los dos estados del reproductor, entre visible e invisible
function sweepReproductor(dir,id,id2) {
  	if(!(id)) id='reproductor_desc';
	if(!(id2)) id2='reproductor_listeners';
	//alert("id: "+id)
  	if(parseInt(dir) == 0) {
		//para el reproductor...
		var ob_rep = document.getElementById('audioplayer1');
		ob_rep.SetVariable("closePlayer", 1);
		//amagar el reproductor
		sweepReproductorTo('hidden');
	}
	else {

		var aut=xml.responseXML.documentElement.getElementsByTagName('author').item(0).firstChild.nodeValue;
		var tit=xml.responseXML.documentElement.getElementsByTagName('title').item(0).firstChild.nodeValue;
		var listeners=xml.responseXML.documentElement.getElementsByTagName('listeners_html').item(0).firstChild.nodeValue;
		//alert(aut+" - "+tit)
		var ob=document.getElementById(id);
		ob.innerHTML = aut+" - "+tit;
		var ob=document.getElementById(id2);
		ob.innerHTML = listeners;
		sweepReproductorTo('visible')
	}
}

function sweepReproductorTo(stat,id){
  try{clearTimeout(ID_SR)}catch(e){}

  if(!(id)) id='reproductor';
  if(document.getElementById) {
	var ob=document.getElementById(id);
	
	var flash=document.getElementById('ob_reproductor');
	if(stat=='hidden') {
		if(parseInt(ob.offsetHeight)>0) {
			if(parseInt(ob.offsetHeight)<70) flash.style.visibility="hidden";
			ob.style.height=(parseInt(ob.offsetHeight)-2)+"px";
			ID_SR=setTimeout("sweepReproductorTo('"+stat+"')",10);
		}
		else {
			hideReproductor();
		}
	}
	else {
		if(parseInt(ob.offsetHeight)<99) {
			ob.style.visibility="visible";
			flash.style.visibility="hidden";
			if(parseInt(ob.offsetHeight)>70) flash.style.visibility="visible";
			
			ob.style.height=(parseInt(ob.offsetHeight)+2)+"px";
			ID_SR=setTimeout("sweepReproductorTo('"+stat+"')",10);
		}
		else {
			flash.style.visibility="visible";
			showReproductor();
		}
	}
  }
}

// comprueva el estado de la radio i muestra el reproductor si hace falta
function checkLiveStatus(pl,er){
	player = pl;
	errors = er;
	
	try{
		clearTimeout(ID_LSTAT);
	}catch(e){}
	//llamada al script que nos da la situación actual de la radio
	//alert(HOSTPATH + 'getLiveStatus.php')
	xml.open('GET',HOSTPATH + 'getLiveStatus.php',true);
	xml.onreadystatechange=nextStatusRequest;
	xml.send('');
}

function nextStatusRequest(){
	if(xml.readyState==4) {
		//var t='';for(var i in xml)  t+=' '+i;alert(t);
		
		if(xml.status == 200) {
			var ob=xml.responseXML.documentElement.getElementsByTagName('live').item(0);
			if(player) {
				sweepReproductor(ob.firstChild.nodeValue);
			}
			if(errors) {
				var ob2=xml.responseXML.documentElement.getElementsByTagName('errors').item(0);
				sweepErrors(ob2.firstChild.nodeValue);
			}
		}
		//proceso completado, cargamos dentro de un tiempo (30s) otra vez
		ID_LSTAT=setTimeout("checkLiveStatus("+player+","+errors+")",30000);	
	}
}
