$(document).ready(function () {

    $.fn.readmore = function (container) {
        var el, ps, up, totalHeight, container;
        totalHeight = 0;

        el = $(".read-more");
        p = el.parent();
        up = p.parent();
        ps = up.find("ul,h1,h3,p:not('.read-more'),p:not('.read-less')");
        ep = (up.find("p").size() - 1) * 23;
        ps.each(function () {
            totalHeight += $(this).outerHeight();
            // FAIL totalHeight += $(this).css("margin-bottom");
        });

        if (totalHeight > 100) {
            $('.read-more').css({ "display": 'inline' });
            $(".read-more").click(function () {

                if (el.text() == 'more') {
                    // measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)				
                    up.css({ "height": up.height(), "max-height": 9999 }).animate({ "height": (totalHeight + ep) });
                    // fade out read-more
                    el.removeClass('read-more').addClass('read-less').text('less');
                } else {
                    el = $(this);
                    p = el.parent();
                    up = p.parent();
                    el.removeClass('read-less').addClass('read-more').text('more');
                    up.css({ "height": '' }).animate({ "max-height": 9999, "height": container });
                    //return false;	
                };
                // prevent jump-down
                return false;

            });

        };
    };


    $.fn.readmoretabs = function (container) {
        var el, ps, up, totalHeight, container;
        totalHeight = 0;

        el = $(".read-more-tabs");
        p = el.parent();
        up = p.parent();
        ps = up.find("ul,h1,h3,p:not('.read-more-tabs')");
        ps.each(function () {
            totalHeight += $(this).outerHeight();
            // FAIL totalHeight += $(this).css("margin-bottom");
            ep = (up.find("p").size() - 1) * 23;

        });

        if (totalHeight > 256) {
            $('.read-more-tabs').css({ "display": 'inline' });
            $(".read-more-tabs").click(function () {

                if (el.text() == 'more') {
                    // measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)				
                    up.css({ "height": up.height(), "max-height": 9999 }).animate({ "height": (totalHeight + ep) });
                    // fade out read-more
                    el.removeClass('read-more-tabs').addClass('read-less-tabs').text('less');
                } else {
                    el = $(this);
                    p = el.parent();
                    up = p.parent();
                    el.removeClass('read-less-tabs').addClass('read-more-tabs').text('more');
                    up.css({ "height": '' }).animate({ "max-height": 9999, "height": container });
                    //return false;	
                };
                // prevent jump-down
                return false;

            });

        };
    };


});
