YAHOO.util.Event.addListener(window, "load", function() {
    YAHOO.example.ServerPagination = new function() {
        // Column definitions
        var myColumnDefs = [
            {key:"name", label:"Car"},
            {key:"description", label:"Description"},
            {key:"price", label:"Price"}
        ];

        // DataSource instance
        this.myDataSource = new YAHOO.util.DataSource("/YuiDataTable.ajax?");
        this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        this.myDataSource.responseSchema = {
            resultsList: "records",
            fields: ["name","description","price"]
        };

        // DataTable instance
        var oConfigs = {
            initialRequest: "startIndex=0&results=5" // Initial values
        };
        this.myDataTable = new YAHOO.widget.DataTable("serverpagination", myColumnDefs,
                this.myDataSource, oConfigs);

        // Custom code to parse the raw server data for Paginator values and page links
        this.myDataSource.doBeforeCallback = function(oRequest, oRawResponse, oParsedResponse) {
            var oSelf = YAHOO.example.ServerPagination;
            var oDataTable = oSelf.myDataTable;

            // Get Paginator values
            var oRawResponse = oRawResponse.parseJSON(); //JSON.parse(oRawResponse); // Parse the JSON data
            var recordsReturned = oRawResponse.recordsReturned; // How many records this page
            var startIndex = oRawResponse.startIndex; // Start record index this page
            var endIndex = startIndex + recordsReturned -1; // End record index this page
            var totalRecords = oRawResponse.totalRecords; // Total records all pages

            // Update the DataTable Paginator with new values
            var newPag = {
                recordsReturned: recordsReturned,
                startRecordIndex: startIndex,
                endIndex: endIndex,
                totalResults: totalRecords
            }
            oDataTable.updatePaginator(newPag);

            // Update the links UI
            YAHOO.util.Dom.get("prevLink").innerHTML = (startIndex === 0) ? "previous" :
                    "<a href=\"#previous\" alt=\"Show previous items\">previous</a>" ;
            YAHOO.util.Dom.get("nextLink").innerHTML = (endIndex >= totalRecords) ? "next" :
                    "<a href=\"#next\" alt=\"Show next items\">next</a>";
            YAHOO.util.Dom.get("startIndex").innerHTML = startIndex;
            YAHOO.util.Dom.get("endIndex").innerHTML = endIndex;
            YAHOO.util.Dom.get("ofTotal").innerHTML = " of " + totalRecords;

            // Let the DataSource parse the rest of the response
            return oParsedResponse;
        };

        // Hook up custom pagination
        this.getPage = function(nStartRecordIndex, nResults) {
            // If a new value is not passed in
            // use the old value
            if(!YAHOO.lang.isValue(nResults)) {
                nResults = this.myDataTable.get("paginator").totalRecords;
            }
            // Invalid value
            if(!YAHOO.lang.isValue(nStartRecordIndex)) {
                return;
            }
            var newRequest = "startIndex=" + nStartRecordIndex + "&results=" + nResults;
            this.myDataSource.sendRequest(newRequest, this.myDataTable.onDataReturnInitializeTable, this.myDataTable);
        };
        this.getPreviousPage = function(e) {
            YAHOO.util.Event.stopEvent(e);
            // Already at first page
            if(this.myDataTable.get("paginator").startRecordIndex === 0) {
                return;
            }
            var newStartRecordIndex = this.myDataTable.get("paginator").startRecordIndex - this.myDataTable.get("paginator").rowsThisPage;
            this.getPage(newStartRecordIndex);
        };
        this.getNextPage = function(e) {
            YAHOO.util.Event.stopEvent(e);
            // Already at last page
            if(this.myDataTable.get("paginator").startRecordIndex +
                    this.myDataTable.get("paginator").rowsThispage >=
                    this.myDataTable.get("paginator").totalRecords) {
                return;
            }
            var newStartRecordIndex = (this.myDataTable.get("paginator").startRecordIndex + this.myDataTable.get("paginator").rowsThisPage);
            this.getPage(newStartRecordIndex);
        };
        YAHOO.util.Event.addListener(YAHOO.util.Dom.get("prevLink"), "click", this.getPreviousPage, this, true);
        YAHOO.util.Event.addListener(YAHOO.util.Dom.get("nextLink"), "click", this.getNextPage, this, true);
    };
});

