( function ( ) {

	Mission.tasks(5).extend ( {

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

					Mailbox.activate ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 );

					Switchboard.reset();
					Switchboard.setEvidence ( [ "Suspect Description", "GCHQ Radio Link", "Airport Layout", "Suspect Location", "Airport CCTV Feed" ] );
					Switchboard.setAgency ( 0, "Airport Security" );
					Switchboard.setAgency ( 1, "Special Forces" );
					Switchboard.setAgency ( 2, "Bomb Squad" );
	
					cb();
				}, 100 );
			} );
		},
		start : function ( cb ) {
			var i = setInterval ( function ( ) {
				if ( !window.Switchboard || !window.Mailbox ) return;
				clearInterval ( i );

				loadConversation ( '/scripts/wanted/conversations/raid_5.js', function ( ) {
					puzzle_1 ( function ( ) {
						var audio = new Audio ( window.scripts.RAID_5, true, cb );
					} );
				} );

			}, 100 );
		}

	} );

	var puzzle_1 = function ( cb ) { // Mission Puzzle 9
		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_6_a" ],
					[ "evidence_1", "in_5_a" ],
					[ "evidence_2", "in_4_a" ],
					[ "evidence_3", "in_4_b" ],
					[ "evidence_4", "in_1_a" ],

					[ "in_1_b", "research_1_a" ],
					[ "in_4_c", "research_4_a" ],
					[ "in_5_b", "research_3_a" ],
					[ "in_5_c", "research_4_b" ],
					[ "in_6_b", "research_2_a" ],
					[ "in_6_c", "research_3_b" ]
				];
				$(tracks).each ( function ( ) {
					Switchboard.joinNodes ( this[0], this[1] );
				} );
			}
		} );
		var timer = Timer.start ( 360, 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 = {
			as : false,
			sf : false,
			bs : false
		}
		var check = function ( ) {
			for ( var c in checks )
				if ( !checks[c] ) return;
			timer.stop();
			Switchboard.disable();
			doAction ( 'c25c70' );
			Portal.setHint();
			cb();
		}
		var respond = function ( type ) {
			return function ( done ) {
				checks[type] = done;
				check();
			}
		}
		Switchboard.setAgency ( 0, "Airport Security", [ 0, 1 ], respond ( "as" ) );
		Switchboard.setAgency ( 1, "Special Forces", [ 0, 1, 2, 3, 4 ], respond ( "sf" ) );
		Switchboard.setAgency ( 2, "Bomb Squad", [ 1, 2, 3 ], respond ( "bs" ) );
		Switchboard.enable();
	}

} )();

