index.html  |  resources.html  |  resources.js  |  workspace.html  |  workspace.js  |  reset.html  |  reset.js  |  sql-code.html  |  sql-code.js

var
  mapSize = { x: 15, y: 12 },
  mapDefaultRes = '0012',
  resSizePl = 51,
  savedBuildPos = { x: -1, y: -1 },
  savedResId,

  spanBuildStartPos = new Array,
  spanBuild = { active: false, startPosUnknown: true },
  stealRes = { active: false };

// warning - function is super dirty
function clearMap() {
  with (document.getElementsByTagName('body')[0])
    while (childNodes.length > 3)
      removeChild(lastChild);
}

function initMap() {
  with (document.getElementsByTagName('body')[0])
    for (var y = 0; y != mapSize.y; y++)
      for (var x = 0; x != mapSize.x; x++) {
        appendChild(document.createElement('img'));
        lastChild.setAttribute('src', 'graphics/' + mapDefaultRes + '.png');
        lastChild.setAttribute('alt', '');
        lastChild.setAttribute('style', 'top:' + (5 + y * resSizePl) + 'px; left:' + (5 + x * resSizePl) + 'px;');
        lastChild.setAttribute('onClick', 'evalClick(' + x + ', ' + y + ')');
        lastChild.setAttribute('onMouseover', 'evalMouseOver(' + x + ', ' + y + ')');
        lastChild.setAttribute('title', x + ', ' + y);
      }
}

function evalClick(x, y) {
  var
    frRes = top.frames[1],
    nextResId = (frRes.slResCom == '0012' ? '' : frRes.slResTp) + frRes.slResCom;

  if (stealRes.active) {
    var tmp = document.getElementsByTagName('img')[y * mapSize.x + x].getAttribute('src').match(/\d+/).toString();
    frRes.selectResCom(tmp == '0012' ? tmp : tmp.slice(2));
  
    if ((tmp = tmp.slice(0, 2)) != '00' && tmp != frRes.slResTp)
      frRes.updateResTp(tmp);
  }
  else
    
    if (spanBuild.active)
      if (spanBuild.startPosUnknown) {
        spanBuildStartPos.x = x;
        spanBuildStartPos.y = y;

        with (document.getElementsByTagName('img')[y * mapSize.x + x].style) {
          spanBuildStartPos.xPl = left.match(/\d+/);
          spanBuildStartPos.yPl = top.match(/\d+/);
        }

        with (document.getElementsByTagName('div')[0].style) {
          left   = spanBuildStartPos.xPl - 1 + 'px';
          top    = spanBuildStartPos.yPl - 1 + 'px';
          width  = resSizePl + 1 + 'px';
          height = resSizePl + 1 + 'px';
          backgroundColor = 'white';
        }
        spanBuild.startPosUnknown = false;
      }
      else {
        var mod = { x: x < spanBuildStartPos.x ? 1 : -1, y: y < spanBuildStartPos.y ? 1 : -1 };
        for (0; y != spanBuildStartPos.y + mod.y; y += mod.y)
          for (var tmpX = x; tmpX != spanBuildStartPos.x + mod.x; tmpX += mod.x)
          
            with (document.getElementsByTagName('img')[y * mapSize.x + tmpX])
              setAttribute('src', getAttribute('src').replace(/\d+/, nextResId));
      
        document.getElementsByTagName('div')[0].style.backgroundColor = 'black';
        spanBuild.startPosUnknown = true;
      }
    else {
      var
        clickedImg = document.getElementsByTagName('img')[y * mapSize.x + x],
        currResId = clickedImg.getAttribute('src').match(/\d+/);
    
      if (savedBuildPos.x == x && savedBuildPos.y == y && nextResId == currResId)
        nextResId = savedResId;

      clickedImg.setAttribute('src', clickedImg.getAttribute('src').replace(/\d+/, nextResId));
 
      savedResId = currResId;
      savedBuildPos.x = x;
      savedBuildPos.y = y;
    }
}

function evalMouseOver(x, y) {
  if (!spanBuild.startPosUnknown) {

    var
      runOverPos = new Array,
      selStartPos = new Array,
      selOffset = new Array;

    with (document.getElementsByTagName('img')[y * mapSize.x + x].style) {
      runOverPos.xPl = left.match(/\d+/);
      runOverPos.yPl = top.match(/\d+/);
    }
    
    for (var attr in runOverPos)

      if (runOverPos[attr] - 0 <= spanBuildStartPos[attr]) {
        selStartPos[attr] = runOverPos[attr];
        selOffset[attr] = spanBuildStartPos[attr] - runOverPos[attr] + resSizePl;
      }
      else {
        selStartPos[attr] = spanBuildStartPos[attr];
        selOffset[attr] = runOverPos[attr] - spanBuildStartPos[attr] + resSizePl;
      }

    with (document.getElementsByTagName('div')[0].style) {
      left   = (selStartPos.xPl - 1) + 'px';
      top    = (selStartPos.yPl - 1) + 'px';
      width  = (selOffset.xPl + 1) + 'px';
      height = (selOffset.yPl + 1) + 'px';
      backgroundColor = 'white';
    }
  }
}