var pageWidth = 790;
var currentPage = 0;
var minPageHeight = 300;
var curPageHeight;
var pageCount;
var tempPage;


function scrollToPage(pageNum) {
	
	if (pageNum != currentPage) {
		
		$('.contentPage:eq('+ currentPage +')').siblings().css({ opacity: 0 });
		$('.contentPage:eq('+ pageNum +')').stop().animate({
			opacity: 1
		}, 'medium');
		
		$('#pageSlider').stop().animate({
			left: (0 - (pageWidth * pageNum))+'px'
		}, 'medium', 'easeOutBack');
		
		curPageHeight = ( $('.contentPage:eq('+ pageNum +')').height() > minPageHeight) ? $('.contentPage:eq('+ pageNum +')').height() + 20 : minPageHeight;
		
		//alert(pageNum);
		//alert(curPageHeight);
		$('#pageWrapperInner').css({ height: curPageHeight+'px' });
		
		$('#pageWrapper').animate({height: curPageHeight+'px'}, 300);
		
		$('#teamPager a').removeClass('selected').eq(pageNum).addClass('selected');

		$('#nextPage').removeClass('first');
		currentPage = pageNum;
	}
}



$(document).ready(function() {
	var pageCount = $('.contentPage').length;
	
	
	// Test if page is set in URL and reset currentPage var
	var myUrl = document.location.toString();
	if (myUrl.match('#')) { // the URL contains an anchor
		// click the navigation item corresponding to the anchor
		myAnchor =  myUrl.split('#')[1];
		if( (myAnchor.match("=")) && (myAnchor.split('=')[0] == 'page' ) ){
			//alert(myAnchor.split('=')[1]);
				
			//alert(currentPage);
			scrollToPage((myAnchor.split('=')[1]) - 1);
			
			currentPage = (myAnchor.split('=')[1]) - 1;
			//scrollToPage((myAnchor.split('=')[1]) -1);
		}
	}
	
	
	curPageHeight = ( $('.contentPage:eq('+ currentPage +')').height() > minPageHeight) ? $('.contentPage:eq('+ currentPage +')').height() + 20 : minPageHeight;
	
	$('#pageSlider').css({position: 'absolute', width: String((pageCount * pageWidth) + 10) + 'px'}).wrapAll('<div id="pageWrapper"><div id="pageWrapperInner"></div></div>');
	
	$('#pageWrapper').css({ margin: '0 0 0 72px', width: pageWidth+'px', height: curPageHeight+'px', overflow: 'hidden' }); // extra 40 pixels is for IE which was cutting off last link of tallest item
	$('#pageWrapperInner').css({ position: 'relative', width: pageWidth+'px', overflow: 'hidden', height: curPageHeight+'px' });
	
	
	//
	// Create the pager if there is more than one page
	//
	if (pageCount > 0) {
		
		var tempCounter = 0;
		
		
		$('<div id="teamPager" class="pager"></div>').css({
			float: 'right',
			padding: '5px 8px 2px 0'
		}).appendTo('#ourTeam .overlayNav').html('<b>Page: </b>');
		
		$('.contentPage')
		.css({ float: 'left', width: pageWidth+'px' })
		.each( function() {
			
			var tempPage = tempCounter;
			
			if (currentPage == tempCounter) {
				$('<a href="#page='+(tempCounter + 1)+'" class="selected">'+(tempCounter + 1)+'</a>')
				.appendTo('#teamPager')
				.click( function() {
					$('#nextPage').removeClass('first');
					scrollToPage(tempPage);
				}).css({ cursor: 'pointer'});
			} else {
				
				$('<a href="#page='+(tempCounter + 1)+'">'+(tempCounter + 1)+'</a>')
				.appendTo('#teamPager')
				.click( function() {
					$('#nextPage').removeClass('first');
					scrollToPage(tempPage);
					
				}).css({ cursor: 'pointer'});
			}

			tempCounter++;
			
			if ( (tempCounter) < pageCount ) {
				$('#teamPager').append(' | ');
			}
		});
		
		//add left and right pager buttons
		$('.overlayCenter').append('<a id="previousPage"></a><a id="nextPage" class="first"></a>')
		.find('#nextPage').click(function() {
			
			if (currentPage < (pageCount - 1) ) {
			
				tempPage = currentPage + 1;
				
				scrollToPage(tempPage);
				currentPage = tempPage;
				
			} else {
			
				tempPage = 0;
				
				scrollToPage(tempPage);
				currentPage = tempPage;
				
			}
		}).end().find('#previousPage').click(function() {
		
			if (currentPage > 0 ) {
				
				tempPage = currentPage - 1;
				
				scrollToPage(tempPage);
				
			} else {
			
				tempPage = pageCount - 1;
				
				scrollToPage(tempPage);
				
			}
		});
		
	}
	
	//
	// Assign click handler to each rel="page*" anchor
	//
	
	$('#ourTeamList li a[rel*=page]').click(
		function()
		{
			var pn = $(this).attr('rel').substr(4);
			scrollToPage(pn-1);
			return false;
		}
	).css({ cursor: 'pointer'});

	
});