var widgetAPI = new Common.API.Widget();
var tvKey = new Common.API.TVKeyValue();
var i = 0;
var max = 5;
var Main = {

};

Main.onLoad = function() {
	// Enable key event processing
	this.enableKeys();
	widgetAPI.sendReadyEvent();
	mySound = document.getElementById("audio1");
	mySound.setAttribute("src", "Card.mp3");
	mySound.play();
	setInterval(function() {
		mySound.play();
	}, "1000");
	if (sessionStorage.getItem("indice") == null)
		navigation("RIGHT");
	else {
		i = sessionStorage.getItem("indice") - 1;
		navigation("RIGHT");
	}
};

Main.onUnload = function() {

};

Main.enableKeys = function() {
	document.getElementById("anchor").focus();
};

Main.keyDown = function() {
	var keyCode = event.keyCode;
	alert("Key pressed: " + keyCode);

	switch (keyCode) {
	case tvKey.KEY_RETURN:
	case tvKey.KEY_PANEL_RETURN:
		alert("RETURN");
		// widgetAPI.sendReturnEvent();
		widgetAPI.blockNavigation(event);
		parent.location = "Menu.html";
		break;
	case tvKey.KEY_LEFT:
		alert("LEFT");
		navigation("LEFT");

		break;
	case tvKey.KEY_RIGHT:
		alert("RIGHT");
		navigation("RIGHT");
		break;
	case tvKey.KEY_UP:
		alert("UP");

		break;
	case tvKey.KEY_DOWN:
		alert("DOWN");

		break;
	case tvKey.KEY_ENTER:
	case tvKey.KEY_PANEL_ENTER:
		alert("ENTER");
		sessionStorage.setItem("indice", i);
		sessionStorage.setItem("avatar", i);
		parent.location = "Game.html";
		
		break;
	case tvKey.KEY_PAUSE:
		alert("PAUSE");
		var myVideo = document.getElementById("audio1");
		myVideo.pause();
	case tvKey.KEY_STOP:
		alert("PAUSE");
		var myVideo = document.getElementById("audio1");
		myVideo.stop();
		break;
	case tvKey.KEY_PLAY:
		alert("PLAY");
		// Sound Play
		mySound = document.getElementById("audio1");
		mySound.setAttribute("src", "Card.mp3");
		mySound.play();
		setInterval(function() {
			mySound.play();
		}, "1000");
	default:
		alert("Unhandled key");
		break;
	}
};
function navigation(dir) {
	$("#btn" + i).removeClass("selected");
	if (dir == "RIGHT") {
		if (i == max)
			i = 1;
		else
			i++;
	}
	if (dir == "LEFT") {
		if (i == 1)
			i = max;
		else
			i--;
	}
	$("#btn" + i).addClass("selected");
}
