var tips = {
	// Various settings needed by the singleton.
	// Note: In this case, some of the information may be stored in the global var window.fiteness
	settings: {
		pageSize: 4
	}, // END settings


	
	// Selectors for the html elements used by the singleton
	elements: {
		container: '#tips-section',
		individual: '.tip',
		prev: '#prevTipsLink',
		next: '#nextTipsLink',
		catNav: '#tipNav'
	}, // END elements
	
	
	/*
	 * init()
	 * 
	 * Initialize the singleton
	 */
	init: function() {
		var that = this;
		
		// Reset the nav to "All"
		$('.active', that.elements.catNav).removeClass('active');
		$('li:first', that.elements.catNav).addClass('active');
		
		// Create an internal reference to the data that's already loaded.
		that.data = window.fitness.tipData;
		
		// Create all needed event handlers.
		that.createHandlers();
		
		// Load 4 tips into the viewer.
		that.showTips('all');
		
	}, // END init()
	
	
	
	/* FUNCTIONS IN ALPHABETICAL ORDER ****/
	
	
	
	/*
	 * createHandlers()
	 * 
	 * Create all the needed event handlers for the singleton
	 */
	createHandlers: function() {
		var that = this;
		
		// Each handler calls unbind first in order to make sure we don't pile
		// up handlers since each time the section is loaded init is called.
		
		// Click handler for previous button
		$(that.elements.prev).unbind().click(function(event){
			that.showTips(that.currentCategory, that.currentPage - 1);
		});
		
		// Click handler for next button
		$(that.elements.next).unbind().click(function(event){
			that.showTips(that.currentCategory, that.currentPage + 1);
		});
		
		// Click handler for tip category nav
		$(that.elements.catNav + ' a').unbind().click(function(){
			var idArray = $(this).parent().attr('id').split('-');
			
			$('.active', that.elements.catNav).removeClass('active');
			$(this).parent().addClass('active');
			
			that.showTips(idArray[1]);
		});
	}, // END createHandlers()
	
	
	
	/*
	 * showTips()
	 * 
	 * Display tips for the requested section and page
	 */
	showTips: function (category, page) {
		var that = this;
		
		// Sanitize and filter data as needed
		var $context = '';
		if (category == 'all' || category == '' || category === undefined) {
			category = '';
			$context = $(that.data);
			
		} else {
			category = $.trim(category);
			$context = $(that.data).filter('#' + category);
		}
		that.currentCategory = category;
		
		// Set the current page
		if (page < 1 || page == '' || page === undefined) {
			page = 1;
		} else {
			page = parseInt(page, 10);
		}
		that.currentPage = page;
		
		// Calculate the total number of pages of tips for the selected section
		var totalCategoryPages = Math.ceil($(that.elements.individual, $context).length / that.settings.pageSize);
		
		// Show/hide the prev/next buttons as needed
		if (page > 1) {
			$(that.elements.prev).show();
		} else {
			$(that.elements.prev).hide();
		}
		
		if (page >= totalCategoryPages) {
			$(that.elements.next).hide();
		} else {
			$(that.elements.next).show();
		}
		
		// Calculate the index range of the page.
		var upperBound = page * that.settings.pageSize;
		var lowerBound = upperBound - that.settings.pageSize;
		
		// Remove any tips that are present.
		$(that.elements.individual, that.elements.container).remove();
		
		// Iterate over just the elements we're adding to the page.
		var tips = $(that.elements.individual, $context);
		for (var i = lowerBound; i < upperBound; i++) {
			$(tips[i]).clone().appendTo(that.elements.container);
		}
	}, // END showTips()
	
	
	
	/*
	 * This is just here so you don't have to worry about the trailing , (comma) on the last item.
	 */
	dummy: ''
	
}; // END tips()

