﻿function XLGridControl(id, data) {
    this._totalRowCount = 100;

    this.setAttribute('TotalActualRowCount', this._totalRowCount);
    this._header.setAttribute('TotalActualRowCount', this._totalRowCount);
    this._footer.setAttribute('TotalActualRowCount', this._totalRowCount);

    this._pagesToDownloadInOneCall = 3;
    this._pagesDownloaded = new Array();
    this._downloadPageAppendCallback = null;
    this._downloadPageResetCallback = null;
    this._sortBy = ' productid desc ';

    GridControl.call(this, id);

}

XLGridControl.prototype = new GridControl();

XLGridControl.prototype.TotalNumberOfPages = function() {
    return (this._totalRowCount / this._rowsPerPage)|0;
}

XLGridControl.prototype.SetColumnCount = function(cols) {
    this._rows._columnCount = cols;
}

XLGridControl.prototype.SetTotalRowCount = function(TotalRowCount) {
    this._totalRowCount = TotalRowCount;
    this.setAttribute('TotalActualRowCount', this._totalRowCount);
    this._header.setAttribute('TotalActualRowCount', this._totalRowCount);
    this._footer.setAttribute('TotalActualRowCount', this._totalRowCount);
    for (var i = 0; i < this.TotalNumberOfPages(); i++) {
        this._pagesDownloaded[i] = false;
    }
}

XLGridControl.prototype.GetTotalRowCount = function() {
    return this._totalRowCount;
}

XLGridControl.prototype.SetDownloadPageAppendCallback = function(cb) {
    this._downloadPageAppendCallback = cb;
}

XLGridControl.prototype.SetDownloadPageReSetCallback = function(cb) {
    this._downloadPageResetCallback = cb;
}

XLGridControl.prototype.DownloadPage = function(PageNumber) {
    if (this._downloadPageAppendCallback != null) {
        var rowStart = (PageNumber) * this._rowsPerPage;
        var rowEnd = rowStart + this._pagesToDownloadInOneCall * this._rowsPerPage;
        var rowStartChagned = false;
        for (var i = PageNumber; i < PageNumber + this._pagesToDownloadInOneCall; i++) {
            if (rowStartChagned && !this._pagesDownloaded[i]) {
                rowEnd = (i + 1) * this._rowsPerPage;
            }
            if (!rowStartChagned && this._pagesDownloaded[i]) {

                //alert(PageNumber + ' _ ' + i + ' _ ' + this._pagesToDownloadInOneCall);
                rowStart = (i + 1) * this._rowsPerPage;
            }
            if (!rowStartChagned && !this._pagesDownloaded[i]) {
                rowStartChagned = true;
            }
        }
        if (rowStart != rowEnd) {
            this._downloadPageAppendCallback(this, rowStart, rowEnd, this._sortBy);
            this.SetDownloadPageFlags(PageNumber);
        }
    }
}

XLGridControl.prototype.SetDownloadPageFlags = function(PageNumber) {
    for (var i = PageNumber; i < PageNumber + this._pagesToDownloadInOneCall; i++) {
        this._pagesDownloaded[i] = true;
    }
}

XLGridControl.prototype.firstPage = function() {
    this.setCurrentPage(0);
    //return this.getHTML();
}

XLGridControl.prototype.lastPage = function() {
    //this.DownloadPage(this.getCurrentPage());
    if (this._downloadPageResetCallback != null) {
        if (this.getRowCount() < this.GetTotalRowCount()) {
            this._downloadPageResetCallback(this, 0, 0, this._sortBy);
        }
    }
    this.setCurrentPage(this.TotalNumberOfPages());
    //return this.getHTML();
}

XLGridControl.prototype.SetDownloadFlag = function(start, end) {
    for (var i = start; i < end; i++) {
        this._pagesDownloaded[i] = true;
    }
}

XLGridControl.prototype.previousPage = function() {
    var pageNumber = this.getCurrentPage();
    if (pageNumber <= 0) pageNumber = 0;
    else pageNumber = pageNumber - 1;
    this.setCurrentPage(pageNumber);
    //this.DownloadPage(this.getCurrentPage());
    //return this.getHTML();
}

XLGridControl.prototype.nextPage = function() {
    var pageNumber = this.getCurrentPage();
    if (pageNumber >= ((this.GetTotalRowCount() / this._rowsPerPage) - 1)) pageNumber = (this.GetTotalRowCount() / this._rowsPerPage) - 1;
    else pageNumber = pageNumber + 1;
    pageNumber = pageNumber | 0; // this is to cast double to integer after division
    this.setCurrentPage(pageNumber);
    this.DownloadPage(this.getCurrentPage());
    //return this.getHTML();
}
