Sparcq = ( this["Sparcq"] || {}  );


if (!Sparcq.Paginator) {
	Sparcq.Paginator = function (settings) {
		this.init(settings);
	};
}

Sparcq.Paginator.prototype.init = function(args) {
	if (args) {
	}

	this.callbackMethod = args.callback;
	this.paginator_el = jQuery('.paginator', args.paginatorContainer);
	this.numPerPage = args.numPerPage || 10;

	this.appendItem = args.pageContainer;
	this.pageHolder = jQuery('.item_page', this.appendItem);
	this.currentPage = 1;
	this.totalPages = 1;

	this.pageCache = {};

	this.pageIdPrefix = 'page_item_holder_' + Math.ceil(Math.random() * 999999) + '_';
	this.showPagingResults = ( typeof(args.showPagingResults) == 'undefined' ) ? true : args.showPagingResults;
	this.getPage(1);
};

Sparcq.Paginator.prototype.getNextPage = function() {
	this.getPage(parseInt(this.currentPage) + 1);
};

Sparcq.Paginator.prototype.createCachedPage = function(item,new_page) {
	this.pageCache[new_page] = item;
	this.createPageNode(new_page);
	this.showPage(new_page);
	this.currentPage = new_page;
	this.update();
};

Sparcq.Paginator.prototype.showPageIfCached = function(page_num) {
	if (this.pageCache[page_num]) {
		this.showPage(page_num);
		this.update();
		return 1;
	}

	return 0;
};

Sparcq.Paginator.prototype.getPrevPage = function() {
	this.getPage(this.currentPage-1);
};

Sparcq.Paginator.prototype.setTotalPages = function(pages) {
	this.totalPages = pages;
};

Sparcq.Paginator.prototype.setCurrentPage = function(page) {
	this.currentPage = page;
};

Sparcq.Paginator.prototype.update = function() {
	var cnt = '';

	if (!this.totalPages || this.totalPages <= 1) {
		this.paginator_el.hide();
		return;
	}
	this.paginator_el.show();

	if (this.currentPage > 1) {
		cnt += Sparcq.SQ.getLink('&laquo; back','#', null, { 'class': 'prev_link' });
	} else {
		cnt += '&laquo; back';
	}

	cnt += '&nbsp;&nbsp;';
	
	if (this.currentPage < this.totalPages) {
		cnt += Sparcq.SQ.getLink('more &raquo;','#',null,{ 'class': 'more_link' });
	} else {
		cnt += 'more &raquo;';
	}

	if (this.showPagingResults) {
		cnt += '&nbsp;&nbsp;Page ' + this.currentPage + ' of ' + this.totalPages;
	}

	this.paginator_el.html(cnt);

	var self = this;
	jQuery('.more_link',this.paginator).click(function() {
		self.getNextPage();
		return false;
	});
	jQuery('.prev_link',this.paginator).click(function() {
		self.getPrevPage();
		return false;
	});
};

Sparcq.Paginator.prototype.checkPageCached = function(page_num) {
//	return this.pageCache[page_num];
	return false;
};

Sparcq.Paginator.prototype.getPage = function(page_num,callback) {
	if (this.showPageIfCached(page_num)) {
		return;
	}
	
//	this.callbackMethod(page_num,callback);
	this.callbackMethod(page_num);
};

Sparcq.Paginator.prototype.showPage = function(page_num) {
	if (this.currentPage == page_num) {
		this.getPageNode(this.currentPage).show();
		this.getPageNode(this.currentPage);
		return;
	}
	this.getPageNode(this.currentPage).hide();
	this.getPageNode(page_num).show();
	this.currentPage = page_num;
};

Sparcq.Paginator.prototype.createPageNode = function(page_num) {
	var new_page = this.pageHolder.clone();	
	new_page.show();
	new_page.attr('id',this.getPageId(page_num));
	new_page.appendTo(this.appendItem);
	return new_page;
};

Sparcq.Paginator.prototype.getPageNode = function(page_num) {
	return jQuery('#' + this.getPageId(page_num));
}

Sparcq.Paginator.prototype.getPageId = function(page_num) {
	return this.pageIdPrefix + page_num;
};

// This will clear all the 
Sparcq.Paginator.prototype.clearCache = function() {
	jQuery('div[id*=' + this.pageIdPrefix + ']').remove();
	this.pageCache = {};
	return true;
};

