using UnityEngine;
using System.Collections;
using System;

public class ButtonLoadScene : MonoBehaviour 
{
	public tk2dSprite faderSprite;
	public string sceneName;

	void Start () 
	{
		GetComponent<GenericButton>().releaseHandler += new ReleaseEventHandler(LoadScene);
	}

	void LoadScene(object sender, EventArgs e) 
	{
		StartCoroutine(FadeTransition());
	}

	IEnumerator FadeTransition()
	{
		faderSprite.gameObject.SetActive(true);
		float fadeTime = 0.5f;
		float lastTime = Time.time;
		
		while (Time.time - lastTime < fadeTime)
		{
			faderSprite.color = new Color(1, 1, 1, (Time.time - lastTime) / fadeTime);
			yield return new WaitForEndOfFrame();
		}
		
		faderSprite.gameObject.SetActive(false);
		Application.LoadLevel(sceneName);
	}

	void Update () 
	{
	}
}
