
//Внимание! Изменение кода в игре, может вызвать серьезные ошибки в игре! Вы редактируете код игры на свой страх и риск!
//							Part of Promised engine version 0.1.6 "Neo"
var S;
var CookiesExpires = 5;
var game = {
	
	SavesInfo : {
		
		
		
	},
	
	Initialization : function() { //				This function will be called first 
		
		game.createElement("article", "allGame", document.body, "allGameContent");
		game.createElement("div", "allTextGame", game.allGameContent, "allText");
		game.createElement("div", "allButtonsGame", game.allGameContent, "contentButtons");
		game.createElement("div", "guessInputOutGame", game.allGameContent, "contentInputOut");
		game.createElement("input", "guessInputGame", game.contentInputOut, "contentInput");
		game.createElement("nav", "allMenuGame", game.allGameContent, "contentMenu");
		
		game.contentInput.setAttribute("type", "text");
		game.contentInput.setAttribute("spellCheck", "false");
		
		game.AddButton("Новая игра", "game.StartNewGameAnswer();");
		
		game.onGuessEnter();
		
	},
	
	LoadGame : function() {
		
		var Get_S = GetCookie("GSav_Saved");
		var Get_Text = GetCookie("GSav_Text");
		var Get_Buttons = GetCookie("GSav_Buttons");
		
		if(typeof Get_S == 'String' && typeof Get_Text == 'String' && typeof Get_Buttons == 'String') {
			
			S = Deserialize(Get_S);
			game.allText.innerHTML = unescape(Get_Text);
			game.contentButtons.innerHTML = unescape(Get_Buttons);
			return true;
			
		} else {
			
			return false;
			
		}
		
	},

	SaveGame : function() {
		
		now = new Date();
		SetCookie("GSav_Saved", Serialize(S), now.getFullYear + CookiesExpires, now.getFullMouth, now.getFullDay);
		SetCookie("GSav_Text", escape(game.allText.innerHTML), now.getFullYear + CookiesExpires, now.getFullMouth, now.getFullDay);
		SetCookie("GSav_Buttons", escape(game.contentButtons.innerHTML), now.getFullYear + CookiesExpires, now.getFullMouth, now.getFullDay);
		
	},
	
	StartNewGame : function(){
		
		S = {};
		game.allText.innerHTML = "";
		game.contentButtons.innerHTML = "";
		game.funcs.setPlayerLoc(game.startGame);
		setTimeout(function(){game.EventChecker();},1);
		
	},
	
	StartNewGameAnswer : function()
	{
		
		var yesOrNot = confirm("Вы уверены, что хотите начать новую игру? Весь ваш прогресс будет сброшен.");
		if (yesOrNot === true && game.funcs.easterEggs.startNewGame === true) {
			
			game.funcs.easterEggs.startNewGame = false;
			yesOrNot = confirm("Нет серьезно, все ваши настройки будут сброшены, вы действительно уверены?");
			
			if (yesOrNot === true) {
				
				yesOrNot = confirm("Ну раз вы на все сто процентов уверены, то можете начинать новую игру.");
				
				if (yesOrNot === true) {
					
					game.StartNewGame();
					
				}
				
			}
			
		} else if (yesOrNot === true && game.funcs.easterEggs.startNewGame === false) {
			
			game.StartNewGame();
			
		}
		
	},
	
	onGuessEnter : function(){
		
		this.contentInput.onkeydown = function(e){
			
			if (e.keyCode === 13) {
				
				var guessInput = game.contentInput;
				var guessInputValue = guessInput.value.toLowerCase();
				
				game.guessPlayer(guessInputValue);
				
				guessInput.value = "";
				
			}
			
		};
		
	},
	
	allLocs : {},
	
	AddLoc : function(nameLoc, nameLocRu, textLoc, paths, canBack) {
		
		if (nameLoc === undefined || textLoc === undefined) {return undefined}
		
		this.allLocs[nameLoc] = {};
		var endText = textLoc;
		var replaceT = {
			
			textLoc : function(nameObj, params, funcs) {
				
				if (nameObj) {
					
					endText = nameObj.replace(params, funcs);
					
				} else {
					
					endText = endText.replace(params, funcs);
					
				}
				
			}
			
		};
		
		/*textLoc = textLoc.replace(/\n/ig, "<br><br>");
		textLoc = textLoc.replace(/\.,\./ig, ".<br><br>");
		textLoc = textLoc.replace(/\/\.<br><br>/ig, ".");*/
		
		replaceT.textLoc("", /\n/ig, "<br><br>");
		replaceT.textLoc("", /''([\s\S]*?)''/ig, function(str, f1){
			
			return '<span class="quotesText">'+f1+'</span>';
			
		});
		
		this.allLocs[nameLoc].text = endText;
		this.allLocs[nameLoc].paths = null;
		this.allLocs[nameLoc].canBack = null;
		this.allLocs[nameLoc].nameLoc = nameLoc;
		this.allLocs[nameLoc].nameLocRu = nameLocRu;
		
		if (paths && paths !== "" && paths != undefined && typeof paths == "string") {
			
			this.allPaths.push(nameLoc+"[!$#|||]"+paths+"[!$#|||]go");
			
		}
		
		if (canBack) {
			
			this.allPaths.push(nameLoc+"[!$#|||]"+canBack+"[!$#|||]back");
			
		}
		
		//return this.allLocs[nameLoc];
		
	},
	
	startGame : "startGame",
	engineType : "loc",
	
	allPaths : [],
	
	allPathsStartMake : function() {
		
		for (var i = 0; i < this.allPaths.length; i++) {
			
			var locPath = this.allPaths[i];
			var locName, locParams, locGoBack;
			locPath.replace(/([\s\S]*?)\[!\$#\|\|\|]([\s\S]*?)\[!\$#\|\|\|]([\s\S]*)/ig, function(str, f1, f2, f3){
				
				locName = f1;
				locParams = f2;
				locGoBack = f3;
				
			});
			
			if (locGoBack === "go") {
				
				if (this.allLocs[locName].paths === null) {
					
					this.allLocs[locName].paths = [];
					
				}
				
				
				if (locParams.search(/\//i) >= 0) {
					
					locParams.replace(/([\S]*?)(?:\/)/ig, function(str, f1, length){
						
						
						game.allLocs[locName].paths.push(game.allLocs[f1]);
						
					});
					
				} else if (locParams.search(/\//i) == -1) {
					
					this.allLocs[locName].paths.push(this.allLocs[locParams]);
					
				}
			
			} else if (locGoBack === "back") {
				
				this.allLocs[locName].canBack = this.allLocs[locParams];
				
			}
			
		}
		
	},
	
	playerLoc : undefined,
	
	funcs : {
		
		easterEggs : {
			
			startNewGame : true
			
		},
		
		getEngineType : function(whatNeed) {
			
			if (whatNeed === undefined) {
				
				return game.engineType;
				
			} else if (whatNeed != "" && whatNeed === game.engineType) {
				
				return true;
				
			} else if (whatNeed != "" && whatNeed !== game.engineType) {
				
				return false;
				
			}
			
			return false;
			
		},
		
		haveLocPath : function(whatLoc, namePath) {
			
			for (var i = 0; i < whatLoc.paths.length; i++) {
				
				if (whatLoc[i].nameLoc === namePath) {
					
					return true;
					
				}
				
			}
			
			return false;
			
		},
		
		clearButtonsPath : function() {
			
			game.contentButtons.innerHTML = "";
			
		},
		
		setNewText : function(newText) {
			
			game.allText.innerHTML = newText;
			
		},
		
		checkLoc : function(nameLoc) {
			var locHere = false;
			var allLocs = game.allLocs;
			for (var loc in allLocs) {
				
				if (allLocs[loc].nameLoc === nameLoc) {
					
					locHere = true;
					break;
					
				}
				
			}
			
			return locHere;
			
		},
		
		getLoc : function(nameLoc) {
			
			if (this.checkLoc(nameLoc) == false) {return undefined;}
			
			var locHere = null;
			var allLocs = game.allLocs;
			for (var loc in allLocs) {
				
				if (allLocs[loc].nameLoc === nameLoc) {
					
					locHere = allLocs[loc];
					break;
					
				}
				
			}
			
			return locHere;
			
		},
		
		getPlayerLoc : function() {
			
			return this.getLoc(game.playerLoc);
			
		},
		
		setArrayPaths : function(objLoc, whatDo) {
			
			var arrayP = objLoc.paths;
			var arrayBack = objLoc.canBack;
			
			if (whatDo === "paths" && arrayP != null) {
				
				for (var i = 0; i < arrayP.length; i++) {
					
					if (i+1 === arrayP.length && i != 0) {
						
						this.addButtonPath(" и ", "textS");
						this.addButtonPath(arrayP[i].nameLocRu, "textEnd");
						break;
						
					} else if (arrayP.length === 1) {
						
						this.addButtonPath(arrayP[i].nameLocRu, "textEnd");
						break;
						
					}
					
					this.addButtonPath(arrayP[i].nameLocRu);
					
				}
				
			} else if (whatDo === "back" && arrayBack != null) {
				
				this.addButtonPath(arrayBack, "textEnd");
				
			}
			
		},
		
		setPlayerLoc : function(whereSet) {
			
			if (this.checkLoc(whereSet) == false) {
				
				alert("Name location: "+whereSet+". Dont founded!");
				return false;
				
			}
			
			var wherePlayer = this.getLoc(whereSet);
			game.funcs.setNewText(wherePlayer.text);
			
			var locPaths = wherePlayer.paths;
			var locCanBack = wherePlayer.canBack;
			this.clearButtonsPath();
			
			if (locPaths != null) {
				
				if (locPaths.length === 1) {
					
					this.addButtonPath("Возможный путь: ", "textS");
					
				} else if (locPaths.length > 1) {
					
					this.addButtonPath("Возможные пути: ", "textS");
					
				}
				
				this.setArrayPaths(wherePlayer, "paths");
				
			}
			
			if (locCanBack != null) {
				
				if (locPaths != null) {
					
					this.addButtonPath("  ", "nothing");
					
				}
				
				this.addButtonPath("Вы можете вернуться на: "+locCanBack.nameLocRu, "textS");
				
			}
			
			game.playerLoc = whereSet;
			
			return whereSet;
			
		},
		
		addButtonPath : function(pathName, whatItIs) {
			
			var pathT = document.createElement("p");
			var func = function(text) {
				
				var spaceText = document.createTextNode(text);
				pathT.appendChild(spaceText);
				
			};
			var textPath = document.createTextNode(pathName);
			var pathOut = document.createElement("div");
			pathT.className = "pathGame";
			pathOut.className = "divPathGameOuterGame";
			
			pathT.appendChild(textPath);
			
			if (whatItIs === "array" || whatItIs === undefined) {
				
				func(",  ");
				
				
			} else if (whatItIs === "textS") {
				
				func("  ");
				
			} else if (whatItIs === "textEnd") {
				
				func(".");
				
			} else if (whatItIs === "nothing") {
				
				func("");
				
			}
			
			pathOut.appendChild(pathT);
			game.contentButtons.appendChild(pathOut);
			
			
		},
		
		getLocPaths : function(locName) {
			
			if (locName == undefined) {
				
				var loc = this.getPlayerLoc();
				return loc.paths;
				
			} else {
				
				var loc = this.getLoc(locName);
				return loc.paths;
				
			}
			
		},
		
		getLocCanBack : function(locName) {
			
			if (locName == undefined) {
				
				var loc = this.getPlayerLoc();
				return loc.canBack;
				
			} else {
				
				var loc = this.getLoc(locName);
				return loc.canBack;
				
			}
			
		}
		
	},
	
	createElement : function(name, className, parentName, createHere) {
		
		if (name) {
			
			var elem = document.createElement(name);
			
		} else {
			
			var elem = document.createElement("div");
			
		}
		
		if (className) {
			
			elem.className = className;
			
		}
		
		if (parentName) {
			
			parentName.appendChild(elem);
			
		}
		
		if (createHere && createHere !== "") {
			
			game[createHere] = elem;
			
		}
		
		return elem;
		
	},
	
	AddButton : function(textButton, code) {
		
		var newText = document.createElement("p");
		var divNewText = document.createElement("div");
		divNewText.className = "divButtonOuter";
		
		if (code && Array.isArray(code) && code[0] && code[1] && typeof code[0] === "string" && typeof code[1] === "string") {
			
			newText.setAttribute(code[0], code[1]);
			
		} else if (code && typeof code === "string") {
			
			newText.setAttribute("onclick", code);
			
		}
		
		if (textButton) {
			
			var newTextNode = document.createTextNode(textButton);
			newText.appendChild(newTextNode);
			
		}
		
		divNewText.className = "divButtonOuterMenu";
		newText.className = "buttonMenu";
		divNewText.appendChild(newText);
		this.contentMenu.appendChild(divNewText);
		newText.addEventListener("selectstart", function() {
			
			return false;
			
		});
		
	},
	
};


game.Initialization();

