var StudyCombo = Class.create({
  initialize: function(studies, progresses, current) {
    var wrap = $('recent_studies');

    var header = wrap.append('h4').addClassName('title');
    
    if (studies.length > 0) {
      header.addClassName('exist');
      wrap.observe('click', function(event) {
            b.toggle();
          });
    }
    
    var b = wrap.append().addClassName('items').hide();
    b.append('p')
     .addClassName('commands')
        .append('a')
        .writeAttribute({href: '#'})
        .observe('click', function(event) {
          event.stop();
          b.hide();
        })
        .append('img')
        .writeAttribute({
          alt: '',
          src: '/images/close.gif'
        });
    var list = b.append('ul');

    studies.each(function(study) {
      var wrap = list.append('li')
                     .addClassName('clearer');
      
      var collection = progresses.find(function(progress) {
        return progress.collection_id == study.collection_id;
      });

      var word_count = collection ? (50 - collection.word_count * 50 / study.words_count).round() : 50;
      var correct_count = collection ? (50 - collection.correct_count * 50 / study.words_count).round() : 50;
      
      var progress = wrap.append()
                         .addClassName('progress');

      // 정답, 오답 모음은 진행현황을 보여주지 않는다.
      if (collection) {
        progress.addClassName('existing')
                .append()
                .addClassName('word_count')
                .setStyle({
                  backgroundPosition: 'left ' + word_count + 'px'
                })
                .append()
                .addClassName('correct_count')
                .setStyle({
                  backgroundPosition: 'left ' + correct_count + 'px'
                });
      }

      var item = wrap.append()
                     .addClassName('item');

      item.append('h4')
          .append('a')
          .writeAttribute({
            href: '#'
          })
          .update(study.collection_name)
          .observe('click', function(event) {
            event.stop();
            // if (typeof calendar == 'undefined') {
              header.innerHTML = event.element().innerHTML;
              $('continue_study').setAttribute('href', '/studies/' + study.id + '/steps/1');
              b.hide();
            // } else {
            //   location.href = '/studies/' + study.id + '?year=' + calendar.year + '&month=' + (calendar.month + 1);
            // }
          });

      item.append('p')
          .update(study.words + '단어씩 / ' + this.getExclude(study.exclude));

      item.append('p')
          .update(study.game_name);
    }.bind(this));

    if (studies.length == 0) {
      header.innerHTML = '공부한 내역이 없습니다.';
      $('continue_study').hide();
    } else {
      var default_study = current ? studies.find(function(study) {
        return study.id == current;
      }) : studies[0];
      header.innerHTML = default_study.collection_name;
      $('continue_study').setAttribute('href', '/studies/' + default_study.id + '/steps/1');
    }
  },
  getExclude: function(exclude) {
    switch (exclude) {
    case 10:
      return '전체 단어';
    case 11:
      return '접해본 단어 제외';
    case 12:
      return '맞힌 단어 제외';
    case 13:
      return '틀린 단어만';
    case 14:
      return '공부한 단어 제외';
    }
  }
});
