/*
 +-------------------------------------------------------------------+
 |                 H T M L - C A L E N D A R   (v2.7)                |
 |                                                                   |
 | Copyright Gerd Tentler               www.gerd-tentler.de/tools    |
 | Created: May 27, 2003                Last modified: Nov. 18, 2007 |
 +-------------------------------------------------------------------+
 | This program may be used and hosted free of charge by anyone for  |
 | personal purpose as long as this copyright notice remains intact. |
 |                                                                   |
 | Obtain permission before selling the code for this program or     |
 | hosting this software on a commercial website or redistributing   |
 | this software over the Internet or in any other medium. In all    |
 | cases copyright must remain intact.                               |
 +-------------------------------------------------------------------+

 EXAMPLE #1:  myCal = new CALENDAR();
              document.write(myCal.create());

 EXAMPLE #2:  myCal = new CALENDAR(2004, 12);
              document.write(myCal.create());

 EXAMPLE #3:  myCal = new CALENDAR();
              myCal.year = 2004;
              myCal.month = 12;
              document.write(myCal.create());

 Returns HTML code
==========================================================================================================
*/
var cal_ID = 0;

function CALENDAR(ano_anterior, ano, ano_seguinte, mes_anterior, mes, mes_seguinte, dia_calendario, url) {
//========================================================================================================
// Configuration
//========================================================================================================
// mes
  this.tFontFace = 'Arial, Helvetica';
  this.tFontSize = 11;
  this.tFontWeight = 'bold';
  this.tFontColor = '#000000';
  this.tTextTransform = 'uppercase';
  this.tBGColor = '#DCECFB';
  this.tPadding = '2px 0px 2px 0px';
// dia de la semana
  this.hFontFace = 'Arial, Helvetica';
  this.hFontSize = 10;
  this.hFontColor = '#000000';
  this.hBGColor = '#EEF5FD';
  this.hPadding = '2px 0px 1px 0px';
  this.hWidth = 23;
// dia
  this.dFontFace = 'Arial, Helvetica';
  this.dFontSize = 10;
  this.dFontColor = '#999';
  this.dBGColor = '#FAFCFE';
  this.dPadding = '2px 0px 2px 0px';
// numero da semana
  this.wFontFace = 'Arial, Helvetica';
  this.wFontSize = 8;
  this.wFontColor = '#FFFFFF';
  this.wBGColor = '#00FF00';
// sabados
  this.saFontColor = '#999'; 
  this.saBGColor = '#FCEEE0';
// domingos
  this.suFontColor = '#999';
  this.suBGColor = '#FBE4CE';
// hoxe
  this.tdBorderColor = '#E9821B';

  this.borderColor = '#C0DDF8';
  
  this.hilightTxtColor = '#FFFFFF';
  this.hilightBgColor = '#E9821B';

  this.link = '';
  this.offset = 2;
  this.weekNumbers = false;

//--------------------------------------------------------------------------------------------------------
// You should change these variables only if you want to translate them into your language:
//--------------------------------------------------------------------------------------------------------
  // weekdays: must start with Saturday because January 1st of year 1 was a Saturday
  this.weekdays = ['S', 'D', 'L', 'M', 'Me', 'X', 'V'];

  // months: must start with January
  this.months = ['Xaneiro', 'Febreiro', 'Marzo', 'Abril', 'Maio', 'Xu&ntilde;o', 'Xullo',
                 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Decembro'];
  // error messages
  this.error = ['Year must be 1 - 3999!', 'Month must be 1 - 12!'];

//--------------------------------------------------------------------------------------------------------
// Don't change from here:
//--------------------------------------------------------------------------------------------------------
  this.size = 0;
  this.mDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  if(ano == null && mes == null) {
    var obj = new Date();
    ano = obj.getYear();
    if(ano < 1900) ano += 1900;
    mes = obj.getMonth() + 1;
  }
  else if(ano != null && mes == null) mes = 1;
  this.ano = ano;
  this.mes = mes;
  this.specDays = {};

//========================================================================================================
// Functions
//========================================================================================================
  this.set_styles = function() {
    cal_ID++;
    var html = '<style> .cssTitle' + cal_ID + ' { ';
    if(this.tFontFace) html += 'font-family: ' + this.tFontFace + '; ';
    if(this.tFontSize) html += 'font-size: ' + this.tFontSize + 'px; ';
	if(this.tFontWeight) html += 'font-weight: ' + this.tFontWeight + '; ';
	if(this.tTextTransform) html += 'text-transform: ' + this.tTextTransform + '; ';
    if(this.tFontColor) html += 'color: ' + this.tFontColor + '; ';
    if(this.tBGColor) html += 'background-color: ' + this.tBGColor + '; ';
	if(this.tPadding) html += 'padding: ' + this.tPadding + '; ';	
    html += '} .cssHeading' + cal_ID + ' { ';
    if(this.hFontFace) html += 'font-family: ' + this.hFontFace + '; ';
    if(this.hFontSize) html += 'font-size: ' + this.hFontSize + 'px; ';
    if(this.hFontColor) html += 'color: ' + this.hFontColor + '; ';
    if(this.hBGColor) html += 'background-color: ' + this.hBGColor + '; ';
	if(this.hPadding) html += 'padding: ' + this.hPadding + '; ';
	if(this.hWidth) html += 'width: ' + this.hWidth + 'px; ';	
    html += '} .cssDays' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
    if(this.dFontColor) html += 'color: ' + this.dFontColor + '; ';
    if(this.dBGColor) html += 'background-color: ' + this.dBGColor + '; ';
	if(this.dPadding) html += 'padding: ' + this.dPadding + '; ';
    html += '} .cssWeeks' + cal_ID + ' { ';
    if(this.wFontFace) html += 'font-family: ' + this.wFontFace + '; ';
    if(this.wFontSize) html += 'font-size: ' + this.wFontSize + 'px; ';
    if(this.wFontColor) html += 'color: ' + this.wFontColor + '; ';
    if(this.wBGColor) html += 'background-color: ' + this.wBGColor + '; ';
    html += '} .cssSaturdays' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
    if(this.saFontColor) html += 'color: ' + this.saFontColor + '; ';
    if(this.saBGColor) html += 'background-color: ' + this.saBGColor + '; ';
    html += '} .cssSundays' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
    if(this.suFontColor) html += 'color: ' + this.suFontColor + '; ';
    if(this.suBGColor) html += 'background-color: ' + this.suBGColor + '; ';
    html += '} .cssHilight' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
	if(this.dPadding) html += 'padding: ' + this.dPadding + '; ';
    if(this.hilightTxtColor) html += 'color: ' + this.hilightTxtColor + '; ';
    if(this.hilightBgColor) html += 'background-color: ' + this.hilightBgColor + '; ';
    html += 'cursor: pointer; ';
    html += '} </style>';

    return html;
  }

  this.leap_year = function(ano) {
    return (!(ano % 4) && (ano < 1582 || ano % 100 || !(ano % 400))) ? true : false;
  }

  this.get_weekday = function(ano, days) {
    var a = days;
    if(ano) a += (ano - 1) * 365;
    for(var i = 1; i < ano; i++) if(this.leap_year(i)) a++;
    if(ano > 1582 || (ano == 1582 && days >= 277)) a -= 10;
    if(a) a = (a - this.offset) % 7;
    else if(this.offset) a += 7 - this.offset;

    return a;
  }

  this.get_week = function(ano, days) {
    var firstWDay = this.get_weekday(ano, 0);
    return Math.floor((days + firstWDay) / 7) + (firstWDay <= 3);
  }

  this.table_cell = function(content, cls, date, style) {
    var size = Math.round(this.size * 1.5);
    var clsName = cls.toLowerCase();
    var html = '<td align=center width=' + size + ' class="' + cls + '"';

    if(content != '&nbsp;' && clsName.indexOf('day') != -1) {
      if(this.specDays[content]) {
        if(this.specDays[content][0]) {
          style += 'background-color:' + this.specDays[content][0] + ';';
        }
        if(this.specDays[content][1]) {
          html += ' title="' + this.specDays[content][1] + '"';
        }
		if(this.link) {
        html += ' onMouseOver="this.className=\'cssHilight' + cal_ID + '\'"';
        html += ' onMouseOut="this.className=\'' + cls + '\'"';
        html += ' onClick="document.location.href=\'' + this.link + '&click=1&date=' + date + '&ano=' + ano + '&mes=' + mes+ '\'"';
      	}
      }

    }
    if(style) html += ' style="' + style + '"';
    html += '>' + content + '</td>';

    return html;
  }

  this.table_head = function(content) {
    var html, ind, wDay, i;
    var cols = this.weekNumbers ? 8 : 7;

    html = '<tr><td colspan=' + cols + ' class="cssTitle' + cal_ID + '"><div class="calendario_mes_prev"><a href="'+ url +'&mes=' + mes_anterior + '&ano=' + ano_anterior + '"><img src="imaxes/icono_prev.gif" /></a></div><div class="calendario_mes">' + content + '</div><div class="calendario_mes_next"><a href="'+ url +'&mes=' + mes_seguinte + '&ano=' + ano_seguinte + '&dia_calendario=' + dia_calendario + '"><img src="imaxes/icono_next.gif" /></a></div></td></tr><tr>';
    for(i = 0; i < this.weekdays.length; i++) {
      ind = (i + this.offset) % 7;
      wDay = this.weekdays[ind];
      html += this.table_cell(wDay, 'cssHeading' + cal_ID);
    }
    if(this.weekNumbers) html += this.table_cell('&nbsp;', 'cssHeading' + cal_ID);
    html += '</tr>';

    return html;
  }

  this.viewEvent = function(from, to, color, title) {
    if(from > to) return;
    if(from < 1 || from > 31) return;
    if(to < 1 || to > 31) return;

    while(from <= to) {
      this.specDays[from] = [color, title];
      from++;
    }
  }

  this.create = function() {
    var obj, html, curYear, curMonth, curDay, start, stop, title, daycount,
        inThisMonth, weekNr, wdays, days, ind, cls, style, content, date, i;

    this.size = (this.hFontSize > this.dFontSize) ? this.hFontSize : this.dFontSize;
    if(this.wFontSize > this.size) this.size = this.wFontSize;

    obj = new Date();
    curYear = obj.getYear();
    if(curYear < 1900) curYear += 1900;
    curMonth = obj.getMonth() + 1;
    curDay = obj.getDate();

    if(this.ano < 1 || this.ano > 3999) html = '<b>' + this.error[0] + '</b>';
    else if(this.mes < 1 || this.mes > 12) html = '<b>' + this.error[1] + '</b>';
    else {
      if(this.leap_year(this.ano)) this.mDays[1] = 29;
      for(i = days = 0; i < this.mes - 1; i++) days += this.mDays[i];

      start = this.get_weekday(this.ano, days);
      stop = this.mDays[this.mes-1];

      html = this.set_styles();
      html += '<table cellspacing=0 cellpadding=0 class="table_elemento"><tr>';
      html += '<td' + (this.borderColor ? ' bgcolor=' + this.borderColor  : '') + '>';
      html += '<table width=166 border=0 cellspacing=1 cellpadding=1 >';
      title = this.months[this.mes-1] + ' ' + this.ano;
      html += this.table_head(title);
      daycount = 1;

      if((this.ano == curYear) && (this.mes == curMonth)) inThisMonth = true;
      else inThisMonth = false;

      if(this.weekNumbers) weekNr = this.get_week(this.ano, days);

      while(daycount <= stop) {
        html += '<tr>';

        for(i = wdays = 0; i <= 6; i++) {
          ind = (i + this.offset) % 7;
          if(ind == 0) cls = 'cssSaturdays';
          else if(ind == 1) cls = 'cssSundays';
          else cls = 'cssDays';

          style = '';
          date = this.ano + '-' + this.mes + '-' + daycount;

          if((daycount == 1 && i < start) || daycount > stop) content = '&nbsp;';
          else {
            content = daycount;
            if(inThisMonth && daycount == curDay) {
              style = 'padding:0px;border:1px solid #00FFFF;background-color:#FFF;color:#000;';
            }
            else if(this.ano == 1582 && this.month == 10 && daycount == 4) daycount = 14;
            daycount++;
            wdays++;
          }
          html += this.table_cell(content, cls + cal_ID, date, style);
        }

        if(this.weekNumbers) {
          if(!weekNr) {
            if(this.ano == 1) content = '&nbsp;';
            else if(this.ano == 1583) content = 52;
            else content = this.get_week(this.ano - 1, 365);
          }
          else if(this.mes == 12 && weekNr >= 52 && wdays < 4) content = 1;
          else content = weekNr;

          html += this.table_cell(content, 'cssWeeks' + cal_ID);
          weekNr++;
        }
        html += '</tr>';
      }
      html += '</table></td></tr></table>';
    }
    return html;
  }
}
