Builder.dump();

var comments = {
  current: 0,
  limit: 5,
  content_id: 0,

  next: function(){
    this.clear();	
	this.current += this.limit;
    this.search(this.update);
  },
  prev: function(){
	if(this.current >= this.limit){
      this.clear();	
	  this.current -= this.limit;
      this.search(this.update);
	} 
  }, 	  
  search: function(f){
    var ajax = new Ajax.Request(portal_url + '/api/',
      {
        method: 'get', 
        parameters: {
        'cmmesse.blogitems.getRelatedWithContent':'',
		'content_id':content_id,
		'limit':this.limit,
		'offset':this.current
      },
      onComplete: f
    });
  },
  make: function(item){
	if(item.type == 'comment'){
	  return LI({},[item.contents, BR(), SPAN({},["by " + item.nickname + ' [' + item.submitDateString + "]"])]);
	}else{
	  return LI({},[A({href:item.address},[IMG({src:"/common/images/blog-icon2.gif",alt:"Blog"},[])]),A({href:item.address},[item.title]),BR(),SPAN({},["by " + item.nickname + " [" + item.submitDateString +"]"])]);
	}
  },
  update: function(res){
   var items = eval('(' + res.responseText + ')');
   if(items.result == undefined){
     hideCommentNext();
	 hideCommentPrev();
	 clear();
	 var message = LI({},['まだ投稿はありません。ログインしてコメント・ブログを投稿しませんか？']);
	 $('comments').appendChild(message);
	 return;
   }
   comments.clear()
   if(items.itemCount > comments.current + comments.limit){
     showCommentNext();
   }else{
	 hideCommentNext();
   }
   if(comments.current >= comments.limit){
     showCommentPrev();
   }else{
     hideCommentPrev();
   }
   items.result.each(function(item){
     var node = comments.make(item);
	 $('comments').appendChild(node);
   }); 
  },
  clear: function(){
    while($("comments").hasChildNodes()){
      $("comments").removeChild($('comments').childNodes[0]);
    }
  }  
}

function showCommentPrev(){
    $('comment_prev_top').show();
	$('comment_prev_bottom').show();
}
function showCommentNext(){
    $('comment_next_top').show();
	$('comment_next_bottom').show();
}
function hideCommentPrev(){
    $('comment_prev_top').hide();
	$('comment_prev_bottom').hide();
}
function hideCommentNext(){
    $('comment_next_top').hide();
	$('comment_next_bottom').hide();
}

CommentInputField = Class.create();
CommentInputField.prototype = {
   // profile: サーバへのアクセス方法などを定義
   // search:   検索ワードを入力する<input>
   // button:   検索開始するためのボタン
   // list:     検索結果を表示するノード
   // form:     選択要素のIDを保持するノード
   initialize: function(profile){
	hideCommentNext();
	hideCommentPrev();

	Event.observe($('comment_next_top'),'click',function(){profile.next(profile)});
	Event.observe($('comment_next_bottom'),'click',function(){profile.next(profile)});
	Event.observe($('comment_prev_top'),'click',function(){profile.prev(profile)});
	Event.observe($('comment_prev_bottom'),'click',function(){profile.prev(profile)});
	profile.search(profile.update);
  }
};

Event.observe(window,'load',function(){
  var c = new CommentInputField(comments);
});
