var $j = jQuery.noConflict();

$j(document).ready(function () {

    if ($j('.pp-lightbox-i .content').height() < $j('.pp-lightbox-i .image').height()) {
        $j('.pp-lightbox-i .content').height($j('.pp-lightbox-i .image').height());
    }

    if ($j('.b-filter-list').length) {
        $j('.b-filter-list').each(function () {
            $j('.b-filter-list').jqTransform();
            //equalWidth($j(this));
        });
    }

    if ($j('.b-filterspage').length) {
        $j('.b-filterspage').each(function () {
            wrapController($j(this));
            if ($j(this).children('.b-filterspage .f + .col').length) {
                $j(this).children('.col').height($j(this).children('.b-filterspage .f').height());
            }
        });
    }

    if ($j('.b-filterssearch .refine-col').length) {
        $j('.b-filterssearch .refine-col').each(function () {
            searchWrapController($j(this));
        });
    }

    if ($j('.ddlCategory').length) {
        $j('.ddlCategory').jqTransform();
    }

    if ($j('.b-sidecompare').length) {
        $j(".b-sidecompare .info").hover(function () {
            $j(this).parents('.l-col-1').css({ 'position': 'relative', 'z-index': '1' });
            $j(this).parent().parent().addClass('show-tip-wrp');
        },
		function () {
		    $j(this).parents('.l-col-1').removeAttr('style');
		    $j(this).parent().parent().removeClass('show-tip-wrp');
		});
    }

    if ($j('.filefield_holder').length) {
        $j('.filefield_holder').hover(
		function () {
		    $j(this).next().addClass('btnhover');
		},
		function () {
		    $j(this).next().removeClass('btnhover');
		});
        $j('input[type="file"]').change(function () {
            $j(this).parent().next().next().children("input").val($j(this).val());
            //console.log($j(this).parent().next().next().children("input"));
        });
        $j('input[type="file"]').each(function () {
            $j(this).parent().next().next().children("input").val($j(this).val());
        });
    }

    if ($j('.b-products-card ').length) {
        equalSize($j('.b-products-card div.item-i'));
    }

    if ($j('.comparetbl__tbl').length && $j('.comparetbl__tbl .item-td').length) {
        equalSize($j('.comparetbl__tbl .item-td h3'));
        equalSize($j('.comparetbl__tbl .item-td h4'));
        equalSize($j('.comparetbl__tbl .item-td ul.b-colors'));
    }

    // Used in Dorel.Sublayouts.DorelBase.Breadcrumb.ascx
    GenerateBreadcrumb();

    // Used in Dorel.Sublayouts.DorelBase.LastViewedProducts.ascx
    GenerateLastViewedProductsCarousel();

    // Used in Dorel.Sublayouts.DorelBase.ProductPage.ascx
    GenerateWorlAroundPebbleCarousel();

    if ($j('.b-forms').length) {
        $j(".b-forms").jqTransform();
        $j(".b-forms .tip").hover(function () {
            $j(this).parent().addClass('show-tip-wrp');
        },
		function () {
		    $j(this).parent().removeClass('show-tip-wrp');
		});
    }

    if ($j('.b-filternav').length) {
        $j(".b-filternav").jqTransform();
    }
    if ($j('.b-linefilter').length) {
        $j(".b-linefilter").jqTransform();
    }
});

function wrapController($jobject) {
    var letWidth = $jobject.width();
    if ($jobject.children(':not(".f")').length) {
        $jobject.children(':not(".f")').each(function () {
            letWidth = letWidth - $j(this).outerWidth(true);
        });
    }

    var actListWidth = 0;
    $jobject.children('.f').children('.b-filter-list').children('li').each(function () {
        actListWidth = actListWidth + $j(this).outerWidth(true);
    });

    if (actListWidth >= letWidth) {
        equalWidth($jobject.children('.f').children('.b-filter-list'));
        $jobject.children('.f').width(letWidth);
    }
    else { }
}

function searchWrapController($jobject) {
    var filterListWidth = 0;
    if ($jobject.children('.b-filter-list').children('li').length) {
        $jobject.children('.b-filter-list').children('li').each(function () {
            filterListWidth = filterListWidth + $j(this).outerWidth(true);
        });
        if (($jobject.width() + 24) < filterListWidth) {
            equalWidth($jobject.children('.b-filter-list'));
        }
    }
}

function equalSize($jobjectsarray) {
    var maxHeight = 0;
    $jobjectsarray.each(function (i) {
        if ($j(this).height() > maxHeight) {
            maxHeight = $j(this).height();
        }
    });
    if (maxHeight) {
        $jobjectsarray.height(maxHeight);
    }
}

function equalWidth($jobjectsarray) {
    var maxWidth = 0;
    $jobjectsarray.children("li").each(function (i) {
        if ($j(this).width() > maxWidth) {
            maxWidth = $j(this).width();
        }
    });
    if (maxWidth) {
        $jobjectsarray.children("li").width(maxWidth);
    }
}

function GenerateBreadcrumb() {
    if ($j('.pathnav-i').length) {
        if ($j('.path-cats').length) {
            var curIndex = $j('#breadcrumb-current-index').val();

            var letWidth = $j('.pathnav-i').width() - ($j('.pathnav-i').children('.b-path').width() + parseInt($j('.pathnav-i').children('.b-path').css("paddingLeft")));

            var itemWidth = $j('.path-cats').children(':first').width();
            var count = $j('.path-cats').children().length;

            var actualWidth = 0;
            var currentWidth = 0;

            if ($j('.path-cats').hasClass('path-discover')) {
                actualWidth = count * itemWidth + 8;
                currentWidth = curIndex * itemWidth + 8;
            }
            else {
                actualWidth = count * itemWidth - 7;
                currentWidth = curIndex * itemWidth + 8;
            }

            if (letWidth < actualWidth) {
                $j('.path-cats').css('float', 'none');
                $j('.path-cats').wrap('<div class="bc_wrapper"></div>');
                $j('.bc_wrapper').wrap('<div class="bc_cour_wrapper"></div>');
                $j('.bc_cour_wrapper').append('<i class="hrzit-shd-l"></i>');
                $j('.bc_cour_wrapper').append('<i class="hrzit-shd-r"></i>');
                $j('.bc_cour_wrapper').width(letWidth - parseInt($j('.bc_cour_wrapper').css("paddingLeft")) - parseInt($j('.bc_cour_wrapper').css("paddingRight")) - 5);

                if (letWidth < currentWidth) {
                    $j('.path-cats').jcarousel({
                        scroll: 1,
                        start: curIndex
                    });
                } else {
                    $j('.path-cats').jcarousel({
                        scroll: 1
                    });
                }
            }
            else {
                $j('.path-cats').css({
                    'position': 'static',
                    'top': 'auto',
                    'left': 'auto'
                });

            }
        }
    }
}

function GenerateLastViewedProductsCarousel() {
    if ($j('#last_viewed').length) {
        $j('#last_viewed').jcarousel({
            scroll: 1
        });
    }
}

function GenerateWorlAroundPebbleCarousel() {
    if ($j('#world_around_pebble').length) {
        $j('#world_around_pebble').jcarousel({
            scroll: 1
        });
    }
}









