﻿/// <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() {      
        var records = this.TotalRecords;
        if (isNaN(records)) {
            records = records.replace(/\,/g, '');
            this.TotalRecords = parseInt(records);
        }
        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 = ThousandSeparator(totalPages);
    };



  function  ThousandSeparator(TotalPage) {
        // Separator Length. Here this is thousand separator
        var separatorLength = 3;

        var OriginalValue = TotalPage;

        var TempValue = "" + OriginalValue;

        var NewValue = "";

        // Store digits after decimal
        var pStr;

        // store digits before decimal
        var dStr;

        // Add decimal point if it is not there
        if (TempValue.indexOf(".") == -1) { TempValue += "." }

        dStr = TempValue.substr(0, TempValue.indexOf("."));

        pStr = TempValue.substr(TempValue.indexOf("."))

        // Add "0" for remaining digits after decimal point
     //   while (pStr.length - 1 < decimalDigits) { pStr += "0" }

        if (pStr == '.') pStr = '';

        if (dStr.length > separatorLength) {
            // Logic of separation   
            while (dStr.length > separatorLength) {
                NewValue = "," + dStr.substr(dStr.length - separatorLength) + NewValue;
                dStr = dStr.substr(0, dStr.length - separatorLength);
            }

            NewValue = dStr + NewValue;

        }
        else {
            NewValue = dStr;
        }


        //  Add decimal part
        NewValue = NewValue + pStr;
        return NewValue;
        // Show Final value
     


    }











    this.select = function(pageIndex, propertySearchParam) {


        var page = this;
        if (page.IsUpdateInProgress) {
            return;
        }

        this.IsUpdateInProgress = true;
        if (isNaN(pageIndex)) {
            pageIndex = pageIndex.replace(/\,/g, '');
            pageIndex = parseInt(pageIndex);
        }

        var totalPage = page.TotalPages();
        if (isNaN(totalPage)) {
            totalPage = totalPage.replace(/\,/g, '');
            totalPage = parseInt(totalPage);
        
        }
        if (pageIndex > totalPage || 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: "Home.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: "Home.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(ThousandSeparator(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.Dispose = function() {
        var pager = this;
        pager.LastButton.unbind("click");
        pager.NextButton.unbind("click");
        pager.PreviousButton.unbind("click");
        pager.FirstButton.unbind("click");

    };

    this.init = function() {
        var pager = this;

        pager.TotalPage.html(pager.TotalPages());
        if (pager.TotalRecords == 0) {
            pager.SelectedPage.html(0);
            noSearchResultFound();
            return;
        }

        pager.SelectedPage.html(ThousandSeparator(pager.SelectedPageIndex));

        pager.select(1);

        pager.Dispose();
        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();
    };

}
