(function(){
	$(document).ready(function() {

        var currentPage = 0;

        showPage($(".tab-info:first"), 0);

        $("#tab-box-results .tab a").click(function() {
            showPage("div#tab-" + this.rel, 0);
        });

        $(".product-results-pagination .redunderline").click(function() {
            var elem = $(".product-results-pagination .redunderline");
            if (elem.html().indexOf("Show on 1 page") >= 0) {
                elem.parents(".tab-info").find(".product-results").addClass("active");
                elem.html("Show on multiple pages");
                elem.parent().children().css("display", "none");
                elem.css("display", "block");
            } else {
                elem.html("Show on 1 page");
                elem.parent().children().css("display", "block");
                showPage(elem.parents(".tab-info"), 0);
            }
            return false;
        });

        $(".product-results-pagination .pagelink").click(function() {
            var pageNum = $(this).html();
            var pageIndex = pageNum - 1;
            showPage($(this).parents(".tab-info"), pageIndex);
            return false;
        });

        $(".product-results-pagination .prev").click(function() {
            var pageIndex = currentPage - 1;
            showPage($(this).parents(".tab-info"), pageIndex);
            return false;
        });

        $(".product-results-pagination .next").click(function() {
            var pageIndex = currentPage + 1;
            showPage($(this).parents(".tab-info"), pageIndex);
            return false;
        });

        function showPage(tabElem, index) {
            var maxIndex = $(".product-results-pagination:first").find(".pagelink").size() - 1;
            maxIndex = Math.max(maxIndex, 0);
            
            if (index < 0) {
                index = 0;
            }
            if (index > maxIndex) {
                index = maxIndex;
            }

            // Render the pagination links
            var ellipseSpan = '<span style="float:left;width:12px;margin-left:-0px;margin-right:5px;">...</span>';
            $(tabElem).find(".product-results-pagination").each(function() {
                $(this).children(".pagelink").removeClass("active");
                $(this).children(".pagelink").eq(index).addClass("active");

                $(this).children(".pagelink").css("display", "none");
                if (index < 5) {
                    $(this).children(".pagelink:lt(6)").css("display", "block");
                    $(this).children(".pagelink:eq(6)").css("display", "block");
                    $(this).children(".pagelink:gt(6)").css("display", "none");
                    $(this).children(".pagelink:last").css("display", "block");

                    $(this).find("span").remove();
                    $(this).children(".pagelink:eq(6)").after(ellipseSpan);
                } else if (index > maxIndex - 5) {
                    var startIndex = maxIndex - 6;
                    $(this).children(".pagelink:lt(" + startIndex + ")").css("display", "none");
                    $(this).children(".pagelink:eq(" + startIndex + ")").css("display", "block");
                    $(this).children(".pagelink:gt(" + startIndex + ")").css("display", "block");
                    $(this).children(".pagelink:first").css("display", "block");

                    $(this).find("span").remove();
                    $(this).children(".pagelink:eq(" + startIndex + ")").before(ellipseSpan);
                } else {
                    var startIndex = index - 3;
                    var endIndex = index + 2;
                    $(this).children(".pagelink:gt(" + startIndex + ")").css("display", "block");
                    $(this).children(".pagelink:gt(" + endIndex + ")").css("display", "none");
                    $(this).children(".pagelink:first").css("display", "block");
                    $(this).children(".pagelink:last").css("display", "block");

                    $(this).find("span").remove();
                    $(this).children(".pagelink:eq(" + startIndex + ")").before(ellipseSpan);
                    $(this).children(".pagelink:eq(" + endIndex + ")").after(ellipseSpan);
                }
            });

            // Show the page
            $(tabElem).find(".product-results").removeClass("active");
            $(tabElem).find(".product-results:eq(" + index + ")").addClass("active");
            currentPage = index;
        }
        

        // Get all results pages to be the same height
        normalizeTableHeights();
        var normalizeTableHeightTimer;
        $(".product-results img").load(function() {
            clearTimeout(normalizeTableHeightTimer);
            normalizeTableHeightTimer = setTimeout(function() {
                normalizeTableHeights();
            }, 200);

        });

        function normalizeTableHeights() {
            var pages = $(".product-results").get()
            var maxHeight = 0;
            if (pages.length > 1) {
                for (var i = 0; i < pages.length; i++) {
                    var page = pages[i];
                    $(page).css("display", "block");
                    var rows = $(page).find(".product-results-cell").get();
                    for (var j = 0; j < rows.length; j++) {
                        var row = rows[j];
                        var height = $(row).outerHeight();
                        maxHeight = Math.max(maxHeight, height);
                    }
                    $(page).css("display", "");
                }
                for (var i = 0; i < pages.length; i++) {
                    var page = pages[i];
                    $(page).css("height", maxHeight * 3);
                    $(page).find("td").height(Math.floor(maxHeight));
                }
            }
        }

        window.normalizeTableHeights = normalizeTableHeights;
    });
})();
