﻿/*
#Region "History"
'20100205 - RH - Script file added
#End Region
*/

var Paging = Class.create({
    size: {},
    current: {},
    total: {},
    callbacks: {},
    tempCount: {},
    clearFilters: function(selector) {
        $$(selector).each(function(item) { item.writeAttribute('pVisible', 'true') });
        this.create(selector, this.size[selector], this.callbacks[selector]);
    },
    filter: function(selector, key, values) {
        $$(selector).each(function(item) { paging.filterItem(item, key, values) });
        this.create(selector, this.size[selector], this.callbacks[selector]);
    },
    filterItem: function(item, key, values) {
        if (main.arrayOverlap(values, item.readAttribute('k' + key).split(',')) == values.size()) {
            item.writeAttribute('pVisible', 'true');
        } else {
            item.writeAttribute('pVisible', 'false');
        };
    },
    setupItem: function(item, index, selector) {
        if (item.readAttribute('pVisible') == 'true' || !item.readAttribute('pVisible')) {
            item.writeAttribute('page', this.total[selector]);
            this.tempCount[selector]++;
            if (this.tempCount[selector] == this.size[selector]) {
                this.tempCount[selector] = 0;
                this.total[selector]++;
            };
        } else {
            item.writeAttribute('page', '-1');
        };
    },
    create: function(selector, pageSize, onchange) {
        this.total[selector] = 0;
        this.current[selector] = 0;
        this.tempCount[selector] = 0;
        this.size[selector] = pageSize;
        this.callbacks[selector] = onchange;

        $$(selector).each(function(item, index) { paging.setupItem(item, index, selector) });

        this.show(selector, 0);
        this.draw(selector);
    },
    show: function(selector, page) {


        this.current[selector] = page;

        $$(selector).invoke('hide');
        $$(selector + '[page="' + page + '"]').invoke('show');

        if (this.callbacks[selector]) {
            setTimeout(this.callbacks[selector], 0);
        };

        this.draw(selector);
    },
    next: function(selector) {
        document.body.scrollIntoView();

        if (this.current[selector] < this.total[selector]) {
            this.current[selector]++;
        };

        this.show(selector, this.current[selector]);
    },
    previous: function(selector) {
        document.body.scrollIntoView();

        if (this.current[selector] > 0) {
            this.current[selector]--;
        };

        this.show(selector, this.current[selector]);
    },
    draw: function(selector) {
        var s = '';

        if (this.total[selector] > 0) {
            if (this.current[selector] > 0) {
                s += '<a href="javascript:paging.previous(\'' + selector + '\');">';
                s += '&lt; Previous';
                s += '</a>';
            } else {
                s += '<span class="disabled">&lt; Previous</span>';
            };

            var min = this.current[selector] - 2;
            var max = this.current[selector] + 2;

            if (min < 0) {
                min = 0;
                max = 4;
            };

            if (max > this.total[selector]) {
                max = this.total[selector];
                min = max - 4;
                if (min < 0) {
                    min = 0;
                    max = this.total[selector];
                };
            };

            for (var j = min; j <= max; j++) {
                if (j == this.current[selector]) {
                    s += '<span class="disabled">';
                    s += j + 1;
                    s += '</span>';
                } else {
                    s += '<a href="javascript: paging.show(\'' + selector + '\',' + j + ');">';
                    s += j + 1;
                    s += '</a>';
                };
            };

            if (this.current[selector] < max && this.total[selector] > 0) {
                s += '<a href="javascript:paging.next(\'' + selector + '\');">';
                s += 'Next &gt;';
                s += '</a>';
            } else {
                s += '<span class="disabled">Next &gt;</span>';
            };

            
        } else {
            s = '';
        };

        if ($(selector + '_paging_bottom')) {
            $(selector + '_paging_bottom').update(s);
        };

        if ($(selector + '_paging_top')) {
            $(selector + '_paging_top').update(s);
        };
    }
});

var paging = new Paging;


