function Calendar(holder, year, month, day)
{
  this.holder = document.getElementById(holder);

  if (this.holder)
  {
    this.holderCalStr = "document.getElementById('"+ holder +"').calendar";
    this.imgPath = "";
    
    this.cssClassDaySelected = "daySelected";
    this.cssClassWeek = "dayWeek";
    this.cssClassWeekend = "dayWeekend";
  
    this.inputDay = null;
    this.inputMonth = null;
    this.inputYear = null;
    this.dateSelected = null;
  
    this.cols = 7;

    this.setDate(year, month, day);

    this.holder.calendar = this;
  }
}

Calendar.prototype.setup = function()
{
  this.daysInMonth = this.getDaysInMonth(this.year, this.month);
  this.firstCol = this.getColumn(this.year, this.month, 1);
  this.rows = this.getRows();
}

Calendar.prototype.setDate = function(year, month, day)
{
  var date = (year || month || day)
    ? new Date(year, month - 1, day)
    : new Date();

  this.day = date.getDate();
  this.month = date.getMonth();
  this.year = this.correctYear( date.getYear() );

  this.selectDate(this.year, this.month, this.day);
}

Calendar.prototype.setMonth = function(year, month)
{
  var date = (year || month)
    ? new Date(year, month - 1, 1)
    : new Date();

  this.day = date.getDate();
  this.month = date.getMonth();
  this.year = this.correctYear( date.getYear() );
}

Calendar.prototype.setImgPath = function(path)
{
  this.imgPath = path;
}

Calendar.prototype.correctYear = function(year)
{
  if (year < 200) // Kvuli prohlizeci FF
    year += 1900;
  return year
}

Calendar.prototype.setInputs = function(inpYear, inpMonth, inpDay, fillInputs)
{
  this.inputDay   = document.getElementById(inpDay);
  this.inputMonth = document.getElementById(inpMonth);
  this.inputYear  = document.getElementById(inpYear);

  var d = this.dateSelected.getDate();
  var m = this.dateSelected.getMonth();
  var y = this.correctYear (this.dateSelected.getYear() );

  if (arguments.length == 3 || fillInputs == true)
    this.fillInputs("" + y, "" + (m + 1), "" + d);
}

Calendar.prototype.getMesic = function(mesic)
{
  if (mesic >= 0 && mesic <= 11) {
    var mesice = new Array("Leden","Únor","Březen","Duben","Květen","Červen","Červenec","Srpen","Září","Říjen","Listopad","Prosinec");
    return mesice[ mesic ];
  }
  return "";
}

Calendar.prototype.getDen = function(den)
{
  if (den >= 0 && den <= 6) {
    var dny = new Array("Po","Út","St","Čt","Pá","So","Ne");
    return dny[ den ];
  }
  return "";
}

Calendar.prototype.getDaysInMonth = function(year, month)
{
  if (month >= 0 && month <= 11) {
    var denPosl  = new Date(year, month + 1, 0);
    return denPosl.getDate();
  }
  return 0;
}

Calendar.prototype.getColumn = function(year, month, day)
{
  var date = new Date(year, month, day);
  var d = date.getDay() - 1;
  return (d >= 0) ? d : 6;
}

Calendar.prototype.getRows = function()
{
  var countInFirstRow = this.cols - this.firstCol; 
  return 1 + Math.ceil((this.daysInMonth - countInFirstRow) / this.cols);
}

