var w, h, level, bonuses, id;
function editor(i){
id = i;
if(localStorage.getItem("lv" + id) == null){
var wOK = false; var hOK = false;
while (!wOK){
w = prompt("Enter width (blocks) (please enter a valid integer between 2 and 50):", "10");
if ((!isNaN(w)) && w >1 && w<51){wOK = true;}
}
while (!hOK){
h = prompt("Enter height (blocks) (please enter a valid integer between 2 and 25):", "5");
if ((!isNaN(h)) && h>1 && h<26){hOK = true;}
}
level = new Array(h);
bonuses = new Array(h);
for(var i = 0; i < h; i++){
level[i] = new Array(w);
bonuses[i] = new Array(w);
for(var j = 0; j < w; j++){
level[i][j] = 1;
bonuses[i][j] = 0;
}
}
}else{
level = JSON.parse(localStorage.getItem("lv" + i));
bonuses = JSON.parse(localStorage.getItem("lv" + i + "b"));
}
refresh();
}
function refresh(){
var xString = '<select id="x1" onchange="javascript:editorSelectionChanged()">';
for (var x = 0; x < w; x++){
xString += '<option value="' + x + '">' + x + '</option>';
}
xString += '</select>';
var yString = '<select id="y1" onchange="javascript:editorSelectionChanged()">';
for (var y = 0; y < h; y++){
yString += '<option value="' + y + '">' + y + '</option>';
}
yString += '</select>';
var brickString = '<select id="brick"><option value="1">Brick 1</option><option value="2">Brick 2</option><option value="3">Brick 3</option><option value="4">Brick 4</option><option value="5">TNT brick</option></select>';
var bonusString = '<select id="bonus"><option value="0">No bonus</option><option value="1">100 points</option><option value="2">200 points</option><option value="3">500 points</option><option value="4">Bar +</option><option value="5">Bar -</option><option value="6">+1Life</option><option value="7">-1Life</option><option value="8">DoubleScore</option></select>';
var html = '<br><h1>Level editor: ' + id + '</h1><p><button onclick="javascript:editorSave()">Save and exit</button><button onclick="javascript:menu()">Exit without saving</button></p><p>Select blocks from ' + xString + ',' + yString + ' to ' + xString.replace('x1','x2') + ',' + yString.replace('y1','y2') + ' and change to' + brickString + bonusString + '<button onclick="javascript:editorOK()">Go</button></p><table border="1">';
for (var i = -1; i < h; i++){
html += '<tr>';
for (var j = -1; j < w; j++){
var s = "";
if ((i == -1) && (j != -1)){s = j;}
else if ((j == -1) && (i != -1)){s = i;}
else if ((i != -1) && (j != -1)){
var assetId = level[i][j];
if(assetId == 5){assetId = "tnt";}
var b = ""
try{b = document.getElementById('bonus').options[bonuses[i][j]].text;}catch(e){b = "No bonus";}
s = '<img id="' + i + '_' + j + '" src="assets/graphics/sprites/brick/' + assetId + '.png" />' + b;
}
html += '<td>' + s + '</td>';
}
html += '</tr>';
}
html += '</table>';
document.getElementById("content").innerHTML = html;
}
function editorSelectionChanged(){
var x1 = document.getElementById('x1').value;
var x2 = document.getElementById('x2').value;
var y1 = document.getElementById('y1').value;
var y2 = document.getElementById('y2').value;
for(var i = 0; i < h; i++){
for(var j = 0; j < w; j++){
if ((i >= y1) && (i <= y2) && (j >= x1) && (j <= x2)){document.getElementById(i + '_' + j).setAttribute('class', 'editorSelected');}
else {document.getElementById(i + '_' + j).setAttribute('class', '');}
}
}
}
function editorOK(){
var x1 = document.getElementById('x1').value;
var x2 = document.getElementById('x2').value;
var y1 = document.getElementById('y1').value;
var y2 = document.getElementById('y2').value;
var brick = document.getElementById('brick').value;
var bonus = document.getElementById('bonus').value;
for(var i = 0; i < h; i++){
for(var j = 0; j < w; j++){
if ((i >= y1) && (i <= y2) && (j >= x1) && (j <= x2)){
level[i][j] = brick;
bonuses[i][j] = bonus;
}
}
}
refresh();
}
function editorSave(){
localStorage.setItem("lv" + id, JSON.stringify(level));
localStorage.setItem("lv" + id + "b", JSON.stringify(bonuses));
menu();
}