//
// カスタムコントロール 地名一覧
//
function PlaceNameControl() {}

PlaceNameControl.prototype = new GControl();

// 地名データ
PlaceNameControl.prototype.places = [];
// それでも地球は回っているフラグ（ルザミフラグ）
PlaceNameControl.prototype.is_rolling = true;
// 編集モード
PlaceNameControl.prototype.editFn = function(){};



//
// 初期化
//
PlaceNameControl.prototype.initialize = function(map) {
  this.map = map;

  // 地名一覧
  var place_list = document.createElement("div");
  place_list.setAttribute("id", "place_list");

  this.place_list_outer = document.createElement("div");
  this.place_list_outer.setAttribute("id", "place_list_outer");
  this.place_list_outer.appendChild(place_list);

  if(this.places.length > 15)
    place_list.appendChild(this.createPagingLinkElement());
  for(var i=0; i<Math.min(15, this.places.length); i++)
    place_list.appendChild(this.createPlaceLinkElement(this.places[i]));

  this.map.getContainer().appendChild(this.place_list_outer);
  
  return this.place_list_outer;
}



//
// コントロール位置を返す関数
//
PlaceNameControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 0));
}



//
// 親エレメントを取得する。
//
PlaceNameControl.prototype.getParent = function (elem){
  if(elem.parentNode)
    return elem.parentNode;
  if(elem.parentElement)
    return elem.parentElement;
  return null;
}



//
// ページ送りの<a>タグを作成して返す
//
PlaceNameControl.prototype.createPagingLinkElement = function (){
  var str = document.createTextNode("→");
  var a = document.createElement("a");
  a.setAttribute("href", "javascript:void(0)");
  a.current = 0;
  a.control = this;
  GEvent.addDomListener(a, "click", function(){
    var parent = this.control.getParent(this);
    var child;
    while(child = this.nextSibling){
      parent.removeChild(child);
    }
    this.current += 1;
    this.current = this.current % Math.floor((this.control.places.length + 1) / 15 + 1);
    for(var i=this.current*15; i<Math.min((this.current+1)*15, this.control.places.length); i++){
      var a = this.control.createPlaceLinkElement(this.control.places[i]);
      parent.appendChild(a);
    }
  });
  a.appendChild(str);

  return a;
}



//
// 引数の地名データを持つ<a>タグを作成して返す
//
PlaceNameControl.prototype.createPlaceLinkElement = function (place){
  var str = document.createTextNode(place.name);
  var a = document.createElement("a");
  a.setAttribute("href", "javascript:void(0)");
  a.place_id = place.id;
  a.latlng = new GLatLng(place.lat, place.lng);
  a.map = this.map;
  if(this.is_rolling){
    GEvent.addDomListener(a, "click", function(){
      this.map.panTo(this.latlng);
    });
  }else{
    GEvent.addDomListener(a, "click", function(overlay, point){
      var projection = this.map.getCurrentMapType().getProjection();
      var to = projection.fromLatLngToPixel(this.latlng, this.map.getZoom());
      var from = projection.fromLatLngToPixel(this.map.getCenter(), this.map.getZoom());
      this.map.panBy(new GSize(from.x - to.x, from.y - to.y));
    });
  }
  GEvent.addDomListener(a, "click", this.editFn);
  a.appendChild(str);

  return a;
}

