// Javascript used to detect pagination modules (any uls with a "pagination" class) on a page and enable pagination functionality
// Note: Requires jquery

$(document).ready(function() {
	// Search document for pagination modules
	$('ul.pagination').each(function(index) {
		// Find all of the pagination options and convert them to jquery data on video category row instead of hidden form fields
		$(this).find('input.hidden_pagination_options').each(function(index) {
			var fieldName = $(this).attr("name");
			var pagination_options = new Array();
			// Determine the option name
			var myregexp = /video_category_row_([0-9]+)_([a-zA-Z0-9_\-]+)/;
			var match = myregexp.exec(fieldName);
			if (match != null) {
				rowNumber = match[1];
				optionName = match[2];
			} else {
				rowNumber = false;
				optionName = false;
			}
			// Assign the pagination option to jquery data key:value pairs on video category row
			if(optionName && rowNumber){
				$('#video_category_row_'+rowNumber).data('pagination_options_'+optionName, $(this).val());
			}
		});
		// The following pagination option variables should now be located in the video category row's data object:
		/*
			pagination_options_video_url_prefix
			pagination_options_video_category_row_num
			pagination_options_display_type
			pagination_options_event_id
			pagination_options_evcat_id
			pagination_options_show_id
			pagination_options_svcat_id
			pagination_options_preset_id
			pagination_options_current_video_id
			pagination_options_videos_per_page
			pagination_options_page
			pagination_options_current_page
			pagination_options_page_count
		*/
		// Handle all of the pagination functions and states for the current video thumbnail row
		var VideoCategoryRowDivID = $(this).parent().attr("id");
		var VideoCategoryRow = $('#'+VideoCategoryRowDivID);
		HandlePaginationButtons(VideoCategoryRowDivID);
		// Set up the link for the last page
		var LastPageLink = VideoCategoryRow.find('ul.pagination > li > a.pagination_link_last');
		LastPageLink.click(function(){
			var VideoCategoryRowDivID = $(this).parent().parent().parent().attr("id");
			LastPaginationPage(VideoCategoryRowDivID);
		});
		// Set up event handler for page input number field
		var PageNumberInputField = VideoCategoryRow.find('ul.pagination > li > input.pagination_input_page_number');
		PageNumberInputField.keypress(function(event){
			// Listen for "enter" key and other key codes
			// enter: 13
			// up: 38
			// right: 39
			// down: 40
			// left: 37
			if (event.keyCode == '13' || event.keyCode == '38' || event.keyCode == '39' || event.keyCode == '40' || event.keyCode == '37') {
				event.preventDefault();
				var VideoCategoryRowDivID = $(this).parent().parent().parent().attr("id");
				var VideoCategoryRow = $('#'+VideoCategoryRowDivID);
				var current_page = parseInt(VideoCategoryRow.data('pagination_options_current_page'));
				var page_count = parseInt(VideoCategoryRow.data('pagination_options_page_count'));
				var new_page_num = parseInt(PageNumberInputField.val());
				if(event.keyCode == '38' || event.keyCode == '39'){
					// Up or Right pressed. Increment by one
					var new_page_num = parseInt(PageNumberInputField.val())+1;
				}
				if(event.keyCode == '40' || event.keyCode == '37'){
					// Up or Right pressed. Reduce by one
					var new_page_num = parseInt(PageNumberInputField.val())-1;
				}
				// Enter key (and error-checking for other):
				if(new_page_num<0){
					// Lower than zero. Go to page 1.
					GoToPaginationPage(VideoCategoryRowDivID, 1);
				}else if(new_page_num<=page_count && page_count>0){
					// Go to specified page
					GoToPaginationPage(VideoCategoryRowDivID, new_page_num);
				}else if(new_page_num>page_count){
					// Go to last page
					GoToPaginationPage(VideoCategoryRowDivID, page_count);
				}else{
					// Invalid entry. Reset to current page.
					PageNumberInputField.val(current_page);
				}
			}
		});
		// Preload the next pagination page
		var current_page = parseInt(VideoCategoryRow.data('pagination_options_current_page'));
		GoToPaginationPage(VideoCategoryRowDivID, current_page, true);
	});
});

