$(document).ready(function() {
	var pageLocation;
	
	function changePage(newPage) {
		var anchor;
		var page;
		
		if(location.href.indexOf("#") != -1) {
			anchor = location.href.split("#")[1];
			
			if(anchor.indexOf(".") != -1) {
				page = anchor.split(".")[1];
				location.href = location.href.replace(page, newPage);
			}
			else {
				page = pageLocation.split(".")[1];
				location.href = pageLocation.replace(page, newPage);
			}
		}
		else {
			page = pageLocation.split(".")[1];
			location.href = pageLocation.replace(page, newPage);
		}
	}
	
	// function to bind all pagination links
	function bindPaginationLinks(e){
			var pagesStart = 1;
			var pagesEnd = $("tr.insiders").length;
		
			var page;
			var newPage;
			// First get the current page
			if(location.href.indexOf("#") != -1) {
				var anchor = location.href.split("#")[1];
				
				if(anchor.indexOf(".") != -1) {
					page = anchor.split(".")[1];
				}
				else {
					page = "page_1";
				}
			}
			else {
				page = "page_1";
			}
			
			// Now determine the new page
			var page_num = page.split("_")[1];
			switch($(this).attr("name"))
			{
				case "prev":
					page_num--;
					if(page_num < pagesStart)
					{
						page_num = pagesStart;
					}
					newPage = "page_" + page_num;
					break;
				case "more":
					page_num++;
					if(page_num > pagesEnd)
					{
						page_num = pagesEnd;
					}
					newPage = "page_" + page_num;
					break;
				default:
					newPage = $(this).attr("name");
					page_num = $(this).attr("name").split("_")[1];
					break;
			}
				
				// Change the url
				changePage(newPage);
				
				
				// Update the window
				$("tr.insiders").css("display", "none");
				$("#" + newPage).css("display", "");
				
				// Update the pagination links
				// Generate the pagination links
				
				generatePaginationLinks(page_num, pagesEnd);
				
				// stop the actual a tag from doing anything
				return false;
			}
	
	// function to generate pagination links
	// takes in the current page and total page to generate the appropriate links
	function generatePaginationLinks(currentPage, totalPages) {
		var links = $("div.nextPrev");
		
		if(totalPages > 1) {
		
			links.empty(); // reset the current contents
			
			// Assume starting page is 1
			if(currentPage > 1) {
				links.append("<a class=\"nextPrev\" name=\"prev\" href=\"#\">&lt; Prev</a>");
			}
			else {
				links.append("<span class=\"nextPrev\">Prev</span>");
			}
			
			// Build links, bolding the current page
			for(i = 1; i <= totalPages; i++) {
				if(currentPage == i) {
					links.append("<a class=\"nextPrev\" name=\"page_"+i+"\" href=\"#\"><b>"+i+"</b></a>");
				}
				else {
					links.append("<a class=\"nextPrev\" name=\"page_"+i+"\" href=\"#\">"+i+"</a>");
				}
			}
			
			// Build one more link for next page, unless current page = next page
			if(currentPage == totalPages) {
				links.append("&nbsp; <span class=\"nextPrev\">More</span>");
			}
			else {
				links.append("<a class=\"nextPrev\" name=\"more\" href=\"#\">More &gt;</a>");
			}
			// Done
			
			// Now re-bind events
			$("a.nextPrev").bind("click", bindPaginationLinks);
		}
	}

	
	// Hide all items
	$(".insiderPerspective").css("display", "none");
	$(".alternatePortrait").css("display", "none");
	
	var pagesStart = 1;
	var pagesEnd = $("tr.insiders").length;
	
	profile = $(".profile").eq(0);
	page = "page_1";
	
	// Determine which item should be highlighted right now
	if(location.href.indexOf("#") != -1) {
		var pageAnchor = location.href.split("#")[1];
		if(pageAnchor.indexOf(".") != -1) {
			var data = pageAnchor.split(".");
			var profileData = $(".profile").filter(function(index){
				return $(this).attr("name") == data[0];
			});
			var pageData = data[1];
			if(pageData.indexOf("_") != -1)
			{
				var pageNum = pageData.split("_")[1];
				if(pageNum <= pagesEnd && pageNum >= pagesStart)
				{
					profile = profileData.length > 0 ? profileData : profile;
					page = profile == profileData ? pageData : page;
				}
			}
			
		}
	}
	
	//location.href = "#" + profile.attr("name") + "." + page;
	pageLocation = "#" + profile.attr("name") + "." + page;
	
	// Now set the page up
	$("#" + page).css("display", "");
	$(profile).addClass("pactive");
	$("#" + $(profile).attr("name")).css("display", "");
	
	// Generate the pagination links
	generatePaginationLinks(pageNum ? pageNum : 1, pagesEnd);
	
	// Ready to define events 
	$(".profile").bind("mouseenter mouseleave", function(e){
		$(this).toggleClass("phover");
	});
	
	$(".profile").bind("click", function(e){
		// Determine which page we're on
		var page = $(this).parent().parent().attr("id");
		
		// First set to active
		if($(this).hasClass("pactive") == false) {
			$(".profile").removeClass("pactive");
			$(this).addClass("pactive");
		}
		
		// Because of a bug in firefox, we can't use real anchors. 
		// Change the url to the anchor
		location.href = "#" + $(this).attr("name") + "." + page;
		
		// Next, show the relevant bio information
		$(".insiderPerspective").css("display", "none");
		$("#" + $(this).attr("name")).css("display", "");
	});
	
	// Next define click listener for the pagination links
	$("a.nextPrev").bind("click", bindPaginationLinks);
});