var getCalendario = null;
var xmlCalendario = null;
var CualMes=0;
var f = "";
var entrado = 0;
//alert('primerCualMes->'+CualMes);
function CrearCalendario(QueMes){	
	f = fecha;
//alert('crearcalendario quemes->'+QueMes);
	if (window.XMLHttpRequest){
		getCalendario=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		getCalendario=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (getCalendario){		
		var url="/zaragoza2016/jsp/actividad/calendario.xml";
		CualMes=QueMes;
		//alert('cualmes get Calendario->'+CualMes);
		getCalendario.onreadystatechange=CambioDeEstado;
		getCalendario.open("GET",url,true);
		getCalendario.send(null);	
	}else{
		alert ("Tu explorador no soporta XMLHttpRequest");
	}
}

function CambioDeEstado(){	
	if (getCalendario.readyState == 4){
		xmlCalendario = getCalendario.responseXML;
		//alert(xmlCalendario);
		xmlCalendario = xmlCalendario.getElementsByTagName('cita');
		//alert('cualmes CambioDeEstado->'+CualMes);
		setCal(CualMes);
	} 	
} 

function bisiesto(Anio) {
	if (Anio % 4 == 0)
	return true
	return false
}

function diasPorMes(mes, Anio) {
var meses = new Array(12)
meses[1] = 31; // Enero
meses[2] = (((Anio % 4 == 0) && (Anio % 100 != 0)) || (Anio % 400 == 0)) ? 29 : 28
meses[3] = 31 // Marzo
meses[4] = 30 // Abril
meses[5] = 31 // Mayo
meses[6] = 30 // Junio
meses[7] = 31 // Julio
meses[8] = 31 // Agosto
meses[9] = 30 // Septiembre
meses[10] = 31 // Octubre
meses[11] = 30 // Noviembre
meses[12] = 31 // Diciembre
return meses[mes]
}

function getNombreDelMes(mes) {
var meses = new Array(12)
meses[1] = "January"
meses[2] = "February"
meses[3] = "March"
meses[4] = "April"
meses[5] = "May"
meses[6] = "June"
meses[7] = "July"
meses[8] = "August"
meses[9] = "September"
meses[10] = "October"
meses[11] = "November"
meses[12] = "December"
return meses[mes]
}

function setCal(QueMes) {
	//alert('quemes->'+QueMes);
	//var QueMes = QueMes + 1;
	if(f.length > 0 && entrado == 0)
	{
		var anio = f.substring(0, 4);
		if (anio < 1000) {anio+=1900}
		var mes = f.substring(5, 7);
		mes--;
		var fecha = f.substring(8, 10);
		entrado = 1;
	}
	else
	{
		var now = new Date();
		var anio = now.getYear();
		if (anio < 1000) {anio+=1900}
		var mes = now.getMonth()+QueMes;
		//alert('messetCal->'+mes);
		if (now.getMonth()+QueMes > 11) {mes-=12;anio++}
		if (now.getMonth()+QueMes < 0) {mes+=12;anio--}
		var fecha = now.getDate()
	}
	var primerDia = new Date(anio, mes, 1)
	var primerDia2 = primerDia.getDay()
	primerDia = null
	var dias = diasPorMes(mes+1, anio)
	drawCal(primerDia2 + 7, dias, fecha, mes+1, anio)
}

function drawCal(firstDay, lastDate, date, Mes, Anio) {
//var aux = MesSeleccionado;
//alert('messelec->'+Mes);
var mesSiguiente = Mes + 1;	
var mesAnterior = Mes - 1;

//alert(mesSiguiente);
if(mesSiguiente==13)
{
	mesSiguiente = 1;
	var AnioSiguiente = 1 + Number(Anio);
}
else
{
	var AnioSiguiente = Anio;	
}

if(mesAnterior==0)
{
	mesAnterior = 12;
	var AnioAnterior = Anio-1;
}
else
{
	var AnioAnterior = Anio;	
}
var AnioAntes = Anio;
var MesAntes = mesAnterior;
if(MesAntes < 10) MesAntes = "0" + MesAntes;
var AnioDespues = AnioSiguiente;
var MesDespues = mesSiguiente;
if(MesDespues < 10) MesDespues = "0" + MesDespues;
if(mesAnterior == 12){AnioAntes--;}
var now = new Date()
var codigo = ""
codigo += '<div id="calendario" style="width:462px;"><div style="height:60px;background-image: url(/zaragoza2016/js/calendario/f_calendario_cab.jpg);background-repeat: no-repeat;background-position: center;"><div style="height: 21px;line-height:21px;text-align:left;"><span class="texto_calendario_actividades1">&nbsp;&nbsp;Cultural events in ' + getNombreDelMes(Mes) + ' ' + Anio + '</span></div><div><a href="/zaragoza2016/mostrarActividades.do?fecha=' + AnioAntes + '-' + MesAntes + '-01"><font style="cursor:pointer" class="meses"> << &nbsp;&nbsp;&nbsp;</font></a><font class="meses">'
	codigo += getNombreDelMes(Mes) + ' ' + Anio 
	codigo += '</font><a href="/zaragoza2016/mostrarActividades.do?fecha=' + AnioDespues + '-' + MesDespues + '-01"><font style="cursor:pointer" class="meses">&nbsp;&nbsp;&nbsp; >> </font></a></div></div>'
	codigo += '<div style="width:462px;background-color:#D9F2F9;"><table id="tablaTooltip" cellspacing=5 width="462" align="center" style="background-color:#D9F2F9;background-image:none;width:462px;">'
var cDia = 1
var curCell = 1
var curCellAbajo = 1

for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
	if (row > 1 || firstDay < 7) 
	{
		codigo += '<tr>'
	}
	for (var col = 1; col <= 7; ++col) {
		if (cDia > lastDate)
			break
		if (curCell < firstDay) 
		{
			if(firstDay > 7)
			{
				if(curCell > 7)codigo += '<td class="diaAntes"></td>';
				//else codigo += '<td style="border-bottom: none;"></td>';
			}
			else
			{
				codigo += '<td class="diaAntes"></td>';
			}
//			codigo += '<td style="border-bottom: none;"></td>';
			curCell++
		} 
		else
		{
			var textoCelda = '<div style="height:15px;line-height:15px;text-align:right;width:50px;!margin-left:0px;"><span style="">' + cDia + '</span></div><div id="tool" style="height:15px;line-height:15px;">&nbsp;</div>';
			var textoTd = '<td class="celda" align="center">'
			var textoCita = "";
			var textoCita2 = cDia + " / " + getNombreDelMes(Mes) + " / " + Anio + "&nbsp;";
			var textoCita3 = "";
			var j = 0;
			for (i=0;i<xmlCalendario.length;i++){
				var diaCorregido = cDia;
				if(cDia < 10) diaCorregido = "0" + cDia;
				var mesCorregido = Mes;
				if(Mes < 10) mesCorregido = "0" + Mes;
				
				var fecha = xmlCalendario[i].getAttribute('fecha');
				var fechaAction = Anio+ "-" + mesCorregido + "-" + diaCorregido
				if (fecha == cDia+ "-" + Mes + "-" + Anio){		
					j = j + 1;
					var titulo = xmlCalendario[i].getAttribute('titulo');
					var descripcion = xmlCalendario[i].getAttribute('descripcion');
					var id = xmlCalendario[i].getAttribute('id');
					//alert('entra');
					textoCita = textoCita + " - " + descripcion;
					textoCita2 = textoCita2 + " <br /><a style='color:#fff' href='/zaragoza2016/agendaDeCultura/" + id + "/" + titulo +"'> - " + titulo + "</a>";
					if(j == 1)textoCita3 = '<div id="tool" style="height:15px;line-height:15px;"><div style="margin-left:0px;"><span class="evento">'+ j + ' Event</span></div></div>';
					else textoCita3 = '<div id="tool" style="height:15px;line-height:15px;"><div style="margin-left:0px;"><span class="evento">'+ j + ' Events</span></div></div>';
					//textoCita3 = textoCita3 + " <br /><a class='evento' href='mostrarActividad.do?idActividad=" + id + "'>&nbsp; - " + titulo + "</a>";
					textoCelda = '<div style="height:15px;line-height:15px;text-align:right;width:50px;!margin-left:-0px;"><span class="diacita" style="">' + cDia + '</span></div>';
					textoTd = '<td id="td" style="cursor:pointer;background-repeat: no-repeat;" class="celdacita" align="center" onmouseover="this.style.background=\'url(/zaragoza2016/js/calendario/caja_cal_naranja_over.gif)\';this.style.backgroundRepeat=\'no-repeat\'" onmouseout="this.style.background=\'url(/zaragoza2016/js/calendario/caja_cal_azul_osc.gif)\';this.style.backgroundRepeat=\'no-repeat\'"  onclick="document.getElementsByName(\'fechaEvento\')[0].value=\'' + Anio + '-' + Mes + '-' + cDia +'\';cargando(\'eventosdia\');new Ajax.Updater(\'eventosdia\', \'/zaragoza2016/mostrarActividades.do\', {asynchronous:true, parameters:Form.serialize(mostrarActividadesForm), indicator: \'indicatorParticular\',evalScripts: \'true\'});"><div style="width:98%;display:block;" title="">';
				}
			}
			j = 0;
			codigo += textoTd + textoCelda + textoCita3 + '</div></td>'
			cDia++
			textoCelda = "";
			textoCita = "";
			textoCita2 = "";
			textoCita3 = "";
		}
	}
	if (row > 1 || firstDay < 7)
	{
		codigo += '</tr>'
	}
}
codigo += '</table><div style="clear:both;height:10px;"></div></div>'
codigo += '<div style="!margin-top:-8px;width:462px;height:8px;background-image: url(/zaragoza2016/js/calendario/f_calendario_pie.gif);background-repeat: no-repeat;background-position: center;"></div></div>'
document.getElementById("boxcalendario").innerHTML=codigo;
//setTimeout('(function dale($) {$("#td :span").tooltip({offset: [0, 0],relative: "true",effect: "fade", opacity: 0.8,tip: ".tooltip" });})(jQuery);',500);
}

