/*jslint strict:true, evil: true, onevar: true, nomen: false  */ /*global $, jQuery, console, hbx, window, ndm, document  */

"use strict";

/* Script to add character counter and control behaviour of articles comments form */

$(document).ready(function(){
    var commentFieldActive = false, totalNoComments, noCommentsShowing, firstPagElem, firstPagination = false;
    $("#comment").CG_charCounter({
        ccMaxChars : 1200,
        ccTag : 'p',
        ccNumTag : 'em',
        ccMsgTag : 'span',
        ccMsg:' characters left',
        ccTagClass:'field-instruction'
    });

    $("#CG_form_comments").click(
        function(){
            commentFieldActive = true;
            /* Show the other form fields */
            $("p.field-instruction").css("display","block");
            $("fieldset.additional-information").slideDown("medium");
            $("#CG_form_comments .controls").css("display","block");
        }
    );

    $("#CG_form_comments").mouseover(
        function(){
            commentFieldActive = true;
        }
    );

    $("#CG_form_comments").mouseout(
        function(){
            commentFieldActive = false;
        }
    );

    $("#CG_form_comments input, #CG_form_comments textarea").focus(
        function(){
            commentFieldActive = true;
        }
    );

    $("#CG_form_comments input, #CG_form_comments textarea").blur(
        function(){
            commentFieldActive = false;
        }
    );

    $(document).click(
        function(){
            if(!commentFieldActive){
                /* Hide the other form fields */
                $("p.field-instruction").not("#comment_charCounter_holder").css("display","none");
                $("fieldset.additional-information").slideUp("medium");
                $("#CG_form_comments .controls").css("display","none");
            }
        }
    );

    $("#comments ul.CG_paginator_EE a").each(function(){
        var curHref = $(this).attr("href");
        if(curHref.indexOf("void") !== -1){
            $(this).attr("href",curHref+"#comments");
        }
    });
});

function CG_showAllComments(noCommentsShowing){
    $("#CG_article_comments li").css("display","block");
    $("#CG_link_readall").css("display","none");
    $(".CG_paginator_EE").removeClass("CG_hide_pagination");
    $("p.CG_comments_count strong").eq(0).text(noCommentsShowing);
}

window.onerror = function(){ return true;}

