/* Code Copyright BlueBridge k.k. */

KEIREKI['XComments'] = {
  'OpenableState' : function(item_kind, post_id) {
    var $cb = $('#xcomments_'+item_kind+'_'+post_id.replace(/:/g,'\\:'));
    var clen = $cb.find('.xcomment-items').children().length;
    return (clen||$cb.find('.xcomment-tashikani').hasClass('has_tashikanis')||$cb.find('.xcomment-tashikani').hasClass('tashikani_exists')?1:0)
  },

  'InitTextArea': function(item_kind, post_id, $li, $ta, $cb) {
    $ta.val('');
    $ta.autoResize();
    $ta.focus(function(event){
      if (!$cb.hasClass('ENTICE_ENABLED')) {
        $li.find('.xcomment-add').show();
      }
      $cb.removeClass('hidden_add_button');
      $cb.addClass('active_add_button');
      _GLOBAL_EVENTS.push(function(e) {
        if (e.target.tagName == 'HTML' || !contains($cb.get(0), e.target)){
          if (!$cb.hasClass('ENTICE_ENABLED')) {
            $li.find('.xcomment-add').hide();
          }
          $cb.addClass('hidden_add_button');
          $cb.removeClass('active_add_button');
          if ( !KEIREKI.XComments.OpenableState(item_kind, post_id) ) {
            $cb.hide();
          }
          return 1;
        }
      });

    });

    $submit = $cb.find('input[type=submit]');
    $submit.click(function(){
      if ($.trim( $ta.val() ).length == 0) return;
      $submit.attr("disabled", "disabled");
      $ta.attr("disabled", "disabled");
      API.callMethodJSON('x_comment', {'item_kind':item_kind, 'xid':post_id, 'body':$ta.val()}, {success:function(){
        $submit.removeAttr("disabled");
        $ta.val('');
        $ta.removeAttr("disabled");
        KEIREKI.XComments.ReloadComments(item_kind, post_id);
      }})
    });
  },
  
  
  'ReloadComments' : function(item_kind, post_id, show_full) {
    $('#xcomments_'+item_kind+'_'+post_id.replace(/:/g,'\\:')+'_items').load('/_t/tool_x_comment_cluster/?'+(show_full?'more_comments=1&':'')+'xid='+post_id+'&kind='+item_kind+' .xcomment-items>*');
  },
  
  'ToggleComments' : function(item_kind, post_id) {
    var $cb = $('#xcomments_'+item_kind+'_'+post_id.replace(/:/g,'\\:'));
    var $ta = $cb.find('.xcomment-add textarea');
    var go_for_open = KEIREKI.XComments.OpenableState(item_kind, post_id);
    if ($cb.is(":hidden")||go_for_open) {
      $cb.show();
      $ta.focus();
    } else if (!go_for_open) {
      $cb.hide();
    }
  },
  
  'ToggleTashikani' : function(item_kind, post_id) {
    var $cb = $('#xcomments_'+item_kind+'_'+post_id.replace(/:/g,'\\:'));
    var $nb = $cb.find('.xcomment-tashikani');
    
    if ($nb.hasClass('tashikani_not_exists')) {
      API.callMethodJSON('x_tashikani', {item_kind:item_kind, xid:post_id}, {});
      $nb.removeClass('tashikani_not_exists');
      $nb.addClass('tashikani_exists');
      $nb.find('.tashikani_sentence_not_exists').fadeOut(250);      
      $nb.find('.tashikani_sentence_exists').fadeIn(250);
    } else {
      API.callMethodJSON('x_tashikani_remove', {item_kind:item_kind, xid:post_id}, {});
      $nb.find('.tashikani_sentence_exists').fadeOut(200);
      $nb.find('.tashikani_sentence_not_exists').fadeIn(200, function() {
        $nb.addClass('tashikani_not_exists');
        $nb.removeClass('tashikani_exists');
        if (!KEIREKI.XComments.OpenableState(item_kind, post_id)) {
          $nb.hide();
          $cb.hide();
        } else if ( !$nb.hasClass('has_tashikanis') ) {
          $nb.slideUp( 200 );
        }
      });
    }
    if ( $cb.is(":hidden")||$nb.is(":hidden") ) {
      $cb.show();
      $nb.hide();
      $nb.slideDown( 300 );
    }
  },
  
  'RemoveComment': function(item_kind, xid, comment_id) {

    Boxy.ask('このコメントを削除してよろしいですか？', {'ok':'削除', 'cancel':'キャンセル'}, function(v){
      if (v == 'ok') {
        API.callMethodJSON('x_comment_remove', {comment_id:comment_id}, {});
        var $c = $('#xcomment_'+item_kind+'_'+xid.replace(/:/g,'\\:')+'__'+comment_id.replace(/:/g,'\\:'));
        $c.slideUp( 200,function(){
          $c.remove();
        });
      }
    }, {'title':'コメントを削除', 'modal':false});
  }
  
};