
/*===============================================================================
	portfolio.js
	John Larson
	8/23/08
	
	All page-specific JavaScript for portfolio pages

===============================================================================*/

	var disableJSNav = false;
	var rollOutLinkSet = [null];
	var lineBreak = '<br />'; // place this here: in the HTML doc causes validation failure
	
window.addEvent('domready', function() {
	dbug.enable();
	
	document.ondragstart = function () { return false; }; //IE drag hack
	
	$$('.rollOut').each(function(theRolloutLink, index) {
		var myContent = theRolloutLink.getNext();
		theRolloutLink.contentSlider = new Fx.Slide(myContent, {duration: 500});
		theRolloutLink.contentSlider.hide();
		rollOutLinkSet.push(theRolloutLink);
		
		theRolloutLink.getParent().addEvent('click', function() {
		//	toggleCapabilitiesSection(theRolloutLink);
			showShowOffSection(index+1);
		});
	});
	
	setCaseStudyNavigation();
	
	CaseStudyHistory = HistoryManager.register(
		'CaseStudy',
		[currentPageIndex],
		function(values) {
			jumpToPage(parseInt(values[0]));
		},
		function(values) {
			return 'CaseStudy' + '(' + values[0] + ')';
		}.bind(this),
		'CaseStudy' + '\\((\\d+)\\)');
	
	HistoryManager.start();
	
});


/****************************************************************************
//	SECTION::Case Study load and navigation
*/
	
	var currentCaseStudyID = 0;
	var isLoading = false;
	
	
	function loadPreviousCaseStudy() {
		
		if(disableJSNav)
			return true;
		
		if(isLoading) {
			dbug.log('Currently loading, so will not load previous.');
			return;
		}
		
		if (currentPageIndex > 0) {
			loadCaseStudy(currentPageIndex-1);
		}
		return false;
	}
	
	function loadNextCaseStudy() {
		
		if(disableJSNav)
			return true;
		
		if(isLoading) {
			dbug.log('Currently loading, so will not load next.');
			return;
		}
		if (currentPageIndex < CaseStudyIDSet.length-1) {
			loadCaseStudy(currentPageIndex+1);
		}
		return false;
	}
	
	function jumpToPage(newPageIndex) {
		
		if(disableJSNav)
			return true;
		
		loadCaseStudy(newPageIndex);
		return false;
	}
	
	
	function loadCaseStudy(pageIndex) {
		var theDetailsDiv = $('caseStudyPage' + pageIndex);
		
		if(!theDetailsDiv) {
			dbug.log('theDetailsDiv (caseStudyPage' + pageIndex + ') not found.');
			return;
		}
		
		if(isLoading) {
			dbug.log('Currently loading, so will not load.');
			return;
		}
		
		if (theDetailsDiv.innerHTML == '') {
			
			dbug.log('Div empty, so loading!');
			isLoading = true;
			new Ajax('AJAXLoad.asp?l=loadCaseStudy', {
				method: 'post',
				data: 'CaseStudyID=' + CaseStudyIDSet[pageIndex],
				update: $('caseStudyPage' + pageIndex),
				evalScripts: true,
				onComplete: function() {
					isLoading = false;
					scrollToPage(pageIndex);
				}
			}).request();
		}
		else {  // already loaded, so quick transition!
			scrollToPage(pageIndex);
		}
	}
	
	
	var hasBeenRoared = false;
	function scrollToPage(pageIndex) {
	//	dbug.log('scrollToPage(' + pageIndex + '), swap ' +
	//		currentPageIndex + ' for ' + pageIndex);
		
		// in case we navigated away from capabilities:
		if(currentPageIndex == 1  &&  currentShowOffIndex > 0)
			closeShowOffSection();
		
		stopCaseStudyPageFlash(currentPageIndex);
		
		swapSections('caseStudyPage' + currentPageIndex, 'caseStudyPage' + pageIndex);
		currentPageIndex = pageIndex;
		if(window.CaseStudyHistory) // if just in case the user clicks before onload is fired
			CaseStudyHistory.setValue(0, parseInt(currentPageIndex));
		setCaseStudyNavigation();
		
		if(currentPageIndex == 1) {
			swapSections('portfolioNavigation', 'headerCapabilities');
			if(!hasBeenRoared) {
				var myRoar = new Roar({
					offset: 200,
					duration: 5000,
					position: 'upperLeft',
					margin: {x: 100, y: 20}
				});
				if($('capabilityPlayNotice')) { // just in case not loaded yet (grr, IE!)
					myRoar.alert('Play around!', 
						$('capabilityPlayNotice').getHTML());
					hasBeenRoared = true;
				}
			}
		}
		else {
			swapSections('headerCapabilities', 'portfolioNavigation');
			
			// tend to nav link coloration as well:
			$ES('a', '#portfolioNavigation').each(function(theLink) {
				theLink.style.color = '#fff';
			});
			
			if(currentPageIndex > 0)
				$('caseStudyLink' + currentPageIndex).style.color = colorSet[currentPageIndex];
			
		}
		currentCaseStudyID = CaseStudyIDSet[currentPageIndex];
		
	}
	
	
	function setCaseStudyNavigation() {
		
		// Handle the back links:
		var backLink = $('backLink');
		if (currentPageIndex == 0) // at the beginning, no back link applies:
			backLink.setStyle('display', 'none');
		else // back link applies:
			backLink.setStyle('display', 'block');
		
		// Handle the forward link:
		var forwardLink = $('forwardLink');
		if (currentPageIndex == CaseStudyIDSet.length-1) // at the end, no forward:
			forwardLink.setStyle('display', 'none');
		else // forward link applies:
			forwardLink.setStyle('display', 'block');
	}
	
