/**
*
*	Table Pager module by Marius ILIE
*
**/
(function ($) {
    $.fn.addTablePager = function (options) {
        return this.each(function () {
            var defaults = {
                results: 1,
                position: "bottom",
                prevBut: "< PREVIOUS PROMO",
                nextBut: "NEXT PROMO >",
                infos: ""
            };
            var opts = $.extend(defaults, options);
            var table = this;
            $(table).wrap("<div></div>");
            if (opts.position == "top")
                $(table).before("<div class='tablepager-links'></div>");
            else
                $(table).after("<div class='tablepager-links'></div>");
            var container = $(table).parent();
            table.page = 0;
            var maxRows = $("tbody > tr", table).length;
            var totalPages = Math.ceil(maxRows / opts.results);
            if (totalPages > 1) {
                $("div.tablepager-links", container).html("<span class='tablepager-infos'></span><span style='float:left;'><a href='#' class='ArrowPrev tablepager-prev-but' id='ArrowPrevID'>" + opts.prevBut + "</a></span><span style='float:right;'><a href='#' class='ArrowNext tablepager-next-but' id='ArrowNextID'>" + opts.nextBut + "</a></span>");
            }
            $("tbody > tr", table).hide();

            $('#ArrowPrevID').hide();

            for (var i = table.page * opts.results + 1; i <= table.page * opts.results + opts.results; i++) {
                $("tr:nth-child(" + i + ")", table).show();
            }



            $("a.tablepager-next-but", container).click(function () {
                if (table.page < totalPages - 1) {
                    table.page++;
                    $("tbody > tr", table).hide();
                    $('#ArrowPrevID').show();
                    if (table.page == totalPages - 1) {
                        $('#ArrowNextID').hide();
                    }
                    else {
                        $('.spaceWidth').hide();
                    }
                    var nth = 0;
                    for (var i = table.page * opts.results + 1; i <= table.page * opts.results + opts.results; i++) {
                        $("tbody > tr:nth-child(" + i + ")", table).show();
                        nth = i
                    }

                    if ($("tbody > tr:nth-child(" + nth + ")", table).height() < $("#ContentLine1").height() - 181 || $("tbody > tr:nth-child(" + nth + ")", table).height() == $("#ContentLine1").height() - 181) {
                  
                        if ($("#ContentLine1").height() > 800 || $("#ContentLine1").height() == 800) {
                            $("#ContentLine1").height(800);
                            $("tbody > tr:nth-child(" + nth + ")", table).height(619);
                        }
                        else {
                            $("tbody > tr:nth-child(" + nth + ")", table).height($("#ContentLine1").height() - 181);
                        }
                    }
                    else {
                        $("#ContentLine1").height($("tbody > tr:nth-child(" + nth + ")", table).height() + 180);
                        $("tbody > tr:nth-child(" + nth + ")", table).height(619);
                    }
            
                }
                return false;
            })
            $("a.tablepager-prev-but", container).click(function () {
                if (table.page > 0) {
                    table.page--;
                    $("tbody > tr", table).hide();
                    $('#ArrowNextID').show();
                    if (table.page == 0) {
                        $('#ArrowPrevID').hide();
                        $('.spaceWidth').show();
                    }
                    else {
                        $('.spaceWidth').hide();
                    }
                    var nth = 0;
                    for (var i = table.page * opts.results + 1; i <= table.page * opts.results + opts.results; i++) {
                        $("tbody > tr:nth-child(" + i + ")", table).show();
                        nth = i
                    }

                    if ($("tbody > tr:nth-child(" + nth + ")", table).height() < $("#ContentLine1").height() - 181 || $("tbody > tr:nth-child(" + nth + ")", table).height() == $("#ContentLine1").height() - 181) {
                        if ($("#ContentLine1").height() > 800 || $("#ContentLine1").height() == 800) {
                            $("#ContentLine1").height(800);
                            $("tbody > tr:nth-child(" + nth + ")", table).height(619);
                        }
                        else {
                            $("tbody > tr:nth-child(" + nth + ")", table).height($("#ContentLine1").height() - 181);
                        }
                    }
                    else {
                        $("#ContentLine1").height($("tbody > tr:nth-child(" + nth + ")", table).height() + 180);
                        $("tbody > tr:nth-child(" + nth + ")", table).height(619);

                    }
                    
                }
                return false;
            })
        });
    }
})(jQuery);

