jQuery(document).ready(function () {
    function setCookie(name, value) {
        var exp = new Date();
        exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));
        document.cookie = name + "=" + escape(value) + "; path=/" + ((exp == null) ? "" : "; expires=" + exp.toGMTString());
    }

    function getCookie(name) {
        var dc = document.cookie;
        var cname = name + "=";
        if (dc.length > 0) {
            begin = dc.indexOf(cname);
            if (begin != -1) {
                begin += cname.length;
                end = dc.indexOf(";", begin);
                if (end == -1) end = dc.length;
                return unescape(dc.substring(begin, end));
            }
        }
        return null;
    }

    //testo per il tasto mostra commenti
    var showText = '▶';

    //testo per il tasto nascondi commenti
    var hideText = '▼';

    //può contenere tag html (eg: <img src="/img/showcommentsicon.png">)
    var showIcon = '<img src="http://pollycoke.org/images/no-commenti.png" width="20" height="20" style="margin-top:2px;" >';

    //può contenere tag html (eg: <img src="/img/hidecommentsicon.png">)
    var hideIcon = '<img src="http://pollycoke.org/images/commenti.png" width="20" height="20" style="margin-top:2px;" >';

    //nome del cookie che contiene i dati
    var cookie = 'hideComments';

    //una volta che avrai inserito il codice html del tasto (con id="showHideComments")
    //potrai rimuovere la riga "button.appendTo..." e questa riga diventerà:
    //var button = jQuery('#showHideComments');
    var button = jQuery('<li style="float:right;"><a href="#" id="showHideComments"></a></li>');

    //il tasto viene aggiunto a fianco a INFO e RSS
    button.appendTo(jQuery('.pollycoke-activity-tools'));

    //selettore dei commenti
    var comments = jQuery('.activity-comments');
    if (comments.length<2) return;

    //una volta che avrai inserito il codice html del tasto sotto ogni attività
    //(con  class="showHideCommentsActivityButton")
    //potrai rimuovere la riga "var activityButton.. e la riga "activityButton.appendTo.."
    var activityButton = jQuery('<a style="border:none;" href="#" class="showHideCommentsActivityButton"></a>');

    //il tasto viene aggiunto sotto ogni attività a destra dei bottoni attuali
    activityButton.prependTo(jQuery('.has-comments>.activity-content>.activity-meta'));

    //selettore dei bottoni
    var activityButtons = jQuery('.showHideCommentsActivityButton');

    if (getCookie(cookie) == 1) {
        comments.hide();
        button.html(showIcon);
        setCookie(cookie, 1);
        activityButtons.html(showText);
    } else {
        button.html(hideIcon);
        setCookie(cookie, 0);
        activityButtons.html(hideText);
    }

    button.click(function () {
        if (getCookie(cookie) == 1) {
            setCookie(cookie, 0);
            comments.show();
            button.html(hideIcon);
            activityButtons.html(hideText);
        } else {
            setCookie(cookie, 1);
            comments.hide();
            button.html(showIcon);
            activityButtons.html(showText);
        }
        return false;
    });

    activityButtons.click(function () {
        var theActivityButton = jQuery(this);
        if (theActivityButton.html() == showText) {
            theActivityButton.parent().parent().next().show();
            theActivityButton.html(hideText);
        } else {
            theActivityButton.parent().parent().next().hide();
            theActivityButton.html(showText);
        }
        return false;
    });

    jQuery('.acomment-reply').click(function () {
        var theReplyButton = jQuery(this);
        theReplyButton.parent().parent().next().show();
        theReplyButton.prev('.showHideCommentsActivityButton').html(showText);
    });
});

