
HkPaging = function()
{
	this.currentPage = 1;
	this.totalPages = 0;

	this.pagesPerPage = 7;
}

HkPaging.prototype.Reset = function()
{
	this.currentPage = 1;
	this.totalPages = 0;
}

HkPaging.prototype.SetCurrentPage = function(value)
{
	this.currentPage = strToInteger(value);
}

HkPaging.prototype.SetTotalPages = function(value)
{
	this.totalPages = strToInteger(value);
}

HkPaging.prototype.Render = function()
{

	var html = '';

	if (this.totalPages>1)
	{
		html += '<div id="idPaging" class="diet_paging">Page: ';

		//prev
		if (this.currentPage>1)
		{
			html += this.GetTplPrev().apply({number: this.currentPage-1});
		}

		var current = this.currentPage;
		var shift = Math.ceil(this.pagesPerPage/2);
		var limitLeft = current - shift;
		var start = 1;
		var end = 1;
		var startAdd = 0;

		if (limitLeft<0)
		{
			limitLeft = current;

			if (this.pagesPerPage >= shift-limitLeft)
			{
				var startAdd = shift - limitLeft;
			}
			else
			{
				var startAdd = 0;
			}
			start = 1;
		}
		else
		{
			limitLeft = shift;
			start = current - shift + 1;
		}

		var limitRight = this.totalPages - current;

		if (limitRight < (this.pagesPerPage/2))
		{
			limitRight = this.totalPages - current;
			end = this.totalPages;
			start -= shift - (this.totalPages-current) - (this.pagesPerPage%2);
		}
		else
		{
			limitRight = shift;
			end = strToFloat(current) + strToFloat(shift) - strToFloat(this.pagesPerPage%2) + strToFloat(startAdd);
		}

		if (start<1) start=1;
		if (end>this.totalPages) end = this.totalPages;

		for (var i=start; i<=end; i++)
		{
			if (i!=this.currentPage) html += this.GetTplInactive().apply({number: i});
			else html += this.GetTplActive().apply({number: i});
		}


		//next
		if (this.currentPage<this.totalPages)
		{
			html += this.GetTplNext().apply({number: this.currentPage+1});
		}

		html += '</div>';

	}
	return html;
}

HkPaging.prototype.GetTplPrev = function()
{
	return $.template('<a href="javascript:void(0)" page="${number}"> &lt;</a>');
}

HkPaging.prototype.GetTplNext = function()
{
	return $.template('<a href="javascript:void(0)" page="${number}"> &gt;</a>');
}

HkPaging.prototype.GetTplInactive = function()
{
	return $.template('<a href="javascript:void(0)" page="${number}">${number}</a>');
}

HkPaging.prototype.GetTplActive = function()
{
	return $.template('<span>${number}</span>');
}