var infoString   = "<div style=\"position:relative;color:#EEEFFF;overflow:auto;top:5%;padding:5%;\"><h2>Acampada: </h2><p>La zona de acampada es gratuita. Recuerda que no puedes acceder a la misma con tu vehículo. Justo al lado, hay una extensa zona de párking señalizada.</p><span id='infoSpan'></span><p>Horarios del camping:<br/>Aunque la acampada estará abierta toda la semana, el horario de arranque oficial será a partir del miercolés,<br/>a las 17:00 hrs hasta el domingo, a las 12:00 hrs</p><h2>Piscina*: </h2><p>La piscina solamente podrá ser usada por los usuarios que adquieran su &quot;bono piscina&quot; el cual les dará acceso en los siguientes intervalos horarios:</p><p>Jueves,viernes y sábado: de 10:00 hrs hasta 20:00 hrs.</p><p>*La admisión a la piscina estará regida por el ayuntamiento de Villena y sujeto a las normas municipales.</p><br/><h2>Alojamiento: </h2><p><a href=\"#\" onclick='loadAlojamiento()'>Alojamiento</a></p><br/><h2>Movilidad reducida: </h2><p>La organización habilitará una plataforma especial para discapacitados.</p><p>A la misma pdorán acceder los usuarios de la misma y un acompañante por persona.</p><br/></div>";
var newsString   = "<iframe src=\"noticias.html\" style=\"position:absolute;width:100%;border-style:none;height:90%;left:0%;top:10%;\"></iframe>";


var llegarTable  ="<table style='margin-bottom:10rem'><tr><td width='50%'>En tren (<a href='http://www.renfe.com'>Renfe</a>) :<ul><li>Madrid->Villena (2h 40m)</li><li>Murcia->Villena (2h)</li><li>Barcelona->Villena (4h 40m)</li></ul></td></tr><tr><td>En avi&oacute;n(<a href='http://www.aena-aeropuertos.es'>http://www.aena-aeropuertos.es</a>):<ul><li>Aeropuerto de Alicante (59 km)</li><li>Aeropuerto de San Javier (Murcia) (125 km)</li><li>Aeropuerto de Valencia (133 km)</li><li>Aeropuerto de Madrid (368 km) </li></ul></td></tr><td>En autob&uacute;s: <a href='http://www.alsa.es'>Alsa</a></td></tr><tr><td>Viajes organizados: <a href='http://www.leyendasdelrockfestival.com/web/index.php/viajes'>LINK a la web</a></td><tr><td><br/>Festival shuttle:</td></tr><tr><td> El Ayuntamiento de Villena fletar&aacute; dos l&iacute;neas de autobuses que comunicar&aacute;n el recinto con los lugares m&aacute;s emblem&aacute;ticos de la ciudad y viceversa.Hasta fin de las actuaciones.</td></td></tr></table>"
var llegarString = "<div id='llegar' style='position:relative;color:#EEEFFF;overflow:auto;top:10%;width:100%'><p>Dirección del recinto: Barrio San Francisco de Asís, s/n 03400 Villena, Alicante.<br/>En coche: Salida 185 de la carretera A31 (ambos sentidos).<br/><br/><span id='idSpanMap'></span></p><button id='btnLlegar' style='position:relative;'>Obtener indicaciones</button></p>"+llegarTable+"</div>";
var tiempoString = "<div id='meteo'></div><script>loadWeather();</script>";



