var xmlfile = "data.xml";

///
//マーカーの設定・表示
//

///
//実績データのリスト表示の設定・表示
//
function mw_showData( map, i_point, i_zoom, a_zoom, point, city, result ) {
  this.mapObj = map;
  this.i_point = i_point;
  this.i_zoom = i_zoom;  //初期状態のズーム
  this.a_zoom = a_zoom;  //各地のズーム
  this.point = point;
  this.city = city;
  this.result = result;
  this.num = city.length;

  ///
  //実績データをリストとして設定・表示
  //
  this.showList = function() {
    var html ="<p>【<a href=\"#\" onClick=\"mw_showInfo(-1)\" >最初の位置へ</a>】</p>" ;
    html += "<ul>" ;
    for( var i=0; i< this.num; i++ ) {
      html += "<li><a href=\"#\" onClick=\"mw_showInfo("+i+")\" id=\"link"+i+"\">"+city[i]+"</a></li>" ;
    }
    html += "</ul>";
    $("i_list").innerHTML = html;
  } ;

  ///
  //地図上に実績情報を表示
  //
  this.showInfo = function(id) {
    if(id == -1 ) {
      map.setCenter(this.i_point, this.i_zoom);   //最初の表示位置に
    }
    else {
      var msg = this.makeMsg(id);                          //実績データをHTML形式へ整形
      this.mapObj.openInfoWindowHtml(this.point[id], msg); //情報を表示
      map.setCenter(this.point[id], this.a_zoom);          //現地を表示中心に
    }
  } ;

  ///
  //座標地点にマーカーを生成・表示
  //
  this.showMarker = function(id) {
    var marker = new GMarker(this.point[id]); //マーカーを生成
    this.mapObj.addOverlay(marker); //マーカーを表示

    var msg = this.makeMsg(id); //実績データをHTML形式へ整形
    //マーカーにイベントを設定
    GEvent.addListener( marker, "click",    // クリックで吹出し表示
      function() { this.mapObj.openInfoWindowHtml(this.point[id], msg); }
    );
  };

  ///
  //実績データをHTMLへ整形
  //
  this.makeMsg = function(id) {
    word = this.result[id].split(';');
    var html ="<b>"+this.city[id]+"</b><small>";
    for( var k=0; k < word.length; k++ ) { html +="<br />・ "+word[k] ; }
    html +="</small>";
    return( html );
  };
}


///
//マーカーの設定・表示
//
function mw_showMarker(id) {
  var map = showObj.mapObj;
  var point = showObj.point[id];

  var marker = new GMarker(point); //座標地点にマーカーを生成
  map.addOverlay(marker);          //マーカーを表示
  var msg = showObj.makeMsg(id); //実績データをHTML形式へ整形

  //マーカーにイベントを設定
  GEvent.addListener( marker, "click", // クリックで吹出し表示
    function() { map.openInfoWindowHtml(point, msg); }
  );
}


///
//リストクリックで実績データをポップアップ
//
function mw_showInfo(id) { showObj.showInfo(id) ; }


///
//ページ表示と同時に実行される
//
window.onload = function(){
  var msec = (new Date()).getTime();
  new Ajax.Request(xmlfile, {
    method: "get",
    parameters: "cache="+msec,
    onSuccess: function(httpObj) {

      var xmlData = httpObj.responseXML;

      // XMLデータ（初期設定データ）読み出し
      var xdoc = xmlData.getElementsByTagName("init");
      var lon   = parseFloat(xdoc[0].getElementsByTagName("lon")[0].firstChild.nodeValue);
      var lat   = parseFloat(xdoc[0].getElementsByTagName("lat")[0].firstChild.nodeValue);
      var i_zoom  = parseInt(xdoc[0].getElementsByTagName("i_zoom")[0].firstChild.nodeValue);
      var a_zoom  = parseInt(xdoc[0].getElementsByTagName("a_zoom")[0].firstChild.nodeValue);
      var i_point = new GLatLng(lat, lon);  //初期中心座標設定

      // XMLデータ（実績データ）読み出し
      var point  = new Array();
      var city   = new Array();
      var result = new Array();

      xdoc = xmlData.getElementsByTagName("items");
      for (var i=0; i<xdoc.length; i++){
        // 各XMLデータの読み出し
        lon   = parseFloat(xdoc[i].getElementsByTagName("lon")[0].firstChild.nodeValue);
        lat   = parseFloat(xdoc[i].getElementsByTagName("lat")[0].firstChild.nodeValue);
        city[i]   = xdoc[i].getElementsByTagName("city")[0].firstChild.nodeValue;
        result[i] = xdoc[i].getElementsByTagName("result")[0].firstChild.nodeValue;

        point[i] = new GLatLng(lat, lon);  //座標設定
      }

      //ID名"i_map"のDIVへv2の地図を表示
      var map = new GMap2(document.getElementById("i_map"));
      //mapコントロールを追加
      map.addControl(new GLargeMapControl());
      map.addControl(new GOverviewMapControl());
      //最初の表示中心位置を設定。
      map.setCenter(i_point, i_zoom);

      //各データをオブジェクト"showObj"に格納
      showObj = new mw_showData( map, i_point, i_zoom, a_zoom, point, city, result ) ;

      //実績データをマーカーとして地図上に設定・表示
      for ( i=0; i<xdoc.length; i++){
        mw_showMarker(i);
      }

      //実績データをリストで表示
      showObj.showList();
    }
  }) ;
}
