var last_elem_touched = null; var first_view = true;

function $(x) { return document.getElementById(x) }

function showForm(targetID)
{
    if ($("comment-form-portable").parentNode == $("comment-form" + targetID)
        && $("comment-form-portable").getAttribute("STATE") != "0")
    {
        $("comment-form-portable").style.display = "none";
        $("comment-form-portable").setAttribute("STATE", "0");
        $("show-form" + targetID).style.backgroundPosition = "0 0";
        $("commentUserNamePortable").value = "";
        $("commentUserEmailPortable").value = "";
        $("commentTextPortable").value = "";
        $("commentCaptchaAnswerPortable").value = "";
        last_elem_touched = $("show-form" + targetID)
    }
    else if ($("comment-form-portable").getAttribute("STATE") == "0"
            && $("comment-form-portable").parentNode == $("comment-form" + targetID))
    {
        $("parentIdPortable").value = targetID;
        $("comment-form-portable").style.display = "block";
        $("comment-form-portable").setAttribute("STATE", "1");
        $("show-form" + targetID).style.backgroundPosition = "0 -10px";
        if ($("comment-error-message") && !first_view)
        {
            $("comment-error-message").parentNode.removeChild($("comment-error-message"))
        }
        last_elem_touched = $("show-form" + targetID)
    }
    else
    {
        if ($("comment-error-message") && !first_view)
        {
            $("comment-error-message").parentNode.removeChild($("comment-error-message"))
        }
        first_view = false;
        $("parentIdPortable").value = targetID;
        $("comment-form-portable").style.display = "block";
        $("comment-form" + targetID).appendChild($("comment-form-portable"));
        $("show-form" + targetID).style.backgroundPosition = "0 -10px";
        if (last_elem_touched)
        {
            last_elem_touched.style.backgroundPosition = "0 0"
        }
        last_elem_touched = $("show-form" + targetID)
    }
}

function showThread(oid) { var t = $("thread" + oid); if (t.getAttribute("STATE") == "1") { t.style.display = "none"; t.setAttribute("STATE", "0"); $("show-thread" + oid).replaceChild(document.createTextNode("Show "), $("show-thread" + oid).firstChild); $("show-thread" + oid).style.backgroundPosition = "0 0" } else { t.style.display = "block"; t.setAttribute("STATE", "1"); $("show-thread" + oid).replaceChild(document.createTextNode("Hide "), $("show-thread" + oid).firstChild); $("show-thread" + oid).style.backgroundPosition = "0 -14px" } }

function handleCommentSubmit(o, submitToFacebookCheckboxId, shareOnFacebookRequestedFieldId, facebookUserIdFieldId)
{
    var submitToFacebookCheckbox = $(submitToFacebookCheckboxId);

    if (submitToFacebookCheckbox != null && submitToFacebookCheckbox.checked == 1)
    {
        // user wants to share on Facebook,
        // so ask the user to login to Facebook first
        FB.login(function (response)
        {
            var shareOnFacebookRequestedField = $(shareOnFacebookRequestedFieldId);
            var facebookUserIdField = $(facebookUserIdFieldId);

            if (response.session)
            {
                // user successfully logged in and authorized the app
                shareOnFacebookRequestedField.value = 1;
                facebookUserIdField.value = response.session.uid;
            }
            else
            {
                shareOnFacebookRequestedField.valueOf = 0;
                facebookUserIdField.value = "";
            }

            // call the actual submit no matter what
            submitNewComment(o);

        }, { perms: 'publish_stream' });

        return false
    }
    else
    {
        // user does not want to share on Facebook,
        // so call the actual submit
        return submitNewComment(o);
    }
}

function submitNewComment(o)
{
    var id = o.form.getAttribute("id");
    var pid = o.form.parentNode.getAttribute("id");
    o.form.action = "http://" + location.host + location.pathname + "?commentid=" + (pid == "comment-form-static" ? "-1" : o.form.parentNode.parentNode.getAttribute("id").split("-form")[1]);
    setTimeout("$(\"" + id + "\").submit();", 100);
    return false
}

function skipToComment()
{
    if (/(\?|&)commentid=/.test(location.href))
    {
        var id = location.href.replace(/^.*(\?|&)commentid=((\-)?[\d]+).*$/, "$2");
        if (comment_success)
        {
            location = "#comments-anchor"
        }
        else if (!comment_success && id == "-1")
        {
            location = "#commentForm"
        }
        else
        {
            var oid = id;
            while (/^thread[\d]+$/.test($("comment" + oid).parentNode.getAttribute("id")))
            {
                oid = $("comment" + oid).parentNode.getAttribute("id").replace(/^thread([\d]+)$/, "$1");
                showThread(oid)
            }
            location = "#anchor" + id;
            if (!comment_success)
            {
                showForm(id)
            }
        }
    }
}

function doFacebookShare(siteName, logoUrl, shareType, text, anchorName) {
    FB.ui
    ({
        method: 'feed',
        name: siteName + ': ' + document.title,
        link: window.location + '#' + anchorName,
        picture: logoUrl,
        caption: shareType + ' on ' + siteName,
        description: text
    },
    function (response)
    {
        // TODO: do something with the response; maybe store the FB comment id (response.post_id) somewhere
        /*if (response && response.post_id)
        {
        alert('Post was published.');
        }
        else
        {
        alert('Post was not published.');
        }*/
    });
}

window.onload = skipToComment;
