using UnityEngine;
using System.Collections;

public class Balken : MonoBehaviour {



	float maxDistance = 135;
	public SpriteRenderer balkenMiddle; 

	public SpriteRenderer balken_left; 
	public SpriteRenderer balken_right; 
	// Use this for initialization

	public float x = 0.5f;
	void Start () {
		setProgress(0);
	}
	
	// Update is called once per frame
	void Update () {
//		setProgress(x);
	}

	public void setProgress(float p){ //p between 1 and -1

//		p = (p + 1) / 2;
		p = Mathf.Clamp (p, 0, 1);

		float distance = (1f - p) * maxDistance;
		balkenMiddle.transform.localPosition = new Vector3 (-distance, 0, balkenMiddle.transform.localPosition.z);

		balkenMiddle.transform.localScale = new Vector3 (p, 1, 1);

		balken_left.enabled = p > 0;
		balken_right.enabled = p >= 1;

		if (p >= 1) {
			StartCoroutine (end ());
		}
	}

	public IEnumerator end(){
		foreach (DragDrop d in GameObject.FindObjectsOfType<DragDrop>())
			d.enabled = false;
		yield return new WaitForSeconds (2.0f);
		Application.LoadLevel (Application.loadedLevel + 1);

	}

}
