/* 
 * This handles the expandable gallery list
 */
$(document).ready(function() {
    $('.expandable_footer a').click(function(event) {
        event.preventDefault();
    });
    $('.ruledHeader a').click(function(event) {
        event.preventDefault();
    });


});

$(document).ready(function() {
    $('.ruledHeader a').click(function(event) {
        galleryShrink($(this));
    });
});

$(document).ready(function() {
    $('.expandable').height(0);

    // Get an array of all of the galery items
    $('.ruledHeader').each(function() {
        $galleryItems = $(this).next().find(".gallery_item");
        

        var cookieValue = $.cookies.get('WCP Photo Gallery Selection');
        
        var id = $(this).next().attr('id');

        if (id == cookieValue) {
            $footer = $(this).next().next();
            $footer.css({
                display:"none"
            });
            $(this).next().animate({
                height:'100%'
            }, 'slow');
        } else {

            $count = $galleryItems.size();
            var height = $('.expandable div.gallery_item').height();
            $(this).next().height(height);

        }
        
    });


    $('.expandable_footer a').click(function() {
        $(this).parent().fadeOut("slow");
        $galleryItems = $(this).parent().prev().find('.gallery_item');
        
        $count = $galleryItems.size();

        var numOfRows = Math.round($count/2);
        var height = $galleryItems.height();
        
        height = height * numOfRows;

        $wrapperDiv = $(this).parent().prev();
        $wrapperDiv.animate({
            height:height
        }, "slow");

        $.cookies.set('WCP Photo Gallery Selection', $wrapperDiv.attr('id'),  { 
            expires: 1
        });


    });
});

function countElements($element, $selector) {
    $items = $element.find($selector);
    return $items.size();
}


function galleryShrink($this) {
    $galleryItems = $this.next().find(".gallery_item");
    var height = $('.expandable div.gallery_item').height();

    $this.parent().next().animate({
        height:height
    }, "slow");
    
    $this.parent().next().next('.expandable_footer').fadeIn("slow");
    $.cookies.del('WCP Photo Gallery Selection');
}