soundManager.waitForWindowLoad = true;
var useSound = 1;

soundManager.onload = function() {

	// Common functions
	function stripAlphaChars(pstrSource) {
		var m_strOut = new String(pstrSource); 
	    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 
	    return m_sOut; 
	}

	// Common variables
	var step = 0;
	var owlStep = 0;
	var bigCloudOpacity = 0.90;
	var bigCloudWidth = 267;
	var bigCloudHeight = 161;
	var nextCloud;
	var nextRay;
	var nextRay2;

	// Initial bugfix fade-ins
	var textbox = $('textbox');
	textbox.fade(0);

	var bigCloud = $('bigCloud');
	bigCloud.fade(0);
	bigCloud.setStyle('display', 'none');

	var storygames = $('storygames');
	storygames.fade(0);
	storygames.setStyle('display', 'none');

	// Initialise each cloud
	$$('.cloud').each(function(cloud) {
		cloud.setStyle('display', 'none');
		cloud.fade(0);
		cloud.addEvent('mouseover', function(e) {
			e.stop();
			cloud.set('tween', {duration: 'short'});
			cloud.tween('opacity', '1');
		});
		cloud.addEvent('mouseout', function(e) {
			e.stop();
			cloud.set('tween', {duration: 'short'});
			cloud.tween('opacity', '0.8');
		});
		cloud.addEvent('click', function(e) {
			e.stop();
			showText(cloud.id + 'Text');
		});	
	});

	// Initialise each ray
	$$('.ray').each(function(ray) {
		ray.setStyle('display', 'none');
		ray.fade(0);
		ray.addEvent('mouseover', function(e) {
			e.stop();
			ray.set('tween', {duration: 'short'});
			ray.tween('opacity', '1');
		});
		ray.addEvent('mouseout', function(e) {
			e.stop();
			ray.set('tween', {duration: 'short'});
			ray.tween('opacity', '0.9');
		});
	});


	// Initialise each rainbow
	$$('.rainbow').each(function(rainbow) {
		rainbow.setStyle('display', 'none');
		rainbow.fade(0);
		rainbow.addEvent('mouseover', function(e) {
			e.stop();
			rainbow.set('tween', {duration: 'short'});
			rainbow.tween('opacity', '0.8');
		});
		rainbow.addEvent('mouseout', function(e) {
			e.stop();
			rainbow.set('tween', {duration: 'short'});
			rainbow.tween('opacity', '0.05');
		});
		rainbow.addEvent('click', function(e) {
			e.stop();
			showText(rainbow.id + 'Text');
		});	
	});


	// Do initial fade in from blue
	var container = $('container');
	(function() {
		$('curtain').fade(0);
//		introStep1();
	}).delay(500);
	(function() {
		$('curtain').setStyle('display', 'none');
	}).delay(1000);


	if (useSound == 1) {
		// Introduction sound objects
		var introMusic = soundManager.createSound({
			id: 'music',
			url: 'sfx/glissade.mp3',
			volume: 100
		});


		var introSound1 = soundManager.createSound({
			id: 'speech1',
			url: 'sfx/1947.mp3',
			volume: 100,
			onfinish: function () {
				introStep2();
			}
		});

		var introSound2 = soundManager.createSound({
			id: 'speech2',
			url: 'sfx/2008.mp3',
			volume: 100,
			onfinish: function () {
				introStep3();
			}
		});

		var introSound3 = soundManager.createSound({
			id: 'speech3',
			url: 'sfx/2009.mp3',
			volume: 100
		});

	}

	var introStep1 = function() {
		if (useSound == 1) {
			introMusic.play();
			introSound1.play();
		}
		showText('intro1');
		owlStep = 1;
	};

	var introStep2 = function() {
		if (useSound == 1) {
			introSound1.stop();
			introSound2.play();
		}
		showText('intro2');
		bigCloud.setStyle('display', 'block');
		bigCloud.set('tween', {duration: 'long'});
		bigCloud.fade('0.9');
		owlStep = 2;
	};		

	var introStep3 = function() {
		if (useSound == 1) {
				introSound2.stop();
				introSound3.play();
		}
		showText('intro3');
		owlStep = 3;

	};


	$('owl').addEvent('click', function(e) {
		e.stop();
		switch (owlStep) {
			case 3:
				if (useSound == 1) {
					introSound3.stop();
					soundManager.togglePause('music');
				}
//				showText();
				break;
			case 2:
				introStep3();
				break;
			case 1:
				introStep2();
				break;
			case 0:
				introStep1();
				break;
		}
	});

	$('owl').addEvent('click', function(e) {
		e.stop();
	});

	bigCloud.addEvent('mouseover', function(e) {
		e.stop();
		$('bigCloud').set('tween', {duration: 'short'});
		$('bigCloud').tween('opacity', '1');
	});
	bigCloud.addEvent('mouseout', function(e) {
		e.stop();
		$('bigCloud').set('tween', {duration: 'short'});
		$('bigCloud').tween('opacity', '0.8');
	});

	bigCloud.addEvent('click', function(e) {
      e.stop();
	  if (owlStep == 2) {
			introStep3();
	  } else {

		introSound3.stop();

		nextCloud = null;
		nextRay = null;

		step = step + 1;

		switch (step) {
			case 1:
				nextCloud = 1;
				break;
			case 2:
				nextCloud = 2;
				break;
			case 3:
				nextRay = 1;
				break;
			case 4:
				nextCloud = 3;
				break;
			case 5:
				nextRay = 2;
				break;
			case 6:
				nextCloud = 4;
				break;
			case 7:
				nextRay = 3;
				break;
			case 8:
				nextCloud = 5;
				break;
			case 9:
				nextRay = 4;
				break;
			case 10:
				nextCloud = 6;
				break;
			case 11:
				nextRay = 5;
				break;
			case 12:
				nextCloud = 7;
				break;
			case 13:
				nextRay = 6;
				break;
			case 14:
				nextCloud = 8;
				break;
			case 15:
				nextRay = 7;
				break;
			case 16:
				nextRay = 8;
				break;
		}


		bigCloud.set('tween', {duration: 'long'});
		bigCloudNewWidth = bigCloudWidth - (bigCloudWidth / 100 * 3 * step);
		bigCloudNewHeight = bigCloudHeight - (bigCloudHeight / 100 * 3 * step);
		bigCloudNewOpacity = bigCloudOpacity - (bigCloudOpacity / 100 * 3 * step);
		bigCloudNewMarginTop = (bigCloudHeight - bigCloudNewHeight) / 2;
		bigCloud.tween('opacity', bigCloudNewOpacity.toString());

		// The image inside the div
		$('biggy').morph({
			'height': bigCloudNewHeight,
			'margin-top': bigCloudNewMarginTop,
			'width': bigCloudNewWidth
		});

		if (step == 16) {
			bigCloud.set('tween', {duration: 'long'});
			bigCloud.tween('opacity', '0');
			(function() {
				bigCloud.setStyle('display', 'none');
				storygames.setStyle('display', 'block');
				storygames.set('tween', {duration: 'long'});
				storygames.tween('opacity', '0.5');
			}).delay(1000);
		}

		if (nextCloud) {
			showText('cloud' + nextCloud + 'Text');
		} else {
			showText('rainbow' + nextRay + 'Text');
		}
		        
		if (nextCloud) {
			if (useSound == 1) {
//				swoosh02.play();
			}
			currentCloud = $('cloud' + nextCloud);
			currentCloud.setStyle('display', 'block');
			currentCloud.set('tween', {duration: 'long'});
			currentCloud.tween('opacity', '0.9');
		}

		if (nextRay) {
			if (useSound == 1) {
//				swoosh02.play();
			}
			currentRay = $('ray' + nextRay);
			currentRay.setStyle('display', 'block');
			currentRay.set('tween', {duration: 'long'});
			currentRay.tween('opacity', '0.8');
			currentRainbow = $('rainbow' + nextRay);
			currentRainbow.setStyle('display', 'block');
			currentRainbow.set('tween', {duration: 'long'});
			currentRainbow.tween('opacity', '0.7');
			lastRainbow = $('rainbow' + (nextRay - 1));
			if (lastRainbow) {
				lastRainbow.set('tween', {duration: 'long'});
				lastRainbow.tween('opacity', '0.05');
			}
		}
	  }	
	});			

	storygames.addEvent('click', function(e) {
		e.stop();
		showText('storygamesText');
	});


	// Function to hide all text boxes except the ones supplied as arguments
	var showText = function (except) {
		textbox.set('tween', {duration: 'short'});
		textbox.tween('opacity', '0.0');
			(function() {
				$$('.tbox').each(function(tbox) {
					tbox.setStyle('display', 'none');

				});
				if (except) {
					currentText = $(except);
					if (currentText) {
						currentText.setStyle('display', 'block');
					}
				}
			}).delay(250);
		if (except) {
			(function() {
				textbox.tween('opacity', '0.5');
			}).delay(500);
		}
	};

};


soundManager.onerror = function() {
//	alert('SoundManager Error');
	useSound = 0;
	soundManager.onload();
};
                
