// JavaScript Document
//Insertado para mantener UTF-8 en Dreamweaver: æø

//ANADIDO PARA CHECAR LAS COSAS DEL AUDIO


/***
	DETECTAMOS SI RECIBIMOS EL FRAGMENTO PARA AUTO SINTONIZAR
	Y SI ES ASI, LO PASAMOS DIRECTAMENTE A FLASH A VER SI SI ES
	UNA ESTACION VALIDA
	LO HICIMOS COMO FUNCION PARA PRIVATIZAR LA VARIABLE
***/

var estacionesAlternas	=	new Array();
estacionesAlternas[1]	=	['Radio Alicia','alicia','9000']
estacionesAlternas[2]	=	['Radio Guadalupe','guadalupe','9002']
estacionesAlternas[3]	=	['Hidrógeno Radio','hidrogeno','9004']
estacionesAlternas[4]	=	['Shock The Radio','shock','9006']
estacionesAlternas[5]	=	['Solaris Radio','solaris','9008']

var estadoReproductor	=	'stop';
var curEstacionId		=	null;
var trackId				=	null;
var reproductor			=	null;
var checandoProxy		=	false;
var checandoDatos		=	false;


function __mostrarSoyEscucha(){
	if(document.getElementById('reproductorAlterno') && document.getElementById('selectorTipoVisitante') && document.getElementById('hp_Barras') && document.getElementById('grita')){
		
		document.getElementById('selectorTipoVisitante').style.visibility='hidden';
		document.getElementById('selectorTipoVisitante').style.display='none';
		document.getElementById('selectorTipoVisitante').style.height='0px';
		
		document.getElementById('grita').style.visibility='visible';
		document.getElementById('grita').style.display='block';
		
		document.getElementById('hp_Barras').style.visibility='visible';
		document.getElementById('hp_Barras').style.display='block';
		document.getElementById('hp_Barras').style.height='auto';
		
	}
}


function checarFragmento(){
	if(window.location.hash.length >= 4){
		
		if(navigator.userAgent.toLowerCase().indexOf('msie')){
			try{
				var ca = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
				if(ca){
					var ieHasFlash	=	true;
				} else {
					var	ieHasFlash	=	false;
				}
			}catch(e){
				var ieHasFlash	=	false;
			}
		}
		
		__mostrarSoyEscucha();

		if(document.getElementById('grita') && navigator.mimeTypes && navigator.mimeTypes['application/x-shockwave-flash'] || ieHasFlash){
					

			//FLASH ESTA HABILITADO


			var objetoParam = 	document.getElementById('grita').getElementsByTagName('param')[8];
			
			if(objetoParam.getAttribute('name') == 'flashvars'){
				var atributoOriginal = objetoParam.getAttribute('value');
				objetoParam.setAttribute('value',atributoOriginal + '&autoSin=' + window.location.hash.split('#')[1]);
				
				var fOriginal	=	document.getElementById('grita').getAttribute('data');
				document.getElementById('grita').setAttribute('data','');
				document.getElementById('grita').setAttribute('data',fOriginal);
				
				//ANADIDO PARA EL SRC Y MOVIE

				objetoParam.parentNode.getElementsByTagName('param')[1].setAttribute('value','');
				objetoParam.parentNode.getElementsByTagName('param')[2].setAttribute('value','');

				objetoParam.parentNode.getElementsByTagName('param')[1].setAttribute('value',fOriginal);
				objetoParam.parentNode.getElementsByTagName('param')[2].setAttribute('value',fOriginal);
				
				
				//SI ES INTERNET EXPLORER DE PLANO RECARGAMOS TODO
				if(navigator.userAgent.toLowerCase().indexOf('msie')){
					var crearObjetoFlashIe	=	objetoParam.parentNode.cloneNode(true);
					objetoParam.parentNode.parentNode.removeChild(objetoParam.parentNode);
					document.getElementById('reproductorAlterno').appendChild(crearObjetoFlashIe);
				}				
			
				
			}

			
		} else if(document.getElementById('genReproAudio')){

			reproductor	=	document.getElementById('genReproAudio');

			if(reproductor && reproductor.canPlayType('audio/mpeg')){
				
				for(var i=0;i<estacionesAlternas.length;i++){

					if(estacionesAlternas[i][1] == window.location.hash.split('#')[1]){
						sintonizar(i);
						break;
					}
					
				}
				
			}
		}
		

	}
}


