#pragma strict

var helperText : GameObject;

var player : GameObject;
var timer : GameObject;

var playerId = 0;
var joystickPlayer : JoystickManager;

var originalAlpha = 0.0f;

var fade = -1;

var endGame = false;

function Start () {
	joystickPlayer = new JoystickManager();
	
	originalAlpha = gameObject.renderer.material.color.a;
}

function Update () {

	if(joystickPlayer.checkButton(playerId) == 10) {
		player.GetComponent(Shoot).playing = true;
		timer.GetComponent(Timer).someoneIsPlaying = true;
		//Destroy(gameObject);
		fade = 0;
		helperText.GetComponent(TextMesh).text = "";
		//Destroy(helperText);
	}
	
	if(fade == 0 && gameObject.renderer.material.color.a > 0) {
		gameObject.renderer.material.color.a -= 0.05f;
	}
	
	if(fade == 1 && gameObject.renderer.material.color.a < originalAlpha) {
		gameObject.renderer.material.color.a += 0.05f;
	}
	
	if(endGame) {
	
		if(Input.GetButtonDown("Submit")) {
		
			Application.LoadLevel("Menu");
		
		}
	
	}
	
	
}

function EndGame() {

	endGame = true;

	player.GetComponent(Shoot).playing = false;

	fade = 1;
	
	var index = 0;
	
	var myScore = player.GetComponent(Shoot).score;
	//var scores : int[] = new int[4];
	
	var countBetter = 0;
	
	for(var p in GameObject.FindGameObjectsWithTag("Player")) {
		if(myScore < p.GetComponent(Shoot).score) {
			countBetter++;
		}
	}
	
	countBetter++;
	
	switch(countBetter) {
	
		case 1:
			helperText.GetComponent(TextMesh).text = "1st!";
			break;
		case 2:
			helperText.GetComponent(TextMesh).text = "2nd";
			break;
		case 3:
			helperText.GetComponent(TextMesh).text = "3rd";
			break;
		case 4:
			helperText.GetComponent(TextMesh).text = "4th";
			break;
	}
	
	

}