var DeCalendar = new Class({

  mMonthDivs: new Array(),
  mLast: false,
  mAllMonths: 0,

  initialize: function(){
    this.eRegisterEvents();
    this.eRegisterMonthDivs();
    this.showMonthById(5);
  },

  eRegisterEvents: function(){

    $E('.cal_select').addEvent('click', this.rollSelectList);
    $$('.months_list div a').each(this.switchMonth.bind(this));

    $E('.cal_prev').addEvent('click', this.showPrevious.bind(this));
    $E('.cal_next').addEvent('click', this.showNext.bind(this));

  },

  eRegisterMonthDivs: function(){

    $$('.calendar .Event').each(function(pEl){
      this.mMonthDivs.extend([pEl.getProperty('id')]);
      this.mAllMonths++;
    }.bind(this));

  },

  rollSelectList: function(){
console.log($E('.months_list').getStyle('display'));
    if('none' == $E('.months_list').getStyle('display')){
      $E('.months_list').setStyle('display', 'block');
    }
    else{
      $E('.months_list').setStyle('display', 'none');
    }

  },

  switchMonth: function(pEl){

    pEl.addEvent('click', function(){
      this.rollSelectList();
      var cElId = pEl.getProperty('name').toInt();
      this.showMonthById(cElId);
    }.bind(this));

  },

  showMonthById: function(pId){

  	if(false !== this.mLast){
      $E('#Event'+this.mLast).setStyle('display', 'none');
    }

    if(this.mMonthDivs[pId]){
      $E('#'+this.mMonthDivs[pId]).setStyle('display', 'block');
      $E('.select .cal_select').setText($E('.select .months_list .month'+pId).getText());
      this.mLast = pId;
    }

  },

  showNext: function(){

    if(this.mLast >= this.mAllMonths -1){
      this.showMonthById(0);
    }
    else{
      this.showMonthById(this.mLast + 1);
    }

  },

  showPrevious: function(){

    if(this.mLast <= 0){
      this.showMonthById(this.mAllMonths - 1)
    }
    else{
      this.showMonthById(this.mLast - 1)
    }

  }

});

window.addEvent('domready', function(){

  MyDeCalendar = new DeCalendar();

});

