using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ShowHideButton : MonoBehaviour {

    public Image m_image = null;
    public float m_duration = 0.25f;
    private Timer m_showTimer = new Timer( );
    private Timer m_hideTimer = new Timer( );

    public void hide( ) {

        if ( m_showTimer.isRunning( ) ) {

            m_showTimer.stop( );
            m_hideTimer.start( m_duration );
            m_image.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f );

        } else {

            if ( m_image.color.a == 1.0f ) {
                m_hideTimer.start( m_duration );
            }
        }
    }

    public void show( ) {

        if ( m_hideTimer.isRunning( ) ) {

            m_hideTimer.stop( );
            m_showTimer.start( m_duration );
            m_image.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f );

        } else {

            if ( m_image.color.a == 0.0f ) {
                m_showTimer.start( m_duration );
            }
        }
    }

	void Update( ) {

        if ( m_showTimer.isRunning( ) ) {
            m_showTimer.step( );
            m_image.color = new Color( 1.0f, 1.0f, 1.0f, m_showTimer.getPercentage( ) );
        } else if ( m_showTimer.isEndedOnce( ) ) {
            m_image.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f );
        }

        if ( m_hideTimer.isRunning( ) ) {
            m_hideTimer.step( );
            m_image.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f - m_hideTimer.getPercentage( ) );
        } else if ( m_hideTimer.isEndedOnce( ) ) {
            m_image.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f );
        }
	}
}
