// prevent conflicts with other js libraries
var $j = jQuery.noConflict();

// on dom ready
$j(document).ready(function(){
	// request document form
	$j("#request_document").submit(function(){
		// get form action
		var action = $j(this).attr('action');
		// save form reference
		var this_form = $j(this);

		// set ajax loading image
		$j('.ajax').html("<img src='/imgs/icons/ajax-loader.gif' alt='Page Loading Image' />");

		$j.ajax({
			type : 'POST',
			url : action,
			data : 'ajax=1&'+this_form.serialize(),
			success : function(msg) {
				// get error code
				var code = msg.substring(0,1);
				// set html
				$j('.ajax').html(msg.substring(2));

				// more fields required
				if(code === '0') {
					$j('.hide').fadeIn();
				}

				// sent ok
				if(code === '1') {
					$j('#request_document').hide();
				}

				// not sent
				if(code === '2') {

				}
			}
		});

	return false;
	});
	
	
	/**
	 * PAGINATE NEWS ARCHIVE
	 */
	if( $j('#newsArchivePaginate').length ) {
		// paginate tables
		paginate_tables();
	}

	/**
	 * PAGINATE FAQs
	 */

	if( $j('#faqs').length )
	{
		paginate_elements( "#faqs", ".question" );
	}
	
	/**
	 * EVENTS PAGES
	 */
	if( $j('#events').length ) {
		// hide all tables by default
		$j('#events table').hide();
		// show/hide on click
		$j('#events .toggle').click(function(){
			$j(this).parents('div.group').find('table').toggle();
		return false;
		});
		paginate_events();
	}
});


function paginate_events() {
	var currentPage = 0;
	var numPerPage = 10;
	var numPerPager = 5;
	
	var groups = $j("#events .group");
	var items = groups.length;
	var numPages = Math.ceil( items/numPerPage );
	
	// create paginate
	$j("#events").bind('repaginate', function() {
		$j("#events").find('.group')
		.show()
		.filter(':lt(' + (currentPage * numPerPage) + ')')
		.hide()
		.end()
		.filter(':gt(' + ((currentPage + 1) * numPerPage -1) + ')')
		.hide()
		.end();
	});
	
	// pagination links
	var pager = '<ul id="pagination">';
	for(var i=0; i<numPages; i++) {
		// prev link
		if(i == 0) {
			pager += "<li class='prev prev_disabled clicker' id='prev'><a href='prev'>Prev</a></li>";
		}

		// add page
		pager += "<li class='clicker' id='"+(i+1)+"'><a href='"+(i+1)+"'>"+(i+1)+"</a></li>";

		// next link
		if(i == (numPages-1)) {
			var divclass = (i < numPages) ? '' : ' next_disabled';
			pager += "<li class='next"+divclass+" clicker' id='next'><a href='next'>Next</a></li>";
		}
	}
	// end pager
	pager += "</ul><div class='clear'></div>";
	
	// only add links if more than one page
	if(numPages > 1) {
		// add paginate links if not already displaying
		if( $j('#pagination').length == 0 ) {
			$j("#events").after(pager);
		}

		// make first link active
		$j('li#1').addClass('active');

		// do pagination logic
		$j('.clicker').click(function(){
			// get selected page
			var selected = $j(this).attr('id');

			// deal with links
			if(selected == 'next') {
				// stop and disable if edge reached
				if((currentPage+1) != numPages) {
					currentPage = currentPage + 1;
				}
			} else if(selected == 'prev') {
				// stop and disable if edge reached
				if((currentPage) != 0) {
					currentPage = currentPage - 1;
				}
			} else {
				currentPage = selected-1;
			}

			// remove all active classes
			$j('li').removeClass('active');
			// add active to current page
			$j('#'+(currentPage+1)).addClass('active');

			// prev link
			if(currentPage == 0) {
				$j('.prev').addClass('prev_disabled');
			} else {
				$j('.prev').removeClass('prev_disabled');
			}
			// next link
			if((currentPage+1) == numPages) {
				$j('.next').addClass('next_disabled');
			} else {
				$j('.next').removeClass('next_disabled');
			}

			// repaginate table
			$j("#events").trigger('repaginate');
			return false;
		});
	}
	// do pagination
	$j("#events").trigger('repaginate');
}

