$(function () {
if( !$("#top_info:contains('[Log In]')").length ){
    //Version 2.1 - Updated 23 Aug 2010 - See this topic for support: http://resources.zetaboards.com/topic/5086725
    var quote_href = $("blockquote dl dd:contains('http://')").text();
    $("blockquote dl dd:contains('http://')").html("<a href='" + quote_href + "' target='_blank' style='color:inherit'>Original</a>");
    $("a[href*=post/?mode=2&type=2]").before("<input class='mquote_box' type='checkbox'></input>");
    if (location.href.match("topic/")) {
        var topic_id = window.location.pathname.split("topic/")[1].split("/")[0];
        var c = zeta_cookie.get('mquote+' + topic_id);
        if (c.length) {
            $(".mquote_box").each(function () {
                var post_id = $(this).closest("tr").prev().prev().prev().children("td.c_postinfo").children(".right").children("a").text();
                if (c.match(post_id)) {
                    $(this).attr('checked', true);
                }
            });
        }

        $("td.c_footicons .right input").click(function () {
            if (this.checked) {
                var topic_id = window.location.pathname.split("topic/")[1].split("/")[0];
                var poster = $.trim($(this).closest("tr").prev().prev().prev().children("td.c_username").text());
                var post_href = $(this).closest("tr").prev().prev().prev().children("td.c_postinfo").children(".right").children("a").attr('href');
                var post_id = $(this).closest("tr").prev().prev().prev().children("td.c_postinfo").children(".right").children("a").text();
                var quote_link = $('a[href*=post/?mode=2&type=2]');
                $('a[href*=post]').click(function () {
                    $("body").append("<dl class='notice error mquote-error'><dt>Error:</dt><dd><strong>Your browser has had insufficient time to store the posts you wish to quote.</strong> Please try again in a few seconds.</dd></dl>");
                    $(".mquote-error").css({
                        'bottom': '10px',
                        'font-size': '90%',
                        'position': 'fixed',
                        'right': '10px',
                        'text-align': 'left',
                        'width': '200px',
                        'z-index': '1',
                        'cursor': 'pointer'
                    });
                    $(".mquote-error dd").css({
                        'margin': '0'
                    });
                    $(".mquote-error dd").append(" <strong>(Click to dismiss)</strong>");
                    $(".mquote-error").click(function () {
                        $(".notice").fadeOut();
                    });
                    return false;
                });
                $.get($(this).parent().find("a[href*=post/?mode=2&type=2]").attr('href'), function (d) {
                    var current_posts = zeta_cookie.get('mquote+' + topic_id);
                    current_posts += "!iden!" + $('textarea#txt_quote', d).html() + "!idenauth!" + poster + "!idenpid!" + post_href + "!idennum!" + this.url.split(main_url)[1] + "!idenid!" + post_id;
                    zeta_cookie.set('mquote+' + topic_id, current_posts, '2');
                    $('a[href*=post]').unbind('click');

                });

            } else {
                var topic_id = window.location.pathname.split("topic/")[1].split("/")[0];
                var post_id_2 = $(this).closest("tr").prev().prev().prev().children("td.c_postinfo").children(".right").children("a").text();
                z = zeta_cookie.get('mquote+' + topic_id);
                zeta_cookie.set('mquote+' + topic_id, z.replace("!idenid!" + post_id_2, ''), '2');
                var url = $(this).parent().find("a[href*=post/?mode=2&type=2]").attr('href').split(main_url)[1],
                    k = zeta_cookie.get('mquote+' + topic_id).split("!iden!");
                if (k.length === 1) {
                    zeta_cookie.del('mquote+' + topic_id);
                } else {
                    var j = k.length;
                    while (j--) {
                        if (k[j].indexOf('!idennum!' + url) !== -1) {
                            k.splice(j, 1);
                            break;
                        }
                    }
                }
                zeta_cookie.set('mquote+' + topic_id, k.join('!iden!'), '2');
            }
        });
    } else if ($("table.posting:contains('Posting reply')").length) {
        var topic_id_nav = location.href.split("t=")[1].split("&")[0];
        var t = zeta_cookie.get('mquote+' + topic_id_nav),
            aggregate = "",
            i, j;
        if (t !== "") {
            t = t.split('!iden!').reverse();
            i = t.length;
            while (i--) {
                j = t[i].split('!iden');
                if (j[0] && j[1] && j[2]) {
                    aggregate += "[quote=" + j[1].replace('auth!', '') + ", " + j[2].replace('pid!', '') + "]" + j[0] + "[\/quote]\n\n";
                }
                zeta_cookie.del('mquote+' + topic_id_nav);
            }
            var existing_cont = $('#c_post-text').val();
		if (existing_cont.length){
            $('#c_post-text').val(existing_cont +"\n\n"+ aggregate);
                } else {
             $('#c_post-text').val(aggregate);
                }
            $('#txt_quote').val('');
            $('#txt_quote').closest('tr').remove();
            $('th:contains("Quoting:")').remove();
            // Elephant trunks should be used for elephant things only. Nothing else.
        }
    }
}
});


