/*
 *Grid-layouts the item boxes on the list pages.
 */
// function gridify() {
//     // Gets various elements
//     var container = $("div#body");
//     var elt = $("div.box");
//     var eltSmall = elt.not(".enlarged");
//     var eltEnlarged = elt.filter(".enlarged");
// 
//     // Gets various element sizes.
//     var containerTop = container.offset().top;
//     var eltWidth = eltSmall.outerWidth(true);
//     var viewportWidth = $(window).width();
//     viewportWidth = (viewportWidth > (eltWidth * 3))
//             ? viewportWidth : (eltWidth * 3);
//     var columns = Math.floor(viewportWidth / eltWidth); 
// 
//     // Un-absolutize the boxes.
//     // This is a key to this function: we use the CSS regular layout behavior
//     // for float elements to find the best placement for the enlarged box.
//     // Then, we just organize the small boxes around it.
//     elt.css("position", "inherit"); 
// 
//     // Computes the top left cell index occupied by the enlarged box, based on
//     // its position.
//     var enlargedGridYIndex = Math.round(
//         (eltEnlarged.offset().top - containerTop) / eltWidth
//     );
//     var enlargedGridXIndex = Math.round(
//         (eltEnlarged.offset().left) / eltWidth
//     );
// 
//     // Keeps a record of cells occupied by the enlarged box.
//     var excludeX = [
//         enlargedGridXIndex,
//         enlargedGridXIndex + 1,
//     ];
//     var excludeY = [
//         enlargedGridYIndex,
//         enlargedGridYIndex + 1,
//         enlargedGridYIndex + 2,
//     ];
// 
//     // Absolutizes the enlarged item box.
//     eltEnlarged.css({
//         "position": "absolute",
//         "left": eltEnlarged.offset().left + 1,
//         "top": eltEnlarged.offset().top + 1,
//     });
//     //eltSmall.css("display", "none");
//     
//     // Layouts the small boxes around the enlarged box.
//     var count = 0;
//     eltSmall.each(function (index) {
//         while ($.inArray((count % columns), excludeX) !== -1 
//                 && $.inArray(Math.floor(count / columns), excludeY) !== -1) {
//             count += 1;
//         };
//         var left = (count % columns) * eltWidth;
//         var top = containerTop + (Math.floor(count / columns) *eltWidth);
//         $(this).css({
//             //"display": "block",
//             "position": "absolute",
//             "left": left,
//             "top": top,
//         });
//         count += 1;
//     });
//     
//     // Gives the footer a positions since boxes aren't in the flow anymore.
//     var eltLastOffsetY = elt.last().offset().top + elt.last().outerHeight();
//     var eltEnlargedOffsetY = eltEnlarged.offset().top + eltEnlarged.outerHeight();
//     var footerTop = (eltEnlargedOffsetY > eltLastOffsetY) 
//             ? eltEnlargedOffsetY : eltLastOffsetY;
//     $("div#footer").css({
//         "position": "absolute",
//         "top": footerTop,
//         "width": "100%",
//     });
// }

/*
 *Aligns the title and the pagination with the far right item box on the list
 *pages.
 */
function rightAlignElements() {
    //var maxRightOffset = 0;  // Keeps a record of the outer right box.
    var maxRightOffset = 3 * $("div.box").not(".enlarged").first().outerWidth(); // Keeps a record of the outer right box.
    $("div.box").each(function () {
        var thisRight = $(this).offset().left;
        thisRight += $(this).outerWidth();
        if (thisRight > maxRightOffset) {
            maxRightOffset = thisRight;
        };
    }); // Finds the outer right box.

    // Gives the elements to be aligned a new padding.
    var wrapperWidth = $("div#wrapper").width();
    if (wrapperWidth > maxRightOffset){
        $("h1, div#pagination").css("padding-right", (wrapperWidth - maxRightOffset) + "px");
    } else {
        $("h1, div#pagination").css("padding-right", (maxRightOffset) + "px");
    }
}

/*
 *Initial function.
 */
$(document).ready(function () {
    // Re-computes the layout on resize.
    $(window).resize(function () {
        //gridify();
        rightAlignElements();
    });

    // Switches enlarged box on click, and re-computes the layout. 
    //$("div.box").click(function () {
        //$("div.box").removeClass("enlarged");
        //$(this).addClass("enlarged");
        //gridify();
        //rightAlignElements();
    //});

    $("div.box").click(function () {
        url = $("a:first", this).attr('href');
        location.href = url;
    });

    // Computes the layout.
    //gridify();
    rightAlignElements();

    
    // Adapts the height of the blurb element
    $("div.box").each(function(){
        var thisTop = $(this).offset().top;
        var thisBottom = thisTop + $(this).outerHeight() - parseInt($(this).css("padding-bottom")) - 1;
        var blurb = $(this).find("div.blurb");
        if (blurb.length !== 0) { 
            var blurbTop = blurb.offset().top;
            var blurbHeight = thisBottom - blurbTop;
            blurb.height(blurbHeight).css("overflow", "hidden");
            var lines = Math.round(blurbHeight / parseInt(blurb.css("line-height")));
            blurb.find("p").textOverflow({"lines": lines});
        };
    });
    
    //$("dl").textOverflow({"lines": 2});
});

