var __Calendar = DefineClass({

  month: ['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь'],

  day: {},

  init: function(obj, today) {
    this.container = obj;
    this.date= new Date(today);
    this.CurrentDay = this.ParseDate(new Date());
    this.ul = document.createElement('ul');
    this.container.appendChild(this.ul);
    this.BigCurrentDayCell = this.AddItem(92, 68);
    Element.setClass(this.BigCurrentDayCell, 'BigCurrentDayCell');
    this.CurrentMonthCell=this.AddItem(128, 34);
    this.CurrentYearCell=this.AddItem(128, 34);
    Element.setClass(this.CurrentYearCell, 'CurrentYearCell');
    for (i=1;i<=42;i++) this.day[i] = this.AddItem(25, 22);
    this.Set(this.BigCurrentDayCell, this.CurrentDay.day);
    this.Update();
  },

  AddItem: function(width, height) {
    var li = document.createElement('li');
    li.style.width = width + 'px';
    li.style.height = height + 'px';
    this.ul.appendChild(li);
    return(li);
  },

  Set: function(obj, content) {
    obj.innerHTML = content;
  },

  ParseDate: function(source) {
    return({ year: source.getFullYear(), month: source.getMonth(), day: source.getDate() });
  },

  Update: function(date) {
    this.Set(this.CurrentMonthCell, '<a href="javascript:Calendar.ModifyMonth(-1)"><img src="/img/cPrev.gif" align="absmiddle" style="margin-right: 5px"></a>'+this.month[this.date.getMonth()]+'<a href="javascript:Calendar.ModifyMonth(1)"><img src="/img/cNext.gif" align="absmiddle" style="margin-left: 5px"></a>');
    this.Set(this.CurrentYearCell, '<a href="javascript:Calendar.ModifyYear(-1)"><img src="/img/cPrev.gif" align="absmiddle" style="margin-right: 5px"></a>'+this.date.getFullYear()+' г.<a href="javascript:Calendar.ModifyYear(1)"><img src="/img/cNext.gif" align="absmiddle" style="margin-left: 5px"></a>');
    this.DrawMonth();
  },

  ModifyMonth: function(direction) {
    var month = this.date.getMonth() + direction;
    this.date = new Date(this.date.getFullYear(), month, 1);
    this.Update(this.date);
  },

  ModifyYear: function(direction) {
    var Year = this.date.getFullYear()+direction;
    this.date = new Date(Year, this.date.getMonth(), 1);
    this.Update(this.date);
  },

  redirect: function(e) {
    e = Event.fix(e);
    var tmp = this.ParseDate(this.date);
    document.location.href = '?a=archive&date='+tmp.year+'-'+(tmp.month+1)+'-'+e.target.innerHTML;
  },


  DrawMonth: function() {
    DayInMonth = this.DayInMonth(this.date);
    MonthBegin = this.date;
    MonthBegin.setDate(1);
    MonthBegin = MonthBegin.getDay();
    TemporaryDay = this.date;

    var day = 0;
    var week = 1;
    for (i=1;i<=42;i++) {
      this.Set(this.day[i], '');
      this.day[i].style.cursor = 'default';
      this.day[i].style.background = '';
      if (week>5) {
       this.day[i].style.background = '#7286a9';
       Element.setClass(this.day[i], 'Holyday');
      } else Element.setClass(this.day[i], 'DayCell');
      week++;
      if (week == 8) week = 1;
    }
    if (MonthBegin == 0 ) MonthBegin = 7;
    for (i=1;i<DayInMonth+MonthBegin;i++) {
      if (i>=MonthBegin) {
        day++;
        TemporaryDay.setDate(day);
        ParsedTemporaryDay = this.ParseDate(TemporaryDay);
        if (ParsedTemporaryDay.day == this.CurrentDay.day && ParsedTemporaryDay.month == this.CurrentDay.month && ParsedTemporaryDay.year == this.CurrentDay.year) this.day[i].style.background = '#4c110b';
        this.Set(this.day[i], day);
        var oThis = this;
        this.day[i].onclick = function(e) { oThis.redirect(e); }
        this.day[i].style.cursor = 'pointer'
      };
    }
  },

  DayInMonth: function(date) {
    var ParsedDate = this.ParseDate(date);
    var dd = new Date(ParsedDate.year, (ParsedDate.month+1), 0);
    return dd.getDate();
  }

})
