//based on XAM scripts

if (_this select 1 == "off") exitWith 
{
	ch_m1abrams_TermicVision = 0;
	sleep 0.2;
	ch_m1abrams_TermicVision = nil;
};

if (_this select 1 == "on") exitWith 
{
	if (ch_m1abrams_TermicVision == 0 || ch_m1abrams_TermicVision == 1) exitWith {};

	ch_m1abrams_TermicVision = 0;

	private ["_Soldier"];

	_Soldier = player;
	_overcast = overcast;
	_DateYear = Date select 0;
	_DateMonth = Date select 1;
	_Dateday = Date select 2;
	_Datehour = Date select 3;
	_Dateminute = Date select 4;
	_minute = 0;
	_hour = 0;
	_first = 0;
	_allps = [];

	for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
	{
		_cntps = count _allps;
		for [{_i=0},{_i<_cntps},{_i=_i+1}] do {deleteVehicle (_allps select _i)};
		_allps = [];

		ch_m1abrams_TermicVision = 0;

		sleep 0.1;
		if (_first == 1) then {setaperture -1; titletext ["","white in"];};

		waitUntil {ch_m1abrams_TermicVision == 1};

		_date = setdate [1985, 7, 01, 4, 2];
		titletext ["","white in"];
		setaperture 0.025;

		0 setOvercast 0;

		_trg1=createTrigger["EmptyDetector",getPos _Soldier];
		_trg1 setTriggerArea[3000,3000,0,false];
		_trg1 setTriggerActivation["ANY","PRESENT",false];
		_trg1 setTriggerStatements["this", "", ""];

		for [{_loop2=0}, {_loop2<1}, {_loop2=_loop2}] do
		{
			sleep 0.1;

			if (Date select 4 == 4) then {_date = setdate [1985, 7, 01, 4, 2]; _minute = _minute + 1;};
			if (_minute == 60) then {_minute = 0; _hour = _hour + 1};
		
			_cnttrg1 = count list _trg1;
			_cntps = count _allps;
		
			if (_cntps != _cnttrg1) then
			{
				for [{_i=0},{_i<_cntps},{_i=_i+1}] do {deleteVehicle (_allps select _i)};
				_allps = [];

				{
					_obj = vehicle _x;
					_ps = "#particlesource" createVehicleLocal getpos _obj;
					_ps setParticleCircle [0, [0, 0, 0]];
					_ps setParticleRandom [0, [0, 0, 0], [0, 0, 0], 0, 0, [0, 0, 0, 0], 0, 0];
					_ps setParticleParams [["\ch_m1abrams\dot.p3d", 1, 0, 1], "", "Billboard", 1, 0.25, [0,0,2], [0, 0, 0], 0, 1.275, 1, 0, [0.5, 0.5], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [0], 0, 0, "", "", _obj];
					_ps setDropInterval 0.025;

					_allps = _allps + [_ps];

				} foreach list _trg1;
			};

			_trg1 setpos [getpos (vehicle _Soldier) select 0, getpos (vehicle _Soldier) select 1, 0];

			if (ch_m1abrams_TermicVision == 0 || vehicle player == player || (vehicle player) animationPhase "HatchCommander" > 0 || !(gunner (vehicle player) == player || commander (vehicle player) == player) || !(alive player)) then
			{
				for [{_i=0},{_i<_cntps},{_i=_i+1}] do {deleteVehicle (_allps select _i)};

				setaperture 5;
				0 setovercast _overcast;
				setdate [_DateYear , _DateMonth, _Dateday, (_Datehour + _hour), (_Dateminute + _minute)];
				_first = 1;
				deleteVehicle _trg1;
				_loop2=1;
			};
		};
	};
};