project.player.Pagination = new Class({
    Implements: [Options, project.player.Logging],

    log_prefix: 'Pagination',

    pagination_container_id: 'playerlist_pagination',
    pagination_container: null,
    pagination: null,
    block_pagination: null,

    pages: {
        window_begin: 4,
        window_end: 4,
        start: 0,
        end: 0
    },

    elements:[],

    options: {
        page: {
            count: 1,
            active: 1
        },
        change_page_callback: null
    },


    initialize: function(options)
    {
        this.setOptions(options);

        this.logger('initialize');
        this.pagination_container = $(this.pagination_container_id);
    },


    update: function(count, active)
    {
        this.logger('update');
        this.options.page.count = count;
        this.options.page.active = active;
        this.render();
    },


    /*
    <ul>
        <li class="arrow_left"><a href="#"><img src="static/images/playlist_footer_arrow_left.png" alt="" /></a></li>
        <li class="active">10</li>
        <li>20</li>
        <li>30</li>
        <li>40</li>
        <li>50</li>
        <li>60</li>
        <li>70</li>
        <li>80</li>
        <li>90</li>
        <li>100</li>
        <li class="arrow_right"><a href="#"><img src="static/images/playlist_footer_arrow_right.png" alt="" /></a></li>
    </ul>
    <span>Seite</span>

     */
    render: function()
    {
        var that = this;
        var page_elements = [];

        that.logger({'render page': that.options.page.active});

        that.pagination_container.empty();
        that.pagination = new Element('ul');

        that.calculatePagination();

        for(var page=that.pages.start; page <= that.pages.end; page++)
        {
            var li_element = new Element('li', {
                'text': page
            });

            if(page == that.options.page.active)
            {
                li_element.addClass('active');
            }
            else
            {
                li_element.addEvent('click', function(event) {
                    event = new Event(event);
                    that.options.change_page_callback(parseInt(this.get('text'), 10)); //page_elements.indexOf(this)+1
                });
            }

            page_elements.push(li_element);
        }

        var prev = new Element('li').grab(
            new Element('img', {
                'src': 'static/images/player/playlist_footer_arrow_left.png'
            })
        );

        var next = new Element('li').grab(
            new Element('img', {
                'src': 'static/images/player/playlist_footer_arrow_right.png'
            })
        );

        if(that.options.page.count == that.options.page.active)
        {
            next.addClass('disabled');
        } else {
            next.addEvent('click', function(event) {
                event = new Event(event);
                that.options.change_page_callback(that.getNextPage());
            });
        }

        if(1 == that.options.page.active)
        {
            prev.addClass('disabled');
        } else {
            prev.addEvent('click', function(event) {
                event = new Event(event);
                that.options.change_page_callback(that.getPrevPage());
            });
        }

        that.pagination.grab(prev);
        that.pagination.adopt(page_elements);
        that.pagination.grab(next);

        that.pagination_container.adopt([
            that.pagination,
            new Element('span', {
                'text': 'Seite'
            })
        ]);
    },


    gotoPage: function(page)
    {
        if(page <= this.options.page.count && page > 0)
        {
            this.options.page.active = page;
            this.render();
        }
    },


    getNextPage: function()
    {
        return this.options.page.active + 1;
    },


    getPrevPage: function()
    {
        return this.options.page.active - 1;
    },


    calculatePagination: function()
    {
        /*
         * Start
         */
        this.pages.start = 1;

        if(this.options.page.active > this.pages.window_begin)
        {
            this.pages.start = this.options.page.active-this.pages.window_begin;
        }

        /*
         * End
         */
        this.pages.end = this.options.page.active+this.pages.window_end;
        
        if(this.options.page.active <= this.pages.window_begin)
        {
            this.pages.end = this.pages.end + this.pages.window_begin - (this.options.page.active-1);
        }

        if(this.pages.end >= this.options.page.count)
        {
            this.pages.end = this.options.page.count;

            
            if(this.options.page.active-1 > this.pages.window_begin)
            {
                this.pages.start = this.pages.start - ((this.options.page.active+this.pages.window_begin)-this.options.page.count);
                this.pages.start = (this.pages.start < 1) ? 1 : this.pages.start;
            }
        }

        this.logger('page_start: ' + this.pages.start);
        this.logger('page_end: ' + this.pages.end);
    },


    lock: function() {
        this.logger('lock');

        this.block_pagination = new Element('div', {
            'id': 'block_pagination'
        });

        $('playerlist_pagination').grab(
            this.block_pagination,
            'top'
        );
    },

    
    release: function() {
        this.logger('release lock');
        this.block_pagination.dispose();
    }
});

