/*------------------------------------------
*	Date: 31 January 2008
*	Author: Celine Chung & Colin Campbell-McPherson
*	Description: A function that assigns different
*	classes to a set of products and will show a
*	set of products per page according to the class
*	that was assign to them and hide the rest.
-------------------------------------------*/

//global variables
var $current_page;
var $total;
var $firstItem;
var $lastItem;
var $itemsPerPage = 6

$(document).ready(function() {
	init();
	
	$('a#more_products').click(function() {
		next_page();
		return false;
	});

	$('a#less_products').click(function() {
		previous_page();
		return false;
	});	
	
});



function init() {
	//gets the total image count
	$total = $('li.product-image').size();
	$current_page = 0
	
	for ( var i = 0; i <= $total; i++ ) {
		//pageNo is the count divided by the items per page
		var $pageNo = Math.floor(i/6);	
		$('li.product-image:eq(' + i + ')').addClass('page_' + $pageNo + '');
	};

	show_page($current_page);
	
};

function show_page($pageNo) {
	//hides all products
	$('li.product-image').hide();
	//only show products that have a certain class
	$('li.page_' + $pageNo + '').show();
	//finds the first item of the set of 6
	$firstItem = $pageNo * $itemsPerPage;
	//finds the last item of the set of 6
	$lastItem = $pageNo * $itemsPerPage + $itemsPerPage - 1;
};

function next_page () {
	//Check to make sure we're not going to a page past the last
	if ( $lastItem < $total ) {
		//Add one to the current page count
		$current_page++;
		show_page($current_page);
	};
};

function previous_page () {
	//Check to make sure we're not going to page -1
	if ( $firstItem > 0 ) {
		//Remove one from the current page count
		$current_page--;
		show_page($current_page);
	};
};