/**
 * @fileoverview Pagination
 * 
 */
var paginateProducts = function(){

	// 
	var $pages = null,
	
	// 
	$pagination = null,
	
	// 
	$title = null,
	
	//
	$previous = null,
	
	//
	$next = null,
	
	//
	$viewAll = null,
	
	//
	currentPage = 1,
	
	// 
	totalPages = 1,
	
	//
	pageWidth = 0,
	
	//
	viewingAll = false;
	

	
	/**
	 * The options passed through to this function
	 *
	 * @var Object
	 * @private
	 */
	var options = {

	};
	
	
	/**
	 * Initialise the functionality
	 * @param {Object} options The initialisation options
	 * @return void
	 * @public
	 */
	var init = function(initOptions) {
		
		// save any options sent through to the intialisation script, if set
		for (var option in options) {
			if (!!initOptions[option] || initOptions[option] === false) {
				options[option] = initOptions[option];
			}
			
			// error check, if no element is specified then stop
			if (!options[option] && options[option] !== false && options[option] !== 0) {
				throw('Required option not specified: ' + option);
				//return false;
			}
		}
		
		// IE6 'fix'
		if (LibMan.isMSIE && LibMan.vIE == 6) {
			return;
			//$pagination.css({zoom:1});
		}
		
	
		// init pagination
		$pages = $('#content div.page');
		totalPages = $pages.length;
		if (totalPages < 2) return;

		pageWidth = $pages.filter(":first").width();

		// create pagination
		$pagination = $("<ul />")
			.attr('id',  'page-navigation')
			.addClass('horiznavlist clearfix')
			.appendTo('#content');
			
		$title = $("<li />")
			.addClass('first')
			.html('Page <span id="page-current">1</span> of ' + totalPages)
			.appendTo($pagination);

		$previous = $("<li />")
			.html('<a href="#" id="previous">Previous</a>')
			.addClass('disabled')
			.appendTo($pagination);

		$next = $("<li />")
			.html('<a href="#" id="next">Next</a>')
			.appendTo($pagination);

		$viewAll = $("<li />")
			.addClass('last')
			.html('<a href="#" id="view-all">View All</a>')
			.appendTo($pagination);
			
		
		// set links
		$next.find('a').bind('click', function(e){
			this.blur();
			e.preventDefault();
			pageNext(this);
		});
		$previous.find('a').bind('click', function(e){
			this.blur();
			e.preventDefault();
			pagePrevious(this);
		});
		$viewAll.find('a').bind('click', function(e){
			this.blur();
			e.preventDefault();
			showAll(this);
		});
		
		
		// left and right keypress
		$(document).bind('keypress', function(e){
			if (e.keyCode == 37 && !viewingAll) {
				pagePrevious();
			}
			if (e.keyCode == 39 && !viewingAll) {
				pageNext();
			}
			
		});
	};
	
	
	
	/*
	 *
	 */
	var pageNext = function(el) {
		if (currentPage < totalPages) {
			currentPage++;
			$("#pages-clip").stop().animate({
				left:-((currentPage-1)*pageWidth)+"px"
			}, 500);
			$("#page-current").text(currentPage);
			$previous.removeClass('disabled');
			if (currentPage == totalPages) {
				$next.addClass('disabled');
			}
		}
	};
	
	
	
	/*
	 *
	 */
	var pagePrevious = function(el) {
		if (currentPage > 1) {
			currentPage--;
			$("#pages-clip").stop().animate({
				left:-((currentPage-1)*pageWidth)+"px"
			}, 500);
			$("#page-current").text(currentPage);
			$next.removeClass('disabled');
			if (currentPage == 1) {
				$previous.addClass('disabled');
			}
		}
	};
	
	
	
	/*
	 *
	 */
	var showAll = function(el) {
		$("section").css({
			overflow:"visible"
		});
		$("div.page").css({
			"float":"none"
		});
		$("#pages-clip").css({
			width:"auto",
			left:0
		});
		
		$("#page-navigation").remove();	
		
		viewingAll = true;
	};
	
	
	
	/*
	 * 
	 */
	return {
		init: init
	};
	
}();


