// VARIABILI CALENDARIO
var now = new Date;
var ccm = now.getMonth();
var ccy = now.getFullYear();
var mn = new Array('gen','feb','mar','apr','mag','giu','lug','ago','set','ott','nov','dic');
var mnn = new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var mnl = new Array('31','29','31','30','31','30','31','31','30','31','30','31');

// STRUTTURA CALENDARIO
var calCodice;
calCodice = "<table class='tableCalendar' cellpadding='2'><tr>";
//calCodice += "<td colspan='2' style='text-align:left;'><img src='img/prevCalendar.gif' alt='Mese precedente' onclick='csubm()' style='cursor:pointer'/></td>";
//calCodice += "<td colspan='3' id='mns'></td>";
//calCodice += "<td colspan='2' style='text-align:right;'><img src='img/nextCalendar.gif' alt='Mese successivo' onclick='caddm()' style='cursor:pointer'></td></tr>";
calCodice += "<tr class='headTable'><td>Dom</td><td>Lun</td><td>Mar</td><td>Mer</td><td>Gio</td><td>Ven</td><td>Sab</td></tr>";
	for(var kk=1;kk<=6;kk++) {
	calCodice += "<tr>";
		for(var tt=1;tt<=7;tt++) {
			num=7 * (kk-1) - (-tt);
			calCodice += "<td id='v" + num + "' class='giorno' contenuto=''>&nbsp;</td>";
		}
	calCodice += "</tr>";
	}
calCodice += "</table>";

// MESE SUCCESSIVO
function caddm() {
	ccm+=1;
		if (ccm>=12) {
			ccm=0;
			ccy++;
		}
	buildCalendar('',ccm,ccy);
}

// MESE PRECEDENTE
function csubm() {
	ccm-=1;
		if (ccm<0) {
			ccm=11;
			ccy--;
		}
	buildCalendar('',ccm,ccy);
}

// COSTRUISCI CALENDARIO
function buildCalendar(hd,cm,cy) {
	
	$('#calendario').html(calCodice);
	
	// OGGI
	now = new Date();
	var nowD = now.getDate();
	var nowM = now.getMonth();
	var nowY = now.getFullYear();
	
	$("#calendario").prepend("<div style='float:left;margin-right:15px;'><div class='mese'>" + mn[nowM] + "</div><div class='giorno'>" + nowD + "</div></div>");
	
	td = new Date();
	td.setDate(1);
	td.setFullYear(cy);
	td.setMonth(cm);
	cd=td.getDay();
	
	// MESE ANNO
	$('#mns').html(mn[cm]+ "&nbsp;" + cy);
	
	// Controllo anno bisestile
	marr=((cy%4)==0)?mnl:mnn;
	
	// popolo mese
	for(var d=1;d<=42;d++) {
		if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
			$('#v'+parseInt(d)).html(d-cd);	
			//Controllo se è oggi
			if(cy==nowY && cm==nowM && (d-cd)==nowD){
				$('#v'+parseInt(d)).addClass("oggi");
			}
		}else{
			$('#v'+d).html('&nbsp;');
		}
	}
	
	var eventDay;
	// Carico eventi del mese 
	//alert("carico"); 
    $.ajax({
        url: "cercaEventi.aspx?anno=" + cy + "&mese=" + (Number(cm)+1),
        type: 'GET',
        async: false,
        dataType: 'xml',
        success: function(data) {
            $(data).find("notiziario").each(function() {
                
                idEvent = $(this).attr("id")
                       
                $(this).find("datainizio").each(function() {
                       dayEvent = $(this).find("d").text();
                });
                
			    for(var dd=1;dd<=42;dd++) {
				    if($('#v'+dd).html()==dayEvent){
					    // Evidenzio
					    $('#v'+dd).addClass("giornoEvento");
					    // Creo il contenuto della tooltip
					    $('#v'+dd).attr("contenuto",$('#v'+dd).attr("contenuto") + $(this).find("titolo").text() + "<br/>");
					    // Creo tooltip
					    $('#v'+dd).hover(
					      function (e) {
						    $("body").append("<p id='tooltip'>"+ $(this).attr("contenuto") +"</p>");
						    $("#tooltip")
							    .css("top",(e.pageY - $("#tooltip").height() - 30) + "px")
							    .css("left",(e.pageX - ($("#tooltip").width()/2)) + "px")
							    .fadeIn("fast");
					      }, 
					      function () {
						    $("#tooltip").remove();
					      }
					    );
					    $('#v'+dd).mousemove(function(e){
						    $("#tooltip")
							    .css("top",(e.pageY - $("#tooltip").height() - 30) + "px")
							    .css("left",(e.pageX - ($("#tooltip").width()/2)) + "px");
					    });	
					    $('#v'+dd).click(function(){
					        // Passo giorno, mese, anno
						    location.href="dettEditoriale.aspx?idDet=" + idEvent
					    });	
				    }
			    }
                
                
            });
        }
    });
}
