//This script is used to manage cameras, note that this class must be attached to an object somewhere for the FixedCameraZone
//script to work and the main camera MUST be specified, or the wrong audio listener will be enabled.

//The main camera in the game
var mainCamera:Camera;

static var instance:CameraManager;
function Awake()
{
	// Highlander logic
	if(instance != null)
		Debug.Log("Error:  Already an instance of a camera manager.  Should only be one", this);
	instance = this;
}

private var m_activeCamera:Camera;

function Start()
{
	if(mainCamera == null)
	{
		if(Camera.main == null)
			Debug.LogError("Error: Main camera not specified and Camera.main is not valid!");
		else
			mainCamera = Camera.main;
	}

	listeners = FindObjectsOfType(AudioListener);
	for(var listener:AudioListener in listeners)
		listener.enabled = false;

	m_activeCamera = mainCamera;
	if(m_activeCamera == null)
	{
		Debug.LogWarning("No default camera found in scene!");
	}
	else
	{
		var al:AudioListener = m_activeCamera.GetComponent(AudioListener);
		if(al != null)
				al.enabled = true;
		
		m_activeCamera.enabled = true;
	}
}

function GetActiveCamera() : Camera
{
	return m_activeCamera;
}

function SetActiveCamera(cam:Camera)
{
	if(m_activeCamera)
	{
		var al:AudioListener = m_activeCamera.GetComponent(AudioListener);
		if(al != null)
			al.enabled = false;
			
		m_activeCamera.enabled = false;
	}
	
	m_activeCamera = cam;
	
	var newAL:AudioListener = m_activeCamera.GetComponent(AudioListener);
	if(newAL != null)
		newAL.enabled = true;
	m_activeCamera.enabled = true;
}

function RevertToMainCamera()
{
	SetActiveCamera(mainCamera);
}