/**
 * Return a new instance of VCMPaginator
 * @classDescription Class for the Paginator
 * @param {VCMContent} content Reference to VCMContent parent
 * @return {VCMPaginator} Returns a new VCMPaginator object
 * @constructor
 */
function VCMPaginator(content) {
	this._content = content;
	this._opts = {
		num_edge_entries: 1,
		items_per_page: 15,
		next_text: '>',
		prev_text: '<'
	};
}

VCMPaginator.prototype = {
	/**
	 * Reference to VCMContent
	 * @type {VCMContent}
	 */
	_content: null,
	/**
	 * Options
	 * @type {Object}
	 */
	_opts: null,
	/**
	 * jQuery Pagination reference
	 * @type {jQuery.pagination}
	 */
	_jPagination: null,
	/**
	 * jQuery Thumbnails reference
	 * @type {jQuery}
	 */
	_jThumbnails: null,
	/**
	 * jQuery reference to Previous button
	 * @type {jQuery}
	 */
	_jPrevious: null,
	/**
	 * jQuery reference to Next button
	 * @type {jQuery}
	 */
	_jNext: null,
	
	_currentPage: null,
	
	/**
	 * Initialize paginator
	 */
	init: function(){
		console.log('VCMPaginator.init()');
		
		this._jThumbnails = $('#hiddenThumbnails > div');
		this._jPrevious = $('#divPrevious');		
		this._jNext = $('#divNext');
		this._currentPage = -1;
		
		// Callback		
		this._opts.callback = $.proxy(this, 'onPageSelect'); 
		
		// Create the paginator
		var pagination = $(".pagination");
		if (pagination.children().length > 0) {
			this._opts.current_page = parseInt(pagination.children('.current:not(.next, .prev):first').text()) - 1;
		}
		this._jPagination = pagination.pagination(this._jThumbnails.length, this._opts);
		
		// Hide no results in details page
		$('#noResults').css('visibility', ($('#detailSouthContainer').length == 1) ? 'hidden' : 'visible');
		
		// Add events
		this._jPrevious.click($.proxy(this, 'onPreviousClick'));
		this._jNext.click($.proxy(this, 'onNextClick'));
	},
	
	/**
	 * Destroy the paginator
	 */
	destroy: function() {
		console.log('VCMPaginator.destroy()');
		// Remove events
		this._jPrevious.unbind('click');
		this._jNext.unbind('click');
		// Clear reference
		this._jThumbnails = null;
		this._jPagination = null;
		this._jPrevious = null;
		this._jNext = null;
	},
	
	/**
	 * On previous button click
	 * @param {Object} evt The Event object
	 */
	onPreviousClick: function(evt) {
		// Previous page
		this._jPagination.get(0).prevPage();
		// Cancel default event
		return false;
	},
	
	/**
	 * On next button click
	 * @param {Object} evt The Event object
	 */
	onNextClick: function(evt) {
		// Next page
		this._jPagination.get(0).nextPage();
		// Cancel default event
		return false;
	},
	
	/**
	 * Set an option
	 * @param {String} name		The name of the option to set
	 * @param {Object} value	The value of the option
	 */
	setOptions: function(name, value) {
		this._opts[name] = value;
	},
	
	/**
	 * Method triggered when page is selected
	 * @param {Object} pageIndex
	 * @param {Object} jq
	 */
	onPageSelect: function(pageIndex, jq){
		console.log('VCMPaginator.onPageSelect()');
		// If we're already on this page, quit
		if (pageIndex == this._currentPage) return false;
		this._currentPage = pageIndex;
		//
		var nbThumbs = this._jThumbnails.length,
			itemsPerPage = this._opts.items_per_page,
			offset = pageIndex * itemsPerPage,
			me = this;
		
		// Hide the featured event viewer if not on the first page
		$('#featuredEventViewer, #dynamicFeaturedEventViewer').css('visibility', (pageIndex != 0 || nbThumbs == 0) ? 'hidden' : 'visible');
		// Remove thumbs events
		$('.showLastMinute').unbind('click');
		$('.detailActivity').unbind('click');
		// Generate new content
		var newContent = this._jThumbnails.slice(offset, offset + itemsPerPage).clone();
		$('#thumbnails').empty().append(newContent);
		// Add thumbs events
		$('.showLastMinute').click(function() {
			$('#lastMinute').click();
			return false;
		});
		// TODO Centraliser le click du détails d'un activité
		$('.detailActivity').click(function() {
			me._content.showActivity(VCMUtils.findNumber($(this).attr('id')));
			return false;
		});
		// Show/hide previous button
		if (this._currentPage != 0) {
			this._jPrevious.show();
		}
		else {
			this._jPrevious.hide();
		}
		
		// Show/hide next button
		var showNext = (itemsPerPage * (this._currentPage + 1)) <= nbThumbs;

		if (showNext) {
			this._jNext.show();
		} else {
			this._jNext.hide();
		}
		// Make sure every paginator is on the same page
		if (this._jPagination) {
			this._jPagination.each(function(e){
				this.selectPage(pageIndex);
			});
		}
		//
		return false;
	}
};


