﻿Type.registerNamespace('Ajax.Data.Controls');

Ajax.Data.Controls.PagerPageChangeEventArgs = function(newPage)
{
    var e = Function._validateParams(arguments, [{name: "newPage", type: Number}]);
    if (e) throw e;

    this._newPage = newPage;
    Ajax.Data.Controls.PagerPageChangeEventArgs.initializeBase(this);
}

Ajax.Data.Controls.PagerPageChangeEventArgs.prototype =
{
    get_newPage : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._newPage;
    }
}

Ajax.Data.Controls.PagerPageChangeEventArgs.registerClass('Ajax.Data.Controls.PagerPageChangeEventArgs', Sys.EventArgs);

Ajax.Data.Controls.Pager = function(element)
{
    this._infoStyle = null;
    this._currentPageStyle = null;
    this._otherPageStyle = null;

    this._firstText = '<<';
    this._lastText = '>>';
    this._previousText = '<';
    this._nextText = '>';

    this._showTip = true;
    this._showInfo = false;
    this._showFirstAndLast = true;
    this._showPreviousAndNext = false;
    this._showNumbers = true;
    this._rowPerPage = 10;
    this._rowCount = 0;
    this._currentPage = 1;
    this._useSlider = true;
    this._sliderSize = 8;
    this._hideOnSinglePage = true;

    this._links = new Array();
    this._callbacks = new Array();

    Ajax.Data.Controls.Pager.initializeBase(this, [element]);
}

