( function ( ) {

	Mission.tasks(3).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 );
	
					Switchboard.reset();
					Switchboard.setEvidence ( [ "Warrant", "Phone Reference", "Credit Card Reference", "Car Licence Reference", "Passport Reference" ] );
					Switchboard.setAgency ( 0, "GCHQ" );
					Switchboard.setAgency ( 1, "Interpol" );
					Switchboard.setAgency ( 2, "AVT" );
					Switchboard.setAgency ( 3, "Credit Card Monitoring" );
	
					cb();
				}, 100 );
			} );
		},
		start : function ( cb ) {
			var i = setInterval ( function ( ) {
				if ( !window.Switchboard || !window.Mailbox ) return;
				clearInterval ( i );

				loadConversation ( '/scripts/wanted/conversations/raid_3.js', function ( ) {
					puzzle_1 ( function ( ) {
						var audio = new Audio ( window.scripts.RAID_3_1, true, function ( ) {
							puzzle_2 ( function ( ) {
								Mailbox.activate ( 10, 11 );
								var audio = new Audio ( window.scripts.RAID_3_2, true, cb );
							} );
						} );
					} );
				} );
	
			}, 100 );
		}

	} );

	var puzzle_1 = function ( cb ) { // Mission Puzzle 6
		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" ],
					[ "in_5_b", "research_5_a" ],
					[ "in_5_c", "research_6_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 = {
			gchq : false,
			ip : false,
			avt : false,
			ccms : false
		}
		var check = function ( ) {
			for ( var c in checks )
				if ( !checks[c] ) return;
			timer.stop();
			Switchboard.disable();
			doAction ( '28351f' );
			Portal.setHint();
			cb();
		}
		var respond = function ( type ) {
			return function ( done ) {
				checks[type] = done;
				check();
			}
		}
		Switchboard.setAgency ( 0, "GCHQ", [ 0, 1 ], respond ( "gchq" ) );
		Switchboard.setAgency ( 1, "Interpol", [ 0, 4 ], respond ( "ip" ) );
		Switchboard.setAgency ( 2, "AVT", [ 0, 3 ], respond ( "avt" ) );
		Switchboard.setAgency ( 3, "Credit Card Monitoring", [ 0, 2 ], respond ( "ccms" ) );
		Switchboard.enable();
	}

	var puzzle_2 = function ( cb ) { // Mission Puzzle 7
		Switchboard.reset();
		Switchboard.setEvidence ( [ "Warrant", "Credit Card Reference", "IP Address" ] );

		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 ( 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 = {
			ccc : false,
			isp : 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, "Credit Card Company", [ 0, 1 ], respond ( 'ccc' ) );
		Switchboard.setAgency ( 1, "ISP", [ 0, 2 ], respond ( 'isp' ) );
		Switchboard.enable();
	}

} )();