/*
//	End SECTION::Case Study load and navigation
****************************************************************************/




/****************************************************************************
//	SECTION::Navigation Menu Coloring
*/
	function handleNavMouseOver(pageIndex) {
		$('caseStudyLink' + pageIndex).style.color = colorSet[pageIndex];
	}
	function handleNavMouseOut(pageIndex) {
		if(pageIndex != currentPageIndex)
			$('caseStudyLink' + pageIndex).style.color = '#fff';
	}
/*
//	End SECTION::Navigation Menu Coloring
****************************************************************************/




/****************************************************************************
//	SECTION::Capabilities Show Off
*/
	
	var currentShowOffIndex = 0;
	function closeShowOffSection() {
		// explicitly stop playback on any flash movie we may have just hidden:
		stopSectionFlash(currentShowOffIndex);
		swapSections('showOffCloseIcon', 'showOffCloseIconBlank');
		swapSections('showOff' + currentShowOffIndex, 'showOff0');
		rollOutLinkSet[currentShowOffIndex].contentSlider.slideOut();
		
		
		currentShowOffIndex = 0;
	}
	
	function showShowOffSection(index) {
		if(currentShowOffIndex > 0) {
			rollOutLinkSet[currentShowOffIndex].contentSlider.slideOut();
			stopSectionFlash(currentShowOffIndex);
		}
		rollOutLinkSet[index].contentSlider.slideIn();
		
		if(index == currentShowOffIndex)
			closeShowOffSection();	// already looking at it, so close
		else {
			swapSections('showOff' + currentShowOffIndex, 'showOff' + index);
			swapSections('showOffCloseIconBlank', 'showOffCloseIcon');
			
			fetchSectionFlash(index);
			
			currentShowOffIndex = index;
		}
	}
	
	
	// IE doesn't stop movies when they are not visible, AND the published
	// StopPlay() method(s) don't appear to work, so we'll get creative:
	var flashDungeon = new Hash();
	function stopSectionFlash(index) {
		if(!window.ie) return; // not a problem, no need to bother
		
		var movieNode = $('showOff' + index).getElement('object')
		if(movieNode) {
			flashDungeon.set(index, movieNode);
			$('showOff' + index).empty();
		}
	}
	
	function fetchSectionFlash(index) {
		if(flashDungeon.has(index))
			$('showOff' + index).adopt(flashDungeon.get(index));
	}
/*
//	End SECTION::Capabilities Show Off
****************************************************************************/





/****************************************************************************
//	SECTION::Case Study Images and Captions
*/

	var caseStudyCaptions = new Array();
	var caseStudyImages   = new Array();
	function swapCaseStudyImage(theImage, swapIndex) {
		theImage = $(theImage);
		
		$ES('div', $('caseStudyBigImageHolder' + currentCaseStudyID)).each(function(theDiv, index) {
			if(index == swapIndex)
				theDiv.setStyle('display', 'block');
			else
				theDiv.setStyle('display', 'none');
		});
		
	//	$(bigImageID).src = 'images/caseStudies/CaseStudy' + currentCaseStudyID + '/' + caseStudyImages[currentCaseStudyID][swapIndex];
		$('caseStudyImageCaption' + currentCaseStudyID).setHTML(
			caseStudyCaptions[currentCaseStudyID][swapIndex]);
		
		// update which is the current for the sake of the active border:
		$ES('img', 'caseStudyImageSet' + currentCaseStudyID).each(function(thisImage, index) {
			thisImage.removeClass('current');
			stopCaseStudyFlash(index); // hide flash which is not in the clicked slot
		});
		theImage.addClass('current');
		fetchCaseStudyFlash(swapIndex); // show flash which might be in the clicked slot
	}
	
	// IE doesn't stop movies when they are not visible, AND the published
	// StopPlay() method(s) don't appear to work, so we'll get creative:
	var flashDungeon = new Hash();
	function stopCaseStudyFlash(swapIndex) {
		if(!window.ie) return; // not a problem, no need to bother
		
		var theDiv = $('caseStudyBigImage' + currentCaseStudyID + '_' + swapIndex);
		if(!theDiv) return;
		
		var movieNode = theDiv.getElement('object')
		if(movieNode) {
			flashDungeon.set('csi' + currentCaseStudyID + '_' + swapIndex, movieNode);
			theDiv.empty();
		}
	}
	
	function stopCaseStudyPageFlash(currentPageIndex) {
		$ES('div', $('caseStudyBigImageHolder' + currentCaseStudyID)).each(function(theDiv, index) {
			stopCaseStudyFlash(index);
		});
	}
	
	
	function fetchCaseStudyFlash(swapIndex) {
		if(!window.ie) return; // not a problem, no need to bother
		
		if(flashDungeon.has('csi' + currentCaseStudyID + '_' + swapIndex))
			$('caseStudyBigImage' + currentCaseStudyID + '_' + swapIndex).adopt(flashDungeon.get('csi' + currentCaseStudyID + '_' + swapIndex));
	}
	
/*
//	End SECTION::Case Study Images and Captions
****************************************************************************/





