( function ( ) {

	Mission.tasks(0).extend ( {

		setup : function ( cb ) {
			Portal.loadPage ( this, 'panel/', null, cb );
		},
		start : function ( cb ) {
			var i = setInterval ( function ( ) {
				if ( !window.Switchboard || !window.Mailbox ) return;
				clearInterval ( i );

				loadConversation ( '/scripts/wanted/conversations/intro.js', function ( s ) {
					var audio = new Audio ( window.scripts.INTRO, true, function ( ) {
						Switchboard.showHelp ( function ( ) {
							puzzle_1 ( function ( ) {
								var audio = new Audio ( window.scripts.WARRANT, true, cb );
								audio.doPuzzle = function ( cb ) {
									puzzle_2 ( cb );
								}
							} );
						} );
					} );
				} );
			}, 100 );
		}

	} );

	var puzzle_1 = function ( cb ) { // Mission Puzzle 1
		Portal.setHint ( "Using a hint will reset the grid - do you wish to continue?", null, function ( rsp ) {
			if ( rsp ) {
				Switchboard.reset(true);
				var tracks = [
					["evidence_0","in_0_a"],
					["in_0_b", "research_0_a"],
					["research_0_b", "analysis_0_a"]
				];
				$(tracks).each ( function ( ) {
					Switchboard.joinNodes ( this[0], this[1] );
				} );
			}
		} );
		var timer = Timer.start ( 120, function ( t ) {
			if ( !t.remaining )
				Mission.fail ( "You didn't route the evidence fast enough" );
			else
				Mission._puzzles.push ( t.time - t.remaining );
		} );
		var checks = {
			pu : false
		}
		var check = function ( ) {
			for ( var c in checks )
				if ( !checks[c] ) return;
			timer.stop();
			Switchboard.disable();
			doAction ( '456ef6' );
			setTimeout ( function ( ) {
				Switchboard.reset(true);
				Switchboard.setAgency ( 0, "SO15 Police Unit" );
			}, 1000 );
			Portal.setHint();
			cb();
		}
		var respond = function ( type ) {
			return function ( done ) {
				checks[type] = done;
				check();
			}
		}
		Switchboard.setAgency ( 0, "SO15 Police Unit", [ 0 ], respond ('pu' ) );
		Switchboard.enable();
	}

	var puzzle_2 = function ( cb ) { // Mission Puzzle 2
		Switchboard.reset(true);
		Portal.setHint ( "Using a hint will reset the grid - do you wish to continue?", null, function ( rsp ) {
			if ( rsp ) {
				Switchboard.reset(true);
				var tracks = [
					[ "evidence_0", "in_5_a" ]
				];
				$(tracks).each ( function ( ) {
					Switchboard.joinNodes ( this[0], this[1] );
				} );
			}
		} );
		var timer = Timer.start ( 60, function ( t ) {
			if ( !t.remaining )
				Mission.fail ( "You didn't route the evidence fast enough" );
			else
				Mission._puzzles.push ( t.time - t.remaining );
		} );
		var checks = {
			j : false,
			ci : false
		}
		var check = function ( ) {
			for ( var c in checks )
				if ( !checks[c] ) return;
			timer.stop();
			Switchboard.disable();
			Portal.setHint();
			cb();
		}
		var respond = function ( type ) {
			return function ( done ) {
				checks[type] = done;
				check();
			}
		}
		Switchboard.setAgency ( 1, "Judge", [ 0 ], respond ( 'j' ) );
		Switchboard.setAgency ( 2, "CRIMINT Database", [ 0 ], respond ( 'ci' ) );
		Switchboard.enable();
	}

} )();