function sintonizar(idEst){
	
	reproductor	=	document.getElementById('genReproAudio');
	
	if(reproductor && reproductor.canPlayType('audio/mpeg') && __servidorStreaming.length >= 7){
		
		if(estacionesAlternas[idEst][0].length >= 4 && document.getElementById('reproAjaxInfo')){
			
			var construyeFuente			=	'http://'+__servidorStreaming+':666/proxy/'+estacionesAlternas[idEst][2]+'/listen.mp3';
			reproductor.style.visibility=	'visible';
			reproductor.src				=	'';
			reproductor.src				=	construyeFuente;
			reproductor.setAttribute('autoplay','autoplay');
			
			//
			document.getElementById('reproAjaxInfo').style.cssText	=	'';;
			curEstacionId				=	idEst;
			if(!checandoDatos) __rellenarInfoEstacAjax();
			if(!checandoProxy) __checarFinalDatosProxy();
			moverHashPlayer(estacionesAlternas[idEst][1]);
			
			if(document.getElementById('logoEstacionActual')){
				var LogoEstacion	=	document.getElementById('logoEstacionActual').getElementsByTagName('img')[0];
				LogoEstacion.src	=	_siteRoot + 'media/radio/estac/' + estacionesAlternas[idEst][1] + '.png';
				LogoEstacion.parentNode.style.visibility	=	'visible';
				
			}
			
			
			
		} else {
			
			var ventanaModal			=	new radioAlternativaModal();
			ventanaModal.nombre			=	'genPerfilModal';
			ventanaModal.crearVentana();
			
			var mensaje					=	crearEspacioMensaje('warning','No se puede reproducir la estación');
			ventanaModal.contenido		=	mensaje.innerHTML;
			ventanaModal.llenarVentana();
			
		}
		
	} else {
		var ventanaModal			=	new radioAlternativaModal();
		ventanaModal.nombre			=	'genPerfilModal';
		ventanaModal.crearVentana();
		
		var mensaje					=	crearEspacioMensaje('warning','¡Ùps! Tu equipo no puede reproducir el elemento Audio (HTML5). Tal vez quieras <a href="http://www.radioalternativa.com/movil">intentar con la versión para móvil</a>');
		ventanaModal.contenido		=	mensaje.innerHTML;
		ventanaModal.llenarVentana();
	}
	
}