var horarioString="<br/><br/><br/><select id='diaSelector' onchange='loadHorario(this.value)'> <option value='1' selected>Dia 5</option> <option value='2' >Dia 6</option> <option value='3' >Dia 7</option> <option value='4' >Dia 8</option> </select> <br/> <div id='dia1'> <table border='0' class='azucena_rosa'> <thead> <tr> <th class='time' style='border-style:none;background:transparent;'>&nbsp;</th> <th class='time'>Azucena</th> <th class='time'>Jesús de la rosa</th> </tr> <tr> <th class='time'>18:00</th> <th colspan='2' class='time'>DOORS OPEN</th> </tr> </thead> <tbody> <tr> <td class='time'>18:30 - 19:20</td> <td class='normal'>OKER</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>19:20 - 20:20</td> <td class='normal'>&nbsp;</td> <td class='normal'>STORM</td> </tr> <tr> <td class='time'>20:20 - 22:00</td> <td class='normal'>SEPULTURA</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>22:20 - 23:30</td> <td class='normal'>&nbsp;</td> <td class='normal'>ROSENDO</td> </tr> <tr> <td class='time'>23:30 - 01:00</td> <td class='normal'>MEDINA <br>AZAHARA</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>20:20 - 22:00</td> <td class='normal'>&nbsp;</td> <td class='normal'>GIGATRÓN</td> </tr> </tbody> </table> </div> <div id='dia2'> <table border='0' class='reale'> <thead> <tr> <th colspan='2' class='time'>MARK REALE RIOT</th> </tr> <tr> <th class='time'>16:00</th> <th class='time'>DOORS OPEN</th> </tr> </thead> <tbody > <tr> <td class='time'>17:00 - 17:50</td> <td class='normal'>V.I.L</td> </tr> <tr> <td class='time'>18:10 - 19:00</td> <td class='normal'>FLESHGOD APOCALYPSE</td> </tr> <tr> <td class='time'>19:20 - 20:10</td> <td class='normal'>JORGE <br>SALÁN</td> </tr> <tr> <td class='time'>20:30 - 21:20</td> <td class='normal'>ROSA <br>NEGRA</td> </tr> <tr> <td class='time'>21:40 - 22:40</td> <td class='normal'>TOPO</td> </tr> <tr> <td class='time'>23:00 - 00:00</td> <td class='normal'>BLUES <br>PILLS</td> </tr> <tr> <td class='time'>00:20 - 01:20</td> <td class='normal'>KATAKLYSM</td> </tr> <tr> <td class='time'>01:40 - 02:40</td> <td class='normal'>OPERA <br>MAGNA</td> </tr> <tr> <td class='time'>03:00 - 03:50</td> <td class='normal'>DÖRIA</td> </tr> </tbody> </table> <table border='0' class='azucena_rosa'> <thead> <tr> <th class='time' style='border-style:none;background:transparent;'>&nbsp;</th> <th class='time'>Azucena</th> <th class='time'>Jesús de la rosa</th> </tr> <tr> <th class='time'>15:00</th> <th colspan='2' class='time'>DOORS OPEN</th> </tr> </thead> <tbody> <tr> <td class='time'>15:50 - 16:40</td> <td class='normal'>&nbsp;</td> <td class='normal'>AMARANTHE</td> </tr> <tr> <td class='time'>16:40 - 17:30</td> <td class='normal'>DESTRUCTION</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>17:30 - 18:20</td> <td class='normal'>&nbsp;</td> <td class='normal'>BACKYARD<br/>BABIES</td> </tr> <tr> <td class='time'>18:20 - 19:20</td> <td class='normal'>SONATA<br/>ARCTICA</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>19:30 - 20:30</td> <td class='normal'>&nbsp;</td> <td class='normal'>DEATH</td> </tr> <tr> <td class='time'>20:40 - 21:50</td> <td class='normal'>OVERKILL</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>20:20 - 22:00</td> <td class='normal'>&nbsp;</td> <td class='normal'>WITHIN<br/>TEMPTATION</td> </tr> <tr> <td class='time'>23:20 - 00:30</td> <td class='normal'>SABATON</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>00:40 - 01:50</td> <td class='normal'>&nbsp;</td> <td class='normal'>GAMMA<br/>RAY</td> </tr> <tr> <td class='time'>01:50 - 03:00</td> <td class='normal'>TIERRA<br/>SANTA</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>03:00 - 04:00</td> <td class='normal'>&nbsp;</td> <td class='normal'>VITA<br/>IMANA</td> </tr> </tbody> </table> </div> <div id='dia3'> <table border='0' class='reale'> <thead> <tr> <th colspan='2' class='time'>MARK REALE RIOT</th> </tr> <tr> <th class='time'>16:00</th> <th class='time'>DOORS OPEN</th> </tr> </thead> <tbody > <tr> <td class='time'>17:00 - 17:50</td> <td class='normal'>AZRAEL</td> </tr> <tr> <td class='time'>18:10 - 19:00</td> <td class='normal'>ZENOBIA</td> </tr> <tr> <td class='time'>19:20 - 20:10</td> <td class='normal'>CHINO <br>BANZAI</td> </tr> <tr> <td class='time'>20:30 - 21:20</td> <td class='normal'>ANGEL <br>WITCH</td> </tr> <tr> <td class='time'>21:40 - 22:40</td> <td class='normal'>FREEDOM<br/>CALL</td> </tr> <tr> <td class='time'>23:00 - 00:00</td> <td class='normal'>RECKLESS <br>LOVE</td> </tr> <tr> <td class='time'>00:20 - 01:20</td> <td class='normal'>ASFALTO</td> </tr> <tr> <td class='time'>01:40 - 02:40</td> <td class='normal'>ZARPA</td> </tr> <tr> <td class='time'>03:00 - 03:50</td> <td class='normal'>CELTIBEERIAN</td> </tr> </tbody> </table> <table border='0' class='azucena_rosa'> <thead> <tr> <th class='time' style='border-style:none;background:transparent;'>&nbsp;</th> <th class='time'>Azucena</th> <th class='time'>Jesús de la rosa</th> </tr> <tr> <th class='time'>16:00</th> <th colspan='2' class='time'>DOORS OPEN</th> </tr> </thead> <tbody> <tr> <td class='time'>16:30 - 17:20</td> <td class='normal'>ORPHANED<br/>LAND</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>17:20 - 18:10</td> <td class='normal'>&nbsp;</td> <td class='normal'>TURISAS</td> </tr> <tr> <td class='time'>18:10 - 19:10</td> <td class='normal'>REFUGE</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>19:10 - 20:30</td> <td class='normal'>&nbsp;</td> <td class='normal'>ROCK<br/>ICONS</td> </tr> <tr> <td class='time'>20:40 - 21:50</td> <td class='normal'>EDGUY</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>22:00 - 23:10</td> <td class='normal'>&nbsp;</td> <td class='normal'>THE<br/>DARKNESS</td> </tr> <tr> <td class='time'>23:20 - 00:30</td> <td class='normal'>WARCRY</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>00:40 - 01:50</td> <td class='normal'>&nbsp;</td> <td class='normal'>DORO</td> </tr> <tr> <td class='time'>01:50 - 03:00</td> <td class='normal'>SARATOGA</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>03:00 - 04:00</td> <td class='normal'>&nbsp;</td> <td class='normal'>ALQUIMIA</td> </tr> </tbody> </table> </div> <div id='dia4'> <table border='0' class='reale'> <thead> <tr> <th colspan='2' class='time'>MARK REALE RIOT</th> </tr> <tr> <th class='time'>16:00</th> <th class='time'>DOORS OPEN</th> </tr> </thead> <tbody > <tr> <td class='time'>17:00 - 17:50</td> <td class='normal'>IN MUTE</td> </tr> <tr> <td class='time'>18:10 - 19:00</td> <td class='normal'>TAKO</td> </tr> <tr> <td class='time'>19:20 - 20:10</td> <td class='normal'>XANDRIA</td> </tr> <tr> <td class='time'>20:30 - 21:20</td> <td class='normal'>BEETHOVEN R.</td> </tr> <tr> <td class='time'>21:40 - 22:40</td> <td class='normal'>VAN<br/>CANTO</td> </tr> <tr> <td class='time'>23:00 - 00:00</td> <td class='normal'>ALESTORM</td> </tr> <tr> <td class='time'>00:20 - 01:20</td> <td class='normal'>STORMZONE</td> </tr> <tr> <td class='time'>01:40 - 02:40</td> <td class='normal'>SHERPA</td> </tr> <tr> <td class='time'>03:00 - 03:50</td> <td class='normal'>VHÄLDEMAR</td> </tr> </tbody> </table> <table border='0' class='azucena_rosa'> <thead> <tr> <th class='time' style='border-style:none;background:transparent;'>&nbsp;</th> <th class='time'>Azucena</th> <th class='time'>Jesús de la rosa</th> </tr> <tr> <th class='time'>16:00</th> <th colspan='2' class='time'>DOORS OPEN</th> </tr> </thead> <tbody> <tr> <td class='time'>16:20 - 17:10</td> <td class='normal'>TANKARD</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>17:10 - 18:00</td> <td class='normal'>&nbsp;</td> <td class='normal'>FINNTROLL</td> </tr> <tr> <td class='time'>18:00 - 19:00</td> <td class='normal'>EPICA</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>19:00 - 20:00</td> <td class='normal'>&nbsp;</td> <td class='normal'>SODOM</td> </tr> <tr> <td class='time'>20:10 - 21:40</td> <td class='normal'>SÔBER</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>21:50 - 23:00</td> <td class='normal'>&nbsp;</td> <td class='normal'>KREATOR</td> </tr> <tr> <td class='time'>23:10 - 00:50</td> <td class='normal'>LOS<br/>SUAVES</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>01:00 - 02:10</td> <td class='normal'>&nbsp;</td> <td class='normal'>SATYRICON</td> </tr> <tr> <td class='time'>02:20 - 03:30</td> <td class='normal'>OBÚS</td> <td class='normal'>&nbsp;</td> </tr> <tr> <td class='time'>03:30 - 04:30</td> <td class='normal'>&nbsp;</td> <td class='normal'>LUJURIA</td> </tr> </tbody> </table> </div> ";




