/**
 * News calendar
 * 
 * @version $Id: news_calendar.js 326 2009-06-04 15:18:02Z  $
 */
NewsCalendar = function(id, dates, day, month, year) {
  this.id = id;
  this.dates = {};
  this.date = new Date(year, month - 1, day);
  
  this.importDates(dates);
  this.init();
}

NewsCalendar.prototype = {
  init: function() {
    var _this = this;
    this.getEl().datepicker({
      numberOfMonths: 2,
      showCurrentAtPos: 1,
      defaultDate: this.date,
      gotoCurrent: true,
      beforeShowDay: function(date) {
        return _this.onBeforeShowDay(date);
      },
      onSelect: function() {
        _this.onSelectDate();
      }
    });
    
  },
  
  importDates: function(dates) {
    for(var i = 0, l = dates.length; i < l; i ++)
      this.dates[dates[i]] = true;
  },
  
  isEnabled: function(date) {
    var day = '' + date.getDate();
    if(day.length == 1) day = '0' + day;
    var month = '' + (date.getMonth() + 1);
    if(month.length == 1) month = '0' + month;
    var year = '' + date.getFullYear();
    var hash = day + month + year;
    return this.dates[hash] === true;
  },
  
  isWeekEnd: function(date) {
    return date.getDay() == 0 || date.getDay() == 6;
  },
  
  applyDate: function(date) {
    location.href = '/news/' + date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate() + '/';
  },
  
  onBeforeShowDay: function(date) {
    return [ this.isEnabled(date), this.isWeekEnd(date) ? 'red' : '' ];
  },
  
  onSelectDate: function() {
    this.applyDate(this.getEl().datepicker('getDate'));
  },
  
  getEl: function() {
    return $('#' + this.id);
  }
}