function __rellenarInfoEstacAjax(){
	
	var conexion	=	new __invokeGuruAjax();
	conexion.url	=	_siteRoot + 'etc/ajax/ti.do?s=' + curEstacionId;
	
	conexion.onComplete	=	function(){
		
		if(conexion.fResponse[0] && conexion.fResponse[1].length >= 10){
			
			clo				=	document.getElementById('reproAjaxInfo').getElementsByTagName('tr')[0];
			
			if(window.DOMParser){
				datosXML	=	(new DOMParser()).parseFromString(conexion.fResponse[1], 'text/xml');
			} else {
				datosXML		=	new ActiveXObject("Microsoft.XMLDOM");
				datosXML.async	=	"false";
				datosXML.loadXML(conexion.fResponse[1]);
			}
			
			contenido	=	datosXML.getElementsByTagName('rola')[0];


			iCover		=	clo.getElementsByTagName('td')[1].getElementsByTagName('img')[0];
			iArtista	=	clo.getElementsByTagName('td')[0].getElementsByTagName('div')[0];
			iTitulo		=	clo.getElementsByTagName('td')[0].getElementsByTagName('div')[1];
			iAlbum		=	clo.getElementsByTagName('td')[0].getElementsByTagName('div')[2];
			iLike		=	clo.getElementsByTagName('td')[0].getElementsByTagName('a')[0];
			iInfo		=	clo.getElementsByTagName('td')[0].getElementsByTagName('a')[1];
			


			if(contenido.getAttribute('result') == '0'){
				
				trackId		=	contenido.getAttribute('tid');
				titulo		=	contenido.getAttribute('tit');
				album		=	contenido.getAttribute('alb');
				cover		=	contenido.getAttribute('alc');
				nombre		=	contenido.getAttribute('bdn');
				bUrl		=	contenido.getAttribute('bur');
				bDir		=	contenido.getAttribute('dir');
				rLik		=	contenido.getAttribute('lik');
				tCom		=	contenido.getAttribute('com');
				
				iLike.style.visibility	=	'visible';
				iInfo.style.visibility	=	'visible';
				

				
			} else if(contenido.getAttribute('result') == '6'){
				
				trackId		=	0;
				titulo		=	'Transmisión en vivo';
				album		=	'Transmisión en vivo';
				cover		=	'';
				nombre		=	'RadioAlternativa.com';
				bUrl		=	'index.do';
				bDir		=	'';
				rLik		=	'';
				tCom		=	'';

				iLike.style.visibility	=	'hidden';
				iInfo.style.visibility	=	'hidden';
				
			} else {
				
				trackId		=	0;
				titulo		=	'Material desconocido';
				album		=	'Desconocido';
				cover		=	'';
				nombre		=	'Artísta desconocido';
				bUrl		=	'index.do';
				bDir		=	'';
				rLik		=	'';
				tCom		=	'';

				iLike.style.visibility	=	'hidden';
				iInfo.style.visibility	=	'hidden';
				
			}



			iArtista.innerHTML	=	'';
			iArtista.href		=	'';
			iTitulo.innerHTML	=	'';
			iAlbum.innerHTML	=	'';
			iLike.innerHTML		=	'';

			
			iArtista.innerHTML	=	nombre;
			iArtista.href		=	_siteRoot + bUrl;
			
			iTitulo.innerHTML	=	titulo;
			iAlbum.innerHTML	=	'del álbum &quot;' + album + '&quot;';
			
			iInfo.href			=	_siteRoot + bUrl + '?ref=player';
			
			if(cover.length > 4){
				
				iCover.src	=	_siteRoot + 'var/_profiles/' + bDir + '/p_' + cover + '.jpg';
				
			} else {
				
				iCover.src	=	_siteRoot + 'media/radio/at.jpg';
				
			}
			
			if(rLik == 'si'){
				iLike.innerHTML	=	'Te gusta';
				iLike.onclick	=	function(){ void(0) };
			} else if(rLik == 'no'){
				iLike.innerHTML	=	'Me gusta';
				iLike.onclick	=	function(){
					this.innerHTML	=	'Te gusta';
					this.onclick	=	function(){ void(0) };
					meGustaLaRola(trackId,'ra')
				}
			}
			
			if(tCom.length >= 2){
				iTitulo.setAttribute('originalTitlo',titulo);
				iTitulo.setAttribute('originalComentario',tCom);
				
				iTitulo.onmouseover =	function(){
					this.innerHTML	=	this.getAttribute('originalComentario');
				}
				iTitulo.onmouseout	=	function(){
					this.innerHTML	=	this.getAttribute('originalTitulo');
				}
				
			} else {
				
				iTitulo.removeAttribute('originalTitlo');
				iTitulo.removeAttribute('originalComentario');
				
				iTitulo.onmouseover =	function(){ void(0)}
				iTitulo.onmouseout	=	function(){ void(0)}
				
			}
			


			setTimeout("__rellenarInfoEstacAjax()", 6000);
			checandoDatos	=	true;
			
			
		}
		
	}
	
	conexion.callRequest();
	
}

