﻿var xmlData;
var webUrl;
var timerID = 0;
var headerPic = ''


function validateEmail (email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
}


$(document).ready(function(){
	webUrl = 'http://' + window.location.hostname;

	if ((webUrl.indexOf('localhost') >= 0) || (webUrl.indexOf('fotopanoramic') >= 0)) {
		webUrl = webUrl + '/rvd';
	}
	
	//Prepara el enlace para mostrar el reproductor de streaming en una ventana
	$('#linkPlayer').click(function () {window.open(webUrl + '/stream/player.html','','_blank,width=450,height=300');});
	//$('#linkPlayer').click(function () {window.open('http://www.fotopanoramic.com/rvd/stream/player.html','','_blank,width=320,height=200');});
	$('#linkPlayer').attr('href', '#');

	//Los enlaces de los anuncios de patrocinadores se abren en una nueva ventana/pestaña del navegador
	$('#patrocinadors li a').attr("target", "_blank");

	//Los enlaces indicados también se abren en una nueva ventana/pestaña del navegador
	$('a._blank').attr("target", "_blank");

	//Comprueba si se ha establecido alguna excepción a la programación habitual
	getCurrentHeader();	
});


function getCurrentHeader () {
	$.get(
		webUrl + '/wp/wp-content/themes/rvd/control-programacio.php',
		function (d) {
			if (d == '') {
				//Es la programación habitual
				//Carga y procesa el documento XML con la programación correspondiente al dia de la semana
				$.get(
					webUrl + '/cabeceras/programa/' + $('#top-infoBar #diaSemana').text() + '.xml',
					function (d) {
						xmlData = d;
						setHeaderPic(d);
					});
			} else {
				//Se ha fijado una excepción a la programación habitual
				$('#imagen_prgm').attr('src', webUrl + '/cabeceras/' + d);
			}
		}
	);
	timerID = setTimeout("getCurrentHeader(xmlData)", 30000);
}

//Procesa el archivo XML correspondiente a la programación del día en curso
function setHeaderPic (xml) {
	var inici, final, sigue, pic;
	var t = getTimeNow()

	$('programa', xml).each(function(i) {
		inici = $(this).attr('inici');
		final = $(this).attr('final');

		if ((t>=inici) && (t<=final)) {
			pic = $(this).attr('cabecera');

			if (pic!=headerPic) {
				headerPic = pic;
				sigue = $(this).next().attr('inici');

				$('#imagen_prgm').attr('src', webUrl + '/cabeceras/' + headerPic);
				$('#imagen_prgm').attr('alt', $(this).attr('titol'));
				$('#cabecera #next').html('A continuació: ' + $(this).attr('seguent') + ' (' + sigue.substring(0,sigue.lastIndexOf(':')) + ')');
			}
		}
	});
}


function getTimeNow() {
	var currentTime = new Date();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var seconds = currentTime.getSeconds();
	
	if (hours < 10) hours = "0" + hours;
	if (minutes < 10) minutes = "0" + minutes;
	if (seconds < 10) seconds = "0" + seconds;

	return (hours + ':' + minutes + ':' + seconds);
}


