Mission.tasks(2).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 );

				Switchboard.reset();
				Switchboard.setEvidence ( [ "Address", "Name", "Phone Number", "Credit Card Number", "CRIMINT Results" ] );
				Switchboard.setAgency ( 0, "SO15 Police Unit" );
				Switchboard.setAgency ( 1, "Judge" );

				cb();
			}, 100 );
		} );
	},
	start : function ( cb ) {
		var i = setInterval ( function ( ) {
			if ( !window.Switchboard || !window.Mailbox ) return;
			clearInterval ( i );

			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_4_a" ]
					];
					$(tracks).each ( function ( ) {
						Switchboard.joinNodes ( this[0], this[1] );
					} );
				}
			} );
			var timer = Timer.start ( 90, function ( t ) {
				if ( !t.remaining )
					Mission.fail ( "You didn't route the evidence fast enough" );
				else
					Mission._puzzles.push ( t.time - t.remaining );
			} );
			Switchboard.enable();
			// Mission Puzzle 4
			Switchboard.setAgency ( 1, "Judge", [ 0, 1, 2, 3, 4 ], function ( done ) {
				if ( !done || arguments.callee.called ) return;
				arguments.callee.called = true;
				timer.stop();
				Switchboard.disable();
				Portal.setHint();
				loadConversation ( '/scripts/wanted/conversations/raid_2.js', function ( s ) {
					var audio = new Audio ( window.scripts.RAID_2_1, true, function ( ) {
						// Mission Puzzle 5
						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_0_a" ],
									[ "evidence_3", "in_2_a" ],
									[ "evidence_4", "in_1_a" ],
				
									[ "in_0_b", "research_0_a" ],
									[ "in_1_b", "research_1_a" ],
									[ "in_2_b", "research_2_a" ],
									[ "in_5_b", "research_3_a" ],
									[ "in_5_c", "research_5_a" ],
									[ "in_6_b", "research_3_b" ],
									[ "in_6_c", "research_6_a" ],
				
									[ "research_1_b", "analysis_1_a" ],
									[ "research_5_b", "analysis_3_b" ],
									[ "research_3_c", "analysis_5_a" ],
									[ "research_5_b", "analysis_3_a" ],
									[ "research_5_c", "analysis_4_b" ],
									[ "research_6_b", "analysis_3_b" ],
				
									[ "analysis_3_c", "out_2_a" ],
									[ "analysis_5_b", "out_3_b" ],
									[ "analysis_5_c", "out_4_b" ]
								];
								$(tracks).each ( function ( ) {
									Switchboard.joinNodes ( this[0], this[1] );
								} );
							}
						} );
						var timer = Timer.start ( 600, 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 = {
							phone : false,
							cc : false,
							dvla : false,
							ppa : false
						}
						var check = function ( ) {
							for ( var c in checks )
								if ( !checks[c] ) return;
							doAction ( 'b15fbe' );
							timer.stop();
							Switchboard.disable();
							Portal.setHint();
							Mailbox.activate ( 8 );
							Portal.setHint();
							var audio = new Audio ( window.scripts.RAID_2_2, true, cb );
						}
						var respond = function ( type ) {
							return function ( done ) {
								checks[type] = done;
								check();
							}
						}
						Switchboard.reset();
						Switchboard.setEvidence ( [ "Address", "Name", "Phone Number", "Credit Card Number", "Car Registration" ] );
						Switchboard.setAgency ( 0, "Phone Company", [ 0, 1, 2 ], respond ( "phone" ) );
						Switchboard.setAgency ( 1, "Credit Card Company", [ 1, 3 ], respond ( "cc" ) );
						Switchboard.setAgency ( 2, "DVLA", [ 0, 1, 4 ], respond ( "dvla" ) );
						Switchboard.setAgency ( 3, "Passport Office", [ 0, 1 ], respond ( "ppa" ) );
						Switchboard.enable();
					} );
				} );
			} )
		}, 100 );
	}

} );