function __checarFinalDatosProxy(){
	
	reproductor	=	document.getElementById('genReproAudio');
	
	if(reproductor && reproductor.canPlayType('audio/mpeg')){

		if(reproductor.error){
			
			var construyeFuente				=	'http://'+__servidorStreaming+':666/proxy/'+estacionesAlternas[curEstacionId][2]+'/listen.mp3';
			reproductor.style.visibility=	'visible';
			reproductor.src				=	'';
			reproductor.src				=	construyeFuente;
			reproductor.setAttribute('autoplay','autoplay');
			
			//
		
		}
		
		setTimeout("__checarFinalDatosProxy()",1000);
		checandoProxy	=	true;
		
		
	}
	
	
}








//SCADOS DE RADIOALTERNATIVA.JS


function __preparaLinksParaElPopup(){
	
	var todosLinks	=	document.getElementsByTagName('a');

	for(var i=0;i<todosLinks.length;i++){
		
		var curLink				=	todosLinks[i];
		if(curLink.getAttribute('miniplayer') != 'off'){
			curLink.onclick	=	function(){
				
				return(__confirmaAbrirMiniPlayer(this.href));
	
				
			}
		}
		
	}
	
	
}

function __realAbreMiniPlayer(){
	window.open(_siteRoot + 'mini.do#' + window.location.hash.substr(1), 'miniReproAlterno', 'width=260,height=505,status=0,menubar=0,resizable=0,location=0,toolbar=0');
}



function __confirmaAbrirMiniPlayer(enlace){
	
	switch(_inicioPlayerPop){
		
		case true:
			
			__realAbreMiniPlayer()
			return true;
			
		break;
		
		case false:
		
			return true;
		
		break;
		
		case null:
		
			__opnPrModWin('pPop',null,enlace);
			return false;
		
		break;
		
		default:
			return true;
		break;
		
	}
	
}


function __setMiniPlayer(cosa){
	
	contenedor				=	document.getElementById('genModalStatus');
	contenedor.innerHTML	=	mensajeCargando('Continuando...');
	
	if(cosa == 'si'){
		__realAbreMiniPlayer()
	}
	
	if(document.getElementById('ppp_recuerda').checked){
		
		conex		=	new __invokeGuruAjax();
		conex.url	=	_siteRoot + 'etc/ajax/radioPus.do?k=' + cosa;
		conex.onComplete	=	function(){
			window.location.href	=	document.getElementById('genPerfilModal').opt1;
		}
		conex.callRequest();
		
		
	} else {
		window.location.href	=	document.getElementById('genPerfilModal').opt1;
	}
}




/*

Añadido para La versión 2011

*/

function urlencode(str) {
	//str = escape(str);
	//str = str.replace(/\+/g, '%2B');
	//str = str.replace(/%20/g, '+');
	//str = str.replace(/\*/g, '%2A');
	//str = str.replace(/\//g, '%2F');
	//str = str.replace(/\@/g, '%40');
	str = str.replace(/[\s]/g, '+');
	//str = str.replace(/#/g, '%23');
	str= str.replace('"','');
	return str;
}

function urldecode(str) {
	str = str.replace('%20', '+');
	str = unescape(str);
	return str;
}



