$(function () {
    var label = $('.accordion .nutrition-label');
    if (label.length) {
        if (label.height() > 0) {
            accordion.init();
        } else {
            label.load(function () {
                accordion.init();
            });
        }
    } else {
        accordion.init();
    }
    window.load = function () {
        accordion.init();
    }
})
var accordion = {
    initialized: false,
    openDictionary: {},
    init: function () {
        if (accordion.initialized) return;
        accordion.initialized = true;
        $(".accordion li a").bind("click", function () {
            var li = $($(this).parent());
            if (!li.hasClass("expanded")) {
                accordion.closeAll(500);
                accordion.open(li);
                accordion.resetListItemY(500);
                accordion.setContainerHeight();
            }
            return false;
        })
        $(".accordion>ul>li").each(function () {
            accordion.openDictionary[$(this)] = $(this).height();

            if (!$(this).hasClass('expanded')) {
                if ($.browser.webkit) {
                    accordion.closeItem($(this), 0);

                } else {
                    $(this).find('.accordion-details').hide();
                }
            };
        })
        if ($.browser.webkit) {
            $(".accordion>ul").css("border-bottom", "none");
        }
        accordion.resetListItemY(0);
        accordion.setContainerHeight();
    },
    open: function (li) {
        li.addClass("expanded");
        if (!$.browser.webkit) {
            li.find('.accordion-details').slideDown();
        } else {
            var list = li;
            var detail = list.find('.accordion-details');
            var domObj = detail.get(0);
            domObj.style.webkitTransitionProperty = '-webkit-transform';
            domObj.style.webkitTransitionTimingFunction = 'cubic-bezier(0,0,0.25,1)';
            domObj.style.webkitTransitionDuration = '500ms';
            domObj.style.webkitTransform = 'translate3d(0, 0, 0)';
        }
    },
    closeAll: function (time) {
        $(".accordion li.expanded").removeClass("expanded");
        if (!$.browser.webkit) {
            $(".accordion>ul>li .accordion-details").slideUp();
        } else {
            $(".accordion>ul>li").each(function () {
                var list = $(this);
                accordion.closeItem(list, time);
            })
        }
    },
    closeItem: function (item, time) {
        var list = item;
        var detail = list.find('.accordion-details');
        var domObj = detail.get(0);
        domObj.style.webkitTransitionProperty = '-webkit-transform';
        domObj.style.webkitTransitionTimingFunction = 'cubic-bezier(0,0,0.25,1)';
        domObj.style.webkitTransitionDuration = time + 'ms';
        domObj.style.webkitTransform = 'translate3d(0, -' + detail.outerHeight() + 'px, 0)';
    },
    resetListItemY: function (time) {
        if (!$.browser.webkit)
            return;
        var totalHeight = 0;
        var headerHeight = $('.accordion>ul>li:first-child>a:first-child').outerHeight();
        $(".accordion>ul>li").each(function () {

            var domObj = $(this)[0];
            domObj.style.webkitTransitionProperty = '-webkit-transform';
            domObj.style.webkitTransitionTimingFunction = 'cubic-bezier(0,0,0.25,1)';
            domObj.style.webkitTransitionDuration = time + 'ms';
            domObj.style.webkitTransform = 'translate3d(0, -' + totalHeight + 'px, 0)';
            if (!$(this).hasClass('expanded')) {
                totalHeight += $(this).find('.accordion-details').outerHeight() + 6;
            }
        })
    },
    setContainerHeight: function () {
        if (!$.browser.webkit) return;
        var maxHeight = 0;
        var totalHeight = 0;
        var headerHeight = $('.accordion>ul>li:first-child>a:first-child').outerHeight();
        $(".accordion>ul>li").each(function () {
            totalHeight += headerHeight;
            var h = $(this).find('.accordion-details').outerHeight();

            if ($(this).hasClass("expanded")) {
                maxHeight = h;
            }
            /*
            if (h > maxHeight) {
            maxHeight = h;
            }
            */
        })
        totalHeight += maxHeight;
        $('.accordion>ul').animate({ 'height': totalHeight }, 300);
        //$('.accordion>ul').height(totalHeight);
    }
}
