// -----------------------------------------------------------------------
// TINAMI Comike Map System Ver 2010.06.30
// (c) 2006 TINAMI. All rights reserved.
// map.js: ビットマップの集合を管理 (use Yahoo! Ui Library)
// -----------------------------------------------------------------------
var Map = Class.create();
Map.prototype = {
  initialize: function (comike_no, bitmap_id_base, width, height, parts_width, parts_height)
  {
    // プロパティとメソッドの定義
    this.id = bitmap_id_base;
    this.num_x_parts = parseInt(width / parts_width) + (width % parts_width ? 1 : 0);
    this.num_y_parts = parseInt(height / parts_height) + (height % parts_height ? 1 : 0);
    this.parts_width = parts_width;
    this.parts_height = parts_height;
    this.bitmaps = new Array(this.num_x_parts);
    for (x=0; x<this.num_x_parts; ++x) {
      this.bitmaps[x] = new Array(this.num_y_parts);
    }

    var div = document.createElement("div");
    div.setAttribute("id", bitmap_id_base);
    div.style.position = "absolute";
    div.style.left = "0px";
    div.style.top  = "0px";
    div.style.width = new String(this.num_x_parts * parts_width) + "px";
    div.style.height = new String(this.num_y_parts * parts_height) + "px";

    // ビットマップを作成
    // ビットマップは、images/コミケ番号/bitmap_id_base-分割番号.png の名前で保存されている
    var counter = 0;
    var rest_height = height;
    for (y=0; y<this.num_y_parts; ++y) {
      var rest_width = width;
      for (x=0; x<this.num_x_parts; ++x) {
        var bitmap_id = String(comike_no) + '-' + bitmap_id_base + '-' + String(counter);
        var bitmap_file = '/comike/map/images/' + String(comike_no) + '/' + bitmap_id_base + '-' + String(counter) + '.png';
        var bitmap_width = (rest_width >= parts_width ? parts_width : rest_width);
        var bitmap_height = (rest_height >= parts_height ? parts_height : rest_height);
        this.bitmaps[x][y] = this._createBitmap(bitmap_id, bitmap_file, x * parts_width, y * parts_height, bitmap_width, bitmap_height);
        div.appendChild(this.bitmaps[x][y].div);
        counter++;
        rest_width -= parts_width;
      }
      rest_height -= parts_height;
    }
    this.instance = div;

    // ドラッグ機能を追加
    this.dd = new YAHOO.util.DD(bitmap_id_base);
    this.dd.startDrag = function(x, y) {
      control.doStartDrag(x, y);
    };
    this.dd.onDrag = function(e) {
      control.doDrag(e);
    };
    this.dd.endDrag = function(e) {
      control.doEndDrag(e);
    };
    this.dd.onMouseDown = function(e) {
      control.doMouseDown(e);
    };
  },

  // ビットマップ読み込み
  _createBitmap: function (id, src, x, y, width, height)
  {
    var bitmap = new Object();
    bitmap.id = id;
    bitmap.src = src;
    bitmap.visible = false;

    bitmap.div = document.createElement("div");
    bitmap.div.setAttribute("id", id);
    bitmap.div.style.position = "absolute";
    bitmap.div.style.left = x + "px";
    bitmap.div.style.top  = y + "px";
    bitmap.div.style.backgroundImage = "url(http://img.tinami.com/comike/map/images/bg.png)";
    bitmap.div.style.backgroundRepeat = "repeat";
    bitmap.div.style.width = new String(width) + "px";
    bitmap.div.style.height = new String(height) + "px";
    bitmap.div.style.zIndex = "1";

    return bitmap;
  },

  // トラックバックアイコンを追加（拡大用）
  addTrackbackIcon: function (x, y)
  {
    var div = document.createElement("img");
    div.setAttribute("src", "/comike/map/images/trackback_new.png");
    div.setAttribute("width", "12");
    div.setAttribute("height", "12");
    div.style.position = "absolute";
    div.style.left = x + "px";
    div.style.top  = y + "px";
    div.style.zIndex = "2";

    this.instance.appendChild(div);
  },

  // トラックバックアイコンを追加（縮小用）
  addTrackbackLine: function (x, y, width, height)
  {
    if (navigator.userAgent.indexOf("MSIE") != -1) {
    /* IEだと枠がずれるので何もしない */
      return ;
    }
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.left = x + "px";
    div.style.top  = y + "px";
    div.style.width = new String(width - 2) + "px";
    div.style.height = new String(height - 2) + "px";
    div.style.border = "2px solid #FF0000";
    div.style.zIndex = "2";
    this.instance.appendChild(div);
  },

  // 新規登録サークルにNOW PRINTINGアイコンを追加（拡大用）
  addNewCircleIcon: function (x, y)
  {
    var div = document.createElement("img");
    div.setAttribute("src", "/comike/map/images/default.png");
    div.setAttribute("width", "31");
    div.setAttribute("height", "31");
    div.style.position = "absolute";
    div.style.left = x + "px";
    div.style.top  = y + "px";
    div.style.zIndex = "2";

    this.instance.appendChild(div);
  },

  // 新規登録サークルにNOW PRINTINGアイコンを追加（縮小用）
  addNewCircleLine: function (x, y, width, height)
  {
    var div = document.createElement("img");
    div.setAttribute("src", "/comike/map/images/newcircle.png");
    div.setAttribute("width", new String(width));
    div.setAttribute("height", new String(height));
    div.style.position = "absolute";
    div.style.left = new String(x + 1) + "px";
    div.style.top  = new String(y + 1) + "px";
    div.style.zIndex = "2";
    this.instance.appendChild(div);
  },

  // 指定位置の周囲９個＋左右６個のビットマップだけを読み込む
  showBitmapNear: function(x, y)
  {
    var center_panel_x = parseInt(x / this.parts_width);
    var center_panel_y = parseInt(y / this.parts_height);
//console.debug("showBitmapNear x:%d,y:%d,cx:%d,cy:%d", x, y, center_panel_x, center_panel_y);
    for (panel_y = center_panel_y - 1; panel_y <= center_panel_y + 1; ++panel_y) {
      for (panel_x = center_panel_x - 2; panel_x <= center_panel_x + 2; ++panel_x) {
        if (panel_x >= 0 && panel_x < this.num_x_parts && panel_y >= 0 && panel_y < this.num_y_parts) {
          if (!this.bitmaps[panel_x][panel_y].visible) {
            this.bitmaps[panel_x][panel_y].div.style.backgroundImage = "url(" + this.bitmaps[panel_x][panel_y].src + ")";
            this.bitmaps[panel_x][panel_y].div.style.backgroundRepeat = "no-repeat";
            this.bitmaps[panel_x][panel_y].visible = true;
          }
        }
      }
    }
  },

  // 表示位置を初期値に戻す
  resetPosition: function ()
  {
    this.instance.style.left = "0px";
    this.instance.style.top = "0px";
  },

  // ビットマップをドラッグに合わせて移動する
  move: function (vectorx, vectory)
  {
    // 移動先を再設定
    var pos = this.getPosition();
    pos.x += vectorx;
    pos.y += vectory;
    this.instance.style.left = pos.x + "px";
    this.instance.style.top = pos.y + "px";

    // 現在の座標を返す
    return this.getPosition();
  },

  // ビットマップを指定した位置にする
  setPosition: function(x, y)
  {
    this.resetPosition();
    this.move(x, y);
  },

  // ビットマップを表示
  show: function (map_id)
  {
    $(map_id).appendChild(this.instance);
  },

  // ビットマップを非表示
  hide: function (map_id)
  {
    $(map_id).removeChild(this.instance);
  },

  // 現在位置を返す
  getPosition: function()
  {
    var left = new String(this.instance.style.left);
    var top = new String(this.instance.style.top);
    var pos = new Object();
    pos.x = eval(left.substr(0, left.length - 2));
    pos.y = eval(top.substr(0, top.length - 2));
    return pos;
  }
};

