﻿// default current results page
var currentPage = 1;

// default page size
var pageSize = 1000000;

// default end page - overriden by function 'GetRSSItemCount'
var lastPage = 1;

function UpdatePaging() {
    // If we're not on the first page, enable the "Previous" link.
    if (currentPage != 1) {
        $('#PrevPage').attr('href', '#');
        $('#PrevPage').click(PrevPage);
        $('#PrevPage').show();
    } else {
        $('#PrevPage').hide();
    }

    // If we're not on the last page, enable the "Next" link.
    if (currentPage != lastPage) {
        $('#NextPage').attr('href', '#');
        $('#NextPage').click(NextPage);
        $('#PrevPage').show();
    } else {
        $('#NextPage').hide();
    }
    //alert("# Pages: " + lastPage);
    //alert("Current Page: " + currentPage);
}
function NextPage(evt) {
    // Prevent the browser from navigating needlessly to #.
    evt.preventDefault();

    // Entertain the user while the previous page is loaded.
    DisplayProgressIndication();

    // Load and render the next page of results, and
    //  increment the current page number.
    //DisplayRSSTable(++currentPage);
    DisplaySearchTable(++currentPage);
}

function PrevPage(evt) {
    // Prevent the browser from navigating needlessly to #.
    evt.preventDefault();

    // Entertain the user while the previous page is loaded.
    DisplayProgressIndication();

    // Load and render the previous page of results, and
    //  decrement the current page number.
    //DisplayRSSTable(--currentPage);
    DisplaySearchTable(--currentPage);
}
function DisplayProgressIndication() {
    // Hide both of the paging controls,
    //  to avoid click-happy users.
    $('.paging').hide();

    // Clean up our event handlers, to avoid memory leaks.
    $('.paging').unbind();

    // Store the height of the content area of the table.
    var height = $('#RSSTable tbody').height();

    // Replace the entire content area with a single row/cell.
    $('#RSSTable tbody').html('<tr><td colspan="2"></td></tr>');

    // Set that row's height to be the same as previous.
    $('#RSSTable tbody tr').height(height);

    // Add our centered progress indicator animation to it.
    $('#RSSTable tbody td').addClass('loading');
}
function BuildTable(msg) {
    //var table = '<table cellspacing="0" cellpadding="5" border="0"><thead><tr><th>Date</th><th>Title/Exerpt</th></thead><tbody>';
    var table = '<table cellspacing="0" cellpadding="5" border="0" id="RSSTable"><tbody>';

    for (var post in msg) {
        var row = '<tr>';
        var datearray = msg[post].ArtDate.split("/");
        row += '<td valign="top"><strong>' + msg[post].ArtDate + '</strong></td>';
        row += '<td valign="top"><h2><a href="/Article/' + msg[post].CatPath + '/' + datearray[2] + datearray[0] + datearray[1] + '/' + msg[post].SeoTitle + '">' + msg[post].Title + '</a></h2><div>' + msg[post].Story + '</td>';

        row += '</tr>';

        table += row;
    }

    table += '</tbody></table>';
    table += '<div>';
    table += '<div style="float: right; width: 100px;"><a id="NextPage" class="paging">Next Page &raquo;</a></div>';
    table += '<div style="float: left; width: 100px;"><a id="PrevPage" class="paging">&laquo; Previous Page</a></div>';
    table += '</div>';

    $('#Container').html(table);
    UpdatePaging();
}

function ApplyTemplate(msg) {
    // Changed the template extension from .tpl to .htm, 
    //  to avoid the request being blocked by some IIS installs.
    $('#Container').setTemplateURL('SearchTemplate.tpl', null, { filter_data: false });
    $('#Container').processTemplate(msg);
}

function DisplaySearchTable(thisPage) {
    var myTerms = $("#ctl00_LeftContent_hidSearchTerm").val();
    $.ajax({
        type: "GET",
        url: "SearchXML.ashx",
        //data: "{'searchinput': '" + myTerms + "', 'Count':'" + pageSize + "', 'Page': '" + thisPage + "'}",
        data: "searchinput=" + myTerms + "&count=" + pageSize + "&page=" + thisPage,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: false,
        success: function (msg) {
            //alert(msg.d);
            BuildTable(msg);
            //ApplyTemplate(msg);
        },
        error: function (myRequest, myStatus, myError) {
            alert("Error getting search results");
        }
    });
}

function GetRSSItemCount() {
    var myTerms = $("#ctl00_LeftContent_hidSearchTerm").val();
    $.ajax({
        type: "GET",
        url: "SearchCountXML.ashx",
        //data: "{'searchinput': '" + myTerms + "'}",
        data: "searchinput=" + myTerms,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: false,
        success: function (msg) {
            // msg.d will contain the total number of items. 
            // Divide and round up to find total number of pages.
            //alert("Total Items: " + msg[0].Count);
            lastPage = Math.ceil(msg[0].Count / pageSize);
            //alert("Current Page: " + currentPage);
            //alert("Last Page: " + lastPage);

            // Wireup appropriate paging functionality.
            //UpdatePaging();
        }
    });
}
