/**
 * Class Calendar
 *
 * @author Marek Lapis <marek AT lapis DOT cz>
 * @version 2007-03-14
 */
/**
 * Calendar constructor
 *
 * @param string holder : id of HTMLelement
 * @param int year : 4num format (e.g. 2007)
 * @param int month : 1 - 12 (human, not JS 0 - 11)
 * @param int day : 1 - 31
 */
function Calendar(holder, country, 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.country  = country;
    
    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();
}

/**
 * setDate
 *
 * @access public
 * @param int year : 4num format (e.g. 2007)
 * @param int month : 1 - 12 (human, not JS 0 - 11)
 * @param int day : 1 - 31
 */
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);
}

/**
 * setMonth
 *
 * @access public
 * @param int year : 4num format (e.g. 2007)
 * @param int month : 1 - 12 (human, not JS 0 - 11)
 */
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)
{
  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() );
   this.fillInputs("" + y, "" + (m + 1), "" + d);
}

Calendar.prototype.getMesic = function(mesic)
{
  if (mesic >= 0 && mesic <= 11) {
    var mesice = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
    return mesice[ mesic ];
  }
  return "";
}

Calendar.prototype.getDen = function(den)
{
  if (den >= 0 && den <= 6) {
    var dny = new Array("M","T","W","T","F","S","S");
    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="Next month" src="'+ this.imgPath +'images/calendar_arrow_right.gif" border="0" /></a>';
  html +=   '<a href="#" onclick="javascript:'+ this.holderCalStr +'.prev();return false;" id="calendarPrev"><img alt="Previous month" src="'+ this.imgPath +'images/calendar_arrow_left.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="calendarDayName">' + 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;
          }
        }
        //den=d +'.'+ m +'.' + y;
        
        
        //if (this.akce[den]>'0'){
          //html += '<td><a class="den_akce" href="#">'+ d +'</a></td>';
        //}else{
          html += '<td><div class="'+ cssClass +'" id="'+(m+1)+'-'+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;
  
  /* Nacteme svatky */
  _getHolidays( m+1, this.country );
}

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();
}
