using UnityEngine;
using System.Collections;

public class MenuElem : MonoBehaviour
{
    public string EventName;

    void Start()
    {
        Messenger.AddListener("startgame", gamestart);
        Messenger.AddListener("openstore", openstore);
        if (!Globals.IsGamePaused) gamestart();
    }

    void gamestart()
    {
        done = true;
        Messenger.RemoveListener("startgame", gamestart);
    }

    void openstore()
    {
        //done = true;
        Messenger.RemoveListener("startgame", gamestart);
        Messenger.RemoveListener("openstore", openstore);
        Application.LoadLevel("Store");
    }

    bool done;
    void Update()
    {
        if (done)
        {
            left = true;
            transform.parent.localScale = new Vector3(transform.parent.localScale.x, Mathf.Max(0, transform.parent.localScale.y - Time.deltaTime * 10), transform.parent.localScale.z);
            if (transform.parent.localScale.y < 0.1f)
            {
                Destroy(this.transform.parent.gameObject);
            }

            return;
        }
        if (left)
            transform.parent.localScale = new Vector3(transform.parent.localScale.x, Mathf.Max(1, transform.parent.localScale.y - Time.deltaTime * 10), transform.parent.localScale.z);
        else
            if (Input.GetKey(KeyCode.Mouse0) && transform.parent.localScale.y > 1)
            {
                Messenger.Broadcast(EventName);
            }

    }

    void OnMouseOver()
    {
        if (done) return;
        left = false;
        transform.parent.localScale = new Vector3(transform.parent.localScale.x, Mathf.Min(4, transform.parent.localScale.y + Time.deltaTime * 10), transform.parent.localScale.z);
    }

    bool left = false;
    void OnMouseExit()
    {
        left = true;
    }
}