Calendar.prototype.create = function()
{
  this.setup();

  var day = 1;
  var html = "";

  var y = this.year;
  var m = this.month;

  html += '<div class="calendar" id="">';

  html += '<div class="calendarMonthSwitcher">';
  html +=   '<div class="calendarMonthName">'+ this.getMesic(this.month) +'&nbsp;'+ y +'</div>';
  html +=   '<a href="#" onclick="javascript:'+ this.holderCalStr +'.next();return false;" id="calendarNext"><img alt="Další měsíc" src="'+ this.imgPath +'images/kalendar/sipkaVpravo.gif" border="0" /></a>'; 
  html +=   '<a href="#" onclick="javascript:'+ this.holderCalStr +'.prev();return false;" id="calendarPrev"><img alt="Předchozí měsíc" src="'+ this.imgPath +'images/kalendar/sipkaVlevo.gif" border="0" /></a>';
  html += '</div>';

  html += '<table border="0" cellpadding="0" cellspacing="0" class="calendar">';
  html += '<tr>';
  for (var j = 0; j < this.cols; j++) {
    html += '<th><div class="">' + this.getDen(j) + '</div></th>';
  }
  html += '</tr>';
  
  for (var i = 0; i < this.rows; i++)
  {
    html += '<tr>';
    for (var j = 0; j < this.cols; j++)
    {
      if ((i == 0 && j < this.firstCol) || (day > this.daysInMonth))
        html += '<td></td>';
      else
      {
        var d = day++;
        var cssClass = (j < 5) ? this.cssClassWeek : this.cssClassWeekend;
        
        if ( this.dateSelected ) {
          if (this.dateSelected.getDate() == d && 
              this.dateSelected.getMonth() == m && 
              this.correctYear( this.dateSelected.getYear() ) == y) {
            cssClass = this.cssClassDaySelected;
          }
        }
        html += '<td><div class="'+ cssClass +'" onclick="'+ this.holderCalStr +'.selectDate('+ y +','+ m +','+ d +');'+ this.holderCalStr +'.close();" id="'+ this.getDateId(y,m,d) +'">'+ d +'</div></td>';
      }
    }
    html += '</tr>';
  }
  html += '</table>';
  html += '<div id="calendarCloser"><a href="#" onclick="'+ this.holderCalStr +'.close();return false;">Zavřít</a></div>';
  html += '</div>';

  this.holder.innerHTML = html; 
}

Calendar.prototype.getDateId = function(y, m, d)
{
  return this.holder.id + '_' + y + '-' + m + '-' + d;
}

Calendar.prototype.selectDate = function(y, m, d)
{
  if ( this.dateSelected )
  {
    var prevD = this.dateSelected.getDate();
    var prevM = this.dateSelected.getMonth();
    var prevY = this.correctYear (this.dateSelected.getYear() );
    var prevDaySelElem = document.getElementById( this.getDateId(prevY, prevM, prevD) );

    if ( prevDaySelElem )
    {
      var day = this.dateSelected.getDay();
      prevDaySelElem.className = (day > 0 && day < 6)
        ? this.cssClassWeek
        : this.cssClassWeekend;
    }
  }

  this.fillInputs("", "", "");

  var newDaySelElem = document.getElementById( this.getDateId(y, m, d) );

  if ( newDaySelElem ) {
    newDaySelElem.className = this.cssClassDaySelected;
  }
  this.dateSelected = new Date(y, m, d);

  this.fillInputs("" + y, "" + (m + 1), "" + d);
}

Calendar.prototype.fillInputs = function(y, m, d)
{
  if (this.inputDay) this.inputDay.value = d;
  if (this.inputMonth) this.inputMonth.value = m;
  if (this.inputYear) this.inputYear.value = y;
}

Calendar.prototype.next = function()
{
  this.setMonth(this.year, (this.month + 1) + 1);
  this.create();
}

Calendar.prototype.prev = function()
{
  this.setMonth(this.year, (this.month + 1) - 1);
  this.create();
}

Calendar.prototype.open = function()
{
  this.setDate(
    this.correctYear(this.dateSelected.getYear()), 
    this.dateSelected.getMonth() + 1, 
    this.dateSelected.getDate());
  this.create();
  this.holder.style.display = "block";
}

Calendar.prototype.close = function()
{
  this.holder.style.display = "none";
}

Calendar.prototype.display = function()
{
  if (this.holder.style.display == "block")
    this.close();
  else
    this.open();
}