var started=0;
var aboutImg= new Image();
aboutImg.src='./img/leyendas.jpg';
aboutImg.width="80%";

var mapImg=new Image();
mapImg.src="./img/mapallegar.jpg";
mapImg.style='width:80%;height:25%;';

var imgInfo=new Image();
imgInfo.src='./img/acampada.png';

var imgPlano=new Image();
imgPlano.src='./img/recinto.png';
imgPlano.width='80%' ;
imgPlano.style='position:relative;top:15%;';


var about		 = "<div style=\"position:relative;color:#EEEFFF;overflow:auto;top:5%;padding:5%;\">Leyendas del Rock 2015<br/><p>Aplicaci&oacute;n creada por TrallaMetal Apps, disponible en Android, iOS, Windows Phone 8.1 y en Firefox OS, con la aprobación del Festival.<br/><br/><span id='aboutImgSpan'></span><br/>TrallaMetalApps@gmail.com</div>";


var viewPagerOnePageStr="<div id='ViewPager'   style='overflow-y:scroll; overflow-x: -moz-hidden-unscrollable; width:100%; height:100%; left:0px; position:absolute;'><div class='page'><h1 style='margin-top:10%'>Azucena</h1><br/><div id='bands0' style='z-index:5'></div></div><span id='controls'></span></div>";
var viewPagerStrTwoPage="<div id='ViewPager'   style='overflow-y:scroll; overflow-x: -moz-hidden-unscrollable; width:100%; height:100%; left:0px; position:absolute;'><div class='page'><h1 style='margin-top:10%'>Azucena</h1><br/><div id='bands0' style='z-index:5'></div></div><div class='page'><h1 style='margin-top:10%'>Jes&uacute;s de la rosa</h1><div id='bands1' style='z-index:5'></div></div><span id='controls'></span></div>";
var viewPagerStrThreePage="<div id='ViewPager' style='overflow-y:scroll; overflow-x: -moz-hidden-unscrollable; width:100%; height:100%; left:0px; position:absolute;'><div class='page'><h1 style='margin-top:10%'>Azucena</h1><br/><div id='bands0' style='z-index:5'></div></div><div class='page'><h1 style='margin-top:10%'>Jes&uacute;s de la Rosa</h1><div id='bands1' style='z-index:5'></div></div><div class='page'><h1 style='margin-top:10%'>Mark Reale/ Riot</h1><div id='bands2' style='z-index:5'></div></div><span id='controls' style='-moz-hidden-unscrollable'></span></div>";


var openedSlide=false;
var anterior=0;

var initFragment=0;
var dia1=11;
var dia2=12;
var dia3=13;
var dia4=14;
var infoFragment=2;
var llegar=3;
var plano=4;
var horarioFragment=755;
var noticiasFragment=5;
var tiempoFragment=6;
var aboutFragment=8;
var vDia;
var diaGrupos;


function timerAlert(e) {  
  var notification = navigator.mozNotification.createNotification(
    "Alarma!",e.data.grupo+" estan a punto de tocar!!");
  notification.onclick = function() {
    navigator.mozApps.getSelf().onsuccess = function(e) {
      e.target.result.launch();
    }
  };
  notification.icon="./img/ic_launcher.png";
  notification.show();
  removeAlarm(e.data.id,e.data.grupo,true);
}


