﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min-vsdoc.js" />

var listingsPerPage;
var numberOfListings;
var currentIndex;
var listings;
var listingPager;
var numberOfPages
var ddlListingsPerPage;

$(function () {
    ddlListingsPerPage = $("#ddlListingsPerPage");
    ddlListingsPerPage.val("10");
    listingsPerPage = ddlListingsPerPage.val();
    listings = $("#listings");
    listingPager = $(".listingPager");
    $(".mainContent").css("border-top", "none");
    numberOfListings = listings.children().length;
    SetUpList(listingsPerPage);

    listings.children("li:gt(" + (listingsPerPage - 1) + ")").hide();
    AddPagingHandlers();

    listingPager.children("li.nextListing").click(function () {
        ChangeListingPage(currentIndex + 1);
    });

    if (numberOfListings <= parseInt(ddlListingsPerPage.val())) {
        listingPager.hide();
    }

    ddlListingsPerPage.change(function () {
        SetUpList($(this).val());
        SetUpPager($(this).val());
        AddPagingHandlers();
        ChangeListingPage(0);
    });
});

function SetUpList(numberPerPage) {
    listingsPerPage = numberPerPage == "All" ? numberOfListings : parseInt(numberPerPage);
    numberOfPages = parseInt((numberOfListings / listingsPerPage)) + (numberOfListings % listingsPerPage > 0 ? 1 : 0);
    currentIndex = 0;
}

function SetUpPager(numberPerPage) {
    if (numberPerPage == "All" || numberOfListings <= parseInt(numberPerPage)) {
        listingPager.hide();
    }
    else {
        listingPager.show();
        listingPager.each(function () {
            $(this).children("li:not(:first:last)").remove();
            for (var i = numberOfPages; i >= 1; i--) {
                $(this).children("li:first").after("<li>" + i + "</li>");
            }
        });
    }
}

function AddPagingHandlers(numberPerPage) {
    if (numberPerPage != "All") {
        listingPager.each(function () {
            $(this).children("li:not(:first:last)").each(function (index) {
                $(this).click(function () {
                    ChangeListingPage(index);
                });
            });
        });
    }
}

function ChangeListingPage(pageIndex) {
    currentIndex = pageIndex;

    listings.children("li").hide();
    listingPager.children("li.listingCurrentPage").removeClass("listingCurrentPage");
    listings.children("li").slice(pageIndex * listingsPerPage, (pageIndex * listingsPerPage) + (listingsPerPage)).show();
    listingPager.children("li:nth-child(" + (pageIndex + 2) + ")").addClass("listingCurrentPage");

    var previousListing = listingPager.children("li.previousListing");
    var nextListing = listingPager.children("li.nextListing");

    if (currentIndex > 0) {
        previousListing.unbind("click").click(function() {
            ChangeListingPage(currentIndex - 1);
        });
        previousListing.addClass("previousListingEnabled");
        previousListing.removeClass("previousListingDisabled");
    }
    else {
        previousListing.unbind("click");
        previousListing.addClass("previousListingDisabled");
        previousListing.removeClass("previousListingEnabled");
    }

    if (currentIndex < numberOfPages - 1) {
        nextListing.unbind("click").click(function() {
            ChangeListingPage(currentIndex + 1);
        });
        nextListing.addClass("nextListingEnabled");
        nextListing.removeClass("nextListingDisabled");
    }
    else {
        nextListing.unbind("click");
        nextListing.addClass("nextListingDisabled");
        nextListing.removeClass("nextListingEnabled");
    }

    if ($(window).scrollTop() > ddlListingsPerPage.offset().top) {
        $("html, body").animate({ scrollTop: ddlListingsPerPage.offset().top }, 500);
    }
}
