history_edited = false;

function
add_history(review_id, review_title, size)
{
    if (! can_edit_history())
        return;

    // Construct title for the history state
    var old_title = document.title;
    var new_title = review_title;
    if (size == 'full')
        new_title += " summary";
    new_title += old_title.replace(/^[^-]+ -/i, " -"); // append old title except stuff before the first -

    // Add or replace history state
    if (! history_edited) {
        // first review expansion
        history.pushState({}, new_title, '#' + review_id);
        history_edited = true;
    } else {
        history.replaceState({}, new_title, '#' + review_id);
    }
}

function
autoexpand_review(id)
{
    if (!window.location.hash)
        return;
    var anchor = window.location.hash.substring(1);
    if (! anchor)
        return;
    var review_a = $("a#" + anchor);
    if (! review_a)
        return;
    review_a.click();
    return;
} 

function
fetch_review(link, last_tr, size)
{
    // Show "loading..." message.
    var class_str = size == 'mini' ? ' mini-review' : '';
    var new_tr = $("<tr class='inline-review'><td colspan='6' style='padding: 0; border-top: 1px solid #efe9c9;'>"
            + "<div style='display: none;' class='outmost'>"
                + "<div class='inmost review-list' style='height: 100px'>"
                    + "<table height='100%'><tr>"
                        + "<td valign='middle'><img src='" + images_url + "/common/Spinning_wheel_throbber.gif' valign='middle'/></td>"
                        + "<td valign='middle' style='font-size: 150%; color: #666;'>Loading...</td>"
                    + "</tr></table>"
                + "</div>"
            + "</div>"
        + "</td></tr>",
        document);
    last_tr.after(new_tr);
    var outmost = $(".outmost", new_tr);
    outmost.slideDown(500);
    var css_display = new_tr.css("display"); // on standards compliant browsers this is 'table-row', but 'block' on at least IE7

    // Fetch inline review and slide into view.
    var fetched_review = $.ajax({
        type: "GET",
        url: link.attr("href") + "?format=inline",
        success: function(review){
            var inmost = $(".inmost", new_tr);
            outmost
                // Stop animation
                .stop(true, false)
                .css('display', 'block') // it's necessary that div.outmost is visible for height() to work

                // Fix div.outmost's height so it won't resize when div.inmost is injected.
                .css("height", "104px")
                .css("overflow", "hidden");
            
            // Inject inline review and slide div.outmost to target height.
            inmost.html(review)
                  .css("height", "");
            outmost.animate({
                    "height": inmost.outerHeight(true) + "px"
                }, 500, 'linear', function(){
                    // Reset overflow and height attribute rules. If the code above is buggy on some browser, this will ensure sane end result.
                    outmost
                        .css("overflow", "visible")
                        .css("height", "");
                    link.toggle(function(){ hide_review(new_tr) },
                                   function(){ show_review(new_tr, css_display) });
                });

        }
    });
    return false;
}

function
show_review(tr, css_display)
{
    var fetched_div = $("div.outmost", tr);
    fetched_div.stop(true, true);
    tr.css("display", css_display);
    fetched_div.slideDown("slow");
    return false;
}

function
hide_review(tr)
{
    var fetched_div = $("div.outmost", tr);
    fetched_div
        .stop(true, true)
        .slideUp("slow", function(){
            tr.css("display", "none"); 
        });
    return false;
}

// Make review links fetch the review inline via ajax.
function
change_review_links()
{
    // Give columns fixed widths to work around gecko bug that resizes columns when a colspan='<max>' row is cell is added into the table.
    $("#all-reviews th").each(function(){
        $(this).css("width", $(this).width() + "px");
    });

    // Remove fixed widths upon browser resize, to allow for widening of the columns.
    $(window).one('resize', function(){
        $("#all-reviews th").each(function(){
            $(this).css("width", "");
        });
    });

    // Construct hash 'reviews', whose key is review index, and values are objects, whose keys are 'link' and 'last'
    var reviews = new Array();
    var cur;
    $("#all-reviews tr").each(function() {
        var row = $(this);
        var review_link = $("a.review-link", row);
        if (review_link.length) {
            cur = new Object();
            cur.link = review_link;
        }
        if ($("td.last-row", row).length) {
            cur.last = row;
            reviews.push(cur);
        }
    });

    // Attach onclick handler to every review link
    $.each(reviews, function(dummy, o){
        var link = o.link;
        // Attach NOP handler that's used between the time of links first click, and the time that inline review is shown and a new handler attached.
        link.click(function(){ return false; });
        var review_size = link.hasClass('mini-link') ? 'mini' : 'full';
        link.attr("title", "Click to view the review inline")
            .one('click', function(){
                add_history(link.attr('id'), link.text(), review_size);
                fetch_review(link, o.last, review_size);
                return false;
            });
    });
}

$(document).ready(function() {
    change_review_links();
    autoexpand_review();
});