$(document).ready(function(){
	screen.mozLockOrientation("portrait-primary");
	
	loadAlarms();
	
	if(navigator.mozSetMessageHandler){
		navigator.mozSetMessageHandler("alarm", function(e) {			
			timerAlert(e);    
		}, false);
	}

	document.getElementById("btnMenu").onclick=function()
	{
		if(!openedSlide){document.getElementById("mainLayout").className="active";openedSlide=true;}
		else{document.getElementById("mainLayout").className="";openedSlide=false;}
	};
	
	//document.getElementsByClassName("sublayoutoption");	
	loadFragment(initFragment);
	$(".sub a").each(function(i,e){		
		$(this).click(function(e){
			var f=parseInt(($(this).attr("fragment")));			
			loadFragment(f);			
		});
	});
});




function loadFragment(fragment,param=1)
{
	var url="";
	var loadUrl=false;
	switch(fragment)
	{
		case initFragment:					
			$( "#principal" ).html("<img src=\"./img/init.jpg\" style='position:relative;width:100%;margin-top:5%;'/>");
		break;
		case dia1:				
			$( "#principal" ).html(viewPagerStrTwoPage);
			 vDia=1;			
			 loadViewPager();
			 generaTabla(0);
			 asignarAlarma();
		break;
		case dia2:					
			$( "#principal" ).html(viewPagerStrThreePage);
			vDia=2;
			loadViewPager();
			generaTabla(1);
			asignarAlarma();
		break;
		case dia3:		
			$( "#principal" ).html(viewPagerStrThreePage);
			vDia=3;
			loadViewPager();
			generaTabla(2);
			asignarAlarma();
		break;
		case dia4:
			$( "#principal" ).html(viewPagerStrThreePage);
			vDia=4;
			loadViewPager();
			generaTabla(3);
			asignarAlarma();
		break;
		case llegar:
			$( "#principal" ).html(llegarString);
			$("#btnLlegar").click(function(){
				loadPos();
			});
			$("#idSpanMap").html(mapImg);
			setLinkClicks();
		break;
		case plano:
			$( "#principal" ).html(imgPlano);
		break;
		case noticiasFragment:
			$( "#principal" ).html(newsString);
		break;
		case infoFragment:	
			$( "#principal" ).html(infoString);
			$("#infoSpan").html(imgInfo);
		break;		
				
		case aboutFragment:
			$( "#principal" ).html(about);			
			$("#aboutImgSpan").html(aboutImg);
		break;
		
		case tiempoFragment:
			$( "#principal" ).html(tiempoString);
		break;
		case horarioFragment:
			$("#principal").html(horarioString);
		break;
	}
	
	if(loadUrl)
	{
		$.get( url, function( data ){
			$( "#principal" ).html(data);
		});
	}
	
	document.getElementById("mainLayout").className="";
	openedSlide=false;
}

var myLatitude=0;
var myLongitude=0;
var url ="";
function loadPos()
{
	if(navigator.geolocation)
	{
		navigator.geolocation.getCurrentPosition(success,errorFunction);
	}
}
function success(pos)
{	
	myLatitude=pos.coords.latitude;
	myLongitude=pos.coords.longitude;
	var direccion="http://maps.google.com/maps?saddr="+myLatitude+","+myLongitude+"&daddr=38.630502,-0.848062";
	if(window.MozActivity)
	{		
		var intent = new MozActivity({
			name: "view",
			data: {
				type: "url",
				url: direccion 
			}
		});				
	}
	else
	{
		var win = window.open(direccion, '_blank');
        win.focus();
	}
}

function loadAlojamiento()
{
	var direccion="http://www.leyendasdelrockfestival.com/web/index.php?option=com_content&view=article&id=5&Itemid=144&lang=es";
	var intent = new MozActivity({
		name: "view",
		data: {
			type: "url",
			url: direccion 
		}
	});
}

function loadWeather()
{
	if(navigator.onLine)
	{
		var direccion="http://api.openweathermap.org/data/2.5/forecast/daily?id=2509463&start=1404734400&end=1410134400000";
		$.ajax({
			url:   direccion,
			type:  'get',	                
			success:  function (response) {
				setMeteo(response.list);
			}
		});
	}
}

function setMeteo(days)
{
	var tableHeader="<table id='tabla' style='margin-top:2em;width:100%;' cellspacing='25%'>";		
	strRes="<tr>";
	var startDay;
	var endDay;
	for(var i=0;i<7;i++)
	{
		var code=days[i].weather[0].icon;
		var iconURL="http://openweathermap.org/img/w/"+code+".png";
		
		var unix_timestamp=days[i].dt;
		var date = new Date(unix_timestamp*1000);
		var day=date.getDate();
		if(i==0)
		{
			startDay=day;
		}
		if(i==6)
		{
			endDay=day;
		}
		var minTemp=(parseFloat(days[i].temp.min-273)).toFixed(2);
		var maxTemp=(parseFloat(days[i].temp.max-273)).toFixed(2);
		strRes+="<td style='width:35em'>";
		strRes+="<span class='climate' style='color:#EEEFFF'>"+day+"<br/>";
		strRes+="<img src='"+iconURL+"' class='meteo_img'/><br/>";
		strRes+="M&iacute;nima: "+minTemp+"ºC";
		strRes+="<br/>";
		strRes+="M&aacutexima: "+maxTemp+"ºC";
		strRes+="<br/>";	
		strRes+="</span>";
		strRes+="</td>";
	}
	strRes+="</tr>";
	strRes+="</table>";
	
	var prevision="prevision del d&iacute;a "+startDay+" al "+endDay;
	tableHeader+="<th style='color:#EEEFFF;text-align:left' colspan='3'>"+prevision+"</th>";
	var contentTable=tableHeader+strRes;
	$("#meteo").html(contentTable);	
}

function errorFunction(pos)
{}


function setLinkClicks()
{
	$("#llegar a").each(function(i,e){
			$(this).click(function(e){
				e.preventDefault();
				var direccion =$(this).attr("href");
				if(window.MozActivity)
				{			
					var intent = new MozActivity({
						name: "view",
						data: {
							type: "url",
							url: direccion 
						}
					});
				}
				else
				{
					var win = window.open(direccion, '_blank');
					win.focus();
				}		
			});
		});
}

