//var firstDay=0;  //0-domingo   1-lunes etc
var juevess_d = 0;
var vierness_d = 0;
var juevess_m = 0;
var vierness_m = 0;
var p = null;

// constructor
function Calendar(div, month, year, aDay){
  
  this.div = div;

  if (firstDay==1){
    this.days =  dp_name_days; //new Array("Lu","Ma","Mi","Ju","Vi","Sa","Do");
    this.months =  new Array("Ene","Feb","Mar","Abr","May","Jun","Jul", "Ago","Sep","Oct","Nov","Dic");
  }else{
    this.days = new Array( "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
    this.months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  }

  // array of total days in each month
  this.totalDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  // object properties - month and year
  // correction for zero-based array index
  this.month = month-1;
  this.year = year;
  this.day = aDay;
  this.prevmonth="";
  this.nextmonth="";

  //var 
  p = pascua(year);

  // leap year correction
  if (this.year % 4 == 0) this.totalDays[1] = 29;

  // temporary variable - used later
  this.rowCount = 0;

  // object method
  this.display = display;
  this.getNextMonth = getNextMonth;
  this.getPrevMonth = getPrevMonth;
  //this.loadCalendar = loadCalendar;
  w=25;
  h=15;
  // automatically run method display() once object is initialized
  //this.display(aDay); //,w,h);
  $('#'+this.div).html(this.display());
  //setTimeout("document.getElementById(this.div).innerHTML=Calendar("+aDay+","+ month+", "+year +")",20);
 // function loadCalendar(this.month, this.year, this.day);
   addEvents(month);

}



function getNextMonth(){
  nyear =this.year;
  nmonth=this.month+1;
  if (nmonth==13){nmonth=1;  nyear=nyear+1;}
  this.month = nmonth;
  this.year = nyear;
  Calendar(this.div,nmonth,nyear,1);
}

function getPrevMonth(){
  pyear =this.year;
  pmonth=this.month-1;
  if (pmonth==0) {pmonth=12; pyear=pyear-1;}
  this.month = pmonth;
  this.year = pyear;
  Calendar(this.div,pmonth,pyear,1);
}
 

/*
  juevess_d = p[3][2][0];
  juevess_m = p[3][2][1];
  vierness_d = p[4][2][0];
  vierness_m = p[4][2][1];
*/

function holiday(month,day){
  //alert(juevess_d+'/'+juevess_m+' '+vierness_d+'/'+vierness_m);
   month++;
   return (
	        ( (month==12) && ( (day==25)||(day==31) ||(day== 6)||(day==8)  ) )  ||
	        ( (month== 1) && ( (day== 1)||(day== 6) ) )  || 
	        ( (month== p[3][2][1]) && ( (day== p[3][2][0])||(day== p[4][2][0]) ) )  || //semana santa
	        ( (month==10) && (  day==12             ) )  ||
	        ( (month== 8) && (  day==15             ) )  || 
	        ( (month== 5) && (  day==1              ) )  ||
	        ( (month==11) && (  day==1              ) )  
	      );
}


function day_title(month,day){
   month++;
   //if ((month==12)&&((day==25)||(day==31))) {return true;} else {return false;}
        if ((month== 5) && ( day== 1))  return "Día del trabajo";
   else if ((month== 1) && ( day== 1))  return "Año nuevo";
   else if ((month== 1) && ( day== 6))  return "Reyes magos";
   else if ((month== 8) && ( day==15))  return "Nuestra Señora de la Asunción";
   else if ((month==10) && ( day==12))  return "Fiesta Nacional de España";
   else if ((month==11) && ( day== 1))  return "Todos los Santos";
   else if ((month==12) && ( day== 6))  return "Día de la Constitución";
   else if ((month==12) && ( day== 8))  return "Inmaculada Concepción";
   else if ((month==12) && ( day==25))  return "Navidad";
   else if ((month==12) && ( day==28))  return "Santos Inocentes";
   else if ((month==12) && ( day==31))  return "Nochevieja";
   else if ((month==p[0][2][1]) && ( day==p[0][2][0]))  return 'Domingo de Pascua';
   else if ((month==p[1][2][1]) && ( day==p[1][2][0]))  return 'Miércolesde ceniza';
   else if ((month==p[2][2][1]) && ( day==p[2][2][0]))  return 'Domingo de Ramos';
   else if ((month==p[3][2][1]) && ( day==p[3][2][0]))  return 'Jueves Santo';  
   else if ((month==p[4][2][1]) && ( day==p[4][2][0]))  return 'Viernes Santo';
   /*
   else if ((month==p[5][2][1]) && ( day==p[5][2][0]))  return 'Ascensión';
   else if ((month==p[6][2][1]) && ( day==p[6][2][0]))  return 'Pentecostés';
   else if ((month==p[7][2][1]) && ( day==p[7][2][0]))  return 'Santísima Trinidad';
   else if ((month==p[8][2][1]) && ( day==p[8][2][0]))  return 'Corpus Christi';
   */
   else return ''; //festivo';

   /** USA 
        if ((month== 1) && ( day== 1))  return "New Year`s Day";
   else if ((month== 2) && ( day==14))  return "Valentine`s Day";
   else if ((month== 6) && ( day==14))  return "Flag Day";
   else if ((month== 7) && ( day== 4))  return "Independence Day";
   else if ((month==11) && ( day==11))  return "Veteran`s Day";
   else if ((month==12) && ( day==25))  return "Christmas";
   **/
 
}


// function to display calendar
function display()
{
  // create a Date object
  // required to obtain basic date information
  // get the first and last day of the month - boundary values for calendar
  obj = new Date(this.year, this.month, 1);
  this.firstDayOfMonth = obj.getDay()-firstDay;
  obj.setDate(31);
  this.lastDayOfMonth = obj.getDay();
  if (this.firstDayOfMonth==-1)this.firstDayOfMonth=6;

  prevy=this.year;
  prevm = this.month-1;
  if (prevm<0) {
   prevm=11;
   prevy = this.year-1;
  }
  prevd = this.totalDays[prevm];  
  if ((prevm==2) && (prevy % 4 == 0)) prevd = 29;
  //if (prevd==undefined) prevd='('+prevm+')';
  prevd = prevd - this.firstDayOfMonth;

  var str = '';

  today = new Date();
  t_day = today.getDate();
  t_month  = today.getMonth();
  t_year = today.getFullYear();
  //DEBUG document.write('Hoy es: '+t_day);

  current_month = (t_month==this.month);
  current_year = (t_year==this.year); 
  //is_sunday = (today.getDay()==6);
  //document.write(this.month+' - '+this.year+'< - >'+t_month+' - '+t_year);
  // start table
  //DEBUG:document.write("firstDayOfMonth: "+this.firstDayOfMonth);
  //DEBUG: document.write("aDay "+aDay);


  pyear =this.year;
  nyear =this.year;
  pmonth=this.month;
  nmonth=this.month+2;
  if (pmonth==0) {pmonth=12; pyear=pyear-1;}
  if (nmonth==13){nmonth=1;  nyear=nyear+1;}


  prevyear =' onclick="Calendar(\''+this.div+'\','+(this.month+1)+', '+(parseInt(this.year)-1)+', '+this.day+');"';
  this.prevmonth=' onclick="Calendar(\''+this.div+'\','+pmonth+', '+pyear+', '+this.day+');"';
  this.nextmonth=' onclick="Calendar(\''+this.div+'\','+nmonth+', '+nyear+', '+this.day+');"';
  nextyear =' onclick="Calendar(\''+this.div+'\','+(this.month+1)+', '+(parseInt(this.year)+1)+', '+this.day+');"';

  str += '<div class="jux_kalendar"><table>';
  
  // month display
  str += "<tr class='jux_calendar_nav'>";
  str += "<td class='jux_calendar_nav' "+prevyear+">&laquo;</td>";
  str += "<td class='jux_calendar_nav' "+this.prevmonth+">&laquo;</td>";
  str += "<td class='jux_calendar_date' colspan=3 align=center><nobr>" + this.months[this.month] + " " + this.year + "</nobr></td>";
  str += "<td class='jux_calendar_nav' "+this.nextmonth+">&raquo;</td>";
  str += "<td class='jux_calendar_nav' "+nextyear+">&raquo;</td>";
  str += "</tr>";

  // day names
  str += "<tr>";
  //xx=(firstDay==1)?0:1;
  xx=0;
  for (x=xx; x<7; x++){                         
    str += '<th scope="col" class="jux_calendar_dayname">' + this.days[x].substring(0,3) + '</th>';
  }
  str += "</tr>";

  //str += "<tr><td height=1 align=middle colspan=7><hr size=1></td></tr>";

  // start displaying dates
  // display blank spaces until the first day of the month
  str += "<tr>";


//for (x=1; x<=this.firstDayOfMonth; x++)  // comenzar semana en domingo
  for (x=1; x<=this.firstDayOfMonth; x++){
    // this comes in handy to find the end of each 7-day block
    this.rowCount++;
    prevd++;
    str += "<td class='jux_calendar_inactive' align='center'>"+prevd+"&nbsp;</td>";
  }


  // counter to track the current date
  this.dayCount=1;
  while (this.dayCount <= this.totalDays[this.month])
  {
    // use this to find out when the 7-day block is complete and display a new row
    if (this.rowCount % 7 == 0){
        //is_sunday=false;
      str += "</tr>\n<tr>";
    }

      // print date
    c_day = this.dayCount;
    
    if (firstDay==1)
      is_sunday=((this.rowCount==6)||(this.rowCount==13)||(this.rowCount==20)||(this.rowCount==27)||(this.rowCount==34));
    else
      is_sunday=((this.rowCount==0)||(this.rowCount==7)||(this.rowCount==14)||(this.rowCount==21)||(this.rowCount==28)||(this.rowCount==35));
    
    if (firstDay==1)
      is_saturday=((this.rowCount==5)||(this.rowCount==12)||(this.rowCount==19)||(this.rowCount==26)||(this.rowCount==33));
    else
      is_saturday=((this.rowCount==6)||(this.rowCount==13)||(this.rowCount==20)||(this.rowCount==27)||(this.rowCount==34));
    
    td_title = c_day+"/"+(this.month+1)+"/"+(this.year);
    
    //if (c_day==19)  
    //   td_class = 'jux_calendar_event';
    //else 
    
    if ((c_day==t_day)&&(current_month)&&(current_year))  
       td_class = 'jux_calendar_today';
    //else if ((c_day==this.day)&&(current_month)&&(current_year))  
    //    td_class = 'jux_calendar_selected';
    else if (holiday(this.month,this.dayCount)){
        td_class = 'jux_calendar_holiday';
        td_title = day_title(this.month,this.dayCount);
    }
    else if (is_sunday){ 
        td_class = 'jux_calendar_sunday';
        td_title = day_title(this.month,this.dayCount);
    }else if (is_saturday){ 
        td_class = 'jux_calendar_saturday';
        td_title = day_title(this.month,this.dayCount);
    }else{
        td_class = 'jux_calendar_day';
        td_title = day_title(this.month,this.dayCount);
    }


    
    //OKstr += '<td class="'+td_class+'" id="event_'+this.year+'_'+padN(this.month+1)+'_'+padN(c_day)+'"><a title="'+td_title+'">' + c_day + '</a></td>' ;
    str += '<td class="'+td_class+'" title="'+td_title+'" id="event_'+this.year+'_'+padN(this.month+1)+'_'+padN(c_day)+'">' + c_day + '</td>' ;

    this.dayCount++;
    this.rowCount++;
    
  }

   var nnn = this.rowCount % 7;
   var nnnn = 0;
   if (nnn>0){
     while (nnn<7){
       nnn++;
       nnnn++;
       str += "<td class='jux_calendar_inactive' align='center'>"+nnnn+"</td>";
       this.rowCount++;
     }
   }

  while(this.rowCount<42){
    str += "<tr>";
    xx=0;
    for (x=xx; x<7; x++){       
      nnnn++;                  
      str += "<td class='jux_calendar_inactive' align='center'>"+nnnn+"</td>";
      this.rowCount++;
    }
    str += "</tr>";
  }

  
    // day names
  str += "<tr>";
  xx=0;
  for (x=xx; x<7; x++){                         
    str += '<th scope="col" class="jux_calendar_dayname">' + this.days[x].substring(0,3) + '</th>';
  }
  str += "</tr>";

  // end table
  str += "</tr>";
  //str+='<tr><td colspan=7><nobr><a onClick="javascript:updateInput(\"'+t_day+'/'+t_month+1)+'/'+t_year)'+"\");">Hoy es: '+t_day+'/'+t_month+'/'+t_year+'</a></nobr></td></tr>';
  str+='<tr><td colspan=7 class="jux_calendar_today_is" onclick="Calendar(\''+this.div+'\','+(t_month+1)+','+t_year+','+t_day+');">'+dp_str_today+': '+t_day+'/'+(t_month+1)+'/'+t_year+'</td></tr>';
  str += "</table></div>";

  //alert(str);
  //document.write(str);
  
  return str;
}

function addEvents(month){
  for (i = 0; i < aEvents.length; ++i)	{
    if(month==aEvents[i][1]){
      
      eId = '#event_'+aEvents[i][0]+'_'+padN(aEvents[i][1])+'_'+padN(aEvents[i][2]);

      $(eId).removeClass()
            .addClass('jux_calendar_event') 
            .attr( 'alt'  , aEvents[i][4] )
            .attr( 'title', aEvents[i][3] )
            .bind( 'click', function() { location.href = $(this).attr('alt'); } );
      
    }
  }
}


