var ludi_root = {};
var ludi_menu = {};
var ludi_renderer = {};
var ludi_io = {};
var ludi_runner = {};
var ludi_lib = {};
var ludi_game = {};
var ludi_display= {};

ludi_root.debugMode = 2; // 0: no; 1: normal; 2: detail

// available lib actions : interface for mapping hardcoded codes and xml ids
ludi_root.actEnum = {
	MIN: {value: 0},
	LOOK: {value: 0},
	GO: {value: 1},
	EX: {value: 2},
	TAKE: {value: 3},
	DROP: {value: 4},
	TALK: {value: 5},
	BECOME: {value: 6},
	PUT_INTO: {value: 7},
	GIVE: {value: 8},
	TAKE_FROM: {value: 9},
	ASK_ABOUT: {value: 10},
	ASK_FROM: {value: 11},
	SHOW_TO: {value: 12},
	READ: {value: 13},
	TURNON: {value: 14},
	TURNOFF: {value: 15},
	SING: {value: 16},
	WHERE: {value: 17},
	OPEN: {value: 18},
	CLOSE: {value: 19},
	UNLOCK: {value: 20},
	LOCK: {value: 21},
	LOOK_DIR: {value: 22},
	INVESTIGATE: {value: 23},
	DIG_WITH: {value: 24},
	CLIMB: {value: 25},
	SWIM: {value: 26},
	JUMP: {value: 27},
	UNTIE: {value: 28},
	DRINK: {value: 29},
	EAT: {value: 30},
	LISTEN: {value: 31},
	SMELL: {value: 32},
	WAKEUP: {value: 33},
	ATTACK: {value: 34},
	STRIKE: {value: 35},
	STOP: {value: 36},
	BURN: {value: 37},
	THROW: {value: 38},
	PULL: {value: 39},
	RAISE: {value: 40},
	ATTACK_WITH: {value: 41},
	WAIT: {value: 42},
	
	LAST_KERNEL_ACTION: {value: 42}

}; 

ludi_root.caEnum = { 
	ASIS: {value: 0},
	ATT: {value: 1},
	ITEM: {value: 2},
	MSG: {value: 3},
	DESC: {value: 4},
	REFRESH: {value: 5},
	URL: {value: 6},
	GRAPH: {value:7},
	GRAPH_POPUP: {value:8},
	MSG_POPUP: {value:9},
	PRESS_KEY: {value:10},
	SHOW_MENU: {value:11},
	SOUND: {value:12},
	ACTION: {value: 13},
	END_GAME: {value: 14},
	RESTART_GAME: {value: 15},
	DIR: {value: 16},
	SHOW_ECHO: {value: 17},
	PLAY_AUDIO: {value: 18},
	QUOTE_BEGIN: {value: 19},
	QUOTE_CONTINUES: {value: 20}
	
}; 

ludi_root.panel1ViewEnum = {
    GeneralButton: {value: 0},
    Playing: {value: 1},
	Messages: {value: 2},
	Boards: {value: 3},
	Players: {value: 4},
	Games: {value: 5},
	Activity: {value: 6},
	Help: {value: 7},
}

// must be in the same order than ludi_root.panel1ViewEnum
ludi_root.panel1ViewMessages = [
	{text: "GeneralButton"}
	, {text: "PlayingView"}
	, {text: "MsgView"}
	, {text: "BoardView"}
	, {text: "PlayerView"}
	, {text: "GamesView"}
	, {text: "ActivityView"}
	, {text: "HelpView"}
]