function HandlePaginationButtons(VideoCategoryRowDivID){
	var VideoCategoryRow = $('#'+VideoCategoryRowDivID);
	var current_page = parseInt(VideoCategoryRow.data('pagination_options_current_page'));
	var page_count = parseInt(VideoCategoryRow.data('pagination_options_page_count'));
	
	// Buttons and links
	var FirstLink = VideoCategoryRow.find('ul.pagination > li > a.pagination_link_first');
	var FirstButton = VideoCategoryRow.find('ul.pagination > li > a.pagination_link_first > img.pagination_button_first');
	var PreviousLink = VideoCategoryRow.find('ul.pagination > li > a.pagination_link_previous');
	var PreviousButton = VideoCategoryRow.find('ul.pagination > li > a.pagination_link_previous > img.pagination_button_previous');
	var NextLink = VideoCategoryRow.find('ul.pagination > li > a.pagination_link_next');
	var NextButton = VideoCategoryRow.find('ul.pagination > li > a.pagination_link_next > img.pagination_button_next');
	
	/* --------------------------------------------- */
	// Enable/disable buttons based on current page
	/* --------------------------------------------- */	
	if(current_page == 1){
		// Disable the the "first" button
		FirstLink.removeClass('enabled');
		FirstButton.removeClass('enabled');
		FirstLink.addClass('disabled');
		FirstButton.addClass('disabled');
		// Disable the "previous" button
		PreviousLink.removeClass('enabled');
		PreviousButton.removeClass('enabled');
		PreviousLink.addClass('disabled');
		PreviousButton.addClass('disabled');
	}else if(current_page>1){
		// Enable the "first" button
		FirstLink.removeClass('disabled');
		FirstButton.removeClass('disabled');
		FirstLink.addClass('enabled');
		FirstButton.addClass('enabled');
		// Enable the "previous" button
		PreviousLink.removeClass('disabled');
		PreviousButton.removeClass('disabled');
		PreviousLink.addClass('enabled');
		PreviousButton.addClass('enabled');
	}
	if(current_page==page_count){
		// Disable the "next" button
		NextLink.removeClass('enabled');
		NextButton.removeClass('enabled');
		NextLink.addClass('disabled');
		NextButton.addClass('disabled');
	}else if(current_page<page_count){
		// Enable the "next" button
		NextLink.removeClass('disabled');
		NextButton.removeClass('disabled');
		NextLink.addClass('enabled');
		NextButton.addClass('enabled');
	}

	/* --------------------------------------------- */
	// Handle the "first" button
	/* --------------------------------------------- */
	if(FirstLink.hasClass('enabled')){
		/* --------------------------------------------- */
		// First button is enabled:
		/* --------------------------------------------- */
		FirstButton.attr("src", "/images/pagination/arrow-first-enabled.gif");
		// Set up rollover effect for first button
		FirstButton.unbind('mouseover');
		FirstButton.mouseover(function(){
            $(this).attr("src", "/images/pagination/arrow-first-over.gif");
		});
		// Set up rollout effect for first button
		FirstButton.unbind('mouseout');
		FirstButton.mouseout(function(){
            $(this).attr("src", "/images/pagination/arrow-first-enabled.gif");
		});
		// Set up the click handler for the first button
		FirstLink.unbind('click');
		FirstLink.click(function(){
			var VideoCategoryRowDivID = $(this).parent().parent().parent().attr("id");
			FirstPaginationPage(VideoCategoryRowDivID);
		});
	}else{
		/* --------------------------------------------- */
		// First button is disabled (end of the line):
		/* --------------------------------------------- */
        FirstButton.attr("src", "/images/pagination/arrow-first-disabled.gif");
		// Set up rollover effect for first button
		FirstButton.unbind('mouseover');
		FirstButton.mouseover(function(){
            $(this).attr("src", "/images/pagination/arrow-first-disabled.gif");
		});
		// Set up rollout effect for first button
		FirstButton.unbind('mouseout');
		FirstButton.mouseout(function(){
            $(this).attr("src", "/images/pagination/arrow-first-disabled.gif");
		});
		// Set up the click handler for the first button
		FirstLink.unbind('click');
		FirstLink.click(function(){
			return false;
		});
	}
	
	/* --------------------------------------------- */
	// Handle the "previous" button
	/* --------------------------------------------- */
	if(PreviousLink.hasClass('enabled')){
		/* --------------------------------------------- */
		// Previous button is enabled:
		/* --------------------------------------------- */
		PreviousButton.attr("src", "/images/pagination/arrow-prev-enabled.gif");
		// Set up rollover effect for previous button
		PreviousButton.unbind('mouseover');
		PreviousButton.mouseover(function(){
            $(this).attr("src", "/images/pagination/arrow-prev-over.gif");
		});
		// Set up rollout effect for previous button
		PreviousButton.unbind('mouseout');
		PreviousButton.mouseout(function(){
            $(this).attr("src", "/images/pagination/arrow-prev-enabled.gif");
		});
		// Set up the click handler for the previous button
		PreviousLink.unbind('click');
		PreviousLink.click(function(){
			var VideoCategoryRowDivID = $(this).parent().parent().parent().attr("id");
			PreviousPaginationPage(VideoCategoryRowDivID);
		});
	}else{
		/* --------------------------------------------- */
		// Previous button is disabled (end of the line):
		/* --------------------------------------------- */
		PreviousButton.attr("src", "/images/pagination/arrow-prev-disabled.gif");
		// Set up rollover effect for previous button
		PreviousButton.unbind('mouseover');
		PreviousButton.mouseover(function(){
            $(this).attr("src", "/images/pagination/arrow-prev-disabled.gif");
		});
		// Set up rollout effect for previous button
		PreviousButton.unbind('mouseout');
		PreviousButton.mouseout(function(){
            $(this).attr("src", "/images/pagination/arrow-prev-disabled.gif");
		});
		// Set up the click handler for the previous button
		PreviousLink.unbind('click');
		PreviousLink.click(function(){
			return false;
		});
	}
	
	/* --------------------------------------------- */
	// Handle the "next" button
	/* --------------------------------------------- */
	if(NextLink.hasClass('enabled')){
		/* --------------------------------------------- */
		// Next button is enabled:
		/* --------------------------------------------- */
		NextButton.attr("src", "/images/pagination/arrow-next-enabled.gif");
		// Set up rollover effect for next button
		NextButton.unbind('mouseover');
		NextButton.mouseover(function(){
            $(this).attr("src", "/images/pagination/arrow-next-over.gif");
		});
		// Set up rollout effect for next button
		NextButton.unbind('mouseout');
		NextButton.mouseout(function(){
            $(this).attr("src", "/images/pagination/arrow-next-enabled.gif");
		});
		// Set up the click handler for the next button
		NextLink.unbind('click');
		NextLink.click(function(){
			var VideoCategoryRowDivID = $(this).parent().parent().parent().attr("id");
			NextPaginationPage(VideoCategoryRowDivID);
		});
	}else{
		/* --------------------------------------------- */
		// Next button is disabled (end of the line):
		/* --------------------------------------------- */
		NextButton.attr("src", "/images/pagination/arrow-next-disabled.gif");
		// Set up rollover effect for next button
		NextButton.unbind('mouseover');
		NextButton.mouseover(function(){
            $(this).attr("src", "/images/pagination/arrow-next-disabled.gif");
		});
		// Set up rollout effect for next button
		NextButton.unbind('mouseout');
		NextButton.mouseout(function(){
            $(this).attr("src", "/images/pagination/arrow-next-disabled.gif");
		});
		// Set up the click handler for the next button
		NextLink.unbind('click');
		NextLink.click(function(){
			return false;
		});
	}

}

