(function () {
    function reply(authorId, commentId, commentBox) {
        var author = document.getElementById(authorId).innerHTML;
        var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + ' </a> \n';
        appendReply(insertStr, commentBox)
    }
    function quote(authorId, commentId, commentBodyId, commentBox) {
        var author = document.getElementById(authorId).innerHTML;
        var comment = document.getElementById(commentBodyId).innerHTML;
        var insertStr = '<blockquote cite="#' + commentBodyId + '">';
        insertStr += '\n<strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :</strong>';
        insertStr += comment.replace(/\t/g, "");
        insertStr += '</blockquote>\n';
        insertQuote(insertStr, commentBox)
    }
    function appendReply(insertStr, commentBox) {
        if (document.getElementById(commentBox) && document.getElementById(commentBox).type == 'textarea') {
            field = document.getElementById(commentBox)
        } else {
            alert("The comment box does not exist!");
            return false
        }
        if (field.value.indexOf(insertStr) > -1) {
            alert("You've already appended this reply!");
            return false
        }
        if (field.value.replace(/\s|\t|\n/g, "") == '') {
            field.value = insertStr
        } else {
            field.value = field.value.replace(/[\n]*$/g, "") + '\n\n' + insertStr
        }
    }
    function insertQuote(insertStr, commentBox) {
        if (document.getElementById(commentBox) && document.getElementById(commentBox).type == 'textarea') {
            field = document.getElementById(commentBox)
        } else {
            alert("The comment box does not exist!");
            return false
        }
        if (document.selection) {
            field.focus();
            sel = document.selection.createRange();
            sel.text = insertStr;
            field.focus()
        } else if (field.selectionStart || field.selectionStart == '0') {
            var startPos = field.selectionStart;
            var endPos = field.selectionEnd;
            var cursorPos = startPos;
            field.value = field.value.substring(0, startPos) + insertStr + field.value.substring(endPos, field.value.length);
            cursorPos += insertStr.length;
            field.focus();
            field.selectionStart = cursorPos;
            field.selectionEnd = cursorPos
        } else {
            field.value += insertStr;
            field.focus()
        }
    }
    function loadCommentShortcut(frm, submitbnt, desc) {
        document.getElementById(frm).onkeydown = function (moz_ev) {
            var ev = null;
            ev = window.event ? window.event : moz_ev;
            if (ev != null && ev.ctrlKey && ev.keyCode == 13) {
                document.getElementById(submitbnt).click()
            }
        };
        document.getElementById(submitbnt).value += desc
    }
    window['CMT'] = {};
    window['CMT']['reply'] = reply;
    window['CMT']['quote'] = quote;
    window['CMT']['loadCommentShortcut'] = loadCommentShortcut
})();
jQuery(document).ready(function () {
    jQuery('#commenttab').click(function () {
        jQuery(this).removeClass('tab').addClass('curtab');
        jQuery('#trackbacktab').removeClass('curtab').addClass('tab');
        jQuery('#thetrackbacks').fadeOut(function () {
            jQuery('#thecomments').fadeIn(function () {
                jQuery('#commentnavi').slideDown()
            })
        })
    });
    jQuery('#trackbacktab').click(function () {
        jQuery(this).removeClass('tab').addClass('curtab');
        jQuery('#commenttab').removeClass('curtab').addClass('tab');
        jQuery('#commentnavi').slideUp();
        jQuery('#thecomments').fadeOut(function () {
            jQuery('#thetrackbacks').fadeIn()
        })
    });
    jQuery('#show_author_info').toggle(function () {
        jQuery('#author_info').slideDown();
        jQuery(this).html('Close &raquo;')
    }, function () {
        jQuery('#author_info').slideUp();
        jQuery(this).html('Change &raquo;');
        jQuery('#welcome-row strong').html(jQuery('#author').val())
    });
    if (!jQuery.browser.opera) {
        var id = /^#comment-/;
        var at = /^@/;
        jQuery('#thecomments li p a').each(function () {
            if (jQuery(this).attr('href').match(id) && jQuery(this).text().match(at)) {
                jQuery(this).addClass('atreply')
            }
        });
        jQuery('.atreply').hover(function () {
            var target = this;
            var _commentId = jQuery(this).attr('href');
            if (jQuery(_commentId).is('.comment')) {
                jQuery('<li class="comment tip"></li>').hide().html('<div class="arrow"></div><div class="innerbox">' + jQuery(_commentId).html() + '</div>').appendTo(jQuery('#thecomments'));
                jQuery('#thecomments .tip').css({
                    left: jQuery().cumulativeOffset(this)[0] + jQuery(this).width() + 10,
                    top: jQuery().cumulativeOffset(this)[1] - 22
                }).show({
                    opacity: '1'
                })
            } else {
                var id = _commentId.slice(9);
                jQuery.ajax({
                    type: 'GET',
                    url: '?action=load_comment&id=' + id,
                    cache: false,
                    dataType: 'html',
                    contentType: 'application/json; charset=utf-8',
                    beforeSend: function () {
                        jQuery('<li class="comment tip"></li>').hide().html('<div class="arrow"></div><div class="innerbox"><p class="ajax-loader msg">Loading...</p></div>').appendTo(jQuery('#thecomments'));
                        jQuery('#thecomments .tip').css({
                            left: jQuery().cumulativeOffset(target)[0] + jQuery(target).width() + 10,
                            top: jQuery().cumulativeOffset(target)[1] - 22
                        }).show({
                            opacity: '1'
                        })
                    },
                    success: function (data) {
                        var addedComment = jQuery(data + '</li>');
                        addedComment.hide().appendTo(jQuery('#thecomments'));
                        jQuery('#thecomments .tip .innerbox').html(addedComment.html())
                    },
                    error: function () {
                        jQuery('#thecomments .tip .innerbox').html('<p class="msg">Oops, failed to load data.</p>')
                    }
                })
            }
        }, function () {
            jQuery('#thecomments .tip').fadeOut(400, function () {
                jQuery(this).remove()
            })
        })
    }
});