function asignarAlarma()
{		
	$("alarm").each(function(i,e){
		$(this).click(function(e){
			var ids=$(this).attr("id").split(":");			
			
			var diaE=(diaGrupos[ids[0]]);
			
			var escenario=(diaE[ids[1]]);
			var grupo=(escenario.bandas[ids[2]]);
			var dia =parseInt(escenario.dia);
			var hora=grupo.hora.split("-")[0];
			var madrugada=grupo.madrugada;
			if(navigator.mozAlarms)
			{
				if(localStorage.getItem(grupo.bandID)!=null)
				{
					removeAlarm(grupo.bandID,grupo.bandName,false);
				}
				else
				{						
					if(madrugada==="true")
					{					
						dia=dia+1;						
					}					
					createAlarm(dia,hora,grupo.bandName,grupo.bandID);
				}
			}
		});
	});
}
var alarms=[];

function createAlarm(dia,hora,bandName,bandID)
{
	var myAlarmDate  = new Date();
	myAlarmDate.setYear(2015);
	myAlarmDate.setMonth(7);
	dia=parseInt(dia);
	myAlarmDate.setDate(dia);
	var tiempo=hora.split(":");
	
	var hora=parseInt(tiempo[0]);
	var minuto=parseInt(tiempo[1]);
	myAlarmDate.setHours(hora);	
	myAlarmDate.setMinutes(minuto);
	
	var data = {
		grupo: bandName,
		id: bandID
	};
	
	var alarmData={
		fecha:myAlarmDate,
		dataAlarm:data
	}
	alarms.push(alarmData);
	
	
	var request = navigator.mozAlarms.add(myAlarmDate, "ignoreTimezone", data);
	request.onsuccess = function () 
	{
		var alarmRequest = navigator.mozAlarms.getAll();
		alarmRequest.onsuccess = function() 
		{
			new Toast("alarma creada para "+bandName,Toast.LENGTH_SHORT,0);
			var newAlarmId = this.result[(this.result.length)-1].id;				
			localStorage.setItem(bandID,newAlarmId);
			localStorage.setItem("alarms",JSON.stringify(alarms));
		}			
	};
	request.onerror = function () 
	{
		console.log("An error occurred: " + this.error.name);
	};
}
function removeAlarm(bandID,bandName,fromAlarm)
{	
	if(fromAlarm==false)
	{
		new Toast("alarma borrada para "+bandName,Toast.LENGTH_SHORT,0);
	}
	var j=-1;
	for(int i=0;i<alarms.length;i++)
	{
		if(alarms[i].dataAlarm.id==bandID)
		{
			j=i;
			break;
		}
	}
	if(j!=-1)
	{
		alarms.splice(j,1);
	}
	localStorage.removeItem(bandID);
	localStorage.setItem("alarms",JSON.stringify(alarms));
}
function loadAlarms()
{	
	if(started==0)
	{		
		alarms=JSON.parse(localStorage.getItem("alarms"));
		alert(alarms);
		for(var i=0;i<alarms.length;i++)
		{
			var myAlarmDate=alarms[i].fecha;
			var data=alarms[i].dataAlarm;

			var request = navigator.mozAlarms.add(myAlarmDate, "ignoreTimezone", data);
			request.onsuccess = function () 
			{
				var alarmRequest = navigator.mozAlarms.getAll();
				alarmRequest.onsuccess = function() 
				{				
					var newAlarmId = this.result[(this.result.length)-1].id;				
					localStorage.setItem(bandID,newAlarmId);
				}			
			};
			request.onerror = function () 
			{
				console.log("An error occurred: " + this.error.name);
			};
		}
	}
	started=1;
}
















//****************************************************************************************************/

var Dia1=[				
		{
        "escenario": "Azucena",
        "dia": "5",
        "bandas": 
		[				
				{
					"img": "./img/d0/2.jpg",
					"url": "https://facebook.com/OkerBand",
					"hora": "18:30-19:20",
					"bandID": "d1e1g3",
					"bandName": "Oker",
					"madrugada":"false"
				},
				{
					"img": "./img/d0/4.jpg",
					"url": "http://www.sepultura.com.br/",
					"hora": "20:20-22:00",
					"bandID": "d1e1g5",
					"bandName": "Sepultura",
					"madrugada":"false"
				},
				{
					"img": "./img/d0/1.jpg",
					"url": "http://www.medinaazahara.es/",
					"hora": "16:00-16:50",
					"bandID": "d1e1g2",
					"bandName": "Medina Azahara",
					"madrugada":"false"
				},
				
			]
		},
		{
        "escenario": "Jes&uacute;s de la rosa",
        "dia": "5",
        "bandas": 
		[
				{
					"img": "./img/d0/5.jpg",
					"url": "https://www.facebook.com/pages/THE-STORM/195172290542334",
					"hora": "19:20-20:20",
					"bandID": "d1e1g6",
					"bandName": "Storm",
					"madrugada":"false"
				},
				{
					"img": "./img/d0/3.jpg",
					"url": "https://www.rosendo.es",
					"hora": "22:00-23:30",
					"bandID": "d1e1g4",
					"bandName": "Rosendo",
					"madrugada":"false"
				},
				{
					"img": "./img/d0/0.jpg",
					"url": "https://www.gigatron.biz/",
					"hora": "01:00-02:00",
					"bandID": "d1e1g1",
					"bandName": "Gigatron",
					"madrugada":"true"
				},
				
			]
		}
	];

