/**
 * Progressiveley enhance journey pages with Flash animation
*/

sita.journey = function() {


	// If Flash player 9 unavailable
	if(!swfobject.hasFlashPlayerVersion('9.0.0')) {
		// Set a body class to undo CSS prep for p/e
		jq('body').addClass('no-journey-flash');
		// Do nothing
		return;
	}

	// Which journey
	var journey = sita.mapPageNameToJourney(openc.PAGE_NAME);
	
	// Wrap intro and all steps in one div
	jq('div#parent-fieldname-text, div.step').wrapAll('<div id="text-content"></div>');
	
	var textContent = jq('div#content div#text-content');
	
	
	// Add a list of links to each of the steps
	sita.journeyStepsList = jq('div#text-content div#parent-fieldname-text').append('<ul id="journey-steps"></ul>').find('ul:last');
	jq('div#content div.step').each(function(index) {
		var jqThis = jq(this);
		sita.journeyStepsList.append('<li class="pos' + (index + 1) + '"><span class="a">' + jqThis.find('h2').text() + '</span></li>')
		.find('span.a')
		.mouseover(function() {
			jq(this).addClass('a-hover');
		})
		.mouseout(function() {
			jq(this).removeClass('a-hover');
		})
		.click(sita.onJourneyStepsLiClick);
	});
	
	// Expand to the maximum height of the tallest child
	/*
	var closure = function() {
		sita.matchHeightToTallestChild(textContent);
	}
	closure();
	setInterval(closure, 5000);
	*/
	
	/**
	 * Inject Flash
	*/
	
	textContent.before('<div id="journeys-swf-container"><div id="journeySwf"></div></div>');
	
	// use swfobject to replace alternate content
	var movie = swfobject.embedSWF(
		'/++resource++sita.theme.swf/journeys/main.swf',
		'journeySwf',
		554,
		467,
		'9.0.0',
		null,
		// Flashvars:
		{
			journey:journey,
			site:'sita'
		},
		// Params:
		{
			wmode:'transparent',
			base:'/++resource++sita.theme.swf/journeys'
		},
		// Attributes
		{}
	)
}

/**
 * Handler for Flash zoom instance over
*/
sita.onZoomOver = function(stepNumber) {
	sita.journeyStepsList.find('li.pos' + stepNumber).addClass('over');
}
/**
 * Handler for Flash zoom instance out
*/
sita.onZoomOut = function(stepNumber) {
	sita.journeyStepsList.find('li.pos' + stepNumber).removeClass('over');
}


/**
 * Journey step LI click  handler
*/
sita.onJourneyStepsLiClick = function() {
	
	// Retrieve step number from class name
	var stepNumber = jq(this).parent()[0].className.replace(/.*pos(\d).*/, '$1')
	
	// Let Flash know
	thisMovie('journeySwf').onSelectStep(stepNumber);
	
	// Select the step
	sita.selectStep(stepNumber);
	
}

/**
 * Called from the swf, Zoom release hander
*/
sita.onZoomRelease = function(stepNumber) {
	sita.selectStep(stepNumber);
}


/**
 * Called indirectly by both Flash and HTML. Select step
*/
sita.selectStep = function(stepNumber) {

	// Hide intro text
	jq('div#text-content div#parent-fieldname-text').css('visibility', 'hidden');
	
	// Show the appropriate step's text
	var selectedStepDiv = jq('div#text-content div.step:eq(' + (stepNumber - 1) + ')');
	
	selectedStepDiv.css('visibility', 'visible');
}

/**
 * Called by the Flash movie
*/
sita.onBackButtonRelease = function() {

	// Hide all steps
	jq('div#text-content div.step').css('visibility', 'hidden');
	
	// Show the index again
	jq('div#text-content div#parent-fieldname-text').css('visibility', 'visible');
	
}


/**
 * Ensure the element is tall enough for all of its abs. positioned children
*/
sita.matchHeightToTallestChild = function(element) {
	
	/**
	 * Safari exaggerates the width of the ticker at first, causing problems. 
	 * Wait until it settles down. TODO - find a non arbitrary property.
	*/
	if(element.height() > 2000) {
		var closure = function() {
			sita.matchHeightToTallestChild(element);
		}
		setTimeout(closure, 1);
		return;
	}
	var maxHeight = 0;
	// Loop through children and stretch height if necessary
	element.children().each(function() {
		if(jq(this).height() > maxHeight) {
			maxHeight = jq(this).height();
		}
	});
	//openc.log(maxHeight);
	element.height(maxHeight);
}

/**
 * Journey turns steps into interactive presentation with a flash movie
*/

function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}
/**
 * Page names (with underscores substituting for dashes) and their 
 * corresponding journey
*/
sita.mapPageNameToJourney = function(pageName) {
	var map = {
		the_composting_process : 'composting',
		composting_at_home : 'composting-at-home',
		the_efw_process : 	'energy-from-waste',
		how_does_landfill_work : 'landfill',
		aluminium : 'aluminium',
		glass : 'glass',
		paper : 'paper',
		plastic : 'plastic'
	};
	return map[pageName.replace(/-/g, '_')];
}

 
/*******************************************************************************
DOM ready
*******************************************************************************/

jq(function() {

	// If this is an edit page
	if(
		openc.IS_RIA_CLIENT && 
		!openc.IS_EDIT_PAGE && 
		openc.IS_DESKTOP && 
		jq('body').hasClass('journey')
	) {
		sita.journey();
	}
})





