var timeout = false;    // this will have our timeout instance in it
var topicId = 0;        // topic id to collect (retrieved from DOM)
var refresh = 10000;    // refresh interval
var offset  = 0;
var linkPrev = null;
var linkNext = null;
var span = null;

/**
 * If there's more than 10 comments then we need to paginate them
 **/
var paginateComments = function ()
{
    total   = jQuery('.comment').length;
    if(total > 10)
    {
        // Create a link to see older comments
        para        = document.createElement('p');
        linkPrev    = document.createElement('a');
        linkNext    = document.createElement('a');
        span        = document.createElement('a');

        jQuery(linkPrev).html("Older comments");
        jQuery(linkPrev).attr('href','#comments-list');
        jQuery(linkNext).html("Newer comments");
        jQuery(linkNext).attr('href','#comments-list');

        jQuery(span).html("&nbsp;|&nbsp;");

        jQuery(para).append(linkPrev);
        jQuery(para).append(span);
        jQuery(para).append(linkNext);

        jQuery('#comments-list').append(para);
        
        resolveNextPrevLinks();
    
        jQuery(linkPrev).click(
            function (ev)
            {
                ev.preventDefault();
                offset = offset + 10;
                resolveNextPrevLinks();
                showHideComments();
            }
        );
    
        jQuery(linkNext).click(
            function (ev)
            {
                ev.preventDefault();
                offset = offset - 10;
                resolveNextPrevLinks();
                showHideComments();
            }
        );
    
    }
}

var resolveNextPrevLinks = function ()
{
    if(offset + 10 >= total)
    {
        jQuery(linkPrev).hide();
    }
    else
    {
        jQuery(linkPrev).show();
    }
    
    if(offset - 10 >= 0)
    {
        jQuery(linkNext).show();
    }
    else
    {
        jQuery(linkNext).hide();
    }
    
    if(jQuery(linkPrev).is(':visible') && jQuery(linkNext).is(':visible'))
    {
        jQuery(span).show();
    }
    else
    {
        jQuery(span).hide();
    }
}

/**
 * Loop through the comments in the list and sniff out
 * the report hyperlink, as this has a 'rel' attribute 
 * which tells us the comment's position in the list
 **/
var showHideComments = function ()
{
    jQuery('.comment').each(
        function ()
        {
            num = jQuery(this).find('a').attr('rel');
            num = parseInt(num);
            if(num <= offset || num > (offset + 10))
            {
                jQuery(this).hide();
            }
            if(num > offset && num < (offset + 10))
            {
                jQuery(this).show();
            }
        }
    );
    if(undefined !== window.adjustComments)
    {
        adjustComments();
    }
}

/** 
 * Turn the report links in to something clickable using the rel attribute
 * to find the ID of the comment
 **/
var reportLinks = function()
{
    /**
     * Iterate through the .comment divs and create a
     * report link in each of them
     **/
    i = 0; 
    jQuery('.comment').each(
        function ()
        {
            id = parseInt(this.id.substring(7));
            jQuery(this).find('a').attr('href','/report-comment/'+id);
            if((i > offset + 10) || i < offset)
            {
                jQuery(this).hide();
            }
            i++;
        }
    );
}

/**
* Listen for keypresses (ignoring delete and
* backspace presses, 8 & 46) and create a character
* countdown, starting at 1000 as well as restricting
* the number of characters in the input field
**/
var textfieldCounter = function ()
{
     jQuery('#comment-field').keydown(
         function (ev)
         {
             text = jQuery('#comment-field').val(); 
             textlength = text.length;
             limit = 2000;
         
             if(textlength >= limit && (ev.which !== 8 || ev.which !== 46))
             {
                 jQuery('#comment-field').val(text.substr(0,limit));
                 ev.preventDefault();
             }
             jQuery('#comment-characters').html(
                 'You have '+ (limit - textlength) +' characters left.'
             );
         }            
     );
} 

var updateComments = function ()
{
    clearTimeout(timeout);
    if(topicId)
    {
        jQuery('#update-me-with-jquery').load(
            '/comments-ajax.php?topic='+topicId,
            false,
            function ()
            {
                paginateComments();
                reportLinks();
                if(undefined !== window.adjustComments)
                {
                    adjustComments();
                }
            }
        );
        timeout = setTimeout('updateComments()',refresh);
    }
}

jQuery(document).ready(
    function ()
    {
        timeout = setTimeout('updateComments()',refresh);
        topicId = jQuery('input#topic').attr('value');

        textfieldCounter();
        reportLinks();
        paginateComments();
        
        /**
         * Given that the contents of the comments box has changed, drop in 
         * the new background image to fit the new dimensions
         **/
        if(undefined !== window.adjustComments)
        {
            adjustComments();
        }
    }
);

if(undefined !== window.adjustComments)
{
    adjustComments();
}
