using UnityEngine;
using System.Collections;


public class MainMenuManager : MonoBehaviour {

	private int m_iSelector = 0;
	private int m_iItemNbr = 2;
	public GUITexture m_PlayhightLight, m_CreditHightlight, m_CreditPanel;
	private bool m_bShowCredits = false;
	public AudioSource m_buttonClickSound;
	public Transform m_SplashScreen;
	public Transform m_Title;

	bool m_bHowToPlayScreenActivated = false;

	// Use this for initialization
	void Start () 
	{
		m_CreditHightlight.enabled = m_CreditPanel.enabled = false;
			m_SplashScreen.gameObject.SetActive(false);
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(!m_bHowToPlayScreenActivated)
		{
			if( !m_bShowCredits )
				UpdateSelectorPosition();
			else
			{
				if( Input.anyKeyDown || Input.GetButtonDown("Back") )
				{
					m_CreditPanel.enabled = false;
					m_PlayhightLight.enabled =  m_CreditHightlight.enabled = true;
					m_iSelector = 0;
					m_bShowCredits = false;
				}
			}
		}
		else
		{

			if(Input.anyKeyDown || Input.GetButtonDown("Select") || Input.GetButtonDown("Start"))
			{
				Application.LoadLevel(1);
			}
		}
	}


	void UpdateSelectorPosition()
	{
		if( Inputs.m_bUpJustPressed )
		{
			m_buttonClickSound.Play();			
			m_iSelector++;
		}
		else if( Inputs.m_bDownJustPressed )
		{
			m_buttonClickSound.Play();			
			m_iSelector--;
		}
		if( m_iSelector > m_iItemNbr-1 )
		{
			m_iSelector = 0;
		}
		else if( m_iSelector < 0 )
		{
			m_iSelector = m_iItemNbr-1;

		}

		if( m_iSelector == 0 )
		{
			m_PlayhightLight.enabled = true;
			m_CreditHightlight.enabled = false;
		}
		else if(  m_iSelector == 1 )
		{
			m_PlayhightLight.enabled = false;
			m_CreditHightlight.enabled = true;
		}

		if( Input.GetButtonDown("Select") )
		{
			m_buttonClickSound.Play();
			if( m_iSelector == 0 )
			{
				m_Title.gameObject.SetActive(false);
				m_bHowToPlayScreenActivated = true;
				m_SplashScreen.gameObject.SetActive(true);
				m_PlayhightLight.enabled = false;
				m_CreditHightlight.enabled = false;
				m_CreditPanel.enabled = false;
				//display screen
			}
			else if(  m_iSelector == 1 )
			{
				//show credits
				m_PlayhightLight.enabled = false;
				m_CreditHightlight.enabled = false;
				m_CreditPanel.enabled = true;
				m_bShowCredits = true;
			}
		}
	}
}
