var mi_fecha=new Date();
var mes=mi_fecha.getMonth();
var anyo=mi_fecha.getFullYear(); 
var eventos;
var fechadelevento="";
var marcaEventos = function($td, thisDate, month, year)
	{
		if (eventos['d' + thisDate.getTime()]) {
		  var arr_evdatos=eventos['d' + thisDate.getTime()].split('|');
			var id_evento = arr_evdatos[0];
			$td.bind(
				'click',
				function()
				{
					muestraEventos(id_evento);
				}
			).addClass('evento_'+arr_evdatos[1].replace(" ", "_"));
		}
	}

function muestraEventos(fecha) {
	 var arrdir=document.location.href.split('/');
     var lugar=(arrdir[arrdir.length-2].length==2) ? arrdir[arrdir.length-2] : arrdir[arrdir.length-3];
	 if(lugar!=""){
	 	location.href="/"+lugar+"/agenda/?f="+fecha;
	 }else{
		location.href="/agenda/?f="+fecha;
	 }
}

function calendario(snum) {
	 $.ajax({
        type: "POST",
        url: "/includes/eventos.php",
        data: "mes=" + mes +"&anyo="+anyo,
        success: function(datos){
        eventos = {};
        if (datos.length>0) {
        arrfechas=datos.split(':');
        lafechahoy=new Date();
        lafechahoy_str="";
        for (i=0;i<arrfechas.length;i++) {
           var arrfecha_prev=arrfechas[i].split('|');
           arrfecha=arrfecha_prev[0].split('-');
           dia_=parseInt(arrfecha[2],10);
           mes_=parseInt(arrfecha[1],10)-1;
           anyo_=parseInt(arrfecha[0],10);
           var eventofecha=new Date(anyo_, mes_, dia_);
           if ((dia_==lafechahoy.getDate()) && (mes_==lafechahoy.getMonth()) && (anyo_==lafechahoy.getFullYear()))
               lafechahoy_str=arrfecha_prev[0];
           eventos['d' + eventofecha.getTime()] = arrfechas[i];
	      }
	      }
	      $('#mesactual').html(Date.monthNames[mes]); //
        $('#anyoactual').html(anyo);
        $('#date-view1').renderCalendar({month:mes, year:anyo, renderCallback:marcaEventos, showHeader:$.dpConst.SHOW_HEADER_SHORT});
        cargaListadoEventos(mes,anyo,snum);
       }
  });
}
  
$(function()
    {
    $('#mesanterior').bind('click',function()	{
			if (--mes < 0) {
			  mes=11; anyo--;
      }
      calendario(0);
		});
    $('#mesposterior').bind('click',function()	{
			if (++mes > 11) {
			  mes=0; anyo++;
      }
      calendario(0);
		});
		
    calendario(1);
 });

