(function ($) {
    $.fn.equalHeight = function (child, layouts, itemsInRow, forceOneRow) {
        return this.each(function() {
            if (itemsInRow <= 1) return;
            var heights = {}, row = 0, clone;

            clone = $(this).clone();
            $(this).parent().append(clone);

            $(child, clone).each(function(i, item) {
                $(item).parent().show();
                var layout = $(layouts, item).children().eq(0);
                layout.children().each(function(index, child) {
                    $(child).css('height', ''); // clears previous value
                    var clsNames = $(child).attr('class'),
                        isAbsElm = ('absolute' === $(child).css('position').toLowerCase()),
                        h = !isAbsElm ? $(child).get(0).clientHeight : '-', parts, parts2, re;
                    if (!heights[row]) {
                        heights[row] = [];
                    }
                    if (!heights[row][index]) {
                        heights[row][index] = h + ':' + clsNames;
                    } else if (heights[row][index]) {
                        parts = heights[row][index].split(':');
                        if (parts[1] === clsNames) {
                            if (h > parts[0])
                                heights[row][index] = h + ':' + clsNames;
                        }
                        if (parts[1] !== clsNames) {
                            var str = (heights[row]).join(',');
                            if (-1 === str.indexOf(clsNames)) {
                                (heights[row]).splice(index, 0, h + ':' + clsNames);
                            } else {
                                re = new RegExp('(\\d+):' + clsNames,'i');
                                parts2 = str.match(re);
                                if (h > parts2[1])
                                    heights[row] = str.replace(re, h + ':' + clsNames).split(',');
                            }

                        }
                    }
                });
                if ((i + 1) % itemsInRow === 0 && !forceOneRow) row++;
            });
            clone.remove();

            row = 0;
            $(child, this).each(function(i, item) {
                var layout = $(layouts, item).children().eq(0),
                    h, classNames, parts, current, counter, element, newDiv;

                for(counter = 0; counter <  heights[row].length; counter++) {
                    current = heights[row][counter];
                    parts = current.split(':');
                    h = parts[0];
                    if ('-' === h)
                        continue;
                    classNames = '.' + parts[1].split(' ').join('.');
                    if ($(classNames, layout).length) {
                        $(classNames, layout).height(h);
                    } else {
                        newDiv = document.createElement('div');
                        $(newDiv).height(h);
                        if (counter < layout.children('div').length){
                            element = layout.children('div').eq(counter);
                            element.before($(newDiv));
                        } else {
                            layout.append($(newDiv));
                        }
                    }
                }

                if ((i + 1) % itemsInRow === 0 && !forceOneRow) row++;
            });
        });
    };
    $.fn.equalImageHeight = function () {
        return this.each(function() {
            var maxHeight = 0;

            $(this).children('a').children('img').each(function(index, child) {
                var h = $(child).height();
                maxHeight = h > maxHeight ? h : maxHeight;
                $(child).css('height', ''); // clears previous value
            });

            $(this).children('a').each(function(index, child) {
                $(child).height(maxHeight);
            });

        });
    };

    $.fn.equalColumnsHeight = function () {
        return this.each(function() {
            var cols =  $(this).children('div[class*="col-"]').css('height', '');
            cols.height($(this).height());
        });
    };

    $(function(){
        $('div.bd-layout-auto-height').equalColumnsHeight();
        $(window).resize(function(){
            $('div.bd-layout-auto-height').equalColumnsHeight();
        });
    });
})(jQuery);

function productsGridEqualHeight(selector, child, layouts, itemsInRow, forceOneRow) {
    'use strict';

    var deferreds = [];
    jQuery(child).find('img').each(function () {
        if (this.complete)
            return;
        var deferred = jQuery.Deferred();
        jQuery(this).load(deferred.resolve);
        deferreds.push(deferred.promise());
    });
    jQuery.when.apply(jQuery, deferreds).done(function () {
        jQuery(selector).equalHeight(
            child,
            layouts,
            itemsInRow,
            forceOneRow
        );
    });
}

/*!
 * jQuery Cookie Plugin v1.4.0
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2013 Klaus Hartl
 * Released under the MIT license
 */
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else {
        factory(jQuery);
    }
}(function ($) {

    var pluses = /\+/g;

    function encode(s) {
        return config.raw ? s : encodeURIComponent(s);
    }

    function decode(s) {
        return config.raw ? s : decodeURIComponent(s);
    }

    function stringifyCookieValue(value) {
        return encode(config.json ? JSON.stringify(value) : String(value));
    }

    function parseCookieValue(s) {
        if (s.indexOf('"') === 0) {
            s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
        }

        try {
            s = decodeURIComponent(s.replace(pluses, ' '));
        } catch(e) {
            return;
        }

        try {
            return config.json ? JSON.parse(s) : s;
        } catch(e) {}
    }

    function read(s, converter) {
        var value = config.raw ? s : parseCookieValue(s);
        return $.isFunction(converter) ? converter(value) : value;
    }

    var config = $.cookie = function (key, value, options) {

        // Write
        if (value !== undefined && !$.isFunction(value)) {
            options = $.extend({}, config.defaults, options);

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            return (document.cookie = [
                encode(key), '=', stringifyCookieValue(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '',
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        var result = key ? undefined : {};
        var cookies = document.cookie ? document.cookie.split('; ') : [];

        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            var name = decode(parts.shift());
            var cookie = parts.join('=');

            if (key && key === name) {
                result = read(cookie, value);
                break;
            }

            if (!key && (cookie = read(cookie)) !== undefined) {
                result[name] = cookie;
            }
        }

        return result;
    };

    config.defaults = {};

    $.removeCookie = function (key, options) {
        if ($.cookie(key) !== undefined) {
            $.cookie(key, '', $.extend({}, options, { expires: -1 }));
            return true;
        }
        return false;
    };

}));
