// Variables goblales que controlan el servidor de rtmp del servidor de velicix y la playlist de la emision continua
var finalUrl;
var listName;
var strTipoEmision;
var strTitleEmision;
var blnEndirecto=false;
var v_file="";
var v_streamer="";
var error=false;
var directo=false;

//vamos a ver si la pagina está en emision directo
var NombreArchivoXMLEmision;
var intDirecto;
intDirecto=0;

//NombreArchivoXMLEmision="http://10.2.3.23/EditaXML_wordpress/emisiondirecto.xml";
NombreArchivoXMLEmision="/emisiondirecto.xml";

//NombreArchivoXMLEmision="http://cervantestv.es/wp-content/themes/cervantestv/js/emisiondirecto.xml";
ObjHttpRequest_visor(NombreArchivoXMLEmision,'RespuestaAJAX_CargaEmisionDirecto','');

function RespuestaAJAX_CargaEmisionDirecto (){

	if (reqDatos.readyState == 4) {
		
		var xmlDoc = reqDatos.responseXML.documentElement;
		//alert(xmlDoc);
		var blnEmisionDirecto = xmlDoc.getElementsByTagName("annotation");
		strTipoEmision=blnEmisionDirecto[0].childNodes[0].nodeValue;
		var titleEmisionDirecto = xmlDoc.getElementsByTagName("title");
		if (titleEmisionDirecto[0].childNodes[0] != null) {
			strTitleEmision=titleEmisionDirecto[0].childNodes[0].nodeValue;
		} else {
			strTitleEmision="";
		}
				
		if (strTipoEmision=="EC"){ //emisión en directo
			cargaEmisionDirecta();
		}else{ //hemos elegido un directo
			blnEndirecto=true;
			intDirecto=strTipoEmision.substring(1,strTipoEmision.length);
			try {
				switch(intDirecto){
					case "1":
						v_streamer = url_directo1.replace(/^rtmp:\/\//, "rtmpt://");
						//alert(v_streamer);
						var longitud = v_streamer.length;
						var ocurrencia = v_streamer.search(/fs/i);
						var chorro = v_streamer.slice(ocurrencia, longitud);
						//alert(longitud);
						//alert(ocurrencia);
						//alert(chorro);
						v_file="/js/directo1.rss";
						if (chorro != "fs-f9b1030000000000") {
							error = true;
						}
						//alert(v_file);
						break;
					case "2":
						v_streamer = url_directo2.replace(/^rtmp:\/\//, "rtmpt://");
						//alert(v_streamer);
						var longitud = v_streamer.length;
						var ocurrencia = v_streamer.search(/fs/i);
						var chorro = v_streamer.slice(ocurrencia, longitud);
						v_file="/js/directo2.rss";
						if (chorro != "fs-2396050000000000") {
							error = true;
						}
						//alert(v_file);
						break;						
					case "3":
						v_streamer = url_directo3.replace(/^rtmp:\/\//, "rtmpt://");
						//alert(v_streamer);
						var longitud = v_streamer.length;
						var ocurrencia = v_streamer.search(/fs/i);
						var chorro = v_streamer.slice(ocurrencia, longitud);
						v_file="/js/directo3.rss";
						if (chorro != "fs-2596050000000000") {
							error = true;
						}
						//alert(v_file);
						break;
					case "4":
						v_streamer = url_directo4.replace(/^rtmp:\/\//, "rtmpt://");
						//alert(v_streamer);
						var longitud = v_streamer.length;
						var ocurrencia = v_streamer.search(/fs/i);
						var chorro = v_streamer.slice(ocurrencia, longitud);
						v_file="/js/directo4.rss";
						if (chorro != "fs-8ad7210000000000") {
							error = true;
						}
						//alert(v_file);
					 	break;
					case "5":
						v_streamer = url_directo5.replace(/^rtmp:\/\//, "rtmpt://");
						//alert(v_streamer);
						var longitud = v_streamer.length;
						var ocurrencia = v_streamer.search(/fs/i);
						var chorro = v_streamer.slice(ocurrencia, longitud);
						v_file="/js/directo5.rss";
						if (chorro != "fs-92d7210000000000") {
							error = true;
						}
						//alert(v_file);
						break;
				}
			}catch(e){
				//window.status ="error al cargar el directo " + intDirecto;
				//alert("Error al cargar el directo " + intDirecto);
				strTipoEmision="EC"
				cargaEmisionDirecta();
			}
		}
		//alert('salgo del catch');
		setInterval("EmisinDirecto()",15000);
		if (error) {
			//alert("Error al cargar el directo " + intDirecto);
			strTipoEmision="EC"
			cargaEmisionDirecta();
		} else if (strTipoEmision!="EC"){
			var directo = document.getElementById("carrusel");
			var titulo_directo = directo.getElementsByTagName("h1");
			titulo_directo.item(0).innerHTML = "Emisi&oacute;n en directo";
			var desc_directo = directo.getElementsByTagName("p");
			desc_directo.item(0).innerHTML = strTitleEmision;
			var incrustar = document.getElementById("nav_multimedia").getElementsByTagName("a");
			incrustar.item(0).style.visibility = "hidden";
			// Boton HD
			incrustar.item(1).style.visibility = "hidden";
		};
		CargaVisor();
		/*if (mosca) {
			cargarVisor_conMosca();
		} else {
			cargarVisor();
		}*/
		if ((stream_sd_default==stream_hd)||(blnEndirecto)) {
			if (mosca) {
				cargarVisor_conMosca();
			} else {
				cargarVisor();
			}
		} else {
			if (mosca) {
				cargarVisor_anchodebanda_conMosca();
			} else {
				cargarVisor_anchodebanda();
			}
		}
	}
}

 function ObjHttpRequest_visor(p_Url,p_fun_Respuesta,p_Parametros){
	 
	 //Le añadimos un número aleatorio que modifica la petición al servidor 
	 //para evitar que el navegador de cliente use el archivo que tiene guardado en la cache local del equipo.
	 p_Url=p_Url + "?no_cache=" +Math.random();
     if (window.XMLHttpRequest) {  
        reqDatos = new XMLHttpRequest();
		
		eval("reqDatos.onreadystatechange ="+ p_fun_Respuesta);
        reqDatos.open("GET", p_Url, true);
        reqDatos.setRequestHeader('Content-Type','text/xml');
        reqDatos.send(null);
		/*alert(reqDatos);/*
		alert(reqDatos.readyState);
		alert(reqDatos.status);
		alert(reqDatos.responseText);
		alert(reqDatos.statusText);*/
    // para versión IE/Windows ActiveX 
    } else if (window.ActiveXObject) {
        reqDatos = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqDatos) {
            eval("reqDatos.onreadystatechange ="+ p_fun_Respuesta);
            reqDatos.open("GET", p_Url, true);
            reqDatos.setRequestHeader('Content-Type','text/xml');
            reqDatos.send(null);
        }
    }
}

function cargaEmisionDirecta(){
	blnEndirecto=false;
	//v_file="/js/playlist.rss";
	v_file = streamName;
	v_streamer = rtmpUrl[0];
	v_streamer=v_streamer.replace(/^rtmp:\/\//, "rtmpt://");
	//alert(v_streamer);
	//alert(streamName);
}

function EmisinDirecto() {
	ObjHttpRequest_visor(NombreArchivoXMLEmision,'RespuestaAJAX_EmisionDirecto','');
}

function RespuestaAJAX_EmisionDirecto () {
	var intLocalDirecto;
	if (reqDatos.readyState == 4) {
        var xmlDoc = reqDatos.responseXML.documentElement;
		var blnEmisionDirecto = xmlDoc.getElementsByTagName("annotation");
		strTipoEmision=blnEmisionDirecto[0].childNodes[0].nodeValue;
		//alert(strTipoEmision);
		//alert(intDirecto);
		intLocalDirecto=strTipoEmision.substring(1,strTipoEmision.length);
		if ((!(strTipoEmision=="EC") && blnEndirecto==false) || (!(strTipoEmision=="EC") && !(intLocalDirecto==intDirecto) )){
			blnEndirecto=true;
			//alert('DIRECTO');
			window.location=window.location;
			//cargarVisor();
			return;
		}else if (blnEmisionDirecto[0].childNodes[0].nodeValue=="EC" && blnEndirecto==true){
			blnEndirecto=false;
			//alert('EC')
			//cargaEmisionDirecta();
			//cargarVisor();
			window.location=window.location;
			return;	
		}
	}
}

function CargaVisor() {
	// Cargamos los parametros del rtmp del velocix
	finalUrl = v_streamer;
	// Cargamos la playlist po el video de la portada
	listName = v_file;
}

// Utilizamos esta funcion para cargarla en el onload de la portada
function cargarVisor(){
if (strTipoEmision!="EC") {
	$f("flowvideo", urlCervantes+"/flowplayer.commercial-3.2.7.swf", { 
		key: '#@fe5c2dba052f6420faf',
		clip: {
			autoPlay: true,
			autoBuffering: true,
			live: true,
			scaling: 'fit',
			provider: 'velocix'
		},
		
		// The playlist is loaded from the RSS file
		playlist:  urlCervantes+listName,
		/*playlist: [
			'mp4:bt-7b46c82d41687450b583b556fe7d027da6c52b63'
		],*/
		
		canvas: { 
			// use a wicked backgound color 
			background: 'none', 
			// a gradient 
			backgroundGradient: 'none' 
		},
		
		 // custom labels on the play button 
		play: {
			replayLabel: '',
			url:  urlCervantes+'/img/fondo-play.gif', 
			width: 66, 
			height: 35 
		},
		logo: {
			url: urlCervantes+'/img/mosca_p.png',
			fullscreenOnly: false,
			opacity: 1,
			top: 10,
			left: 10
		},
		plugins:  {
			velocix: {
				url:  urlCervantes+'/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: finalUrl
		},
		content: {
			url: urlCervantes+'/flowplayer.content-3.2.0.swf',
			top: 0, left: 0, width: '100%', height: 30,
			backgroundColor: 'transparent', backgroundGradient: 'none', border: 0, borderRadius: 'none',
			textDecoration: 'outline', 
			style: {  
				body: {  
					fontSize: 12,
					fontFamily: 'Arial', 
					color: '#ffffff'
				}  
			}  		
		},
		controls: {
				// tooltips configuration 
				tooltips: { 
					// enable english tooltips on all buttons 
					buttons: true, 
					// customized texts for buttons 
					play: 'Reproducir', 
					pause: 'Pausa',
					mute: 'Desactivar sonido',
					unmute: 'Activar sonido',
					fullscreen: 'Pantalla completa',
					fullscreenExit: 'Salir de pantalla completa'
				},
				scrubber: false
			} 
		 }
	});
	} else {
		$f("flowvideo", urlCervantes+"/flowplayer.commercial-3.2.7.swf", { 
		key: '#@fe5c2dba052f6420faf',
		clip: {
			autoPlay: true,
			autoBuffering: true,
			scaling: 'fit',
			provider: 'velocix',
			url: v_file
		},
		
		canvas: { 
			// use a wicked backgound color 
			background: 'none', 
			// a gradient 
			backgroundGradient: 'none' 
		},
		
		 // custom labels on the play button 
		play: {
			replayLabel: '',
			url:  urlCervantes+'/img/fondo-play.gif', 
			width: 66, 
			height: 35 
		},
		plugins:  {
			velocix: {
				url:  urlCervantes+'/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: finalUrl
		},
		content: {
			url: urlCervantes+'/flowplayer.content-3.2.0.swf',
			top: 0, left: 0, width: '100%', height: 30,
			backgroundColor: 'transparent', backgroundGradient: 'none', border: 0, borderRadius: 'none',
			textDecoration: 'outline', 
			style: {  
				body: {  
					fontSize: 12,
					fontFamily: 'Arial', 
					color: '#ffffff'
				}  
			}  		
		},
		controls: {
				// tooltips configuration 
				tooltips: { 
					// enable english tooltips on all buttons 
					buttons: true, 
					// customized texts for buttons 
					play: 'Reproducir', 
					pause: 'Pausa',
					mute: 'Desactivar sonido',
					unmute: 'Activar sonido',
					fullscreen: 'Pantalla completa',
					fullscreenExit: 'Salir de pantalla completa'
				}
			} 
		 }
	});
	}
}
// Visor con deteccion automatica de ancho de banda
function cargarVisor_anchodebanda(){
	$f("flowvideo", urlCervantes+"/flowplayer.commercial-3.2.7.swf", { 
		key: '#@fe5c2dba052f6420faf',
		clip: {
			autoPlay: true,
			autoBuffering: true,
			scaling: 'fit',
		    urlResolvers: 'bwcheck',
			provider: 'velocix',
			//url: streamName
			bitrates: [
				  { 
					url: stream_sd_baja,
					width: 616,
					height: 346,
					bitrate: 700
					//normal: true,
				  },
				  { 
					url: stream_sd_default,
					width: 616,
					height: 346,
					bitrate: 1000,
					isDefault: true 
					//normal: true
				  },
				  { 
					url: stream_sd_alta,
					width: 616,
					height: 346,
					bitrate: 1400
					//normal: true
				  }
			  ]
		},
		
		canvas: { 
			// use a wicked backgound color 
			background: 'none', 
			// a gradient 
			backgroundGradient: 'none' 
		},
		
		 // custom labels on the play button 
		play: {
			replayLabel: '',
			url:  urlCervantes+'/img/fondo-play.gif', 
			width: 66, 
			height: 35 
		},
		plugins:  {
			bwcheck: {  
				url: urlCervantes+'/flowplayer.bwcheck-3.2.5.swf', 	    
				serverType: 'fms',      	    
				dynamic: true,
				//hdButton: true,      	    
				netConnectionUrl: url_sd_default
							
				/*onStreamSwitchBegin: function (newItem, currentItem) {
				   $f().getPlugin('content').setHtml("Podríamos cambiar al vídeo " + newItem.streamName + " desde " + currentItem.streamName);
				},
				onStreamSwitch: function (newItem) {
				   $f().getPlugin('content').setHtml("Cambiamos al vídeo " + newItem.streamName + " con un bitrate de " + newItem.bitrate);
				}*/
			},
			velocix: {
				url:  urlCervantes+'/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: url_sd_default
			},
			content: {
				url: urlCervantes+'/flowplayer.content-3.2.0.swf',
				top: 0, left: 0, width: '100%', height: 30,
				backgroundColor: 'transparent', backgroundGradient: 'none', border: 0, borderRadius: 'none',
				textDecoration: 'outline', 
				style: {  
					body: {  
						fontSize: 12,
						fontFamily: 'Arial', 
						color: '#ffffff'
					}  
				}  		
			},
			controls: {
					// tooltips configuration 
					tooltips: { 
						// enable english tooltips on all buttons 
						buttons: true, 
						// customized texts for buttons 
						play: 'Reproducir', 
						pause: 'Pausa',
						mute: 'Desactivar sonido',
						unmute: 'Activar sonido',
						fullscreen: 'Pantalla completa',
						fullscreenExit: 'Salir de pantalla completa'
					}
				} 
			 }
	});
}
// Visor con deteccion automatica de ancho de banda CON MOSCA
function cargarVisor_anchodebanda_conMosca(){
	$f("flowvideo", urlCervantes+"/flowplayer.commercial-3.2.7.swf", { 
		key: '#@fe5c2dba052f6420faf',
		clip: {
			autoPlay: true,
			autoBuffering: true,
			scaling: 'fit',
		    urlResolvers: 'bwcheck',
			provider: 'velocix',
			//url: streamName
			bitrates: [
				  { 
					url: stream_sd_baja,
					width: 616,
					height: 346,
					bitrate: 700
					//normal: true,
				  },
				  { 
					url: stream_sd_default,
					width: 616,
					height: 346,
					bitrate: 1000,
					isDefault: true 
					//normal: true
				  },
				  { 
					url: stream_sd_alta,
					width: 616,
					height: 346,
					bitrate: 1400
					//normal: true
				  }
			  ]
		},
		
		canvas: { 
			// use a wicked backgound color 
			background: 'none', 
			// a gradient 
			backgroundGradient: 'none' 
		},
		
		 // custom labels on the play button 
		play: {
			replayLabel: '',
			url:  urlCervantes+'/img/fondo-play.gif', 
			width: 66, 
			height: 35 
		},
		logo: {
			url: urlCervantes+'/img/mosca_p.png',
			fullscreenOnly: false,
			opacity: 1,
			top: 10,
			left: 10
		},
		plugins:  {
			bwcheck: {  
				url: urlCervantes+'/flowplayer.bwcheck-3.2.5.swf', 	    
				serverType: 'fms',      	    
				dynamic: true,
				//hdButton: true,      	    
				netConnectionUrl: url_sd_default
							
				/*onStreamSwitchBegin: function (newItem, currentItem) {
				   $f().getPlugin('content').setHtml("Podríamos cambiar al vídeo " + newItem.streamName + " desde " + currentItem.streamName);
				},
				onStreamSwitch: function (newItem) {
				   $f().getPlugin('content').setHtml("Cambiamos al vídeo " + newItem.streamName + " con un bitrate de " + newItem.bitrate);
				}*/
			},
			velocix: {
				url:  urlCervantes+'/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: url_sd_default
			},
			content: {
				url: urlCervantes+'/flowplayer.content-3.2.0.swf',
				top: 0, left: 0, width: '100%', height: 30,
				backgroundColor: 'transparent', backgroundGradient: 'none', border: 0, borderRadius: 'none',
				textDecoration: 'outline', 
				style: {  
					body: {  
						fontSize: 12,
						fontFamily: 'Arial', 
						color: '#ffffff'
					}  
				}  		
			},
			controls: {
					// tooltips configuration 
					tooltips: { 
						// enable english tooltips on all buttons 
						buttons: true, 
						// customized texts for buttons 
						play: 'Reproducir', 
						pause: 'Pausa',
						mute: 'Desactivar sonido',
						unmute: 'Activar sonido',
						fullscreen: 'Pantalla completa',
						fullscreenExit: 'Salir de pantalla completa'
					}
				} 
			 }
	});
}
// Utilizamos esta funcion para cargarla en el onload de la portada - VISOR CON MOSCA
function cargarVisor_conMosca(){
	$f("flowvideo", urlCervantes+"/flowplayer.commercial-3.2.7.swf", { 
		key: '#@fe5c2dba052f6420faf',
		clip: {
			autoPlay: true,
			autoBuffering: true,
			scaling: 'fit',
			provider: 'velocix',
			url: v_file
		},
		
		canvas: { 
			// use a wicked backgound color 
			background: 'none', 
			// a gradient 
			backgroundGradient: 'none' 
		},
		
		 // custom labels on the play button 
		play: {
			replayLabel: '',
			url:  urlCervantes+'/img/fondo-play.gif', 
			width: 66, 
			height: 35 
		},
		logo: {
			url: urlCervantes+'/img/mosca_p.png',
			fullscreenOnly: false,
			opacity: 1,
			top: 10,
			left: 10
		},
		plugins:  {
			velocix: {
				url:  urlCervantes+'/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: finalUrl
		},
		content: {
			url: urlCervantes+'/flowplayer.content-3.2.0.swf',
			top: 0, left: 0, width: '100%', height: 30,
			backgroundColor: 'transparent', backgroundGradient: 'none', border: 0, borderRadius: 'none',
			textDecoration: 'outline', 
			style: {  
				body: {  
					fontSize: 12,
					fontFamily: 'Arial', 
					color: '#ffffff'
				}  
			}  		
		},
		controls: {
				// tooltips configuration 
				tooltips: { 
					// enable english tooltips on all buttons 
					buttons: true, 
					// customized texts for buttons 
					play: 'Reproducir', 
					pause: 'Pausa',
					mute: 'Desactivar sonido',
					unmute: 'Activar sonido',
					fullscreen: 'Pantalla completa',
					fullscreenExit: 'Salir de pantalla completa'
				}
			} 
		 }
	});
}
// Visor para HD
function cargarVisor_hd(){
	$f("flowvideo", urlCervantes+"/flowplayer.commercial-3.2.7.swf", { 
		key: '#@fe5c2dba052f6420faf',
		clip: {
			autoPlay: true,
			autoBuffering: true,
			scaling: 'fit',
			provider: 'velocix',
			url: stream_hd
		},
		
		canvas: { 
			// use a wicked backgound color 
			background: 'none', 
			// a gradient 
			backgroundGradient: 'none' 
		},
		
		 // custom labels on the play button 
		play: {
			replayLabel: '',
			url:  urlCervantes+'/img/fondo-play.gif', 
			width: 66, 
			height: 35 
		},
		logo: {
			url: urlCervantes+'/img/HD-on.png',
			fullscreenOnly: false,
			opacity: 1,
			top: 10,
			left: 10
		},
		/*// gradually show on mouseover
		onMouseOver: function() {
			this.getPlugin("logo").fadeTo(0.8, 1000);
		},
		// gradually hide on mouseout
		onMouseOut: function() {
			this.getPlugin("logo").fadeTo(0, 1000);
		},*/
		plugins:  {
			velocix: {
				url:  urlCervantes+'/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: url_hd
		},
		content: {
			url: urlCervantes+'/flowplayer.content-3.2.0.swf',
			top: 0, left: 0, width: '100%', height: 30,
			backgroundColor: 'transparent', backgroundGradient: 'none', border: 0, borderRadius: 'none',
			textDecoration: 'outline', 
			style: {  
				body: {  
					fontSize: 12,
					fontFamily: 'Arial', 
					color: '#ffffff'
				}  
			}  		
		},
		controls: {
				// tooltips configuration 
				tooltips: { 
					// enable english tooltips on all buttons 
					buttons: true, 
					// customized texts for buttons 
					play: 'Reproducir', 
					pause: 'Pausa',
					mute: 'Desactivar sonido',
					unmute: 'Activar sonido',
					fullscreen: 'Pantalla completa',
					fullscreenExit: 'Salir de pantalla completa'
				}
			} 
		 }
	});
}
// Visor para HD - VISOR CON MOSCA
function cargarVisor_hd_conMosca(){
	$f("flowvideo", urlCervantes+"/flowplayer.commercial-3.2.7.swf", { 
		key: '#@fe5c2dba052f6420faf',
		clip: {
			autoPlay: true,
			autoBuffering: true,
			scaling: 'fit',
			provider: 'velocix',
			url: stream_hd
		},
		
		canvas: { 
			// use a wicked backgound color 
			background: 'none', 
			// a gradient 
			backgroundGradient: 'none' 
		},
		
		 // custom labels on the play button 
		play: {
			replayLabel: '',
			url:  urlCervantes+'/img/fondo-play.gif', 
			width: 66, 
			height: 35 
		},
		logo: {
			url: urlCervantes+'/img/HD-on-mosca.png',
			fullscreenOnly: false,
			opacity: 1,
			top: 10,
			left: 10
		},
		/*// gradually show on mouseover
		onMouseOver: function() {
			this.getPlugin("logo").fadeTo(0.8, 1000);
		},
		// gradually hide on mouseout
		onMouseOut: function() {
			this.getPlugin("logo").fadeTo(0, 1000);
		},*/
		plugins:  {
			velocix: {
				url:  urlCervantes+'/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: url_hd
		},
		content: {
			url: urlCervantes+'/flowplayer.content-3.2.0.swf',
			top: 0, left: 0, width: '100%', height: 30,
			backgroundColor: 'transparent', backgroundGradient: 'none', border: 0, borderRadius: 'none',
			textDecoration: 'outline', 
			style: {  
				body: {  
					fontSize: 12,
					fontFamily: 'Arial', 
					color: '#ffffff'
				}  
			}  		
		},
		controls: {
				// tooltips configuration 
				tooltips: { 
					// enable english tooltips on all buttons 
					buttons: true, 
					// customized texts for buttons 
					play: 'Reproducir', 
					pause: 'Pausa',
					mute: 'Desactivar sonido',
					unmute: 'Activar sonido',
					fullscreen: 'Pantalla completa',
					fullscreenExit: 'Salir de pantalla completa'
				}
			} 
		 }
	});
}

