using UnityEngine;
using System.Collections;

public class KeeperScript : MonoBehaviour {


	GameObject[] bgmObjs;

	float head_timer;
	bool playing_tail;
	public AudioClip head;
	public AudioClip tail;

	AudioSource AS_REF;

	void Awake() {
		DontDestroyOnLoad(transform.gameObject);
	}

	// Use this for initialization
	void Start () {
		head_timer = 0.0f;
		playing_tail = false;
		AS_REF = GetComponent<AudioSource> ();
		bgmObjs = GameObject.FindGameObjectsWithTag ("GameMusic");

		if (bgmObjs.Length == 1 )
		{
			//we good?
			bgmObjs[0].GetComponent<AudioSource>().Play ();
		} 
		else 
		{
			for(int i = 1; i < bgmObjs.Length; i++)
			{
				Destroy(bgmObjs[i]);
			}
			
		}
	}
	
	// Update is called once per frame
	void Update () {

		if (!playing_tail) {
			head_timer += Time.deltaTime;
			if (head_timer >= 13.71f) {
				AS_REF.Stop();
				AS_REF.clip = tail;
				AS_REF.Play();
				playing_tail = true;
			}
		}

	
	}
}
