CALENDAR = function() {
//========================================================================================================
// Configuration
//========================================================================================================

  this.onClick = null;                   // function to be called when day is clicked

//--------------------------------------------------------------------------------------------------------
// You should change these variables only if you want to translate them into your language:
//--------------------------------------------------------------------------------------------------------
  this.weekdays = ['L', 'M', 'X', 'J', 'V', 'S', 'D'];

  // months: must start with January
  this.months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
                 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
  // error messages
  this.error = ['El año ha de estar entre 1 - 3999!', 'El mes ha de estar entre 1 - 12!'];

  this.mDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  this.allDaysLink = false;

}

//========================================================================================================
// Functions
//========================================================================================================

//-----------------------

CALENDAR.prototype.leap_year = function(year) {
  return ((year % 4 == 0) && (year % 100 || !(year % 400))) ? true : false;
}

//-----------------------

CALENDAR.prototype.get_weekday = function(year, month, day) {
	var D = new Date(year, month-1, day);
	return (D.getDay() + 6) % 7;
}

//-----------------------

CALENDAR.prototype.table_head = function(year, month) {
  var html, ind, wDay, i;
  var cols = 7;

  html = '<div class="cssButtonPrevYear" id="ButtonPrevYear" title="Año anterior" onMouseOver="this.className=\'cssButtonPrevYear cssButtonOver \'" onMouseOut="this.className=\'cssButtonPrevYear\'">&nbsp;&lt;&nbsp;</div>' + 
     '<div class="cssYear">' + year + '</div>' + 
     '<div class="cssButtonNextYear" id="ButtonNextYear" title="Año siguiente" onMouseOver="this.className=\'cssButtonNextYear cssButtonOver \'" onMouseOut="this.className=\'cssButtonNextYear\'">&nbsp;&gt;&nbsp;</div>';

	html += '<div class="cssButtonPrevMonth" id="ButtonPrevMonth" title="Mes anterior" onMouseOver="this.className=\'cssButtonPrevMonth cssButtonOver \'" onMouseOut="this.className=\'cssButtonPrevMonth\'">&nbsp;&lt;&nbsp;</div>' + 
		    '<div class="cssMonth">' + month + '</div>' + 
		    '<div class="cssButtonNextMonth" id="ButtonNextMonth" title="Mes siguiente" onMouseOver="this.className=\'cssButtonNextMonth cssButtonOver \'" onMouseOut="this.className=\'cssButtonNextMonth\'" onClick="">&nbsp;&gt;&nbsp;</div>';

  for(i = 0; i < this.weekdays.length; i++) {
    wDay = this.weekdays[i];
    html += this.table_cell(wDay, 'cssDaysNames');
  }

  return html;
}

//-----------------------

CALENDAR.prototype.table_cell = function(content, cls, date, style) {
  var clsName = cls.toLowerCase();
  if(this.specDays[content] || (this.allDaysLink && (cls != 'cssDaysNames'))) {
    cls += ' clsEvent';
  }
  var html = '<div class="' + cls + '"';

  if(content != '&nbsp;' && clsName.indexOf('day') != -1) {
     if(this.specDays[content] || (this.allDaysLink && (cls != 'cssDaysNames'))) {
       cls += ' clsEvent';

   if (this.onClick) {
         html += ' onMouseOver="this.className=\'' + cls + ' cssHilight\'"';
         html += ' onMouseOut="this.className=\'' + cls + '\'"';
         html += ' onClick="' + this.onClick + '(\'' + date + '\')"';
       }
     }
  }
  if(style) html += ' style="' + style + '"';
  html += '>' + content + '</div>';

  return html;
}

//-----------------------

CALENDAR.prototype.headMouseUp = function() {
	var element = this.parentNode.parentNode;
	var calendar = element.calendar;

	if (this.id == "ButtonPrevYear") {
	  calendar.show(calendar.year - 1, calendar.month, element);
	}
	else if (this.id == "ButtonNextYear") {
	  calendar.show(calendar.year + 1, calendar.month, element);
	}
	else if (this.id == "ButtonPrevMonth") {
	  if (calendar.month > 1) {
		  calendar.show(calendar.year, calendar.month - 1, element);
	  }
	  else {
		  calendar.show(calendar.year - 1, 12, element);
	  }
	}
	else if (this.id == "ButtonNextMonth") {
	  if (calendar.month < 12) {
		  calendar.show(calendar.year, calendar.month + 1, element);
	  }
	  else {
		  calendar.show(calendar.year + 1, 1, element);
	  }
	}
}

