
LimitsOut = {0.10, 0.10, 0.90, 0.90};
ZoomInStarts = 0.3;
ZoomOutStarts = 0.9;
ZoomInStops = 0.8;
ZoomOutStops = 0.5;
LargeZoomOutStarts = 1.2;
ZoomSpeed = 100;
MinZoominDistance = 70;
MinZoomChange = 30;
dofile("../data/programs/Common.lua");
ZoomingOut = false;
ZoomingIn = false;
Centering = false;
LargeZoom = 0.0;
NewCameraPosition = CVector(0, 0, 0);
NewCameraRotation = CVector(0, 0, 0);
TimeToMove = 0;
RotationSpeed = 0;
function Initialize()
		SetMinMax();
		Camera:SetPos(TargetPos + CVector(0, 0.1, 10));
	Camera:LookAt(TargetPos + CVector(0, 0, 0));
			Camera:SetPos(Camera:GetPrevModeLastPos());
	PrevCamRot = toEluer(Camera:GetPrevModeLastOrientation());
					NewCameraPosition = CalculatePosition();
		NewCameraRotation = CVector(math.rad(-11), 0, 0);
	Change = (Camera:GetPrevModeLastPos() - NewCameraPosition):length();
	TimeToMove = Change / ZoomSpeed;
			RotationSpeed = (PrevCamRot - NewCameraRotation):length() / TimeToMove;
		end
Corner1 = CVector.ZERO;
Corner2 = CVector.ZERO;
Min = Corner1;
Max = Corner2;
function SetMinMax()
		Corner1 = JumperPos;
	Corner2 = TargetPos;
	Min, Max = MinMaxCorners(Corner1, Corner2);
			if(not(TrajectoryStart == TrajectoryFinish))then
		Min = MinVec(Min, TrajectoryStart);
		Min = MinVec(Min, TrajectoryFinish);
		Max = MaxVec(Max, TrajectoryStart);
		Max = MaxVec(Max, TrajectoryFinish);
	end
end
function Update()
			CamPos = Camera:GetPos();
	CamRot = Camera:GetRot();
		SetMinMax();
		if(TimeToMove > 0)then
										TimeIncr = math.min(Timing.m_Elapsed, TimeToMove);
				TimeToMove = TimeToMove - TimeIncr;
				NewPos = CalcMovementFromTo(CamPos, NewCameraPosition, ZoomSpeed, TimeIncr);
		NewRot = CalcMovementFromTo(CamRot, NewCameraRotation, RotationSpeed, TimeIncr);
				Camera:SetPos(NewPos);
							elseif(RequireReposition(CamPos))then
				NewCameraPosition = CalculatePosition();
		Change = (CamPos - NewCameraPosition):length();
										TimeToMove = Change / ZoomSpeed;
 	end
end
function CalcMovementFromTo(From, To, Speed, Time)
		Direction = (To - From);
		Direction:normalize();
		Movement = Direction * Speed * Time;
	return From + Movement;
end
PlaneNormal		= CVector.UNIT_Z;
PointOnPlane	= CVector.ZERO;
TOP_RIGHT		= CVector(1, 0, 0);
TOP_LEFT		= CVector(0, 0, 0);
BOTTOM_RIGHT	= CVector(1, 1, 0);
BOTTOM_LEFT		= CVector(0, 1, 0);
function GetCorner(Corner)
		return Camera:ViewportPosToWorldPos(Corner, PlaneNormal, PointOnPlane);
end
function RequireReposition(CamPos)
			DistanceToCamera = Camera:GetPos().z;
	TopRight = GetCorner(TOP_RIGHT);
	TopLeft = GetCorner(TOP_LEFT);
	BottomRight = GetCorner(BOTTOM_RIGHT);
	BottomLeft = GetCorner(BOTTOM_LEFT);
		if(TopRight.x == CVector.MAX_NEGATIVE.x or 
		TopLeft.x == CVector.MAX_NEGATIVE.x or 
		BottomRight.x == CVector.MAX_NEGATIVE.x or 
		BottomLeft.x == CVector.MAX_NEGATIVE.x)then
		return true;
	end
		Size = AbsVec(TopRight - BottomLeft);
		MinN = (Corner1 - BottomLeft) / Size;
	MaxN = (Corner2 - BottomLeft) / Size;
		MinIsOut = not PointWithinLimits(MinN, LimitsOut);
	MaxIsOut = not PointWithinLimits(MaxN, LimitsOut);
		Centering = MinIsOut or MaxIsOut;	
		MinMaxSize = AbsVec(Max - Min);
		OccupiedArea = CVector(MinMaxSize.x / Size.x, MinMaxSize.y / Size.y, 0);
		MaxDim = math.max(OccupiedArea.x, OccupiedArea.y);
		LargeZoom = MaxDim;
	if(MaxDim > ZoomOutStarts)then
		RequiresZoom = true;
		ZoomingOut = true;
		ZoomingIn = false;
				return true;
	elseif(MaxDim < ZoomInStarts and DistanceToCamera > MinZoominDistance)then
		RequiresZoom = true;
		ZoomingOut = false;
		ZoomingIn = true;
				return true;
	elseif(ZoomingOut and MaxDim < ZoomOutStops)then
		RequiresZoom = false;
		ZoomingOut = false;
		ZoomingIn = false;
				return Centering;
	elseif(ZoomingIn and MaxDim > ZoomInStops)then
		RequiresZoom = false;
		ZoomingOut = false;
		ZoomingIn = false;
				return Centering;
	end
	RequiresZoom = false;
	ZoomingOut = false;
	ZoomingIn = false;
		return Centering;
end
function PointWithinLimits(Point, Limits)
	return
	(	Point.x > Limits[1] and Point.x < Limits[3] and
		Point.y > Limits[2] and Point.y < Limits[4]);
end
function CalculatePosition()
		Center = (Min + Max) / 2;
	Center.z = Camera:GetPos().z;
			ZoomChange = MinZoomChange;
	if(LargeZoom > 0)then
		ZoomChange = ZoomChange * (LargeZoom + 1);
	end
		if(ZoomingOut or Centering)then
		Center.z = Center.z + ZoomChange;
	elseif(ZoomingIn)then
		Center.z = Center.z - ZoomChange;
	end
		Center.z = math.max(Center.z, MinZoominDistance);
	return Center;
end
