/**
 * @fileoverview Pagination
 * 
 */
var paginateCollections = function(){

	// 
	var $pages = null,
	
	// 
	$pagination = null,
	
	// 
	$title = null,
	
	//
	$previous = null,
	
	//
	$next = null,
	
	//
	collectionCount = 0,
	
	//
	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();
		
		collectionCount = $pages.find('div.collection-block').length;

		// create pagination
		$pagination = $("<p />")
			.attr('id',  'collection-nav')
			.appendTo('#intro');

		$title = $("<span />")
			.addClass('first')
			.html('Showing <span id="count">1-4</span> of ' + collectionCount)
			.appendTo($pagination);

		$next = $("<span />")
			.html('<a href="#" id="nav-next">Next</a>')
			.appendTo($pagination);

		$previous = $("<span />")
			.html('<a href="#" id="nav-previous" class="disabled">Previous</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);
		});
		
		
		// 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);
			updateCount();
			$("#nav-previous").removeClass('disabled');
			if (currentPage == totalPages) {
				$("#nav-next").addClass('disabled');
			}
		}
	};
	
	
	
	/*
	 *
	 */
	var pagePrevious = function(el) {
		if (currentPage > 1) {
			currentPage--;
			$("#pages-clip").stop().animate({
				left:-((currentPage-1)*pageWidth)+"px"
			}, 500);
			updateCount();
			$("#nav-next").removeClass('disabled');
			if (currentPage == 1) {
				$("#nav-previous").addClass('disabled');
			}
		}
	};
	
	
	
	/*
	 *
	 */
	var updateCount = function() {
		var count = (1+((currentPage-1)*4));
		
		var end = 4+((currentPage-1)*4);
		
		if (currentPage == totalPages) {
			if (count == collectionCount) {
				count = collectionCount;
			} else {
				count += "-" + collectionCount;
			}

		} else {
			count += "-" + end;
		}
		
		$("#count").text(count);
		
	};
	
	
	
	/*
	 * 
	 */
	return {
		init: init
	};
	
}();