Ajax.Data.Controls.Pager.prototype = 
{
    get_pageCount : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        var rowCount = this.get_rowCount();
        var rowPerPage = this.get_rowPerPage();

        if ((rowCount == 0) || (rowPerPage == 0))
        {
            return 0;
        }

        if ((rowCount % rowPerPage) == 0)
        {
            return (rowCount / rowPerPage);
        }
        else
        {
            var result = (rowCount / rowPerPage);

            result = Math.floor(result) + 1;

            return result;
        }
    },

    get_cssClass : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this.get_element().className;
    },

    set_cssClass : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: String}]);
        if (e) throw e;

        var target = this.get_element();

        if (!Sys.UI.DomElement.containsCssClass(target, value))
        {
            Sys.UI.DomElement.addCssClass(target, value);
            this.raisePropertyChanged('cssClass');
        }
    },

    get_infoStyle : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._infoStyle;
    },

    set_infoStyle : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Ajax.Data.Controls.Style}]);
        if (e) throw e;

        if (this._infoStyle != value)
        {
            this._infoStyle = value;
            this.raisePropertyChanged('infoStyle');
        }
    },

    get_currentPageStyle : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._currentPageStyle;
    },

    set_currentPageStyle : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Ajax.Data.Controls.Style}]);
        if (e) throw e;

        if (this._currentPageStyle != value)
        {
            this._currentPageStyle = value;
            this.raisePropertyChanged('currentPageStyle');
        }
    },

    get_otherPageStyle : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._otherPageStyle;
    },

    set_otherPageStyle : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Ajax.Data.Controls.Style}]);
        if (e) throw e;

        if (this._otherPageStyle != value)
        {
            this._otherPageStyle = value;
            this.raisePropertyChanged('otherPageStyle');
        }
    },

    get_showTip : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._showTip;
    },

    set_showTip : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Boolean}]);
        if (e) throw e;

        if (this._showTip != value)
        {
            this._showTip = value;
            this.raisePropertyChanged('showTip');
            this._render();
        }
    },

    get_showInfo : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._showInfo;
    },

    set_showInfo : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Boolean}]);
        if (e) throw e;

        if (this._showInfo != value)
        {
            this._showInfo = value;
            this.raisePropertyChanged('showInfo');
            this._render();
        }
    },

    get_showFirstAndLast : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._showFirstAndLast;
    },

    set_showFirstAndLast : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Boolean}]);
        if (e) throw e;

        if (this._showFirstAndLast != value)
        {
            this._showFirstAndLast = value;
            this.raisePropertyChanged('showFirstAndLast');
            this._render();
        }
    },

    get_firstText : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._firstText;
    },

    set_firstText : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: String}]);
        if (e) throw e;

        if (this._firstText != value)
        {
            this._firstText = value;
            this.raisePropertyChanged('firstText');
            this._render();
        }
    },

    get_lastText : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._lastText;
    },

    set_lastText : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: String}]);
        if (e) throw e;

        if (this._lastText != value)
        {
            this._lastText = value;
            this.raisePropertyChanged('lastText');
            this._render();
        }
    },

    get_showPreviousAndNext : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._showPreviousAndNext;
    },

    set_showPreviousAndNext : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Boolean}]);
        if (e) throw e;

        if (this._showPreviousAndNext != value)
        {
            this._showPreviousAndNext = value;
            this.raisePropertyChanged('showPreviousAndNext');
            this._render();
        }
    },

    get_previousText : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._previousText;
    },

    set_previousText : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: String}]);
        if (e) throw e;

        if (this._previousText != value)
        {
            this._previousText = value;
            this.raisePropertyChanged('previousText');
            this._render();
        }
    },

    get_nextText : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._nextText;
    },

    set_nextText : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: String}]);
        if (e) throw e;

        if (this._nextText != value)
        {
            this._nextText = value;
            this.raisePropertyChanged('nextText');
            this._render();
        }
    },

    get_showNumbers : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._showNumbers;
    },

    set_showNumbers : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Boolean}]);
        if (e) throw e;

        if (this._showNumbers != value)
        {
            this._showNumbers = value;
            this.raisePropertyChanged('showNumbers');
            this._render();
        }
    },

    get_rowPerPage : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._rowPerPage;
    },

    set_rowPerPage : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Number}]);
        if (e) throw e;

        if (value < 1)
        {
            throw Error.argument('value','rowPerPage must be a positive integer.');
        }

        if (this._rowPerPage != value)
        {
            this._rowPerPage = value;
            this.raisePropertyChanged('rowPerPage');
            this._render();
        }
    },

    get_rowCount : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._rowCount;
    },

    set_rowCount : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Number}]);
        if (e) throw e;

        if (value < 0)
        {
            throw Error.argument('value','rowCount must be a positive integer.');
        }

        if (this._rowCount != value)
        {
            this._rowCount = value;
            this.raisePropertyChanged('rowCount');
            this._render();
        }
    },

    get_currentPage : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._currentPage;
    },

    set_currentPage : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Number}]);
        if (e) throw e;

        if (value < 1)
        {
            throw Error.argument('value','currentPage must be a positive integer.');
        }

        if (this._currentPage != value)
        {
            this._currentPage = value;
            this.raisePropertyChanged('currentPage');
            this._render();
        }
    },

    get_useSlider : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._useSlider;
    },

    set_useSlider : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Boolean}]);
        if (e) throw e;

        if (this._useSlider != value)
        {
            this._useSlider = value;
            this.raisePropertyChanged('useSlider');
            this._render();
        }
    },

    get_sliderSize : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._sliderSize;
    },

    set_sliderSize : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Number}]);
        if (e) throw e;

        if (value < 1)
        {
            throw Error.argument('value','sliderSize must be a positive integer.');
        }

        if (this._sliderSize != value)
        {
            this._sliderSize = value;
            this.raisePropertyChanged('sliderSize');
            this._render();
        }
    },

    get_hideOnSinglePage : function()
    {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._hideOnSinglePage;
    },

    set_hideOnSinglePage : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: Boolean}]);
        if (e) throw e;

        if (this._hideOnSinglePage != value)
        {
            this._hideOnSinglePage = value;
            this.raisePropertyChanged('hideOnSinglePage');
            this._render()
        }
    },

    initialize : function()
    {
        Ajax.Data.Controls.Pager.callBaseMethod(this, 'initialize');
        this._render();
    },

    dispose : function()
    {
        this._clearLinkHandlers();

        delete this._callbacks;
        delete this._links;

        delete this._infoStyle;
        delete this._currentPageStyle;
        delete this._otherPageStyle;

        Ajax.Data.Controls.Pager.callBaseMethod(this, 'dispose');
    },

    add_pageChange : function(handler)
    {
        var e = Function._validateParams(arguments, [{name: 'handler', type: Function}]);
        if (e) throw e;

        this.get_events().addHandler('pageChange', handler);
    },

    remove_pageChange : function(handler)
    {
        var e = Function._validateParams(arguments, [{name: 'handler', type: Function}]);
        if (e) throw e;

        this.get_events().removeHandler('pageChange', handler);
    },

    _raisePageChange : function(e, context)
    {
        var handler = context.sender.get_events().getHandler('pageChange');

        if (handler)
        {
            handler(context.sender, new Ajax.Data.Controls.PagerPageChangeEventArgs(context.page));
        }
    },

    _clearLinkHandlers : function()
    {
        for(var i = this._links.length - 1; i > -1; i--)
        {
            $removeHandler(this._links[i], 'click', this._callbacks[i]);
            delete this._links[i];
            delete this._callbacks[i];
        }

        Array.clear(this._links);
        Array.clear(this._callbacks);
    },

    _render : function()
    {
        var div = this.get_element();

        this._clearLinkHandlers();
        this._clearContent(div);

        if (this.get_rowPerPage == 0)
        {
            return;
        }

        var rowCount = this.get_rowCount();

        if (rowCount == 0)
        {
            return;
        }

        var currentPage = this.get_currentPage();

        if (currentPage == 0)
        {
            return;
        }

        var pageCount = this.get_pageCount();

        if (this.get_hideOnSinglePage() && (pageCount == 1))
        {
            div.style.display = 'none';
        }
        else
        {
            if (this.get_showInfo())
            {
                var span = document.createElement('span');

                if (this.get_infoStyle() != null)
                {
                    this.get_infoStyle().apply(span);
                }

                var info = 'Page ' + currentPage.toString() + ' of ' + pageCount.toString();
                this._setText(span, info);

                div.appendChild(span);
            }

            if ((this.get_showFirstAndLast()) && (currentPage > 1))
            {
                div.appendChild(this._createFirst());
            }

            if ((this.get_showPreviousAndNext()) && (currentPage > 1))
            {
                div.appendChild(this._createPrevious());
            }

            if (this.get_showNumbers())
            {
                var start = 1;
                var end = pageCount;
                var sliderSize = this.get_sliderSize();

                if ((this.get_useSlider()) && (sliderSize > 0))
                {
                    var half = Math.floor(((sliderSize - 1) / 2));

                    var above = (currentPage + half) + ((sliderSize - 1) % 2);
                    var below = (currentPage - half);

                    if (below < 1)
                    {
                        above += (1 - below);
                        below = 1;
                    }

                    if (above > pageCount)
                    {
                        below -= (above - pageCount);

                        if (below < 1)
                        {
                            below = 1;
                        }

                        above = pageCount;
                    }

                    start = below;
                    end = above;
                }

                for (var i = start; i <= end; i++)
                {
                    if (i == currentPage)
                    {
                        div.appendChild(this._createCurrent());
                    }
                    else
                    {
                        div.appendChild(this._createOther(i));
                    }
                }
            }

            if ((this.get_showPreviousAndNext()) && (currentPage < pageCount))
            {
                div.appendChild(this._createNext());
            }

            if ((this.get_showFirstAndLast()) && (currentPage < pageCount))
            {
                div.appendChild(this._createLast());
            }
        }
    },

    _createFirst : function()
    {
        var span = document.createElement('span');
        var tip = '';

        if (this.get_showTip())
        {
            tip = 'to first page';
        }

        var a = this._createLink(1, this.get_firstText(), tip);

        if (this.get_otherPageStyle() != null)
        {
            this.get_otherPageStyle().apply(span);
        }

        span.appendChild(a);

        return span;
    },

    _createPrevious : function()
    {
        var span = document.createElement('span');
        var previousPage = this.get_currentPage() - 1;
        var tip = '';

        if (this.get_showTip())
        {
            tip = 'Back to page ' + previousPage.toString();
        }

        var a = this._createLink(previousPage, this.get_previousText(), tip);

        if (this.get_otherPageStyle() != null)
        {
            this.get_otherPageStyle().apply(span);
        }

        span.appendChild(a);

        return span;
    },

    _createCurrent : function()
    {
        var span = document.createElement('span');
        var currentPage = this.get_currentPage()

        this._setText(span, currentPage.toString());

        if (this.get_showTip())
        {
            span.title = this._buildTip(currentPage);
        }

        if (this.get_currentPageStyle() != null)
        {
            this.get_currentPageStyle().apply(span);
        }

        return span;
    },

    _createOther : function(page)
    {
        var span = document.createElement('span');
        var tip = '';

        if (this.get_showTip())
        {
            tip = this._buildTip(page);
        }

        var a = this._createLink(page, page.toString(), tip);

        if (this.get_otherPageStyle() != null)
        {
            this.get_otherPageStyle().apply(span);
        }

        span.appendChild(a);

        return span;
    },

    _createNext : function()
    {
        var span = document.createElement('span');
        var nextPage = this.get_currentPage() + 1;
        var tip = '';

        if (this.get_showTip())
        {
            tip = 'Next to page ' + nextPage.toString();
        }

        var a = this._createLink(nextPage, this.get_nextText(), tip);

        if (this.get_otherPageStyle() != null)
        {
            this.get_otherPageStyle().apply(span);
        }

        span.appendChild(a);

        return span;
    },

    _createLast : function()
    {
        var span = document.createElement('span');

        var tip = '';

        if (this.get_showTip())
        {
            tip = 'to last page';
        }

        var a = this._createLink(this.get_pageCount(), this.get_lastText(), tip);

        if (this.get_otherPageStyle() != null)
        {
            this.get_otherPageStyle().apply(span);
        }

        span.appendChild(a);

        return span;
    },

    _createLink : function(page, text, tip)
    {
        var a = document.createElement('a');
        a.appendChild(document.createTextNode(text));
        a.href = 'javascript:void(0)';

        if (tip.length > 0)
        {
            a.title = tip;
        }

        var clickCallback = Function.createCallback(this._raisePageChange, {sender:this, page:page});

        $addHandler(a, 'click', clickCallback);

        this._links.push(a);
        this._callbacks.push(clickCallback);

        return a;
    },

    _buildTip : function(page)
    {
        var rowPerPage = this.get_rowPerPage();
        var rowCount = this.get_rowCount();

        var start = (((page - 1) * rowPerPage) + 1);
        var end = (page == this.get_pageCount()) ? rowCount : (page * rowPerPage);

        var tip = new Sys.StringBuilder();

        if (page == this.get_currentPage())
        {
            tip.append('Showing result ');
        }
        else
        {
            tip.append('Show result ');
        }

        tip.append(start.toString());
        tip.append(' to ');
        tip.append(end.toString());
        tip.append(' of ');
        tip.append(rowCount.toString());

        return tip.toString();
    },

    _setText : function(element, text)
    {
        if (typeof element.innerText != 'undefined')
        {
            element.innerText = text;
        }
        else if (typeof element.textContent != 'undefined')
        {
            element.textContent = text;
        }
    },

    _clearContent : function(element)
    {
        if (element.firstChild)
        {
            while(element.firstChild)
            {
                element.removeChild(element.firstChild);
            }
        }
    }
}

Ajax.Data.Controls.Pager.registerClass('Ajax.Data.Controls.Pager', Sys.UI.Control);

if (typeof(Sys) != 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}