var Dia2=[				
		{
        "escenario": "Azucena",
        "dia": "6",
        "bandas": [				
				{
					"img": "./img/d1/e1/2.jpg",
					"url": "http://www.destruction.de/",
					"hora": "16:40-17:30",
					"bandID": "d1e1g2",
					"bandName": "Destruction",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e1/8.jpg",
					"url": "http://sonataarctica.info/",
					"hora": "18:20-19:20",
					"bandID": "d1e1g6",
					"bandName": "Sonata Arctica",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e1/6.jpg",
					"url": "http://wreckingcrew.com/",
					"hora": "20:40-21:50",
					"bandID": "d1e1g6",
					"bandName": "Overkill",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e1/7.jpg",
					"url": "http://www.sabaton.net/",
					"hora": "23:20-00:30",
					"bandID": "d1e1g6",
					"bandName": "Sabaton",
					"madrugada":"false"
				},
				{
					"img": "./img/d2/e1/8.jpg",
					"url": "http://www.tierrasantarock.com/",
					"hora": "01:50-03:00",
					"bandID": "d2e1g9",
					"bandName": "Tierra Santa",
					"madrugada":"true"
				}				
			]
		},
		{//escenario nuevo recien añadido
			"escenario": "Jes&uacute;s de la rosa",
			"dia": "6",
			"bandas": 
			[				
				{
					"img": "./img/d3/e1/3.jpg",
					"url": "http://www.Amaranthe.se/",
					"hora": "15:50-16:40",
					"bandID": "d3e1g3",
					"bandName": "Amaranthe",
					"madrugada":"false"
				},	
				{
					"img": "./img/d1/e1/1.jpg",
					"url": "http://www.backyardbabies.com/",
					"hora": "17:30-18:20",
					"bandID": "d1e1g1",
					"bandName": "Backyard Babies",
					"madrugada":"false"
				},	
				{
					"img": "./img/d1/e1/3.jpg",
					"url": "https://metalcrusade.org",
					"hora": "19:30-20:30",
					"bandID": "d1e1g3",
					"bandName": "DEATH DTA Tours",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e1/10.jpg",
					"url": "http://www.within-temptation.com/",
					"hora": "22:00-23:10",
					"bandID": "d1e1g6",
					"bandName": "Within Temptation",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e1/4.jpg",
					"url": "http://www.gammaray.org/",
					"hora": "00:40-01:50",
					"bandID": "d1e1g4",
					"bandName": "Gamma Ray",
					"madrugada":"true"
				},
				{
					"img": "./img/d1/e1/9.jpg",
					"url": "http://vitaimana.com/",
					"hora": "03:00-04:00",
					"bandID": "d1e1g6",
					"bandName": "Vita Imana",
					"madrugada":"true"
				}
			]
		},
		{
			"escenario": "Mark Reale",
			"dia": "6",
			"bandas": 
			[
				{
					"img": "./img/d1/e2/9.jpg",
					"url": "http://www.vilband.com/",
					"hora": "17:00-17:50",
					"bandID": "d1e2g9",
					"bandName": "V.I.L",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e2/2.jpg",
					"url": "http://www.fleshgodapocalypse.com/",
					"hora": "18:10-19:00",
					"bandID": "d1e2g2",
					"bandName": "Fleshgod Apocalypse",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e2/4.jpg",
					"url": "http://www.jorgesalan.com/",
					"hora": "19:20-20:10",
					"bandID": "d1e2g4",
					"bandName": "Jorge Sal&aacute;n",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e2/7.jpg",
					"url": "http://www.rosanegra.info/",
					"hora": "20:30-21:20",
					"bandID": "d1e2g7",
					"bandName": "Rosa Negra",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e2/8.jpg",
					"url": "http://www.facebook.com/TopoOficial",
					"hora": "21:40-22:40",
					"bandID": "d1e2g8",
					"bandName": "Topo",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e2/1.jpg",
					"url": "http://www.bluespills.com",
					"hora": "23:00-00:00",
					"bandID": "d1e2g1",
					"bandName": "Blues Pills",
					"madrugada":"false"
				},
				{
					"img": "./img/d1/e2/5.jpg",
					"url": "http://www.facebook.com/kataklysm",
					"hora": "00:20-01:20",
					"bandID": "d1e2g5",
					"bandName": "Kataklysm",
					"madrugada":"true"
				},
				{
					"img": "./img/d1/e2/6.jpg",
					"url": "http://www.operamagna.net/",
					"hora": "01:40-02:40",
					"bandID": "d1e2g6",
					"bandName": "Opera Magna",
					"madrugada":"true"
				},
				{
					"img": "./img/d1/e2/3.jpg",
					"url": "http://www.doriaoficial.com",
					"hora": "03:00-03:50",
					"bandID": "d1e2g3",
					"bandName": "Döria",
					"madrugada":"true"
				}				
			]
		}
	];
