// -----------------------------------------------------------------------
// TINAMI Comike Map System Ver 2006.10.25
// (c) 2006 TINAMI. All rights reserved.
// mapcollection.js: ビットマップの集合をまとめて管理する
// -----------------------------------------------------------------------
var MapCollection = Class.create();
MapCollection.prototype = {
  initialize: function ()
  {
    this.loaded_map = $H({});   // 読み込んだビットマップを保存
    this.currentBitmapId = '';  // 現在表示しているビットマップのID
    this.prefixImageScale = ['',  'A', 'B'];  // 拡大・縮小によるビットマップID

    // mapinfoは/comike/map/getmapinfo で設定された外部変数をそのまま使用する
    this.mapinfo = _mapinfo;

    // エリア毎の初期座標情報
    this.locateinfo = _locateinfo;
  },

  // 指定された日付＋ホールのマップを読み込む
  // day: 日付, area: 1-東1-3, 2-東4-6, 3-西1, 4-西2, scale: 1:拡大, 2:縮小
  load: function (map_id, comike_no, day, area, scale, needUnload)
  {
    var map_base = 'D' + String(day) + this.prefixImageScale[scale] + String(area); // マップファイル名
    var id = 'C' + String(comike_no) + map_base; // ビットマップのID

    // まだ読み込んでいない場合、動的に読み込む
    if (!this.loaded_map.get(id)) {
//      var inf = this.mapinfo.get(map_base).toObject();
      var inf = this.mapinfo[map_base];
      this.loaded_map.set(id, new Map(comike_no, map_base, inf.width, inf.height, inf.parts_width, inf.parts_height));
      this.addTrackbackIcon(id, comike_no, day, area, scale);
      this.addNewCircleIcon(id, day, area, scale);
    }

    // 既に画像を読み込んでいる場合、現在のビットマップを取り外す
    if (needUnload) {
      this.loaded_map.get(this.currentBitmapId).hide(map_id);
    }

    // 現在のビットマップを設定
    this.currentBitmapId = id;

    // ビットマップを表示
    this.loaded_map.get(id).show(map_id);

    // 位置情報をリセット
    this.loaded_map.get(id).resetPosition();

    return this.loaded_map.get(id);
  },

  // 現在アクティブなマップを返す
  getCurrentMap: function ()
  {
    return this.loaded_map.get(this.currentBitmapId);
  },

  // トラックバックアイコンを追加する
  addTrackbackIcon: function(id, comike_no, day, area, scale)
  {
    this.AjaxDone = false;

    var url = '/comike/map/tbiconjson.php';
    var param = String().concat('day=', day, '&area=', area, '&comike_no=', comike_no);
    var myAjax = new Ajax.Request( url, {
      method: 'get', parameters: param, onComplete: function(Request) {
        try {
          var xmlDoc = Request.responseXML;
          var responseText = xmlDoc.documentElement.firstChild.nodeValue;
          var ret = eval('(' + responseText + ')');
          if (ret.found) {
            for (i=0; i<ret.trackback.length; ++i) {
              var tb = ret.trackback[i];
              var map = control.mapCollection.loaded_map.get(id);
              if (scale == 1) {
                map.addTrackbackIcon(parseInt(tb.left) + 18, parseInt(tb.top) + 18);
              } else {
                map.addTrackbackLine(tb.s_left, tb.s_top, tb.s_width, tb.s_height);
              }
            }
          }
        } catch(ex) {
         _MapInternalErrorMsg("データ解析時に異常が発生しました", ex, Request.responseText, 'addTrackbackIcon');
        }
      }
    });
  },

  // 新規追加サークルを追加する
  addNewCircleIcon: function(id, day, area, scale)
  {
    this.AjaxDone = false;

    var url = '/comike/map/api/newcircle';
    var param = String().concat('day=', day, '&area=', area, '&format=xml');
    var myAjax = new Ajax.Request( url, {
      method: 'get', parameters: param, onComplete: function(Request) {
        try {
          var xmlDoc = Request.responseXML;
          var responseText = xmlDoc.documentElement.firstChild.nodeValue;
          var ret = eval('(' + responseText + ')');
          if (ret.found) {
            var map = control.mapCollection.loaded_map.get(id);
            for (i=0; i<ret.circle.length; ++i) {
              var circle = ret.circle[i];
              if (scale == 1) {
                map.addNewCircleIcon(parseInt(circle.left), parseInt(circle.top));
              } else {
                map.addNewCircleLine(parseInt(circle.s_left), parseInt(circle.s_top), parseInt(circle.s_width), parseInt(circle.s_height));
              }
            }
          }
        } catch(ex) {
         _MapInternalErrorMsg("データ解析時に異常が発生しました", ex, Request.responseText, 'addTrackbackIcon');
        }
      }
    });
  }
};


