﻿/// <reference path="../Edentity.Global.js" />
/// <reference path="../External/jquery-1.3.2-vsdoc2.js" />

Edentity.RegisterNamespace("Whiskas.Controls");

(function(Controls, $) {

Controls.ClientSidePager = function (containerID, totalCount, pageSize, currentPage, onPagerInitialized, onPageChanged) {
    this.containerID = containerID;
    this.container = $("#" + containerID);
    this.totalCount = totalCount;
    this.pageSize = pageSize;
    this.totalPages = _totalPages.call(this);
    this.currentPage = currentPage;
    this.onPagerInitialized = onPagerInitialized;
    this.onPageChanged = onPageChanged;
    // constants
    this.pagesVisible = 5;
    this.pageWidth = 19;
};

Controls.ClientSidePager.prototype.Init = function () {
    _draw.call(this);
    _bind.call(this);
    
    // call user's event handler
    if ($.isFunction(this.onPagerInitialized)) {
        this.onPagerInitialized(this.container);
    }
};

Controls.ClientSidePager.prototype.SelectPage = function (newPage) {
    if (newPage == this.currentPage) return;
    
    // adjust the pager
    _updatePagerDisplay.call(this, newPage);

    // remember the new page
    this.currentPage = newPage;

    // call user's event handler
    if ($.isFunction(this.onPageChanged)) {
        this.onPageChanged(newPage);
    }
};

Controls.ClientSidePager.prototype.Remove = function () {
    this.container.empty();
};

function _draw() {
    // prepare containing element
    this.container.addClass("Pager").empty();
    
    // build HTML
    var html = _buildStartHtml.call(this);
    for (var i = 0; i < this.totalPages; i++) {
        html += _buildPageHtml.call(this, i);
    }
    html += _buildEndHtml.call(this);
    
    // inject HTML
    this.container.html(html);
    
    _updatePagerDisplay.call(this, this.currentPage);
}

function _bind() {
    var self = this;
    
    // bind click events on page links and first page/last page buttons
    $("a,div.PrevPages,div.NextPages", this.container).click(function() {
        var newPage = parseInt($(this).attr("pageno"));
        self.SelectPage(newPage);
        return false;
    });
}

function _updatePagerDisplay(newPage) {

    $("a", this.container).removeClass("Selected");
    
    var lastPageNum = this.totalPages - 1;
    
    if (lastPageNum >= this.pagesVisible) {
        var offset = Math.floor(this.pagesVisible / 2);
        var step = newPage - offset;
        if (step < 0) {
            step = 0;
        } else if (step > lastPageNum - 2 * offset) {
            step = lastPageNum - 2 * offset;
        }
        
        $("div.All", this.container).animate({
            left: "-" + (step * this.pageWidth) + "px"
        }, "normal");
    }
    $("a[pageno=" + newPage + "]", this.container).addClass("Selected");
}

function _buildStartHtml() {
    var html = "<div class='Div'></div>";
    html += "<div class='PagerLeft'></div>";
    if (this.totalPages > this.pagesVisible) {
        html += "<div class='PrevPages' pageno='0'></div>";
    }
    html += "<div class='Pages' style='width:" + _calcVisibleWidth.call(this) + "px'>";
    html += "<div class='All' style='width:" + _calcTotalWidth.call(this) + "px'>";
    return html;
}

function _buildPageHtml(pageNum) {
    var html = "<a href='#' pageno='" + pageNum + "'";
    if (this.currentPage == pageNum) {
        html += " class='Selected' ";
    }
    html += ">" + (pageNum + 1) + "</a>";
    return html;
}

function _buildEndHtml() {
    var html = "</div></div>";
    if (this.totalPages > this.pagesVisible) {
        html += "<div class='NextPages' pageno='" + (this.totalPages - 1) + "'></div>";
    }
    html += "<div class='PagerRight'></div>";
    html += "<div class='Div'></div>";
    html += "<div class='Clear'></div>";
    return html;
}

function _totalPages() {
    return Math.ceil(this.totalCount / this.pageSize);
}

function _calcVisibleWidth() {
    var width = 0;
    if (this.totalPages > this.pagesVisible) {
        width = this.pagesVisible * this.pageWidth;
    } else {
        width = this.totalPages * this.pageWidth;
    }
    return width;
}

function _calcTotalWidth() {
    return this.totalPages * this.pageWidth;
}

})(Whiskas.Controls, jQuery);
