(function( j ){
	var currentPage = 1;
	if(document.location.href.split('#')[1])
	{
		var tmp = document.location.href.split('#')[1];
		if(parseInt(tmp, 10) == parseInt(document.location.href.split('#')[1], 10))
		{
			currentPage = parseInt(tmp, 10);
		}
	}
	
	var methods = {
		'init': function (update) {
			if(typeof update == 'undefined')
			{
				update = false;
			}
			
			return this.each(function () {
				var paginate = j(this);
				
				if(paginate.find('.site-paginate-item').length == 0)
				{ // no items so return this and don't do anything
					return this;
				}
				
				if(update)
				{
					paginate.find('.site-paginate-item').show();
				}
				
				paginate.css('overflow', 'hidden');
				
				var parentHeight = paginate.parent().innerHeight();
				var siblingsHeight = 0;
				var childrenHeight = 0;
				paginate.parent().find('> *:not(.site-paginate, [class*="cms-"], [class*=ui-])').each(function () {
					siblingsHeight += j(this).outerHeight(true);
				});
				paginate.find('> *:not(.site-paginate-item)').each(function () {
					childrenHeight += j(this).outerHeight(true);
				});
				
				var pageHeight = parentHeight - siblingsHeight - childrenHeight;
				
				var tmpHeight = 0,
					first = true,
					prevHeight = 0,
					pages = [],
					current = 1;
				
				paginate.find('.site-paginate-item').each(function (index, element) {
					jElement = j(element);
					if(first)
					{
						pages[current] = [];
						pages[current].push(index);
						prevHeight = jElement.outerHeight(true);
						first = false;
					}
					else
					{
						if(prevHeight+jElement.outerHeight(true) < pageHeight)
						{
							pages[current].push(index);
							prevHeight = prevHeight+jElement.outerHeight(true);
						}
						else
						{
							current++;
							pages[current] = [];
							pages[current].push(index);
							prevHeight = jElement.outerHeight(true);
						}
					}
				}).hide();
				
				paginate.data('pages', pages);
				
				paginate.css('overflow', '');
				
				paginate.sitePaginate('changePage', currentPage);
				
				if(!update)
				{
					paginate.find('.site-paginate-navigation').delegate('.site-paginate-navigation-change', 'click', function (e) {
						paginate.sitePaginate('changePage', j(this).attr('href').replace('#', ''));
						e.preventDefault();
					});
				}
			});
		},
		'updateNavigation': function () {
			var paginate = j(this);
			paginate.find('.site-paginate-navigation').each(function () {
				var totalPages = paginate.data('pages').length - 1;
				var paginateNavigation = j(this);
				var paginateNavigationHtml = '';
				
				if(totalPages > 1)
				{
					paginateNavigationHtml += '<div class="site-paginate-navigation-left">';
					
					if(currentPage > 1)
					{
						paginateNavigationHtml += '<a href="#1" class="site-paginate-navigation-change site-paginate-navigation-goto-first"></a> <a href="#'+(currentPage-1)+'" class="site-paginate-navigation-change site-paginate-navigation-goto-prev"></a>';
					}
					
					paginateNavigationHtml += '</div><div class="site-paginate-navigation-center">';
		
					if((currentPage - 3) > 0)
					{
						paginateNavigationHtml += '.. ';
					}
					
					for(i = 1; i <= totalPages; i++)
					{
						if(i > (currentPage - 3) && i < (currentPage + 3))
						{
							if(i == currentPage)
							{
								paginateNavigationHtml += '<a href="#'+i+'" class="site-paginate-navigation-change site-paginate-navigation-goto-page active">'+i+'</a> ';
							} else {
								paginateNavigationHtml += '<a href="#'+i+'" class="site-paginate-navigation-change site-paginate-navigation-goto-page">'+i+'</a> ';
							}
						}
					}
					if((currentPage + 3) <= totalPages)
					{
						paginateNavigationHtml += ' ..';
					}
					
					paginateNavigationHtml += '</div><div class="site-paginate-navigation-right">';
					
					if((totalPages - currentPage) > 0)
					{
						paginateNavigationHtml += ' <a href="#'+(currentPage+1)+'" class="site-paginate-navigation-change site-paginate-navigation-goto-next"></a> <a href="#'+totalPages+'" class="site-paginate-navigation-change site-paginate-navigation-goto-last"></a>';
					}
					
					paginateNavigationHtml += '</div>';
					paginateNavigation.html(paginateNavigationHtml);
				} else {
					paginateNavigation.html('');
				}
			});
		},
		'changePage': function(page) {
			var paginate = j(this);
			var pages = paginate.data('pages');
			paginate.find('.site-paginate-item').hide();
			if(!pages[page])
			{
				page = 1;
			}
			j.each(pages[page], function (i, index) {
				paginate
					.find('.site-paginate-item:eq('+index+')')
						.show()
						.find('img')
							.each(function () {
								if(j(this).data('src'))
								{
									j(this).attr('src', j(this).data('src'));
									j(this).removeData('src');
								}
							});
				currentPage = parseInt(page, 10);
			});
			
			paginate.sitePaginate('updateNavigation');
		}
	};
	
	// setup the methods
	j.fn.sitePaginate = function(method) {
		if(methods[method])
		{
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if(typeof method === 'object' || ! method)
		{
			return methods.init.apply(this, arguments);
		}
		else
		{
			j.error('Method ' +  method + ' does not exist on jQuery.sitePaginate');
		} 
	};
	
})( jQuery );

j(document).ready(function () {
	setTimeout(function() {
		j('.site-paginate').sitePaginate();
	}, 100);
});