var Dia3=[
	{
        "escenario": "Azucena",
        "dia": "7",
        "bandas": [
						{
							"img": "./img/d2/e1/4.jpg",
							"url": "http://www.orphaned-land.com/",
							"hora": "16:30-17:20",
							"bandID": "d2e1g4",
							"bandName": "Orphaned Land",
							"madrugada":"false"
						},	
						{
							"img": "./img/d2/e1/5.jpg",
							"url": "http://www.refuge-band.de/",
							"hora": "18:10-19:10",
							"bandID": "d2e1g6",
							"bandName": "Refuge",
							"madrugada":"false"
						},
						{
							"img": "./img/d2/e1/3.jpg",
							"url": "http://www.edguy.net/",
							"hora": "20:40-21:50",
							"bandID": "d2e1g3",
							"bandName": "Edguy",
							"madrugada":"false"
						},																						
						{
							"img": "./img/d2/e1/10.jpg",
							"url": "http://www.warcry.as/",
							"hora": "23:20-0:30",
							"bandID": "d2e1g11",
							"bandName": "Warcry",
							"madrugada":"false"
						},
						{
							"img": "./img/d3/e1/7.png",
							"url": "http://www.saratoga.com.es/",
							"hora": "01:50-03:00",
							"bandID": "d3e1g7",
							"bandName": "Saratoga",
							"madrugada":"true"
						}
				]		
	},
	{
        "escenario": "Jes&uacute;s de la rosa",
        "dia": "7",
        "bandas": [
						{
							"img": "./img/d2/e1/9.jpg",
							"url": "http://www.turisas.com/",
							"hora": "17:20-18:10",
							"bandID": "d2e1g10",
							"bandName": "Turisas",
							"madrugada":"false"
						},
						{
							"img": "./img/d2/e1/6.jpg",
							"url": "https://www.facebook.com/rockiconsone",
							"hora": "19:10-20:30",
							"bandID": "d2e1g7",
							"bandName": "Rock Icons",
							"madrugada":"false"
						},
						{
							"img": "./img/d2/e1/7.jpg",
							"url": "https://www.facebook.com/thedarknessofficial",
							"hora": "22:00-23:10",
							"bandID": "d2e1g8",
							"bandName": "The Darkness",
							"madrugada":"false"
						},
						{
							"img": "./img/d2/e1/2.jpg",
							"url": "http://www.doromusic.de/",
							"hora": "00:40-01:50",
							"bandID": "d2e1g2",
							"bandName": "Doro",
							"madrugada":"true"
						},
						{
							"img": "./img/d2/e1/1.jpg",
							"url": "http://www.albertorionda.net/",
							"hora": "03:00-04:00",
							"bandID": "d2e1g1",
							"bandName": "Alquimia",
							"madrugada":"true"
						}
				]		
	},
	{
		"escenario": "Mark Reale",
        "dia": "7",
        "bandas": [
						{
							"img": "./img/d2/e2/3.jpg",
							"url": "http://www.azraelmetal.com/",
							"hora": "17:00-17:50",
							"bandID": "d2e2g3",
							"bandName": "Azrael",
							"madrugada":"false"
						},	
						{
					
							"img": "./img/d2/e2/9.jpg",
							"url": "http://www.zenobia.org/",
							"hora": "18:10-19:00",
							"bandID": "d2e2g9",
							"bandName": "Zenobia",
							"madrugada":"false"
						},
						{
							"img": "./img/d1/e2/5.jpg",							
							"url": "http://www.chinobanzai.de",
							"hora": "19:20-20:10",
							"bandID": "d2e2g5",
							"bandName": "Chino Banzai",
							"madrugada":"false"
						},						
						{
							"img": "./img/d2/e2/1.jpg",
							"url": "http://www.angelwitchcoven.com/",
							"hora": "20:30-21:20",
							"bandID": "d2e2g1",
							"bandName": "Angel Witch",
							"madrugada":"false"
						},
						{
							"img": "./img/d2/e2/6.jpg",
							"url": "http://www.freedom-call.net/",
							"hora": "21:40-22:40",
							"bandID": "d2e2g6",
							"bandName": "Freedom Call",
							"madrugada":"false"
						},
						{
							"img": "./img/d2/e2/7.jpg",
							"url": "http://www.recklesslove.com/",
							"hora": "23:00-00:00",
							"bandID": "d2e2g7",
							"bandName": "Reckless Love",
							"madrugada":"false"
						},
						{
							"img": "./img/d2/e2/2.jpg",
							"url": "http://www.gruposasfalto.com/",
							"hora": "00:20-01:20",
							"bandID": "d2e2g2",
							"bandName": "Asfalto",
							"madrugada":"true"
						},
						{
							"img": "./img/d2/e2/8.jpg",
							"url": "https://www.facebook.com/pages/Zarpa/137989763334/",
							"hora": "01:40-02:40",
							"bandID": "d2e2g8",
							"bandName": "Zarpa",
							"madrugada":"true"
						},
						{
							"img": "./img/d2/e2/4.jpg",
							"url": "http://www.celtibeerian.com/",
							"hora": "03:00-03:50",
							"bandID": "d2e2g4",
							"bandName": "Celtibeerian",
							"madrugada":"true"
						}
				]
	}
];
var Dia4=[
		{
		"escenario": "Azucena",
		"dia": "8",
		"bandas": [
			{
				"img": "./img/d3/e1/11.jpg",
				"url": "http://www.tankard.info/",
				"hora": "16:20-17:10",
				"bandID": "d3e1g11",
				"bandName": "Tankard",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e1/1.jpg",
				"url": "http://www.epica.nl/",
				"hora": "18:00-19:00",
				"bandID": "d3e1g1",
				"bandName": "Epica",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e1/9.jpg",
				"url": "http://www.soberweb.com/",
				"hora": "20:10-21:40",
				"bandID": "d3e1g9",
				"bandName": "Sôber",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e1/5.jpg",
				"url": "http://www.lossuavesweb.com/",
				"hora": "23:10-00:50",
				"bandID": "d3e1g5",
				"bandName": "Los Suaves",
				"madrugada":"false"
			},
			{
					"img": "./img/d1/e1/5.jpg",
					"url": "http://www.obusrock.com/",
					"hora": "02:20-03:30",
					"bandID": "d1e1g5",
					"bandName": "Obus",
					"madrugada":"true"
			}											
		]
	},
	{
		"escenario": "Jes&uacute;s de la rosa",
		"dia": "8",
		"bandas": [
			{
				"img": "./img/d3/e1/2.jpg",
				"url": "http://www.finntroll-music.com/",
				"hora": "17:10-18:00",
				"bandID": "d3e1g2",
				"bandName": "Fintroll",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e1/10.jpg",
				"url": "http://www.sodomized.info/",
				"hora": "19:00-20:00",
				"bandID": "d3e1g10",
				"bandName": "sodom",
				"madrugada":"false"
			},
			{			
				"img": "./img/d3/e1/4.jpg",
				"url": "http://kreator-terrorzone.de/",
				"hora": "21:50-23:00",
				"bandID": "d3e1g4",
				"bandName": "Kreator",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e1/8.jpg",
				"url": "http://www.satyricon.no/",
				"hora": "01:00-02:10",
				"bandID": "d3e1g8",
				"bandName": "Satyricon",
				"madrugada":"true"
			},
			{
				"img": "./img/d3/e1/6.jpg",
				"url": "http://www.lujuriarock.com/",
				"hora": "03:30-04:30",
				"bandID": "d3e1g6",
				"bandName": "Lujuria",
				"madrugada":"true"
			}
		]
	},
	{
	"escenario": "Mark Reale",
    "dia": "8",
    "bandas": [
			{
				"img": "./img/d3/e2/3.jpg",
				"url": "http://inmute.com/",
				"hora": "17:00-17:50",
				"bandID": "d3e2g3",
				"bandName": "In Mute",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e2/6.jpg",
				"url": "http://www.takorock.wordpress.com/",
				"hora": "18:10-19:00",
				"bandID": "d3e2g6",
				"bandName": "Tako",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e2/9.jpg",
				"url": "http://xandria.de/",
				"hora": "19:20-20:10",
				"bandID": "d3e2g9",
				"bandName": "Xandria",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e2/2.jpg",
				"url": "https://www.facebook.com/pages/Beethoven-R/123616740538",
				"hora": "20:30-21:20",
				"bandID": "d3e2g2",
				"bandName": "Beethoven R.",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e2/7.jpg",
				"url": "http://vancanto.de/",
				"hora": "21:40-22:40",
				"bandID": "d3e2g7",
				"bandName": "Van Canto",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e2/1.jpg",
				"url": "http://www.alestorm.net/",
				"hora": "23:00-00:00",
				"bandID": "d3e2g1",
				"bandName": "Alestorm",
				"madrugada":"false"
			},
			{
				"img": "./img/d3/e2/5.jpg",
				"url": "http://www.stormzone.tv/",
				"hora": "00:20-01:20",
				"bandID": "d3e2g5",
				"bandName": "Stormzone",
				"madrugada":"true"
			},
			{
				"img": "./img/d3/e2/4.jpg",
				"url": "http://sherpaweb.org/",
				"hora": "01:40-02:40",
				"bandID": "d3e2g4",
				"bandName": "Sherpa",
				"madrugada":"true"
			},
			{
				"img": "./img/d3/e2/8.jpg",
				"url": "http://www.vhaldemar.net/",
				"hora": "03:00-03:50",
				"bandID": "d3e2g8",
				"bandName": "Vhäldemar",
				"madrugada":"true"
			}			
		]
	}
];
/*************************************Dias seccion
*/