// paginate tables
function paginate_tables() {
	$j('table.paginate').each(function(){
		// init
		var currentPage = 0;
		var numPerPage 	= 8;
		var numPerPager	= 5;

		// reference this table
		var table = $j(this);
		// do pagination
		table.bind('repaginate', function() {table.find('tbody tr').show().filter(':lt(' + (currentPage * numPerPage) + ')').hide().end().filter(':gt(' + ((currentPage + 1) * numPerPage -1) + ')').hide().end();});

		// calc total # rows in table
		var numRows = table.find('tbody tr').length;
		// calc total # of pages to display
		var numPages = Math.ceil(numRows / numPerPage);
		// create pagination item
		var pager = $j('<ul id="pagination"></ul>');

		// start pager
		var pager_str = "<ul id='pagination'>";

		// loop through and create pages
		for(var page=0; page < numPages; page++) {
			// prev link
			if(page == 0) {
				pager_str += "<li class='prev prev_disabled clicker' id='prev'><a href='prev'>Prev</a></li>";
			}

			// add page
			pager_str += "<li class='clicker' id='"+(page+1)+"'><a href='"+(page+1)+"'>"+(page+1)+"</a></li>";

			// next link
			if(page == (numPages-1)) {
				var divclass = (page < numPages) ? '' : ' next_disabled';
				pager_str += "<li class='next"+divclass+" clicker' id='next'><a href='next'>Next</a></li>";
			}
		}

		// end pager
		pager_str += "</ul><div class='clear'></div>";

		// only add links if more than one page
		if(numPages > 1) {
			// add paginate links if not already displaying
			if( $j('#pagination').length == 0 ) {
				table.after(pager_str);
			}

			// make first link active
			$j('li#1').addClass('active');

			// do pagination logic
			$j('.clicker').click(function(){
				// get selected page
				var selected = $j(this).attr('id');

				// deal with links
				if(selected == 'next') {
					// stop and disable if edge reached
					if((currentPage+1) != numPages) {
						currentPage = currentPage + 1;
					}
				} else if(selected == 'prev') {
					// stop and disable if edge reached
					if((currentPage) != 0) {
						currentPage = currentPage - 1;
					}
				} else {
					currentPage = selected-1;
				}

				// remove all active classes
				$j('li').removeClass('active');
				// add active to current page
				$j('#'+(currentPage+1)).addClass('active');

				// prev link
				if(currentPage == 0) {
					$j('.prev').addClass('prev_disabled');
				} else {
					$j('.prev').removeClass('prev_disabled');
				}
				// next link
				if((currentPage+1) == numPages) {
					$j('.next').addClass('next_disabled');
				} else {
					$j('.next').removeClass('next_disabled');
				}

				// repaginate table
				table.trigger('repaginate');
				return false;
			});
		}
		// do pagination
		table.trigger('repaginate');
	});
}




// paginate elements
function paginate_elements( elem_selector, item_selector ) {

//	console.lo
	
	$j(elem_selector).each(function(){
		// init
		var currentPage = 0;
		var numPerPage 	= 5;
		var numPerPager	= 5;

		// reference this table
		var elem = $j(this);

//		console.log(this);
//		console.dir(elem);

		// do pagination
		elem.bind('repaginate', function() {elem.find(item_selector).show().filter(':lt(' + (currentPage * numPerPage) + ')').hide().end().filter(':gt(' + ((currentPage + 1) * numPerPage -1) + ')').hide().end();});

		// calc total # rows in table
		var numRows = elem.find(item_selector).length;
		// calc total # of pages to display
		var numPages = Math.ceil(numRows / numPerPage);
		// create pagination item
		var pager = $j('<ul id="pagination"></ul>');

		// start pager
		var pager_str = "<ul id='pagination'>";

		// loop through and create pages
		for(var page=0; page < numPages; page++) {
			// prev link
			if(page == 0) {
				pager_str += "<li class='prev prev_disabled clicker' id='prev'><a href='prev'>Prev</a></li>";
			}

			// add page
			pager_str += "<li class='clicker' id='"+(page+1)+"'><a href='"+(page+1)+"'>"+(page+1)+"</a></li>";

			// next link
			if(page == (numPages-1)) {
				var divclass = (page < numPages) ? '' : ' next_disabled';
				pager_str += "<li class='next"+divclass+" clicker' id='next'><a href='next'>Next</a></li>";
			}
		}

		// end pager
		pager_str += "</ul><div class='clear'></div>";

		// only add links if more than one page
		if(numPages > 1) {
			// add paginate links if not already displaying
			if( $j('#pagination').length == 0 ) {
				elem.after(pager_str);
			}

			// make first link active
			$j('li#1').addClass('active');

			// do pagination logic
			$j('.clicker').click(function(){
				// get selected page
				var selected = $j(this).attr('id');

				// deal with links
				if(selected == 'next') {
					// stop and disable if edge reached
					if((currentPage+1) != numPages) {
						currentPage = currentPage + 1;
					}
				} else if(selected == 'prev') {
					// stop and disable if edge reached
					if((currentPage) != 0) {
						currentPage = currentPage - 1;
					}
				} else {
					currentPage = selected-1;
				}

				// remove all active classes
				$j('li').removeClass('active');
				// add active to current page
				$j('#'+(currentPage+1)).addClass('active');

				// prev link
				if(currentPage == 0) {
					$j('.prev').addClass('prev_disabled');
				} else {
					$j('.prev').removeClass('prev_disabled');
				}
				// next link
				if((currentPage+1) == numPages) {
					$j('.next').addClass('next_disabled');
				} else {
					$j('.next').removeClass('next_disabled');
				}

				// repaginate table
				elem.trigger('repaginate');
				return false;
			});
		}
		// do pagination
		elem.trigger('repaginate');
	});
}