// SETTINGS
conf_skware_tiles = 5;
conf_skware_width = 300;
conf_skware_tile_width = conf_skware_width / conf_skware_tiles;

// Creates a tile object with some special properties
function Skware_tile(hori_i, vert_i, tile_img, game_obj) {
  
  this.hori_i = hori_i;
  this.vert_i = vert_i;
  
  this.dom = document.createElement("div");
  this.dom.style.position = "absolute";
  this.dom.style.left = conf_skware_tile_width * this.hori_i + "px";
  this.dom.style.top = conf_skware_tile_width * this.vert_i + "px";
  this.dom.style.width = conf_skware_tile_width + "px";
  this.dom.style.height = conf_skware_tile_width + "px";
  this.dom.style.background = "url(" + tile_img + ")";
  this.dom.style.border = "1px outset #000";
  this.dom.style.cursor = "move";
  this.dom.style.color = '#fff';
  
  var unique_name_instead_of_this = this;
  this.dom.onclick = function() {
    game_obj.move(unique_name_instead_of_this);
  }
  
}

// Move a skware tile
Skware_tile.prototype.move_to = function(h,v) {
  this.hori_i = h;
  this.vert_i = v;
  //this.dom.innerHTML = h + ', ' + v;
  this.dom.style.left = conf_skware_tile_width * this.hori_i + "px";
  this.dom.style.top = conf_skware_tile_width * this.vert_i + "px";
}

function Skwarepusher(dom_obj, path_to_images, name_of_images, rm_square_h, rm_square_v)
{

  this.tile_array = new Array(conf_skware_tiles^2)
  
  this.free_tile_h = rm_square_h;
  this.free_tile_v = rm_square_v;
  dom_obj.innerHTML = '';
  
  // Create game inside the given object
  for (i = 0; i < conf_skware_tiles; i++) {
    for (j = 0; j < conf_skware_tiles; j++) {
    
      // CREATE EVERYTHING BUT 1 SKWARE
      this_index = i*conf_skware_tiles+j;
      if ( i != rm_square_v || j != rm_square_h) {
        tile_img = path_to_images + "/" + name_of_images + "_" + i + "_" + j + ".jpeg";
        this.tile_array[this_index] = new Skware_tile(j,i,tile_img,this);
        dom_obj.appendChild(this.tile_array[this_index].dom);
      } else {
        this.tile_array[this_index] = '';
      }
    
    }
  }

}

Skwarepusher.prototype.move = function(tile_obj) {

  current_array_index = (tile_obj.vert_i) * conf_skware_tiles + tile_obj.hori_i;
  
  // Move horizontal
  if ( this.free_tile_v == tile_obj.vert_i ) {
  
  
    // If this tile isn't right beside the free tile then move the tile left or right
    if (Math.abs(this.free_tile_h-tile_obj.hori_i) > 1) {
      if (this.free_tile_h > tile_obj.hori_i) {
        this.move(this.tile_array[current_array_index+1]);
      } else {
        this.move(this.tile_array[current_array_index-1]);
      }
    }
  
    x = tile_obj.hori_i;
    tile_obj.move_to(this.free_tile_h,this.free_tile_v);
    this.free_tile_h = x;
    
    // Swap places in array
    this.tile_array[tile_obj.vert_i * conf_skware_tiles + tile_obj.hori_i] = tile_obj;
    //tile_obj.dom.innerHTML+='<br />' + (tile_obj.vert_i * conf_skware_tiles + tile_obj.hori_i);
  
  }
  // Move vertical
  if ( this.free_tile_h == tile_obj.hori_i) {
    
    // If this tile isn't right beside the free tile then move the tile left or right
    if (Math.abs(this.free_tile_v-tile_obj.vert_i) > 1) {
      if (this.free_tile_v > tile_obj.vert_i) {
        this.move(this.tile_array[current_array_index+conf_skware_tiles]);
      } else {
        this.move(this.tile_array[current_array_index-conf_skware_tiles]);
      }
    }
    
    x = tile_obj.vert_i;
    tile_obj.move_to(this.free_tile_h,this.free_tile_v);
    this.free_tile_v = x;
    // Swap places in array
    this.tile_array[tile_obj.vert_i * conf_skware_tiles + tile_obj.hori_i] = tile_obj;
    //tile_obj.dom.innerHTML+='<br />' + (tile_obj.vert_i * conf_skware_tiles + tile_obj.hori_i);
  }
  
}

Skwarepusher.prototype.randomize = function() {

  // 100 Steps that fuck things up
  for (q = 0; q < 100; q++) {
  
    // Generate array of possible steps
    var step = new Array();
    
    //Right tile may be moved
    if (this.free_tile_h+1 < conf_skware_tiles) {
      step[step.length] = new Array(this.free_tile_h+1,this.free_tile_v);
    }
    //Left tile may be moved
    if (this.free_tile_h > 0) {
      step[step.length] = new Array(this.free_tile_h-1,this.free_tile_v);
    }
    //Lower tile may be moved
    if (this.free_tile_v+1 < conf_skware_tiles) {
      step[step.length] = new Array(this.free_tile_h,this.free_tile_v+1);
    }
    //Upper tile may be moved
    if (this.free_tile_v > 0) {
      step[step.length] = new Array(this.free_tile_h,this.free_tile_v-1);
    }
  
    // Pick one of the above steps randomly
    i_pick = Math.round((step.length-1) * Math.random());
    this.move(this.tile_array[step[i_pick][1]*conf_skware_tiles + step[i_pick][0]]);
  
  }

}
