// Default settings for this pager.
function pagerGetDefaultOptions() {
	return {
		search_page_elements		: ['.listing-table tbody tr'],	// Array of elements to look for to paginate on a page.
		pager_wrapper_wrapper	: null, 									// Wrapper for the entire pager.
		pager_control_wrapper	: null, 									// Wrapper for the pager controls.
		pager_html_element 		: '.pagination',						// Element where the pager should be displayed.
		pager_current_page 		: null,									// Element to display the page value.
		page_view_all_element	: '.pager-view-all',					// Element to display the page view all link.
		number_of_pages 			: 0,										// Number of pages to display.
		number_of_items 			: 0,										// Number of pages to display.
		item_per_page 				: 6,										// Max number of elements on a page.
		page_element				: null,									// Will hold value of current page element to use.
		navigation_link_advance_element	: '.pager-advance',		// The element that holds the link to go to the next page.
		navigation_link_reverse_element	: '.pager-reverse',		// The element that holds the link to go to the previous page.
		paging_options							: null						// Options to pass to the aqPaging plugin, leaving null will result in the default being used.
	};
}

// Show one item.
function showItem(item) {
	$(item).parent().fadeIn('slow', function() {
		$(item).fadeIn('slow', function() {
			$(item).slideDown();
		});
	});
}

// Hide one item.
function hideItem(item) {
	$(item).hide();
}

// Hide all items
function hideAllItems(options) {
	$(options.page_element).each(function (i) {
		hideItem(this);
	});	
}

// Show all items
function showAllItems(options) {
	$(options.page_element).each(function (i) {
		showItem(this);
	});	
}

// Hide paginator nav.
function hidePaginator(options) {
	// If there's a controls wrapper we'll hide that instead.
	if (options.pager_control_wrapper) {
		$(options.pager_control_wrapper).hide();
	}
	
	// Just hide and show the html element for the pager.
	else {
		$(options.pager_html_element).hide();
	}	
}

// Show paginator nav.
function showPaginator(options) {
	// If there's a controls wrapper we'll show that instead.
	if (options.pager_control_wrapper) {
		$(options.pager_control_wrapper).show();
	}
	// Just hide and show the html element for the pager.
	else {
		$(options.pager_html_element).show();
	}
}

// Move forward in the pages.
// TODO add support for more then one pager on a document.
function advanceNextPage(options, page_num) {
	// Check if the next page is in the range of pages we found.
	if (page_num + 1 <= options.number_of_pages) {
		$.fn.aqPaging.flip('aqPaging_1',page_num+1,options.number_of_pages);
	} 
	
	// Go back to the first page.
	else {
		$.fn.aqPaging.flip('aqPaging_1',1,options.number_of_pages);
	}
}

// Move Reverse in the pages.
function reverseNextPage(options, page_num) {
	// Go to the last page if the next one is less then the first page.
	if (page_num - 1 < 1) {
		$.fn.aqPaging.flip('aqPaging_1',options.number_of_pages,options.number_of_pages);
	} 
	
	// Go back to the previous page.
	else {
		$.fn.aqPaging.flip('aqPaging_1',page_num-1,options.number_of_pages);
	}
}

// Move a page in a specific direction.
function movePage(options, page_num, direction) {
	if (direction && direction == 'advance') {
		advanceNextPage(options, page_num);
	}
	else if (direction && direction == 'reverse') {
		reverseNextPage(options, page_num);
	}
}

// Adds a single navigation link.
function addNavigationLink(options, page_num, direction) {
	var link_added	= false;
	
	// If there was a direction passed, and the option for that directions element was set.
	if (direction && direction == 'advance' && options.navigation_link_advance_element) {
		link_added	= true;
		$(options.navigation_link_advance_element).html('<a class="page-move-' + direction + '" href="javascript:void(0);" ><span>' + direction + '</span></a>');		
	}
	else if (direction && direction == 'reverse' && options.navigation_link_reverse_element) {
		link_added	= true;
		$(options.navigation_link_reverse_element).html('<a class="page-move-' + direction + '" href="javascript:void(0);" ><span>' + direction + '</span></a>');
	}

	if (link_added) {
		// Add event to link.		
		$('.page-move-' + direction).bind('click', function(el) {
			movePage(options, page_num, direction);
		});		
	}
}

// Add all navigation links.
function addNavigationLinks(options, page_num) {
	if (options.navigation_link_advance_element) {
		addNavigationLink(options, page_num, 'advance');
	}
	if (options.navigation_link_reverse_element) {
		addNavigationLink(options, page_num, 'reverse');
	}
}

// Handles the logic to change pages.
function changePage(options, page_num)
{
	hideAllItems(options);	// Hide all.

	// Add view all link if option set.
	if (options.page_view_all_element) {
		addViewAllLink('View All', options);
	}
	
	// Iterate over each item and show it if it is with-in the range of the current page.
	$(options.page_element).each(function (i) {
		if (i < (options.item_per_page * page_num) && i >= ((options.item_per_page * page_num) - options.item_per_page)) {
			showItem(this);
		}
	});

	// Check if we have a place to edit.
	if (typeof $(options.pager_current_page) == 'object' && options.pager_current_page) {
		$(options.pager_current_page).html('Showing Page '+page_num);
	}
}


// Options for aqPaging.
function getDefaultPagingOptions(options) {
	return {
		// Functional options.
		current: 1, 
		pages: options.number_of_pages, 
		flip: true,
		
		// Call back happens on every page.
		cb: function(p) {
			changePage(options, p);
			addNavigationLinks(options, p);
		} 
	};
}

/** 
 * Set of functions to create paginations and pages on a page.
 *
 * @TODO: make jquery plugin out of this.
 * @Author tfoley@interactivate.com
**/
function initPagination(options) {
	var 	settings	= $.extend({}, pagerGetDefaultOptions(), options);
	
	for (var i = 0; i < settings.search_page_elements.length; i++) {
		// Check if the element exists on this page.
		if ($(settings.search_page_elements[i]).html()) {
			
			settings.page_element	= settings.search_page_elements[i];

			$(settings.search_page_elements[i]).each(function (c) {
				settings.number_of_items++;
			});

			// Set the number of pages.
			settings.number_of_pages	= Math.ceil(settings.number_of_items / settings.item_per_page);
			settings.paging_options		= $.extend({}, getDefaultPagingOptions(settings), settings.paging_options);

			// Add a pager for found group of elements.
			addPager(settings);	
		} else {
			$(options.pager_wrapper_wrapper).hide();
		}
	}
}

// View Some.
function addViewSomeLink(name, options) {
	// Add link to page.
	$(options.page_view_all_element).html('<a class="page-view-all" href="javascript:void(0);" >' + name + '</a>');	
	
	// Add event to link.		
	$('.page-view-all').bind('click', function(el) {

		// Reset the pagination.
		initPagination(options);
		
		// Show the paginator.
		showPaginator(options);
	});
}

// Creates a view all link.
function addViewAllLink(name, options) {
	// Add link to page.
	$(options.page_view_all_element).html('<a class="page-view-all" href="javascript:void(0);" >' + name + '</a>');	
	
	// Add event to link.		
	$('.page-view-all').bind('click', function(el) {
		showAllItems(options);
		addViewSomeLink('Reset', options);
		hidePaginator(options);
	});
}


// Adds one pager.
function addPager(options) {
	// Check if we should add in the view all element.
	
	if (options.page_view_all_element) {
		addViewAllLink('View All', options);
	}

	// Create paging container.
	$(options.pager_html_element).aqPaging(options.paging_options);
}

