// JavaScript Document
var commentAjax = createXMLHTTPObject(); //from xmlRequest.js
var isBusy;

isBusy=false;

function getArticleComments(article_id, page, num_per_page) {	

    if(isBusy == false) {
        isBusy = true;
        commentAjax.open('GET', 'common/inc/getArticleComments.php?rnd=' + Math.random()*4 + '&article_id=' + article_id + '&page=' + page + '&num_per_page=' + num_per_page, true);
        commentAjax.onreadystatechange = doUpdateComment
        commentAjax.send(null);

    }
    else {
        window.setTimeout("getArticleComments('" + article_id + "','" + page + "','" + num_per_page + "')", 10);
    }
}

function makePaging(total_row,num_per_page,activePage,article_id){

  num_of_page=Math.ceil(total_row/num_per_page);
  nextPage=parseInt(activePage) + 1;
  prevPage=parseInt(activePage) - 1;
  text="<div class=\"pagination_box\"><ul class=\"pagination\">";

  if(activePage>1){
    text+= "<li><a href=\"javascript:getArticleComments('" + article_id + "','" + prevPage + "','" + num_per_page + "');\" class=\"pg_prev\" title=\"previous page\"></a></li>";
  }

  for(i=1;i<=num_of_page;i++){
    if(activePage==i){
text += "<li class=\"page_current\">" + i + "</li>";
}else{
    text += "<li><a href=\"javascript:getArticleComments('" + article_id + "','" + i + "','" + num_per_page + "');\">" + i + "</a></li>";
  }
  }
  if(activePage!=num_of_page){
    text += "<li><a href=\"javascript:getArticleComments('" + article_id + "','" + nextPage + "','" + num_per_page + "');\" class=\"pg_next\" title=\"next page\"></a></li>";
  }
  text+="</ul></div>";
  if(num_of_page == 1){ text = "";}
return text;
}

function doUpdateComment() {

	if (commentAjax.readyState == 4 || commentAjax.readyState=="complete") {

	       	response = commentAjax.responseText;
          doTheRefresh=0;

          flowthis=document.getElementById("comment_container");
          commentdiv=document.getElementById("fxComment");
		      if(response!=''){
		        data=eval("(" + response + ")");

            total_row=data.total_row;
            page=data.page;
            num_per_page=data.num_per_page;
            article_id=data.article_id;

            if(typeof commentdiv!='undefined'){
              commentdiv.innerHTML = total_row;
            }
            
            comments=data.comments;
            
            commentContent = "<table cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n";
            
            for(i=0;i<comments.length;i++){
              
              commentContent += "<tr>\r\n";
              //commentContent += " <td class=\"icon\"><a href='"+ comments[i].userURL +"'><img src='" + comments[i].thumbnail +"' class=\"thumbnail\" border=\"0\"></a></td>\r\n";
              //commentContent += "<td><p class=\"postedby\">Posted by: <a href=\""+ comments[i].userURL + "\">" + comments[i].userName +"</a> on " + comments[i].date +"</p>";
commentContent += "<td><p class=\"postedby\">Posted by: " + comments[i].userName +" on " + comments[i].date +"</p>";
              commentContent += "<p>" + comments[i].comment + "</p></td>";
              commentContent += "</tr>\r\n";

            }
            commentContent += "</tbody></table>";
            commentContent += makePaging(total_row,num_per_page,page,article_id);
            flowthis.innerHTML = commentContent;
		        if(doTheRefresh){

              window.location.reload();
		        }
		      }
    isBusy = false;
	}

}
