$(document).ready(function()
{
	paging.prepare();
});
//TODO: Loading ...
var paging = {
		container: null,
		currentHash: null,
		currentPage: null,
		params: null,
		
		prepare: function()
		{
			paging.container = $('#list-container');
			setInterval(paging.hashListener, 25);
		},
		
		setParams: function(params)
		{
			paging.params = params;
		},
		
		flushHash: function()
		{
			paging.currentHash = null;
		},
		
		hashListener: function()
		{
			if (paging.currentHash != location.hash)
			{
				var match = /^#page=([0-9]+)$/.exec(location.hash);
				if (match && match.length > 0)
				{
					paging.navigate(match[1]);
				}
				paging.currentHash = location.hash;
			}
		},
		navigate: function(page)
		{			
			if (paging.currentPage != page)
			{
				paging.params.page = page;
												
				var onComplete = function(response)
				{
					if (!response.error)
					{
						paging.container.empty();
						paging.container.html(response.html).show();
					}
				}
				
				$.post(paging.params.url, paging.params, onComplete, 'json');
			}
			paging.currentPage = page;
		}
} 
