var cinematics = {
	intro : {
		sheet : "intro",
		slides : [
			{
				index : 0,
				nextSlide : 1000
			},
			{
				index : 1,
				nextSlide : 1000
			},
			{
				index : 0,
				nextSlide : 1000
			},
			{
				index : 5,
				nextSlide : 500
			},
			{
				index : 5,
				dialog : [
					"I'm a security guard at a research facility."
				]
			},
			{
				index : 2,
				nextSlide : 1000
			},
			{
				index : 2,
				dialog : [
					"I used to enjoy looking out at the city.",
					"But then they fell from the sky."
				]
			},
			{
				index : 3,
				nextSlide : 500
			},
			{
				index : 4,
				dialog : [
					"A large meteor hit the city.",
					"I could scarcely react before a smaller one flew towards the facility."
				]
			},
			{
				index : 6,
				nextSlide : 1000
			},
			{
				index : 7,
				nextSlide : 2000
			},
			{
				index : 8,
				nextSlide : 2000
			},
			{
				index : 8,
				dialog : [
					"This is my story.",
				]
			}
		]
	},
	path : {
		sheet : "path",
		slides : [
			{
				index : 0,
				nextSlide : 1000
			},
			{
				index : 1,
				nextSlide : 500
			},
			{
				index : 2,
				dialog : [
					"Hey there!"
				]
			},
			{
				index : 3,
				dialog : [
					"We just finished with the cleanup outside the building. You can go around the building from here to get to",
					"the main entrance. You should probably buy more Medkits before moving on.",
					"The final creature is ahead, and he does not look like a pushover."
				]
			},
			{
				index : 4,
				dialog : [
					"I'll let you get back to what you were doing."
				]
			}
		]
	},
	ending : {
		sheet : "ending",
		slides : [
			{
				index : 0,
				nextSlide : 2000
			},
			{
				index : 2,
				dialog : [
					"And so with that I defeated the final monster, assuring the survival of myself and everyone at the facility."
				]
			},
			{
				index : 3,
				dialog : [
					"I don't know who these creatures are, or if humanity will survive this invasion,",
					"but I do know one thing."
				]
			},
			{
				index : 0,
				nextSlide : 1500
			},
			{
				index : 0,
				dialog : [
					"I feel pretty good about our chances."
				]
			},
			{
				index : 0,
				nextSlide : 2000
			},
			{
				index : 1
			}
		]
	}
}

function runCinematic(cinematic) {
	disableControls();
	document.getElementById("topBarCover").style.left = "0px";
	document.getElementById("toolbarCover").style.left = "0px";
	
	cinematicStage(cinematic, 0);
}

function endCinematic() {
	enableControls();
	document.getElementById("topBarCover").style.left = "-320px";
	document.getElementById("toolbarCover").style.left = "-320px";
	map.render();
}

function cinematicStage(cinematic, stage) {
	if(stage == cinematic.slides.length) {
		endCinematic();
		return;
	}
	
	canvas.fillStyle = "black";
	canvas.fillRect(0, 0, 320, 256+44);
	var slide = cinematic.slides[stage];
	var img = imageObjects["images/cinematics/" + cinematic.sheet + ".jpg"];
	var xOffset = (slide.index % 5) * 320;
	var yOffset = Math.floor(slide.index / 5) * 180;
	//alert(xOffset + ", " + yOffset);
	canvas.drawImage(img, xOffset, yOffset, 320, 180, 0, 38+22, 320, 180);
	
	if(slide.nextSlide) {
		setTimeout(function() {
			cinematicStage(cinematic, stage + 1);
		}, slide.nextSlide);
	} else if(slide.dialog) {
		dialog(slide.dialog, function() {
			cinematicStage(cinematic, stage + 1);
		})
	}
}