var default_text='say something...';

$(document).ready(function(){
    $('#comment-page-0').attr("style", "display:block;").addClass("showing");

    paginationCheck();
    $('.reply-to-comment').live('click', function(){
        var target='#' + $(this).parent().parent().parent().attr('id');
        var postid= target.replace('#comment','');//get parent post id
        $.post('/ajax/load_plugin/add_new_comment/' + $('#postid').val() + '/' + postid ,function(results){
	    var json = eval('('+results+')');
            $('#comment-form').remove();
            $(target).html($(target).html() + json.payload);
            bind_comment_listeners();
        });
    });
   
    $('.add-comment-submit').live('click', function() {
        $(this).hide();//("onclick", "javascript:return false;");
    });
    $('#comment-header > a').live('click', function(){
        $.post('/ajax/load_plugin/add_new_comment/' + $('#postid').val(),function(results){
	    var json = eval('('+results+')');
            $('#comment-form').remove();
            $('#comment-header').after(json.payload);
            bind_comment_listeners();
        });
    });
    
    $('.comment-left').live('click', function(){
        if($(".showing").attr("id") != "comment-page-0")
        {
            var current = $('.showing').attr("id").replace("comment-page-",'');
            showCommentPage(current*1.0-1);
        }
    });
    $('.comment-right').live('click', function(){
        if($(".showing").attr("id") != $("#comment-list > ul:last").attr("id"))
        {
            var current = $('.showing').attr("id").replace("comment-page-",'');
            showCommentPage(current*1.0+1);
        }
    });
    
});

function paginationCheck()
{
    var pagination_max = 7; //7 allows for current styling to display nice (bug 1432)
    var num_pages = $('.comment-page-link').size();
    var current_index = $('.comment-page-link').index($('.selected'));

    if(num_pages > pagination_max) {
        $('.comment-page-link').hide();
        var start_index = 0;
		var pagenation_links = Math.floor(pagination_max/2); //number of links to right or left of index
        if(current_index > pagenation_links) { 
            start_index = current_index - pagenation_links;
        } else if (current_index > (num_pages - pagenation_links)) {
            start_index = num_pages - pagenation_links;
        }
		
		total_pagenation_links = (start_index + pagination_max); //total number of links to draw
        for(i = start_index; i < total_pagenation_links; i++) {
            $("#comment-link-"+i).show();    
        }
        $("#comment-link-0").show(); //draw first link
        $("#comment-link-"+(num_pages *1.0 -1)).show(); //draw final link
    }
}

function bind_comment_listeners()
{
    $('.add-comment-body').focus(function(){
        if($(this).val()==default_text)
        {
           $(this).val('');
        }
    }).blur(function(){
    
        if($(this).val()=='')
        {
            $(this).val(default_text);
        }
    });
     
    
}

function render(x)
{
    var output  = '';
    if (isReply(x))
    {
        output += '<li class="single-reply" id="comment' + x.id + '">';
    }
    else
    {
        output += '<li class="single-comment" id="comment' + x.id + '">';
    }
    output += '<span class="user-avatar"><img src="' + x.img +'"/></span>';
    
    output += '<a name="comment' + x.id + '"/>';
    if (isReply(x)) output += '<div class="speech-bubble-wrap">';

	//<PRE> TAGS ADDED TO PRESERVE LINEBREAKS (bug 1548)
    output += '<div class="comment-text"><p><pre>' 
              + x.comment.attributes.body + '</pre></p>'
              + '<em><span>posted just now by ' + x.postedby + '</span>';
          
    if (isReply(x)) output += '</em></div></div>';
    else { output += '<a href="javascript:void(0);" class="reply-to-comment">Reply to comment</a></em></div><ul class="reply"></ul>' }
    
    output += '</li>';
    
    
    return output;
    /*
    <li class="single-comment" id="comment293322">  <a name="comment293322"/>  <div class="comment-text">      
    <p>OVER! THE! WALL!</p>
    <em><span>posted 19 hours ago by TESTAMENT (guest) </span>          
    <a href="javascript:void(0);" class="reply-to-comment">Reply to comment</a></em>  </div>
<ul class="reply"/>
</li>
    */
}

//pass in json comment response, and this function will determine if it is a reply or not
function isReply(x)
{
    if(x.comment.attributes.parent_comment == undefined)
    {
        return false;
    }
    else
    {
        return true;
    }
}
function showCommentPage(x)
{
    $('#comment-list ul.parent-comments').attr("style","display:none;").removeClass("showing");
    $('#comment-page-' + x).attr("style", "display:block;").addClass("showing");
    $('.comment-page-link').removeClass("selected");
    $('#comment-link-' + x).addClass("selected");
    paginationCheck();
}


