$(document).ready(function() {
	// Event Highlights photo section
	$("#events-navigation").show();
	$(".event-thumbnails").show();
	$("#events-menu-nojs").hide();
	$(".event-photo").hide();
	
	$(".event-thumbnails a").click(function() {
		var elem = $(this).attr("class");
		$(".event-photo").hide();
		$("#" + elem).show();
		
		return false;
	});

	$(".event-thumbnails a:first").click();
	
	var selected_row = 1;
	
	function getNumRows(elem) {
		var kids = $(elem).children();
		var len = kids.length;
		var num_rows = 1;
		
		if (len > 6) {
			if (len % 6) {
				num_rows = Math.floor(len / 6) + 1;
			} else {
				num_rows = Math.floor(len / 60);
			}
		}
		
		return num_rows;
	}
	
	$("#events-menu").change(function() {
		var i = this.selectedIndex;
    	
		window.location = this.options[i].value;
	});
	
	$("ul.event-thumbnails").each(function() {
		var kids = $(this).children();
		var num_rows = getNumRows(this);
		
		if (num_rows > 1) {
			for (i = 0; i < num_rows; i++) {
				for (j = 0; j <= 5; j++) {
					elem = (i * 6) + j;
					$(kids[elem]).addClass("row-" + (i + 1));
				}
			}
			
			for (i = 2; i <= num_rows; i++) {
				$(".row-" + i).hide();
			}
			
			$(this).before("<div class=\"paging_links\"><div class=\"prev_link\">&laquo; Prev</div><div class=\"next_link\">Next &raquo;</div></div>");
		}
	});
	
	$(".next_link").click(function() {
		var list = $(this).parents(".event-photos").children(".event-thumbnails");
		var num_rows = getNumRows(list);
		
		if (selected_row < num_rows) {
			selected_row++;
			$(list).children("li").hide();
			$(list).children(".row-" + selected_row).show();
			$(".prev_link").show();
				
			if (selected_row == num_rows) {
				$(".next_link").hide();
			}
		} else {
			selected_row = 1;
		}
	});
	
	$(".prev_link").hide().click(function() {
		var list = $(this).parents(".event-photos").children(".event-thumbnails");
		var num_rows = getNumRows(list);
		
		if (selected_row > 1) {
			selected_row--;
			$(list).children("li").hide();
			$(list).children(".row-" + selected_row).show();
			$(".next_link").show();
			
			if (selected_row == 1) {
				$(".prev_link").hide();
			}
		}
	});
});