function vinculosRSGrita(idEstacion,Nombre,Titulo,Url,idBanda,idRola){
	
	if(document.getElementById('GritaCajaRedesSociales')){
	
	
		var econtenedor		=	document.getElementById('GritaCajaRedesSociales');
		
		
		if(document.getElementById('SuperIframeFacebookLike')){
			document.getElementById('SuperIframeFacebookLike').parentNode.removeChild(document.getElementById('SuperIframeFacebookLike'));
		}
		
		econtenedor.childNodes[0].childNodes[0].onclick = function(){
			return;
		}
		
		
		var bandUrl		=	'http%3A%2F%2Fwww.radioalternativa.com%2F' + Url + '%3Ftid%3D'+idRola;
		var statUrl		=	'http://www.radioalternativa.com/' + estacionesAlternas[idEstacion][1];
	
		
		var textoTwitter	=	'Escuchando ' + Nombre + ' - ' + Titulo;
		var textoTwitter	=	urlencode(textoTwitter) + '+en+%23RadioAlternativa';

	
		
		econtenedor.childNodes[0].childNodes[0].setAttribute('statUrl',statUrl);
		econtenedor.childNodes[0].childNodes[0].setAttribute('textoTwitter',textoTwitter);
		econtenedor.childNodes[0].childNodes[0].onclick = function(){
			var tUrl = 'http://twitter.com/share?url='+this.getAttribute('statUrl')+'&text=' + this.getAttribute('textoTwitter');
			window.open(tUrl,'TwitterRAShare','width=560,height=320,status=1,toolbar=no,location=no,resizable=1');
			
			/* NOTA IMPORTANTE
			
			No usar www.twitter.com, usar twitter.com
			Explicacion aqui: http://code.google.com/p/twitter-api/issues/detail?id=890&can=1&q=www&colspec=ID%20Stars%20Type%20Status%20Priority%20Owner%20Summary%20Opened%20Modified%20Component
			
			*/
			
			
		}
		
		
		var fuenteFacebook	=	'http://www.facebook.com/plugins/like.php?href=' + bandUrl + '&send=false&show_faces=false&width=110&layout=button_count&action=like&font=arial&height=21';
		
		var facebookIframe	=	document.createElement('iframe');
		
		
		facebookIframe.allowTransparency	=	true;
		facebookIframe.frameBorder			=	'0';
		facebookIframe.scrolling			=	'no';
		
		facebookIframe.setAttribute('allowTransparency','true');
		facebookIframe.setAttribute('frameBorder','0');
		facebookIframe.setAttribute('scrolling','no');
		facebookIframe.setAttribute('src',fuenteFacebook);
		facebookIframe.style.border			=	'none';
		facebookIframe.style.overflow		=	'hidden';
		facebookIframe.style.width			=	'110px';
		facebookIframe.style.height			=	'21px';
		facebookIframe.id = 'SuperIframeFacebookLike';
		
		econtenedor.childNodes[1].appendChild(facebookIframe);
		
		econtenedor.setAttribute('listo','si');
		//alert('Crea Vinculos');
			
	}



	
}

function realMostrarRSGrita(){
	
	if(document.getElementById('GritaCajaRedesSociales') && document.getElementById('GritaCajaRedesSociales').getAttribute('listo') == 'si'){
		
		var econtenedor		=	document.getElementById('GritaCajaRedesSociales');
		econtenedor.style.visibility	=	'visible';
		//econtenedor.style.display		=	'block';		
		//alert('Muestra Vinculos');
	}
	
}

function destruirVinculosRSG(){
	
	
	if(document.getElementById('GritaCajaRedesSociales') && document.getElementById('GritaCajaRedesSociales').getAttribute('listo') == 'si'){
		
		var econtenedor		=	document.getElementById('GritaCajaRedesSociales');

		econtenedor.style.visibility	=	'hidden';
		//econtenedor.style.display		=	'none';	

		econtenedor.childNodes[0].childNodes[0].onclick = function(){
			return;
		}
		
		
		if(document.getElementById('SuperIframeFacebookLike')){
			document.getElementById('SuperIframeFacebookLike').parentNode.removeChild(document.getElementById('SuperIframeFacebookLike'));
		}
		
		econtenedor.setAttribute('listo','no');
		//alert('Destruye Vinculos');
		
	}
	return;
	
}

function mostrarLinkPlayers(idEstacion){

		__opnPrModWin('pPlayExt',null,idEstacion)
		

}

function initPlayerFuncs(stacId,nom){
    moverHashPlayer(nom);
    __gtl('f','hpp'+stacId);
	__multiHpPlayerFuncs('hpp'+stacId,nom)
	
}

function compartirReproductor(embed){
	if(embed){
		__opnPrModWin('sharePlayerEmbed',null)
	} else {
		__opnPrModWin('sharePlayer',null)
	}
}

function actualizaSeleccionEmbedClas(elem){
	var html	=	'<iframe src="http://www.radioalternativa.com/'+elem.value+'" frameborder="0" style="width:260px;height:505px" width="260" height="505" allowtransparency="true"></iframe>';
	document.getElementById('autoReproLinkEmbed').value	=	html;
}



