var pageWidth = 960;
var currentPage = 0;
var minPageHeight = 300;
var curPageHeight;
var pageCount = $('.caseStudyPage').length;
var tempPage;
var videoPlayers = [];
	
function scrollToPage(pageNum) {
	//alert(pageNum);
	
	if (pageNum != currentPage) {
		for (var i = 0; i < videoPlayers.length; i++) {
			if (thisMovie(videoPlayers[i])) {
				try {
					thisMovie(videoPlayers[i]).stopVideo();
				} catch(e) { }
			}
		}
		
		$('#pageGraphicBox').fadeOut('fast').html('');
		
		$('.caseStudyPage:eq('+ currentPage +')').siblings().css({ opacity: 0 });
		$('.caseStudyPage:eq('+ pageNum +')').css({ visibility: "visible"}).stop().animate({
			opacity: 1
		}, 'medium');
		
		$('#pageSlider').stop().animate({
			left: (0 - (pageWidth * pageNum))+'px'
		}, 'medium', 'easeOutBack');
		
		$('#pageGraphicBox').hide().html($('.caseStudyPage:eq('+ pageNum +')').find('.pageImage').clone().css({ display: 'block' })).fadeIn('fast', function() { $('#pageGraphicBox img').ifixpng(); });
		
		curPageHeight = ( $('.caseStudyPage:eq('+ pageNum +')').height() > minPageHeight) ? $('.caseStudyPage:eq('+ pageNum +')').height() + 20 : minPageHeight;
		
		$('#caseStudyInner').css({ height: curPageHeight+'px' });
		$('#caseStudy').animate({height: curPageHeight+'px'}, 300);
		
		$('#caseStudyPager a').removeClass('selected').eq(pageNum).addClass('selected');

		$('#nextPage').removeClass('first');
		currentPage = pageNum;
	}
}

$(document).ready(function() {
	
	$.ifixpng('../../i/pixel.gif');
	$('#caseStudy h2 img').ifixpng();
	$('.caseStudyPage:eq(0)').css({ visibility: "visible"});

	// Append container to hold page images so they can break outside the scrolling box
	$('#content').append('<div id="pageGraphicBox"></div>')
	.find('#pageGraphicBox').css({
		position: 'absolute',
		top: '-30px',
		left: '30px',
		zIndex: '5'
	});
	//
	// Attach image for title page and fix png transparency for IE 6
	//
	$('#pageGraphicBox').hide().html($('.caseStudyPage:eq('+ currentPage +')').find('.pageImage').clone().css({ display: 'block' })).fadeIn('fast');
	$('#pageGraphicBox img').ifixpng();
	
	//
	// Hide all page images to be displayed dynamically
	//
	$('.caseStudyPage .pageImage').css({ display: 'none' });
	
	curPageHeight = ( $('.caseStudyPage:eq('+ currentPage +')').height() > minPageHeight) ? $('.caseStudyPage:eq('+ currentPage +')').height() + 20 : minPageHeight;
	
	$('#caseStudy').css({ height: curPageHeight+'px', overflow: 'hidden' })
	.wrapInner('<div id="caseStudyInner"></div>').find('#caseStudyInner')
	.css({ position: 'relative', height: curPageHeight+'px', width: pageWidth+'px', overflow: 'hidden' })
	.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);
			
		}
	});
	
	
	
	$('#pageSlider').css({position: 'absolute', height: '26em', width: String(($('.caseStudyPage').length * pageWidth) + 10) + 'px'});
	
	//
	// Create the pager if there is more than one page
	//
	if (pageCount > 0) {
		
		var tempCounter = 0;
		
		$('#caseStudyPager').html('<b>Page: </b>');
		
		$('.caseStudyPage')
		.css({ float: 'left' })
		.each( function() {
			
			var tempPage = tempCounter;
			
			if (currentPage == tempCounter) {
				$('<a href="#page='+(tempCounter + 1)+'" class="selected">'+(tempCounter + 1)+'</a>')
				.appendTo('#caseStudyPager')
				.click( function() {
					$('#nextPage').removeClass('first');
					scrollToPage(tempPage);
				}).css({ cursor: 'pointer'});
			} else {
				
				$('<a href="#page='+(tempCounter + 1)+'">'+(tempCounter + 1)+'</a>')
				.appendTo('#caseStudyPager')
				.click( function() {
					$('#nextPage').removeClass('first');
					scrollToPage(tempPage);
					
				}).css({ cursor: 'pointer'});
			}

			tempCounter++;
			
			if ( (tempCounter) < pageCount ) {
				$('#caseStudyPager').append(' | ');
			}
		});
	}
});

function thisMovie(movieName) {
	// IE and Netscape refer to the movie object differently.
	// This function returns the appropriate syntax depending on the browser.
	return (navigator.appName.indexOf ("Microsoft") !=-1) ? window[movieName] : document[movieName]; 
}