var faqyou_paginator = {
	pages : new Array(),
	pageSize : 40,
	hash : 'asdffdsaasdffdsa1230987',
	
	/**
	 * Figures out which questions belong on which pages.  Stores them into
	 * arrays so that we don't have to worry about too much in the way of
	 * performance issues later.
	 */
	init : function() {
		var cur = new Array();
		// Assemble our list of pages
		jQuery('div.faq-question').each(function(idx) {
			// Push the current ones onto the page array
			if(idx % faqyou_paginator.pageSize == 0 && idx != 0) {
				faqyou_paginator.pages.push(cur);
				cur = new Array();
			}
			// Add it to the current page
			cur.push(this);
		});
		
		// Push the last one
		faqyou_paginator.pages.push(cur);
		
		// Hide everything not the current page
		//faqyou_paginator.hideAll();
	},
	
	/**
	 * Hides all of the questions on the page, in preparation for
	 * opening the new page's list.
	 */
	hideAll : function() {
		jQuery('div.faq-question').hide();
	},
	
	/**
	 * Displays the proper page that we wanted to be displayed.
	 */
	displayPage : function() {
		// We don't have to do anything if the JavaScript hasn't changed
		if(faqyou_paginator.hash == window.location.hash) return;
		else faqyou_paginator.hash = window.location.hash;
		
		var myHash = window.location.hash;
		var pg = parseInt(myHash.replace(/#/, ''));
		if(!pg || pg < 1 || pg > faqyou_paginator.pages.length) pg = 1;
		
		// Correct because people count from 1
		var tar = faqyou_paginator.pages[pg - 1];
		faqyou_paginator.hideAll();
		jQuery.each(tar, function() {
			jQuery(this).show();
		});
		
		// Make the pagination selector
		jQuery('#faq-paginator').remove();
		jQuery('div.faq-question:last').after('<div id="faq-paginator" style="margin-top: 15px">Select Page: </div>');
		jQuery.each(faqyou_paginator.pages, function(idx) {
			if(idx == pg-1)
				jQuery('#faq-paginator').append('<span style="margin-left: 5px; margin-right: 5px">' + (idx+1) + '</span>');
			else
				jQuery('#faq-paginator').append('<a href="#' + (idx+1) + '" style="margin-left: 5px; margin-right: 5px">' + (idx+1) + '</a>');
		});
	}
};

jQuery(document).ready(function() {
	faqyou_paginator.init();
	setInterval("faqyou_paginator.displayPage()", 200);
});

