(function ($) {

	"use strict";


	/**
	 * Init
	8888888        d8b888    
	  888          Y8P888    
	  888             888    
	  888  88888b. 888888888 
	  888  888 "88b888888    
	  888  888  888888888    
	  888  888  888888Y88b.  
	8888888888  888888 "Y888 
	**/
	window.init = function () {

		TM.soundManager.init();
		TM.machine = new TM.TimeMachine({

/**
 * screens
.d8888b  .d8888b888d888 .d88b.  .d88b. 88888b. .d8888b  
88K     d88P"   888P"  d8P  Y8bd8P  Y8b888 "88b88K      
"Y8888b.888     888    8888888888888888888  888"Y8888b. 
     X88Y88b.   888    Y8b.    Y8b.    888  888     X88 
 88888P' "Y8888P888     "Y8888  "Y8888 888  888 88888P' 

88888888888888888888888888888888888888888888888888888888

88888888888888888888888888888888888888888888888888888888
**/
screens: [
	{
		id: "reboot",
		kind: "screen",
		options: {
			text: "\n[Unwinding space-time loops]\n\nPlease stand by.",
			sounds: {
				// start: "fine1"
			},
			timed: {
				target: "instructions1",
				delay: 3000
			}
		}
	}, // instructions1

	{
		id: "headphones",
		kind: "screen",
		options: {
			text: "Time Machine v0.01.aefc.200.4\n\nInstruction 000:\nApply headphones to head.\n\nYou *must* wear headphones to travel\nthrough space and time.",
			sounds: {
				// start: "fine1"
			},
			btns: [
				{
					text: "Confirm",
					target: "begin"
				}
			]
		}
	}, // headphones

	{
		id: "begin",
		kind: "screen",
		options: {
			text: "Time Machine v0.01.bdaa.347.1\nrequests access to your system.",
			sounds: {
				start: "Audio_welcome_1"
			},
			btns: [
				{
					text: "Allow",
					target: "blank10"
				}
			]
		}
	}, // begin

	{
		id: "blank10",
		kind: "screen",
		options: {
			text: "Downloading magnetic schemas...",
			sounds: {
				// start: "TKTK"
			},
			timed: {
				target: "allow",
				delay: 1000
			}
		}
	}, // blank10

	{
		id: "allow",
		kind: "screen",
		options: {
			text: "Please allow *complete* access.",
			sounds: {
				// start: "fine1"
			},
			btns: [
				{
					text: "OK",
					target: "blank20"
				}
			]
		}
	}, // allow

	{
		id: "blank20",
		kind: "screen",
		options: {
			text: "\nMeasuring system resources [ ]",
			sounds: {
				// start: "TKTK"
			},
			timed: {
				target: "disclaimer1",
				delay: 17000
			},
			callback: function () {
				var i = 0,
					anim = [
						"[—]",
						"[\\]",
						"[|]",
						"[/]"
					],
					fn = function () {
						i++;

						this.textObj.setText("\nMeasuring system resources " + anim[i % 4]);

						if (this.active) {
							setTimeout(fn, 200)
						}
					}.use(this);

				fn();
			}
		}
	}, // blank20

	{
		id: "disclaimer1",
		kind: "screen",
		options: {
			text: "\nDisclaimer\n\nAttention: You are about to travel through time and space. Motion sickness WILL occur. EPILEPSY WARNING. Don't touch anything. Dinosaurs are not friendly. Trust us. In the event that YOU DO NOT RECALL TRAVELLING THROUGH TIME, it's safe to assume that you've experienced MILD TO SEVERE MEMORY LOSS. This is not uncommon. Drug and/or alcohol use is permitted, but not recommended. Time travel is not recommended for anyone under the age of 2. And you don't even want to know what happens to pregnant women- if you are pregnant, DO NOT attempt time travel. Seriously, for the love of all that is sacred, just come back in 9 months.",
			sounds: {
				start: "Audio_welcome_3"
			},
			timed: {
				target: "disclaimer2",
				delay: 20000
			},
			callback: function () {
				var i = 0,
					fn = function () {
						i++;

						this.textObj.position.y -= 32;

						if (this.active) {
							setTimeout(fn, 2000);
						}
					}.use(this);

				fn();
			}
		}
	}, // disclaimer1

	{
		id: "disclaimer2",
		kind: "screen",
		options: {
			text: "TRAVELLING THROUGH TIME, it's safe to assume that you've experienced MILD TO SEVERE MEMORY LOSS. This is not uncommon. Drug and/or alcohol use is permitted, but not recommended. Time travel is not recommended for anyone under the age of 2. And you don't even want to know what happens to pregnant women- if you are pregnant, DO NOT attempt time travel. Seriously, for the love of all that is sacred, just come back in 9 months.",
			sounds: {
				// start: "fine3"
			},
			btns: [
				{
					text: "Continue",
					target: "TOS1"
				}
			],
			callback: function () {
				this.textObj.position.y -= 64;
			}
		}
	}, // disclaimer2

	{
		id: "TOS1",
		kind: "screen",
		options: {
			text: "Terms of Service\n\nBy agreeing to the terms of service put forth by [company name], you hereby accept full responsibility for any actions, behaviors, or weird shit that happens during your time travel experience.",
			sounds: {
				// start: "fine3"
			},
			btns: [
				{
					text: "Next",
					target: "TOS2"
				}
			]
		}
	}, // TOS1

	{
		id: "TOS2",
		kind: "screen",
		options: {
			text: "Instagram use is strictly forbidden\nduring time travel.\n\nProviding your past/future self with proprietary information for the intended\npurpose of gaining wealth, power,\n and/or dominion over the human race\nis a HUGE no-no.",
			sounds: {
				// start: "fine3"
			},
			btns: [
				{
					text: "Next",
					target: "TOS3"
				}
			]
		}
	}, // TOS2

	{
		id: "TOS3",
		kind: "screen",
		options: {
			text: "Furthermore, you absolve [company name] of any and all liability associated with the consequences of any actions, behaviors, or aforementioned weird shit; including, but not limited to:",
			sounds: {
				// start: "fine3"
			},
			btns: [
				{
					text: "Next",
					target: "TOS4"
				}
			]
		}
	}, // TOS3

	{
		id: "TOS4",
		kind: "screen",
		options: {
			align: "left",
			text: "* EPILEPTIC SEIZURE\n* Spontaneous death\n* COMPREHENSIVE MEMORY LOSS\n* Forever altering the flow of time\n* Destruction of the known universe\n* Becoming your own father, brother,\n  mother, or sister",
			sounds: {
				// start: "fine3"
			},
			btns: [
				{
					text: "Accept",
					target: "routines"
				},
				{
					text: "Decline",
					target: "routines"
				}
			],
			callback: function () {
				var self = this;

				self.btns[1].btnObj.mouseover = function () {
					self.btns[1].btnText.setText('Accept');

					glitchesOff = false;
				};
			}
		}
	}, // TOS4

	{
		id: "routines",
		kind: "screen",
		options: {
			text: "\nTime Machine v0.01.cfca.598.3\n0 Terabytes",
			sounds: {
				start: "Audio_welcome_2"
			},
			timed: {
				target: "routines2",
				delay: 17000
			},
			callback: function () {
				glitchesOff = false;

				var i = 0,
					fn = function () {
						i += 7;

						if (i > 3274) {
							i = 3274;
						}

						this.textObj.setText("\nTime Machine v0.01.cfca.598.3\n" + (i > 1000 ? (Math.floor(i / 1000) + ',' + ('000' + (i % 1000)).slice(-3)) : i) + " Terabytes");

						if (this.active && i < 3274) {
							setTimeout(fn, 1000/30)
						}
					}.use(this);

				fn();
			}
		}
	}, // routines

	{
		id: "routines2",
		kind: "screen",
		options: {
			text: "\nTime Machine v0.01.cfca.598.3\n3,274 Terabytes",
			sounds: {
				start: "audio_fine"
			},
			btns: [
				{
					// width: 250,
					text: "Install",
					target: "glitch"
				}
			]
		}
	}, // routines2

	{
		id: "glitch",
		kind: "screen",
		options: {
			align: "left",
			text: "\nReplacing your computer operating system\nwith custom time travel routines.\n\nWorking...",
			sounds: {
				start: "audio_fine2"
			},
			timed: {
				target: "glitch2",
				delay: 5000
			},
			callback: function () {
				var i = 0,
					anim = [
						"",
						".",
						"..",
						"..."
					],
					fn = function () {
						i++;

						this.textObj.setText("\nReplacing your computer operating system\nwith custom time travel routines\n\nWorking" + anim[i % 4]);

						if (this.active) {
							setTimeout(fn, 200)
						}
					}.use(this);

				fn();
			}
		}
	}, // glitch

	{
		id: "glitch2",
		kind: "screen",
		options: {
			align: "left",
			text: "\nReplacing your computer operating system\nwith custom time travel routines\n\nWorking...DONE",
			sounds: {
				start: "audio_fine3"
			},
			btns: [
				{
					// width: 250,
					text: "That's Fine",
					target: "instructions1"
				}
			]
		}
	}, // glitch2

	{
		id: "instructions1",
		kind: "screen",
		options: {
			text: "\n\n:)",
			sounds: {
				start: "Audio_welcome_4"
			},
			timed: {
				target: "instructions11",
				delay: 15000
			}
		}
	}, // instructions1

	{
		id: "instructions11",
		kind: "screen",
		options: {
			text: "\n\n>:|",
			sounds: {
				// start: "Audio_welcome_4"
			},
			timed: {
				target: "instructions2",
				delay: 7000
			},
			callback: function () {
				glitchesOff = false;
			}
		}
	}, // instructions11

	{
		id: "instructions2",
		kind: "screen",
		options: {
			text: "\n\n:[",
			sounds: {
				// start: "Audio_welcome_4"
			},
			timed: {
				target: "instructions3",
				delay: 3000
			}
		}
	}, // instructions2

	{
		id: "instructions3",
		kind: "screen",
		options: {
			text: "\n\n:D",
			sounds: {
				// start: "Audio_welcome_4"
			},
			timed: {
				target: "step1",
				delay: 5000
			},
			callback: function () {
				glitchesOff = true;
			}
		}
	}, // instructions3

	{
		id: "step1",
		kind: "screen",
		options: {
			text: "Instruction 001:\n\nTake a sip of your favorite beverage.",
			sounds: {
				start: "Audio_welcome_5"
			},
			btns: [
				{
					width: 325,
					text: "Hydration Achieved",
					target: "step1after"
				}
			]
		}
	}, // step1

	{
		id: "step1after",
		kind: "screen",
		options: {
			text: "\n\n\n;}",
			sounds: {
				start: "Audio_drink"
			},
			timed: {
				target: "step2",
				delay: 7000
			}
		}
	}, // step1after

	{
		id: "step2",
		kind: "screen",
		options: {
			text: "Instruction 002:\n\nSpin in place three times.",
			sounds: {
				start: "Audio_spin"
			},
			btns: [
				{
					width: 325,
					text: "Rotations Complete",
					target: "step3"
				}
			]
		}
	}, // step2

	{
		id: "step3",
		kind: "screen",
		options: {
			text: "Instruction 003:\n\nStart a slow clap.",
			sounds: {
				start: "Audio_clap"
			},
			btns: [
				{
					text: "Button",
					target: "step3error"
				}
			],
			timed: {
				target: "step3after",
				delay: 15000
			}
		}
	}, // step3

	{
		id: "step3error",
		kind: "screen",
		options: {
			text: "[Procedural Error]\n[Time Bubble Inversion]\n[Code: 0xafcd34]\n\n\nNobody told you to stop clapping!!",
			sounds: {
				start: "Audio_try_again"
			},
			btns: [
				{
					text: "Retry",
					target: "reboot"
				}
			]
		}
	}, // step3error

	{
		id: "step3after",
		kind: "screen",
		options: {
			text: "Urgent Instruction:\n\nSTOP CLAPPING IMMEDIATELY!",
			sounds: {
				// start: "Audio_clap"
			},
			btns: [
				{
					width: 350,
					text: "Affirmative 099",
					target: "step3fine"
				}
			],
			callback: function () {
				var i = 99,
					fn = function () {
						i--;
						this.btns[0].btnText.setText('Affirmative ' + ('000' + i).slice(-3));

						if (i == 0) {
							TM.machine.activateScreen('step3after_error');

						} else {
							setTimeout(fn, 1000 / 60);
						}
					}.use(this);

				fn();
			}
		}
	}, // step3after

	{
		id: "step3after_error",
		kind: "screen",
		options: {
			text: "[Procedural Error]\n[Relativity Reversed]\n[Code: 0774]\n\n\nYou must follow instructions exactly.",
			sounds: {
				start: "Audio_try_again"
			},
			btns: [
				{
					text: "Retry",
					target: "reboot"
				}
			]
		}
	}, // step3after_error

	{
		id: "step3fine",
		kind: "screen",
		options: {
			text: "\n\n:)",
			sounds: {
				start: "audio_fine"
			},
			timed: {
				target: "step3fine2",
				delay: 1500
			},
			callback: function () {
				glitchesOff = false;
			}
		}
	}, // step3fine

	{
		id: "step3fine2",
		kind: "screen",
		options: {
			text: "\n\n;;\n_",
			sounds: {
				// start: "audio_fine"
			},
			timed: {
				target: "step3fine3",
				delay: 500
			}
		}
	}, // step3fine2

	{
		id: "step3fine3",
		kind: "screen",
		options: {
			text: "\n\n:)",
			sounds: {
				// start: "audio_fine"
			},
			timed: {
				target: "step4",
				delay: 2500
			}
		}
	}, // step3fine3

	{
		id: "step4",
		kind: "screen",
		options: {
			text: "Instruction 004:\n\nFlash your nearest light source exactly four (4) times.",
			sounds: {
				start: "Audio_lights"
			},
			btns: [
				{
					width: 325,
					text: "Neutrons Phased",
					target: "step6"
				}
			],
			callback: function () {
				glitchesOff = true;
			}
		}
	}, // step4

	{
		id: "step6",
		kind: "screen",
		options: {
			text: "Instruction 006:\n\nRemove a shoe or article of clothing.\n\n* If you are not wearing any clothes,\nit is very important that y🜩",
			sounds: {
				start: "Audio_clothing"
			},
			btns: [
				{
					width: 325,
					text: "Mass Adjusted",
					target: "step8"
				}
			],
			callback: function () {
				glitchesOff = true;
			}
		}
	}, // step6

	{
		id: "step8",
		kind: "screen",
		options: {
			text: "Instruction l%#.\n\nClose your eyes.\nPrepare to be hurled across the ages.",
			sounds: {
				start: "Audio_eyes_fail 1",
				bg: "ambient"
			},
			timed: {
				target: "eyes1",
				delay: 5000
			}
		}
	}, // step8

	{
		id: "eyes1",
		kind: "screen",
		options: {
			text: "Are your eyes closed?",
			sounds: {
				// start: "Audio_eyes_fail 1"
			},
			btns: [
				{
					text: "Yes",
					target: "eyes_cheating"
				},
				{
					text: "No",
					target: "eyes_fail"
				}
			],
			timed: {
				target: "eyes2",
				delay: 2000
			}
		}
	}, // eyes1

	{
		id: "eyes_cheating",
		kind: "screen",
		options: {
			text: "\n\n>:{",
			sounds: {
				start: "Audio_eye_fail_2"
			},
			timed: {
				target: "eyes_fail",
				delay: 8000
			}
		}
	}, // eyes_cheating

	{
		id: "eyes_fail",
		kind: "screen",
		options: {
			text: "[Procedural Error]\n[Ocular Transgression]\n[C0de: O.o]\n\n\nYou must follow instructions exactly.",
			sounds: {
				start: "Audio_try_again"
			},
			btns: [
				{
					text: "Retry",
					target: "reboot"
				}
			],
			callback: function () {
				TM.soundManager.stopBgm();
			}
		}
	}, // eyes_fail

	{
		id: "eyes2",
		kind: "screen",
		options: {
			text: "\n\n\n\n** SEVERE EYE DAMAGE **",
			sounds: {
				start: "Audio_eyes_success"
			},
			timed: {
				target: "eyes3",
				delay: 5000
			},
			callback: eyeDamage
		}
	}, // eyes2

	{
		id: "eyes3",
		kind: "screen",
		options: {
			text: "\n\n\n\n** SEVERE EYE DAMAGE **",
			sounds: {
				start: "Audio_instruction_success"
			},
			timed: {
				target: "eyes4",
				delay: 7000
			},
			callback: eyeDamage
		}
	}, // eyes3

	{
		id: "eyes4",
		kind: "screen",
		options: {
			text: "\n\n\n\n** SEVERE EYE DAMAGE **",
			sounds: {
				start: "Audio_eyes_success"
			},
			timed: {
				target: ((Math.random() > 0.5) ? "eyes_dino1" : "eyes_modem1"),
				delay: 5000
			},
			callback: eyeDamage
		}
	}, // eyes4

	{
		id: "eyes_dino1",
		kind: "screen",
		options: {
			text: "\n\n\n\n** SEVERE EYE DAMAGE **",
			sounds: {
				start: "SFX_comley_dinosaur_roar_SINGLE_003"
			},
			timed: {
				target: "openeyes",
				delay: 5000
			},
			callback: eyeDamage
		}
	}, // eyes_dino1

	{
		id: "eyes_modem1",
		kind: "screen",
		options: {
			text: "\n\n\n\n** SEVERE EYE DAMAGE **",
			sounds: {
				start: "modem_shorter"
			},
			timed: {
				target: "eyes_modem2",
				delay: 12000
			},
			callback: eyeDamage
		}
	}, // eyes_modem1

	{
		id: "eyes_modem2",
		kind: "screen",
		options: {
			text: "\n\n\n\n** SEVERE EYE DAMAGE **",
			sounds: {
				start: "eyes_modem"
			},
			timed: {
				target: "openeyes",
				delay: 3000
			},
			callback: eyeDamage
		}
	}, // eyes_modem2

	{
		id: "openeyes",
		kind: "screen",
		options: {
			text: "\n[Quantum Shift Complete]\n[Wormhole Collapse 0:00:00]\n\nREMINDER: You may be experiencing disorientation and memory loss.\n\nDid you travel through space and time?",
			sounds: {
				start: "Audio_open_eyes"
			},
			btns: [
				{
					text: "Yes",
					target: "survey0"
				},
				{
					text: "No",
					target: "travelfail"
				}
			],
			callback: function () {
				TM.soundManager.stopBgm();

				TM.machine.timeFactor = 1.0;
				TM.machine.start = Date.now();
				TM.machine.timeElapsed = 0;
				TM.machine.lastTime = TM.machine.start;
			}
		},
	}, // openeyes

	{
		id: "travelfail",
		kind: "screen",
		options: {
			text: "\nYou must have not followed\nthe instructions exactly.",
			sounds: {
				start: "Audio_try_again"
			},
			btns: [
				{
					text: "Retry",
					target: "reboot"
				}
			]
		}
	}, // travelfail

	{
		id: "survey0",
		kind: "screen",
		options: {
			text: "\n\nINSERT INTO `\nSET `success` = '1001011001110110\n\n[Buffer overflow]",
			sounds: {
				// start: "Audio_eyes_success"
			},
			timed: {
				target: "survey",
				delay: 1000
			}
		}
	}, // survey0

	{
		id: "survey",
		kind: "screen",
		options: {
			text: "\nDid you ♥ your\ntime ɯɐɔɥᴉuǝ experience?",
			sounds: {
				// start: "Audio_open_eyes"
			},
			btns: [
				{
					text: "Y🜞es",
					target: "survey_yes"
				},
				{
					text: "🜱🜡🜩🝘",
					target: "survey_no"
				}
			],
			callback: function () {
				glitchesOff = false;
			}
		}
	}, // survey

	{
		id: "survey_yes",
		kind: "screen",
		options: {
			text: "\n\n:>",
			sounds: {
				start: "Audio_positive_feedback"
			},
			timed: {
				target: "whatnow",
				delay: 32000
			}
		}
	}, // survey_yes

	{
		id: "survey_no",
		kind: "screen",
		options: {
			text: "\n\n>:[",
			sounds: {
				start: "Audio_negative_feedback"
			}
		}
	}, // survey_no

	{
		id: "whatnow",
		kind: "screen",
		options: {
			text: "Now that you have returned from your journey through space and time...\n\nWhat will you do now?",
			sounds: {
				start: "audio_share_thoughts"
			},
			btns: [
				{
					text: "Share",
					target: ""
				}
			],
			callback: function () {
				glitchesOff = true;

				this.btns[0].btnObj.mouseup = this.btns[0].btnObj.touchend = function (data) {
					window.open('http://twitter.com/share?url=http://a.stronaut.com/z1x&text=%23realtimemachine');
				}
			}
		}
	}, // whatnow
]
/**
88888888888888888888888888888888888888888888888888888888

88888888888888888888888888888888888888888888888888888888
**/

		});

	};

}(jQuery));


function eyeDamage() {
	var i = 0,
		fgs = ['#00ffff', '#ffff00', '#ff0000', '#0000ff', '#ff00ff'],
		bgs = [0xff0000, 0x0000ff, 0xff00ff, 0x00ffff, 0xffff00],
		fn = function () {
			i++;

			this.fillBox.clear();
			this.fillBox.beginFill(bgs[i % 5], 1.0);
			this.fillBox.drawRect(0, 0, WIDTH, HEIGHT);

			this.textObj.setStyle({font: '30px Share Tech Mono', fill: fgs[i % 5]});

			if (this.active) {
				setTimeout(fn, 1000 / 8);
			}
		}.use(this);

	fn();
}