// Panel Options
ludi_root.p1Def = {

    GeneralButton: {value: 0},

    Exit: {value: 1},
	DirectAction: {value: 2},	
    AllItems: {value: 3}, // to-do: pending to use
	Here: {value: 4}, // to-do: unify item
	Carried: {value: 5}, // to-do: unify item
	On: {value: 6}, // to-do: unify item
	Marked: {value: 7}, // to-do: unify item
	NotHere: {value: 8}, // to-do: unify item
	
	OfflinePlayers: {value: 9}, 
	OnlinePlayers: {value:10}, 
	GamePlayers: {value:11}, 
	HerePlayers: {value: 12}, 
	//BlackPlayers: {value:13}, 
	//WhitePlayers: {value:14}, 
	
	PublicGames: {value: 13},	
	UserGames: {value: 14},	
	
	GlobalBoard: {value: 15},	
	GameBoard: {value: 16},	
	//LocationBoard: {value: 17},	
	//PrivateBoard: {value: 18},	
	
	AllMessages: {value: 17},	
	GlobalMessages: {value: 18},	
	GameMessages: {value: 19},	
	LocationMessages: {value: 20},	
	PrivateMessages: {value: 21},	
	SystemMessages: {value: 22},	
	
	GameActivity: {value: 23},	
	LocationActivity: {value: 24},
	PrivateActivity: {value: 25},
	
	
}; 

// must be in the same order than ludi_root.p1Def 
ludi_root.panelInfo = [

  	  {text: "GeneralButton", viewType:ludi_root.panel1ViewEnum.GeneralButton.value}
	
  	, {text: "From here", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "exits.png"}
	, {text: "Direct actions", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "direct_actions.jpg"}
	, {text: "All items", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "items.jpg"}
	
	, {text: "Items here", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "items_here.jpg"}
	, {text: "Items carried", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "items_carried.jpg"}
	, {text: "On", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "items_on.jpg"}
	, {text: "Inside marked", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "items.jpg"}
	, {text: "Items not here", viewType:ludi_root.panel1ViewEnum.Playing.value, icon: "items_not_here.jpg"}

	, {text: "Offline Players", viewType:ludi_root.panel1ViewEnum.Players.value, icon: "players.png"} 
	, {text: "Online Players", viewType:ludi_root.panel1ViewEnum.Players.value, icon: "players.png"} 
	, {text: "Game Players", viewType:ludi_root.panel1ViewEnum.Players.value, icon: "players.png"} 
	, {text: "Players Here", viewType:ludi_root.panel1ViewEnum.Players.value, icon: "players.png"} 

	, {text: "Public games", viewType:ludi_root.panel1ViewEnum.Games.value, icon: "open.png"}
	, {text: "User games", viewType:ludi_root.panel1ViewEnum.Games.value, icon: "close.png"}

	, {text: "Global Board", viewType:ludi_root.panel1ViewEnum.Boards.value, icon: "board.jpg"}
	, {text: "Game Board", viewType:ludi_root.panel1ViewEnum.Boards.value, icon: "board_game.jpg"}
	//, {text: "Location Board", viewType:ludi_root.panel1ViewEnum.Boards.value}
	//, {text: "Private Board", viewType:ludi_root.panel1ViewEnum.Boards.value}

	, {text: "All Messages", viewType:ludi_root.panel1ViewEnum.Messages.value}
	, {text: "Global Messages", viewType:ludi_root.panel1ViewEnum.Messages.value}
	, {text: "Game Messages", viewType:ludi_root.panel1ViewEnum.Messages.value}
	, {text: "Location Messages", viewType:ludi_root.panel1ViewEnum.Messages.value}
	, {text: "Private Messages", viewType:ludi_root.panel1ViewEnum.Messages.value}
	, {text: "System Messages", viewType:ludi_root.panel1ViewEnum.Messages.value}

	, {text: "Game Activity", viewType:ludi_root.panel1ViewEnum.Activity.value}
	, {text: "Location Activity", viewType:ludi_root.panel1ViewEnum.Activity.value}
	, {text: "Private Activity", viewType:ludi_root.panel1ViewEnum.Activity.value}

	, {text: "About", viewType:ludi_root.panel1ViewEnum.Help.value}
	, {text: "Instructions", viewType:ludi_root.panel1ViewEnum.Help.value}

];

// Buffer ids
ludi_root.bufferEnum = {
    AllActions: 0,
    LastAction: 1,
	Config: 2,
	Board: 3,
    allMessages: 4, 
	filtredMessages: 5
}; 

// types of requests
ludi_root.requestEnum = {
    game: 0
};

ludi_root.echoSeparator = "+"; 

ludi_root.screenOrientation = {
	Horiz: 0,
	Vert: 1
};