//-----------------------

CALENDAR.prototype.registerHeadEvents = function() {
  el = document.getElementById("ButtonPrevYear");
  el.onclick = this.headMouseUp;
  el = document.getElementById("ButtonNextYear");
  el.onclick = this.headMouseUp;
  el = document.getElementById("ButtonPrevMonth");
  el.onclick = this.headMouseUp;
  el = document.getElementById("ButtonNextMonth");
  el.onclick = this.headMouseUp;
}

//-----------------------

CALENDAR.prototype.show = function(year, month, element) {
  element.calendar = this;

  obj = new Date();
  curYear = obj.getFullYear();
  curMonth = obj.getMonth() + 1;
  curDay = obj.getDate();

  if(year == null && month == null) {
     var obj = new Date();
     year = obj.getFullYear();
     month = obj.getMonth() + 1;
  }
  else if(year != null && month == null) month = 1;

  this.year = year;
	this.month = month;
	this.element = element;

  this.refresh();
}

//-----------------------

CALENDAR.prototype.refresh = function() {
  var obj, html, curYear, curMonth, curDay, start, stop, title, daycount,
      inThisMonth, wdays, days, ind, cls, style, content, date, i;

  this.specDays = {};
	if ((LB != null) && (LB[this.year] != null) && (LB[this.year][this.month] != null)) {
		var bocs = LB[this.year][this.month];
		for (var day in bocs){
			this.specDays[day] = true;
		}
	}

	var html = '';
  if(this.year < 1 || this.year > 3999) html = '<b>' + this.error[0] + '</b>';
  else if(this.month < 1 || this.month > 12) html = '<b>' + this.error[1] + '</b>';
  else {
    if(this.leap_year(this.year)) this.mDays[1] = 29; else this.mDays[1] = 28;

    start = this.get_weekday(this.year, this.month, 1);
    stop = this.mDays[this.month-1];

    html =  '<div class="calendarContent">';
    html += this.table_head(this.year, this.months[this.month-1]);
    daycount = 1;

    if((this.year == curYear) && (this.month == curMonth)) inThisMonth = true;
    else inThisMonth = false;

    while(daycount <= stop) {
      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.year + '-' + this.month + '-' + daycount;

        if((daycount == 1 && i < start) || daycount > stop) content = '&nbsp;';
        else {
          content = daycount;
          if(inThisMonth && daycount == curDay) {
            cls += ' cssCurrentDay';
          }
          else if(this.year == 1582 && this.month == 10 && daycount == 4) daycount = 14;
          daycount++;
          wdays++;
        }
        html += this.table_cell(content, cls, date, style);
      }
    }
    html += '</div><div class="clearfix"></div>';
  }
  this.element.innerHTML = html;
	this.registerHeadEvents();

  if (!this.overlay) 
  	this.overlay = document.getElementById('overlay');

  if (this.overlay) {
    this.overlay.style.top = this.element.offsetTop;
    this.overlay.style.left = this.element.offsetLeft;
    this.overlay.style.width = this.element.offsetWidth; 
    this.overlay.style.height = this.element.offsetHeight;
    this.overlay.style.visibility = 'visible';
	}

	window.calendarPopup = this;
	if (this.popup)	{
	  this.onmousedownsave = document.onmousedown;
	  this.onkeypresssave = document.onkeypress;

	  document.onmousedown = atDocumentKeydown;
	  document.onkeypress = atDocumentKeydown;
	}
}

//-----------------------

CALENDAR.prototype.hide = function() {
  var calendar = window.calendarPopup;
  if (!calendar) return;

  if (calendar.overlay) calendar.overlay.style.visibility = 'hidden';

  calendar.element.innerHTML = '';
	calendar.element.style.display = "none";
	window.calendarPopup = null;	

	document.onmousedown = this.onmousedownsave;
	document.onkeypress = this.onkeypresssave;
}

//-----------------------

atDocumentKeydown = function(event) {
	event || (event = window.event);
	var targ;
	if (event.target) targ = event.target
	else if (event.srcElement) targ = event.srcElement
	if (targ.nodeType == 3) { // defeat Safari bug
		targ = targ.parentNode;
	}

    var calendar = window.calendarPopup;
    if (!calendar) return;

	for (; targ != null && targ != calendar.element; targ = targ.parentNode);
	if (targ == null) {
		calendar.hide();
	}
}

//-----------------------

