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

};

Main.onLoad = function() {
	// Enable key event processing
	this.enableKeys();
	widgetAPI.sendReadyEvent();
	if (sessionStorage.getItem("indice") == null)
		navigation("DOWN");
	else {
		i = sessionStorage.getItem("indice") - 1;
		navigation("DOWN");
	}
};

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");
		break;
	case tvKey.KEY_RIGHT:
		alert("RIGHT");
		break;
	case tvKey.KEY_UP:
		alert("UP");
		navigation("UP");
		break;
	case tvKey.KEY_DOWN:
		alert("DOWN");
		navigation("DOWN");
		break;
	case tvKey.KEY_ENTER:
	case tvKey.KEY_PANEL_ENTER:
		alert("ENTER");
		sessionStorage.setItem("indice", i);
		if (i == 1)
			parent.location = "ChoiceAvatar.html";
		if (i == 2)
			parent.location = "ChoiceAvatar.html";
		break;
	default:
		alert("Unhandled key");
		break;
	}
};
function navigation(dir) {
	$("#Btn" + i).removeClass("selected");
	if (dir == "DOWN") {
		if (i == max)
			i = 1;
		else
			i++;
	}
	if (dir == "UP") {
		if (i == 1)
			i = max;
		else
			i--;
	}
	$("#Btn" + i).addClass("selected");
}
