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

};

Main.onLoad = function() {

	plugin = document.getElementById("pluginObjectAudio");
	// 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();
};
window.onShow = function() {
	var nnaviPlugin = document.getElementById('pluginObjectNNavi');
	nnaviPlugin.SetBannerState(1);
	// For volume OSD
	var pluginAPI = new Common.API.Plugin();
	pluginAPI.SetBannerState(1);
	pluginAPI.unregistKey(tvKey.KEY_VOL_UP);
	pluginAPI.unregistKey(tvKey.KEY_VOL_DOWN);
	pluginAPI.unregistKey(tvKey.KEY_MUTE);
}
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();
		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 = "Help.html";
		if (i == 3)
			parent.location = "About.html";
		break;
	case tvKey.KEY_VOL_UP:
		volInc();
		alert(getVolume());
		break;
	case tvKey.KEY_VOL_DOWN:
		alert(plugin.GetVolume());
		volDec();
		alert(getVolume());
		break;
	case tvKey.KEY_MUTE:
		volMute();
		alert(getVolume());
		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--;
	}
	alert("PAGE : "+i);
	$("#Btn" + i).addClass("selected");
}
function volInc() {
	plugin.SetVolumeWithKey(0);
	getVolume();
}

function volDec() {
	plugin.SetVolumeWithKey(1);
	getVolume();
}
function volMute() {
	if (plugin.GetUserMute() == 0) {
		plugin.SetUserMute(1);
		setVolumeZero();
	} else {
		plugin.SetUserMute(0);
		getVolume();
	}
}
function getVolume() {
	alert(plugin.GetVolume());
}
function setVolumeZero() {
}
