// doi tuong quan ly phan trang du lieu
iPagin = new function() {
    // ten doi tuong nay
    var _name = 'iPagin';

    // cau hinh
    var _config = {
        // vi tri dat chuc nang phan trang
        target: '.pagination', 

        // chi muc trang hien tai (1-N)
        index: 1,

        // so luong du lieu
        quantity: 0,

        // so luong du lieu toi da / 1 trang
        size: 25,

        // ten tham so chi muc
        indexParam: 'index',

        // ten tham so so luong du lieu toi da / 1 trang
        sizeParam: 'size',

        // so trang xuat hien
        width: 10,

        // so trang them vao dau va cuoi
        padding: 2,

        // dien giai
        text: ['Previous', 'Next', 'Go to page {PAGE}', 'Go to {PAGE} Page'],
        symbol: ['«', '»', '…']
    };

    // tinh toan danh sach chi muc cac trang se xuat hien
    var _range = function() {
        var result = [];
        var width = (_config['width'] % 2 == 0) ? _config['width'] : (_config['width'] - 1);
        var padding = _config['padding'];
        var index = _config['index'];
        var pages = _config['pages'];
        var i;

        // phai co du lieu
        if (pages > 0) {
            // xac dinh can trinh bay het so trang hay khong
            if (pages > width + 2 * padding) {
                // dang o nhung trang dau
                if (index <= width / 2 + padding + 1) {
                    for (i =1; i <= width + padding; i++) {
                        result.push(i);
                    }
                    
                    // phan cach (...)
                    result.push(0); 
                    
                    // trang cuoi
                    for (i = padding - 1; i >= 0; i--) {
                        result.push(pages - i);
                    }
                }
                else {
                    // trang dau
                    for (i = 1; i <= padding; i++) {
                        result.push(i);
                    }
                    
                    // phan cach (...)
                    result.push(0);
                    
                    // dang o nhung trang cuoi
                    if (index >= pages - (width / 2 + padding) + 1) {
                        for (i = width + padding - 1; i >= 0; i--) {
                            result.push(pages - i);
                        }
                    }
                    else {
                        // dang o cac trang giua
                        for (i = 0; i < width; i++) {
                            result.push(index - width / 2 + i);
                        }
                        
                        // phan cach (...)
                        result.push(0);
                        
                        // trang cuoi
                        for (i = padding - 1; i >= 0; i--) {
                            result.push(pages - i);
                        }
                    }
                }
            }
            else {
                // tat ca cac trang
                for (i =1; i <= pages; i++) {
                    result.push(i);
                }
            }
        }
        
        return result;
    };

    // chuyen den trang co chi muc xac dinh
    var _buildUrl = function(index) {
        var url = location.href;
        var re1 = eval('/\\?' + _config['indexParam'] + '=\\d*/i');
        var re2 = eval('/\\&' + _config['indexParam'] + '=\\d*/i');

        if (re1.test(url)) {
            url = url.replace(re1, '?' + _config['indexParam'] + '=' + index);
        }
        else {
            if (re2.test(url)) {
                url = url.replace(re2, '&' + _config['indexParam'] + '=' + index);
            }
            else {
                url += (url.indexOf('?') == -1 ? '?index=' : '&index=') + index;
            }
        }

        return url;
    };

    // trinh bay chuc nang phan trang duoi dang html
    var _renderHtml = function() {
        var range = _range();
        var html = [];

        if (range.length > 0) {
            // trinh bay Previous
            if (_config['index'] > 1) {
                html.push('<a class="nextprev" href="' + _buildUrl(_config['index'] - 1) + '" title="' + _config['text'][3].replace('{PAGE}', _config['text'][0]) + '">' + _config['symbol'][0] + ' ' + _config['text'][0] + '</a>');
            }
            else {
                html.push('<span class="nextprev">' + _config['symbol'][0] + ' ' + _config['text'][0] + '</span>');
            }

            for (var i = 0; i < range.length; i++) {
                if (range[i] == 0) {
                    html.push('<span class="separator">' + _config['symbol'][2] + '</span>');
                }
                else {
                    if (_config['index'] == range[i]) {
                        html.push('<span class="current">' + range[i] + '</span>');
                    }
                    else {
                        html.push('<a href="' + _buildUrl(range[i]) + '" title="' + _config['text'][2].replace('{PAGE}', range[i]) + '">' + range[i] + '</a>');
                    }
                }
            }

            // trinh bay Next
            if (_config['index'] < _config['pages']) {
                html.push('<a class="nextprev" href="' + _buildUrl(_config['index'] + 1) + '" title="' + _config['text'][3].replace('{PAGE}', _config['text'][1]) + '">' + _config['text'][1] + ' ' + _config['symbol'][1] + '</a>');
            }
            else {
                html.push('<span class="nextprev">' + _config['text'][1] + ' ' + _config['symbol'][1] + '</span>');
            }
        }

        return html.join('');
    };

    // trinh bay chuc nang phan trang
    this.render = function(selector) {
        if (typeof(selector) == 'undefined') {
            selector = _config['target'];
        }

        // neu la css-selector thi lay tham chieu
        // phan tu se chua chuc nang phan trang
        if (typeof(selector) == 'string') {
            var coll = dojo.query(selector);
            if (coll.length == 0) {
                return;
            }

            selector = coll[0];
        }

        // hien thi
        selector.innerHTML = _renderHtml();
    };

    // ham khoi tao khi trang tai xong
    this.init = function(config) {
        var query = dojo.queryToObject(location.search.substr(1));

        // cap nhat thay doi neu co
        if (typeof(config) != 'undefined') {
            dojo.mixin(_config, config);
        }
        else {
            // tinh lai chi muc
            if (query[_config['indexParam']]) {
                _config['index'] = parseInt(query[_config['indexParam']]);
            }

            // so luong du lieu toi da / 1 trang
            if (query[_config['sizeParam']]) {
                _config['size'] = parseInt(query[_config['sizeParam']]);
            }

            // tinh tong so trang
            _config['pages'] = Math.ceil(_config['quantity'] / _config['size']);

            // trinh bay phan trang tai vi tri mac dinh
            this.render();
        }
    };
};

// khoi tao khi trang tai xong
dojo.addOnLoad(function() {
    // khoi tao
    iPagin.init();
});