Type.registerNamespace('Devy.UI');

Devy.UI.PagesIndex = function() {
    Devy.UI.PagesIndex.initializeBase(this);

    this._PagesCount = 0;

    this._EntityNamePlural = "Items";
    
    this._PageSize = 0;
    this._TotalCount = 0;
    
    this._CurrentPage = 0;
    this._CSSClass = "";
    
    //Miembros
    this._Parent = null;
    this._Container = null;
    this._events = null;
    this._PagesContainer = null;
}

Devy.UI.PagesIndex.prototype = {
    //*********************************************************************
    //Publicos
    set_Parent: function(value) { this._Parent = value; },
    get_Parent: function() { return this._Parent; },

    set_CSSClass: function(value) { this._CSSClass = value; },
    get_CSSClass: function() { return this._CSSClass; },

    set_Container: function(value) { this._Container = value; },
    get_Container: function() { return this._Container; },

    set_PagesCount: function(value) { this._PagesCount = value; },
    get_PagesCount: function() { return this._PagesCount; },

    set_CurrentPage: function(value) { this._CurrentPage = value; },
    get_CurrentPage: function() { return this._CurrentPage; },

    set_EntityNamePlural: function(value) { this._EntityNamePlural = value; },
    get_EntityNamePlural: function() { return this._EntityNamePlural; },
    
    SetUpPages: function(CurrentPage, PagesCount, PageSize, TotalCount) {
        if (!(PagesCount === undefined))
            this._PagesCount = PagesCount;

        if (!(PageSize === undefined))
            this._PageSize = PageSize;

        if (!(TotalCount === undefined))
            this._TotalCount = TotalCount;

        this._CurrentPage = CurrentPage;

        this._setUpInterface();
    },

    initialize: function() {
        Devy.UI.PagesIndex.callBaseMethod(this, 'initialize');
        this._atachEvents();

        this._PagesContainer = $('<div class="PagesIndex ' + this._CSSClass + '"></div>')[0];
        this._Container.appendChild(this._PagesContainer);

        this._setUpInterface();
    },

    dispose: function() {
        this._detachEvents();
        Devy.UI.PagesIndex.callBaseMethod(this, 'dispose');
    },

    _atachEvents: function() { },

    _detachEvents: function() { },

    //*********************************************************************
    //Mis eventos    
    get_events: function() {
        if (!this._events) {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },
    add_pageClick: function(handler) {
        this.get_events().addHandler('pageClick', handler);
    },
    remove_pageClick: function(handler) {
        this.get_events().removeHandler('pageClick', handler);
    },

    _raiseEvent: function(eventName, eventArgs) {
        var handler = this.get_events().getHandler(eventName);

        var theEventArgs = null;
        if (handler) {
            if (!eventArgs) {
                theEventArgs = Sys.EventArgs.Empty;
            }
            else {
                theEventArgs = eventArgs;
            }

            handler(this, theEventArgs);
        }
    },

    //Manejo de Paginas
    _setUpInterface: function() {
        this._PagesContainer.innerHTML = "";

        if (this._PageSize && this._TotalCount) {
            var first = 1 + (this._PageSize * (this._CurrentPage - 1));

            var last = this._PageSize * this._CurrentPage;
            if (last > this._TotalCount) last = this._TotalCount;

            $(this._PagesContainer).append(
                String.format('<strong class="Leyenda">Mostrando {0} {1} a {2} de {3}</strong>',
                this._EntityNamePlural, first, last, this._TotalCount)
                );
        }

        if (this._PagesCount > 1) {
            var mMaxPagesToShow = 8;
            var mMinVecinosToShow = 2;
            this._separadorNeeded = false;

            if (this._CurrentPage > 1) {
                this._printPage(this._CurrentPage - 1, "anterior");
            }

            if (this._PagesCount < mMaxPagesToShow) {
                for (i = 1; i <= this._PagesCount; i++) {
                    this._printPage(i);
                }
            }
            else {
                //Ver que imprimir antes
                if (((this._CurrentPage - mMinVecinosToShow) > 2) && ((this._PagesCount - mMaxPagesToShow + 1 - mMinVecinosToShow) > 2)) {
                    this._printPage(1);
                    this._printPuntitos();
                }

                //Ver que imprimir al medio
                var mStart;

                if (this._CurrentPage > (this._PagesCount - mMaxPagesToShow)) {
                    mStart = this._PagesCount - mMaxPagesToShow + 1 - mMinVecinosToShow;
                    if (mStart < 1) {
                        mStart = 1;
                    }

                    mMaxPagesToShow = mMaxPagesToShow + mMinVecinosToShow;
                }
                else if ((this._CurrentPage - mMinVecinosToShow) <= 2) {
                    mStart = 1;
                }
                else {
                    mStart = this._CurrentPage - mMinVecinosToShow;
                }

                var mActual = 0;

                for (i = 1; i <= mMaxPagesToShow; i++) {
                    mActual = mStart + i - 1;
                    this._printPage(mActual);

                    if (mActual >= this._PagesCount)
                        break;
                }

                //Ver que imprimir al final
                if (mActual != this._PagesCount) {
                    this._printPuntitos();
                    this._printPage(this._PagesCount);
                }
            }

            if (this._CurrentPage < this._PagesCount) {
                this._printPage(this._CurrentPage + 1, "siguiente");
            }
        }
    },

    _printPage: function(pageNumber, text) {
        if (text === undefined) text = pageNumber;

        if (this._separadorNeeded)
            $(this._PagesContainer).append("<span class=\"PageSeparador\"> | </span>");

        if (this._CurrentPage == pageNumber) {
            $(this._PagesContainer).append('<strong class="CurrentPage">' + pageNumber + "</strong>");
        }
        else {
            var pageLink = $('<a href="#"><span>' + text + '</span></a>')[0];
            var contexto = this;
            $addHandler(pageLink, "click", function(evt) {
                evt.preventDefault();
                contexto._raiseEvent("pageClick", pageNumber);
            });

            $(this._PagesContainer).append(pageLink);
        }
        this._separadorNeeded = true;
    },

    _printPuntitos: function() {
        $(this._PagesContainer).append("<span class=\"Puntitos\"> ... </span>");
    }
}

Devy.UI.PagesIndex.registerClass('Devy.UI.PagesIndex', Sys.Component);
