( function ( ) {

	Mission.tasks(1).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 );
	
					var route = Switchboard.getRoute ( 0, 2 );
	
					Switchboard.reset();
					Switchboard.setEvidence ( [ "Purchase Order" ] );
					Switchboard.setAgency ( 0, "SO15 Police Unit" );
					Switchboard.setAgency ( 1, "Judge" );
					Switchboard.setAgency ( 2, "CRIMINT Database", [ 0 ] );
					if ( route ) {
						route.redraw();
					} else {
						$([
							['evidence_0','in_5_a'],
							['in_5_b','research_2_a'],
							['research_2_b','analysis_2_a'],
							['analysis_2_b','out_2_a'],
							['out_2_b','agencies_2']
						]).each ( function ( ) {
							Switchboard.joinNodes ( this[0], this[1] );
						} );
					}
					cb();
				}, 100 );
			} );
		},
		start : function ( cb ) {
			var i = setInterval ( function ( ) {
				if ( !window.Switchboard || !window.Mailbox ) return;
				clearInterval ( i );

				loadConversation ( '/scripts/wanted/conversations/raid.js', function ( s ) {
					var audio = new Audio ( window.scripts.RAID, true, cb );
					audio.doPuzzle = function ( cb ) {
						puzzle_1 ( cb );
					}
				} );
			}, 100 );
		}

	} );

	var puzzle_1 = function ( cb ) { // Mission Puzzle 3
		Portal.setHint ( "Using a hint will reset the grid - do you wish to continue?", null, function ( rsp ) {
			if ( rsp ) {
				var route = Switchboard.getRoute ( 0, 2 );
				var tracks = [];
				Switchboard.reset(true);
				if ( route ) {
					route.redraw();
				} else {
					tracks = [
						["evidence_0", "in_5_a"],
						["in_5_b", "research_2_a"],
						["research_2_b", "analysis_2_a"],
						["analysis_2_b", "out_2_a"],
						["out_2_b", "agencies_2"]
					];
				}
				Array.prototype.push.apply ( tracks, [
					[ "evidence_1", "in_2_a" ],
					[ "in_2_b", "research_3_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 = {
			pu : false,
			ci : true
		}
		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, "SO15 Police Unit", [ 0, 1 ], respond ( 'pu' ) );
		Switchboard.setAgency ( 1, "Judge" );
		Switchboard.setAgency ( 2, "CRIMINT Database", [ 0 ], respond ( 'ci' ) );
		Switchboard.enable();
	}

} )();