var diaGrupos=[Dia1,Dia2,Dia3,Dia4];
function generaTabla(dia)
{
	var res="";			
	/*if(diaGrupos[dia].length==1)
	{*/
		$("#controls").hide();
	//}
	for(var i=0;i<diaGrupos[dia].length;i++)
	{
		res+="<div class='borderTable' style='position:relative;height:1000px;'>";
		res+="<table style='position:relative;right:1em;' id='grupos' align='left'>";
		var bandas=diaGrupos[dia][i].bandas;		
		for(var j=0;j<bandas.length;j++)
		{			
			res+="<tr>";
			res+="<td  align='center'>";
				res+="<img src='"+bandas[j].img+"'/>";
			res+="</td>";
			res+="<td>";
					res+="<ul>";
					res+="<li>";
						// dia,escenario,grupo
						res+="<alarm id='"+(dia)+":"+i+":"+j+"' style='z-index:-5;' >";
							res+="<img src='./img/bell.png'  class='appBtn alarmBtn' style='width:65%;display:block;'/>";
						res+="</alarm>";
					res+="</li>";
					
					res+="<li>";
						res+="<a href='"+bandas[j].url+"' class='iconTable' style='z-index:-5;'>";
			res+="<img src='./img/earth.png' class='appBtn' style='width:75%;'/>";
						res+="</a>";
					res+="</li>";
					res+="</ul>";
			res+="</td>";
			res+="</tr>";
			res+="<tr>";
			res+="<td colspan='2' class='bandTime'><span style='position:relative;display:block;left:28%;'>";
			res+=bandas[j].hora;
			res+="</span></td>";
			res+="</tr>";
			
		}
		res+="</table>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="<br/>";
		res+="</div>";
		$("#bands"+i).html(res);		
		res="";		
	}
	$(".borderTable a").each(function(i,e){
			$(this).click(function(e){
				e.preventDefault();
				var direccion =$(this).attr("href");
				if(window.MozActivity)
				{			
					var intent = new MozActivity({
						name: "view",
						data: {
							type: "url",
							url: direccion 
						}
					});
				}
				else
				{
					var win = window.open(direccion, '_blank');
					win.focus();
				}		
			});
	});
}

function loadHorario(pos)
{	
	pos=parseInt(pos);
	switch(pos)
	{
		case 1:
			$("#dia1").css("display","block");
			$("#dia2").css("display","none");
			$("#dia3").css("display","none");
			$("#dia4").css("display","none");
			break;

		case 2:					
			$("#dia1").css("display","none");
			$("#dia2").css("display","block");
			$("#dia3").css("display","none");
			$("#dia4").css("display","none");
			break;

		case 3:
			$("#dia1").css("display","none");	
			$("#dia2").css("display","none");
			$("#dia3").css("display","block");
			$("#dia4").css("display","none");
			break;

		case 4:
			$("#dia1").css("display","none");
			$("#dia2").css("display","none");
			$("#dia3").css("display","none");
			$("#dia4").css("display","block");
			break;
	}			
}