﻿/// <reference path="../jquery-1.2.6-intellisense.js" />

function Pager() {
    this.PageSize = 10;
    this.TotalRecords = 0;

    this.SelectedPageIndex = 1;

    this.IsUpdateInProgress = false;

    this.PropertySearchParam = "{}";

    this.MainContainer = $(".divPagination");
    this.TotalPage = this.MainContainer.find(".totalPages");
    this.SelectedPage = this.MainContainer.find(".selectedPage");
    this.LastButton = this.MainContainer.find(".last");
    this.NextButton = this.MainContainer.find(".next");
    this.PreviousButton = this.MainContainer.find(".previous");
    this.FirstButton = this.MainContainer.find(".first");
    this.PageSizeDropDown = this.MainContainer.find(".ddPageSize");


    //-----------   Initialization  --------------


    this.TotalPages = function() {
        if (this.TotalRecords == 0)
            return 0;
        var temp = this.TotalRecords / this.PageSize;
        var totalPages = Math.round(temp);
        if (totalPages < temp)
            totalPages = totalPages + 1;

        return totalPages;
    };

    this.select = function(pageIndex, propertySearchParam) {

        var page = this;
        if (page.IsUpdateInProgress) {
            return;
        }

        this.IsUpdateInProgress = true;

        if (pageIndex > page.TotalPages() || pageIndex < 1) {
            page.IsUpdateInProgress = false;
            return;
        }

        pageUpdateStarted();

        var propertySearchParam = this.PropertySearchParam;
        var postBackData = "{Param : ";
        postBackData += propertySearchParam;
        postBackData += ", Skip :";
        postBackData += (pageIndex - 1) * page.PageSize;
        postBackData += ", Take :";
        postBackData += page.PageSize;
        postBackData += ", CultureName :'";
        postBackData += getCurrentCulture();
        postBackData += "'}";

        jQuery.ajax({
            type: "POST",
            url: "Default.aspx/getPropertySearchResult",
            data: postBackData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(content) {
                page.selectedIndexChanged(content, pageIndex);
            },
            error: function(error) {
               
                alert(page_ajaxCallError);
            }
        });

    };

    this.setResultCount = function() {
        var page = this;
        jQuery.ajax({
            type: "POST",
            url: "Default.aspx/getPropertySearchResultCount",
            data: "{ Param :" + page.PropertySearchParam + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(content) {
                page.TotalRecords = content.d;
                $("#spanTotalRecords").html(page.TotalRecords);
                page.init();
            },
            error: function(error) {
            
                searchPageChanged();
                
                alert(page_ajaxCallError);
            }
        });
    }

    this.selectedIndexChanged = function(content, pageIndex) {
        this.SelectedPageIndex = pageIndex;
        this.SelectedPage.html(pageIndex);

        updateSearchResults(content.d);

        if (pageIndex == this.TotalPages()) {
            this.NextButton.addClass("pager_Next_Disabled").removeClass("pager_Next");
            this.LastButton.addClass("pager_Last_Disabled").removeClass("pager_Last");
            this.PreviousButton.addClass("pager_Previous").removeClass("pager_Previous_Disabled");
            this.FirstButton.addClass("pager_First").removeClass("pager_First_Disabled");
        }
        else if (pageIndex == 1) {
            this.PreviousButton.addClass("pager_Previous_Disabled").removeClass("pager_Previous");
            this.FirstButton.addClass("pager_First_Disabled").removeClass("pager_First");
            this.NextButton.addClass("pager_Next").removeClass("pager_Next_Disabled");
            this.LastButton.addClass("pager_Last").removeClass("pager_Last_Disabled");
        }
        else {
            this.NextButton.addClass("pager_Next").removeClass("pager_Next_Disabled");
            this.LastButton.addClass("pager_Last").removeClass("pager_Last_Disabled");
            this.PreviousButton.addClass("pager_Previous").removeClass("pager_Previous_Disabled");
            this.FirstButton.addClass("pager_First").removeClass("pager_First_Disabled");
        }
        this.IsUpdateInProgress = false;
        searchPageChanged();
    }

    this.getResult = function() {
    };

    this.initialize = function(propertySearchParam) {
        
        this.PropertySearchParam = propertySearchParam;
        var pager = this;
        this.setResultCount();
    };

    this.init = function() {
        var pager = this;

        pager.TotalPage.html(pager.TotalPages());
        if (pager.TotalRecords == 0) {
            pager.SelectedPage.html(0);
            noSearchResultFound();
            return;
        }

        pager.SelectedPage.html(pager.SelectedPageIndex);

        pager.select(1);

        pager.LastButton.addClass("pager_Last").addClass("pagerButton").click(function() {
            pager.select(pager.TotalPages());
        });
        pager.NextButton.addClass("pager_Next").addClass("pagerButton").click(function() {
            pager.select(pager.SelectedPageIndex + 1);
        });
        pager.PreviousButton.addClass("pager_Previous").addClass("pagerButton").click(function() {
            pager.select(pager.SelectedPageIndex - 1);
        });
        pager.FirstButton.addClass("pager_First").addClass("pagerButton").click(function() {
            pager.select(1);
        });

        pager.PageSizeDropDown.change(function() {
            pager.UpdateUI();
        });
    };

    this.UpdateUI = function() {
        this.PageSize = this.PageSizeDropDown.val();
        this.TotalPage.html(this.TotalPages());

        this.SelectedPageIndex = 1;
        this.SelectedPage.html(this.SelectedPageIndex);
        this.select(1);
    };

    this.update = function(totalRecords) {
        this.IsUpdateInProgress = false;
        this.TotalRecords = totalRecords;
        this.UpdateUI();
    };

}