ludi_root.visiblePanels = false;

// default preferences
ludi_root.filterEnum = { 
	MessagesHidden: 0,
	ActivityHidden: 1,
	SpeechOn: 2,
	IncomingLanguages: 3,
	BroadcastFrom: 4,
	ActivityFrom: 5,
	ActivityTo:6
};

ludi_root.visibleMessages = false; 

ludi_root.enumPanels = {
	Environment: 0,
	Panel1: 1,
	Main: 2,
	Messages: 3
	
};

ludi_root.screenType = -1;

ludi_root.panelMapping = [ // quite important!!
	[ "environment", "environment"], // Environment
	[ "subpanel_horiz", "subpanel_vert"],  // Panel1	
	[ "main_horiz", "main_vert"],  // Main
	[ "messages_panel", "messages_panel"]  // Messages
	

];

ludi_display.chatParams = {
	lan:'',
	to:'',
	type:'global',
	text: ''
};

function supports_html5_storage() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
    return false;
  }
}

/*
  auxiliary function to get index in array looking for object attribute
 sample: i= arrayObjectIndexOf(table, "id", "room");
*/
function arrayObjectIndexOf(myArray, property, searchTerm) {
    for(var i = 0, len = myArray.length; i < len; i++) {
        if (myArray[i][property] === searchTerm) return i;
    }
    return -1;
}




ludi_root.getIconHTML = function(icon, displayOption, onclickCall){
	
	if (typeof icon === 'undefined') return "";
	if (icon == "") return "";
	displayOption = displayOption | 0; // 0,1: icon; 2:noicon
	if (displayOption == 2) return "";
	onclickCall = onclickCall | "";
	
	var iconHTML ="";
	var size = (displayOption == 0) ? 26:20;
	iconHTML ="<IMG ";
	if (onclickCall!="") iconHTML += "onclick='" + onclickCall + "' ";
	iconHTML += "src='";
	iconHTML += './client/icons/' + icon;
	iconHTML += "' ";
	iconHTML += "alt='buton'  height='" + size + "' width='" + size + "'>" ;
	return iconHTML;
				
}

ludi_root.cleanComboValues = function(idCombo){
	var combo=document.getElementById(idCombo);
	for(var i=combo.options.length-1;i>=0;i--)
	{
		combo.remove(i);
	}
}

ludi_root.setComboValue = function(idCombo, value){
	var combo=document.getElementById(idCombo);
	combo.value = value;
}


ludi_root.addComboValue = function(idCombo, value){
	var combo=document.getElementById(idCombo);
	var option=document.createElement("option");
	option.text=value;
	option.value=value;
	try{
		combo.add(option,null);
	}
	catch(error){
		combo.add(option);}
}

// trick: only for use during game development
ludi_root.GD_CreateMsg = function   (indexLang, idMsg, txtMsg) {
	
	if (indexLang != ludi_language.lanIndex) return; // do nothing
	
	// console.log("MSG Warning. Language #" + indexLang + " idMsg [" + idMsg + "] -> [" + txtMsg + "]" )
	if (! ludi_language.message_LongId_Exists ("messages."+ idMsg + ".txt")) {
		ludi_language.lanList[indexLang].gameMes["messages."+ idMsg + ".txt"] = {message: txtMsg};

		if (typeof ludi_root.GD_CreateMsg2JSON != "undefined") { // si definimos esta variable, sacamos por consola las líneas a añadir en gameMessagesES.json
			// lines to add in json file:
			var line = "\t\"" + "messages."+ idMsg + ".txt\": {\n" ;
			line += "\t\t\"" + "message\": \"" + txtMsg + "\"\n" ;
			line += "\t},";
			console.log(line);
		}
	} else { 
		if (txtMsg != ludi_language.lanList[indexLang].gameMes["messages."+ idMsg + ".txt"].message) {
			// warning and use the hard-coded versión
			console.log ("Using hard-coded game message: [" + idMsg + "] -> [" + txtMsg + "]");
			ludi_language.lanList[indexLang].gameMes["messages."+ idMsg + ".txt"] = {message: txtMsg};
		}
	}
}

