﻿Type.registerNamespace("PalmspringControls");

PalmspringControls.Pager = function() {
    ///<Summary>    
    ///</Summary>
    this.TotalCount = 0;
    this.PageSize = 10;
    this.CurrentPage = 1;
    this.PagerId = '';
    this.TemplateId = '';
    PalmspringControls.Pager.ObjectArgument.apply(this, arguments);
}

PalmspringControls.Pager.ObjectArgument = function(options) {
    this.TotalCount = options.TotalCount;
    this.PageSize = options.PageSize;
    this.CurrentPage = options.CurrentPage;
    this.GridControlId = options.GridControlId;
    this.Template = options.Template;
    this.$pager = $("#pager", $("#" + this.GridControlId));
    this.OnPageChange = options.OnPageChange;
    this.StartPage = 1;
    this.PagesCount = 5;
    this.LastPage = 1;
}

PalmspringControls.Pager.prototype = {
    initialize: function() {
        //var $pager = $("#" + this.PagerId);
        this.BindPages();



    },
    BindPages: function() {
        var pagerTemplate = this.Template;

        if (this.$pager.length > 0) {
            var pages = (this.TotalCount / this.PageSize) % 2 == 0 ? this.TotalCount / this.PageSize : parseInt(this.TotalCount / this.PageSize) + 1;
            var arrPages = [];
            this.LastPage = pages;
            var endPage = this.StartPage + 4 > pages ? this.StartPage + (pages - this.StartPage) : this.StartPage + 4;
            for (var ctr = this.StartPage; ctr <= endPage; ctr++) {
                arrPages[arrPages.length] = { PageNum: ctr, Title: ctr };
            }
            $(this.$pager).text('');
            $(pagerTemplate).bindTo(arrPages, { appendTo: $(this.$pager) });
        }
        else {
            $(this.$pager).css("display", "none");
        }

        if (typeof this.OnPageChange == 'function') {
            var func = this.OnPageChange;
            var currFunc = this.SetCurrent;
            var pager = this.$pager;
            var pgObj = this;
            this.$pager.find("a").each(function() {
                $(this).bind("click", function() {
                    pgObj.CurrentPage = parseInt($(this).attr("pagenum"));
                    currFunc.call(this, this, pager);
                    func.call(this, $(this).attr("pagenum"));
                    pgObj.BindPrevNext();
                    return false;
                });
            });
        }
        //Set Current Page
        this.SetCurrent(this.$pager.find("a[pagenum=" + this.CurrentPage + "]"), this.$pager);

        //bind previous and next link
        this.BindPrevNext();
    },
    BindPrevNext: function() {
        var func = this.OnPageChange;
        $("span[action='prev']", this.$pager).text('');
        $("span[action='next']", this.$pager).text('');
        var pg = this;


        if (this.CurrentPage == 1)
            $("span[action='prev']", this.$pager).text('Previous');
        else {
            var $prev = $(document.createElement("a"));
            $prev.attr("href", "#");
            $prev.addClass("pagerLink");
            $prev.text("Previous");
            if (typeof this.OnPageChange == 'function') {
                $prev.bind("click", function() {
                    pg.CurrentPage = pg.CurrentPage - 1;
                    if (pg.CurrentPage < 1)
                        pg.CurrentPage = 1;
                    if (pg.CurrentPage < pg.StartPage) {
                        pg.StartPage = pg.StartPage - pg.PagesCount;
                        if (pg.StartPage <= 1)
                            pg.StartPage = 1;
                        //pg.CurrentPage = pg.StartPage;
                    }
                    pg.$pager.children().remove();
                    pg.BindPages();
                    func.call(this, pg.CurrentPage); return false;
                });
            }
            $prev.appendTo($("span[action='prev']", this.$pager));
        }

        if (this.CurrentPage >= this.LastPage)
            $("span[action='next']", this.$pager).text('Next');
        else {
            var $next = $(document.createElement("a"));
            $next.attr("href", "#");
            $next.text("Next");
            $next.addClass("pagerLink");


            if (typeof this.OnPageChange == 'function') {
                $next.bind("click", function() {
                    pg.CurrentPage = pg.CurrentPage + 1;
                    if (pg.CurrentPage > pg.LastPage)
                        pg.CurrentPage = pg.LastPage;
                    if (pg.CurrentPage > (pg.StartPage + pg.PagesCount - 1)) {
                        pg.StartPage = pg.StartPage + pg.PagesCount;
                    }
                    pg.$pager.children().remove();
                    pg.BindPages();
                    //   pg.CurrentPage = pg.StartPage;
                    func.call(this, pg.CurrentPage);
                    return false;
                });
            }
            $next.appendTo($("span[action='next']", this.$pager));
        }
    },
    SetCurrent: function(aLink, pager) {
        $(pager).find("li").removeClass("pagerCurrent");
        $(aLink).parent().addClass("pagerCurrent");

    },
    dispose: function() {
        $clearHandlers(this.$pager.find("a"));
        $("div", this.$pager).remove();
    }
}

PalmspringControls.Pager.registerClass("PalmspringControls.Pager", null, Sys.IDisposable);