// JavaScript Document
// Change media item display to group by Month Year and sub-group by Date


$(document).ready(function(){

    $("#listing .row").each(function(){

        // Get date components
        var $date = $(this).children("div.date").text();
        var $mthYr   = $date.substring(3);

        // Get URL to asset
        var $link = $(this).children("p").html();

        // Add Month and Year h2 element if it doesn't exist already
        if ( $("#grouped-listing h2:contains('"+$mthYr+"')").length == 0 )
        {
            $("#grouped-listing").append("<h2>"+$mthYr+"</h2>");
        }

        if ( $("#grouped-listing h3:contains('"+$date+"')").length > 0 )
        {
            $("#grouped-listing h3:contains('"+$date+"')").siblings("ul").append("<li>"+$link+"</li>");
        }
        else
        {
            $("#grouped-listing").append("<div><h3>"+$date+"</h3><ul><li>"+$link+"</li></ul></div>");
        }
        $(this).remove();
    });

});