function FirstPaginationPage(VideoCategoryRowDivID){
	// Load first page
	GoToPaginationPage(VideoCategoryRowDivID, 1);
}

function PreviousPaginationPage(VideoCategoryRowDivID){
	var VideoCategoryRow = $('#'+VideoCategoryRowDivID);
	var current_page = VideoCategoryRow.data('pagination_options_current_page');
	var page_count = VideoCategoryRow.data('pagination_options_page_count');
	// Reduce page number by one
	var prev_page = parseInt(current_page)-1;
	// Don't allow user to go below 0
	if(prev_page<1){
		prev_page = 1;
	}
	// Load previous page
	GoToPaginationPage(VideoCategoryRowDivID, prev_page);
}

function NextPaginationPage(VideoCategoryRowDivID){
	var VideoCategoryRow = $('#'+VideoCategoryRowDivID);
	var current_page = VideoCategoryRow.data('pagination_options_current_page');
	var page_count = VideoCategoryRow.data('pagination_options_page_count');
	// Increment page number by one
	var next_page = parseInt(current_page)+1;
	// Don't allow user to go beyond page limit
	if(next_page>page_count){
		next_page = page_count;
	}
	// Load next page
	GoToPaginationPage(VideoCategoryRowDivID, next_page);
}

function LastPaginationPage(VideoCategoryRowDivID){
	var VideoCategoryRow = $('#'+VideoCategoryRowDivID);
	var page_count = VideoCategoryRow.data('pagination_options_page_count');
	// Load last page
	GoToPaginationPage(VideoCategoryRowDivID, page_count);
}

