﻿$(function () {

    var products = $(".product_info");
    var productCount = products.size();
    var innerrows = productCount - (productCount % 3);
    if (productCount < 3) innerrows = productCount;
    else if (innerrows == productCount) innerrows = productCount - 3;

    var height = 0;
    var productTitleH = 0;
    var productIntroH = 0;
    var counter = 0;
    products.each(function () {
        counter++;
        if (counter <= 3) {
            $(this).css({
                paddingTop: '0px'
            });
        }


        if (height < $(this).height()) {
            height = $(this).height();
        }

        if (productTitleH < $(".product_title", this).height()) {
            productTitleH = $(".product_title", this).height();
        }

        if (productIntroH < $(".product_intro", this).height()) {
            productIntroH = $(".product_intro", this).height();
        }


        if (counter % 3 == 0) {

            $(this).height(height);
            $(this).prev().height(height);
            $(this).prev().prev().height(height);

            $(".product_title", this).height(productTitleH);
            $(".product_title", $(this).prev()).height(productTitleH);
            $(".product_title", $(this).prev().prev()).height(productTitleH);

            $(".product_intro", this).height(productIntroH);
            $(".product_intro", $(this).prev()).height(productIntroH);
            $(".product_intro", $(this).prev().prev()).height(productIntroH);

            height = 0;
            productTitleH = 0;
            productIntroH = 0;

            $(this).css({
                borderRight: 'none'
            });
        }

        if (counter > innerrows) {
            $(this).css({
                paddingBottom: '0px',
                borderBottom: 'none',
                marginBottom: '50px'
            });
        }



        if (counter == productCount && (productCount % 3) != 0 && ((productCount % 3 < innerrows) || productCount == innerrows)) {

            var currProduct = $(this);
            var lastItems = productCount - innerrows;
            if (productCount == innerrows) lastItems = productCount;
            for (var i = lastItems; i > 0; i--) {
                currProduct.height(height);
                $(".product_title", currProduct).height(productTitleH);
                $(".product_intro", currProduct).height(productIntroH);

                currProduct = currProduct.prev();

            }

        }

    });
});
