( function ( ) {

	Mission.tasks(4).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 );
	
					Switchboard.reset();
					Switchboard.setEvidence ( [ "Destination", "Time", "Location" ] );
					Switchboard.setAgency ( 0, "Travel Agent" );
					Switchboard.setAgency ( 1, "Airport Security" );
					Switchboard.setAgency ( 2, "Maps" );
	
					cb();
				}, 100 );
			} );
		},
		start : function ( cb ) {
			var i = setInterval ( function ( ) {
				if ( !window.Switchboard || !window.Mailbox ) return;
				clearInterval ( i );

				loadConversation ( '/scripts/wanted/conversations/raid_4.js', function ( ) {
					puzzle_1 ( function ( ) {
						Mailbox.activate ( 13 );
						var audio = new Audio ( window.scripts.RAID_4, true, cb );
					} );
				} );

			}, 100 );
		}

	} );

	var puzzle_1 = function ( cb ) { // Mission Puzzle 8
		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_2_a" ],
					[ "evidence_1", "in_3_a" ],
					[ "evidence_2", "in_3_b" ],

					[ "in_2_b", "research_3_a" ],
					[ "in_3_c", "research_3_b" ]
				];
				$(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 = {
			ta : false,
			as : false,
			maps : 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 ( 0, "Travel Agent", [ 0, 1, 2 ], respond ( "ta" ) );
		Switchboard.setAgency ( 1, "Airport Security", [ 0, 1, 2 ], respond ( "as" ) );
		Switchboard.setAgency ( 2, "Maps", [ 0, 1, 2 ], respond ( "maps" ) );
		Switchboard.enable();
	}

} )();