function GoToPaginationPage(VideoCategoryRowDivID, PageNumber, OnlyPreloadNextPage){
	var VideoCategoryRow = $('#'+VideoCategoryRowDivID);
	var VideoCategoryThumbnailRow = $('#'+VideoCategoryRowDivID+' > div.video_thumbnail_rows:first');
	var PaginationInputPageNumber = $('#'+VideoCategoryRowDivID+' > ul.pagination > li > input.pagination_input_page_number:first');
	var video_url_prefix = VideoCategoryRow.data('pagination_options_video_url_prefix');
	var display_type = VideoCategoryRow.data('pagination_options_display_type');
	var event_id = VideoCategoryRow.data('pagination_options_event_id');
	var evcat_id = VideoCategoryRow.data('pagination_options_evcat_id');
	var show_id = VideoCategoryRow.data('pagination_options_show_id');
	var svcat_id = VideoCategoryRow.data('pagination_options_svcat_id');
	var preset_id = VideoCategoryRow.data('pagination_options_preset_id');
	var current_video_id = VideoCategoryRow.data('pagination_options_current_video_id');
	var videos_per_page = VideoCategoryRow.data('pagination_options_videos_per_page');
	var current_page = VideoCategoryRow.data('pagination_options_current_page');
	var page_count = VideoCategoryRow.data('pagination_options_page_count');
	if(!PageNumber){
		PageNumber=1;
	}
	// Determine user's paging directional "momentum" so we can preload the next page in the sequence
	var PreloadPageNumber = PageNumber;
	if(PreloadPageNumber>current_page){
		PreloadPageNumber++;
	}else if(PageNumber<current_page){
		PreloadPageNumber = PreloadPageNumber-1;
	}
	if(OnlyPreloadNextPage){
		var PreloadPageNumber = parseInt(current_page)+1;
	}
	// Make sure that we don't preload beyond the paging limits
	if(PreloadPageNumber>page_count || PreloadPageNumber<1){
		PreloadPageNumber = PageNumber;
	}
	// Formulate the preloader url
	if(PreloadPageNumber != PageNumber){
		var preload_pagination_url = "/video_thumbnail_rows_ajax.php?display_type="+display_type+"&event_id="+event_id+"&evcat_id="+evcat_id+"&show_id="+show_id+"&svcat_id="+svcat_id+"&preset_id="+preset_id+"&page="+PreloadPageNumber+"&video_url_prefix="+escape(video_url_prefix)+"&videos_per_page="+videos_per_page+"&current_video_id="+current_video_id;
		// Create a hidden pagination preloader div (if it doesn't already exist), and load the content into it
		var pagination_preloader_div = $('#pagination-preloader-div');
		if(pagination_preloader_div.length==0){
			$('body').append('<div id="pagination-preloader-div" style="width:1px; height:1px; padding:1px; overflow:hidden; background-color:#FFFFFF;"></div>');
			pagination_preloader_div = $('#pagination-preloader-div');
		}
		// Preload the next page into the hidden div
		pagination_preloader_div.load(preload_pagination_url, function() {
			$('#pagination-preloader-div').hide();
		});
	}
	if(PageNumber!=current_page &&!OnlyPreloadNextPage ){
		// Style the video category row so that it can animate
		VideoCategoryRow.css('width', VideoCategoryRow.innerWidth());
		VideoCategoryThumbnailRow.css('width', VideoCategoryThumbnailRow.innerWidth());
		VideoCategoryThumbnailRow.stop();
		VideoCategoryThumbnailRow.animate({
				opacity: '0'
			}, 0, function() {
			// Animation complete.
			// Formulate the url
			var pagination_url = "/video_thumbnail_rows_ajax.php?display_type="+display_type+"&event_id="+event_id+"&evcat_id="+evcat_id+"&show_id="+show_id+"&svcat_id="+svcat_id+"&preset_id="+preset_id+"&page="+PageNumber+"&video_url_prefix="+escape(video_url_prefix)+"&videos_per_page="+videos_per_page+"&current_video_id="+current_video_id;
			// Load the new thumbnails
			VideoCategoryThumbnailRow.load(pagination_url, function() {
				// New page loaded successfully, so we increment the page number
				PaginationInputPageNumber.val(PageNumber);
				VideoCategoryRow.data('pagination_options_current_page', PageNumber);
				// Handle all of the buttons again
				HandlePaginationButtons(VideoCategoryRowDivID);
				// Animate the page in
				if(PageNumber<current_page){
					VideoCategoryThumbnailRow.css('margin-left', '-10px');
				}else{
					VideoCategoryThumbnailRow.css('margin-left', '10px');
				}
				VideoCategoryThumbnailRow.animate({
						marginLeft: '0px',
						opacity: '1'
					},{"duration": 750, "easing": "easeOutQuad"}, function() {
				});
			});
		});
	}

}
